|
本人的程序OLED只能顯示0~99cm的范圍,就算設(shè)置成3位數(shù),超過99cm就直接顯示為999cm。求方法解決
- #include "oled.h"
- #include "oledfont.h"
- #include "delay.h"
- u8 OLED_GRAM[128][8];
- //?üD???′?μ?LCD
- void OLED_Refresh_Gram(void)
- {
- u8 i,n;
- for(i=0;i<8;i++)
- {
- OLED_WR_Byte (0xb0+i,OLED_CMD);
- OLED_WR_Byte (0x00,OLED_CMD);
- OLED_WR_Byte (0x10,OLED_CMD);
- for(n=0;n<128;n++)
- OLED_WR_Byte(OLED_GRAM[n][i],OLED_DATA);
- }
- }
- void OLED_WR_Byte(unsigned char dat ,unsigned char cmd)
- {
- unsigned char i=8, temp=0;
- LCD_DC(cmd);
- for(i=0;i<8;i++)
- {
- LCD_SCL(0);
-
- temp = dat&0x80;
- if (temp == 0)
- {
- LCD_SDA(0);
- }
- else
- {
- LCD_SDA(1);
- }
- LCD_SCL(1);
- dat<<=1;;
- }
- }
- //?a??OLED??ê?
- void OLED_Display_On(void)
- {
- OLED_WR_Byte(0X8D,OLED_CMD);
- OLED_WR_Byte(0X14,OLED_CMD);
- OLED_WR_Byte(0XAF,OLED_CMD);
- }
- //1?±?OLED??ê?
- void OLED_Display_Off(void)
- {
- OLED_WR_Byte(0X8D,OLED_CMD);
- OLED_WR_Byte(0X10,OLED_CMD);
- OLED_WR_Byte(0XAE,OLED_CMD);
- }
- //???áoˉêy,??íê?á,?????á??ê?oúé?μ?!oí??μ?ááò??ù!!!
- void OLED_Clear(void)
- {
- u8 i,n;
- for(i=0;i<8;i++)for(n=0;n<128;n++)OLED_GRAM[n][i]=0X00;
- OLED_Refresh_Gram();
- }
- //?-
- //x:0~1
- //y:0~
- //t:1 ì?3? 0,????
- void OLED_DrawPoint(u8 x,u8 y,u8 t)
- {
- u8 pos,bx,temp=0;
- if(x>127||y>63)return;
- pos=7-y/8;
- bx=y%8;
- temp=1<<(7-bx);
- if(t)OLED_GRAM[x][pos]|=temp;
- else OLED_GRAM[x][pos]&=~temp;
- }
- //x1,y1,x2,y2 ì?3???óòμ?????×?
- //è·±£x1<=x2;y1<=y2 0<=x1<=127 0<=y1<=63
- //dot:0,????;1,ì?3?
- void OLED_Fill(u8 x1,u8 y1,u8 x2,u8 y2,u8 dot)
- {
- u8 x,y;
- for(x=x1;x<=x2;x++)
- {
- for(y=y1;y<=y2;y++)OLED_DrawPoint(x,y,dot);
- }
- OLED_Refresh_Gram();
- }
- //?ú???¨??????ê?ò???×?·?,°üà¨2?·?×?
- //x:0~1
- //y:0~
- //mode:0,·′°×??ê?;1,?y3£??ê?
- //size:????×?ì? 12/16/
- void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 size,u8 mode)
- {
- u8 temp,t,t1;
- u8 y0=y;
- u8 csize=(size/8+((size%8)?1:0))*(size/2);
- chr=chr-' ';
- for(t=0;t<csize;t++)
- {
- if(size==12)temp=asc2_1206[chr][t];
- else if(size==16)temp=asc2_1608[chr][t];
- else if(size==24)temp=asc2_2412[chr][t];
- else return;
- for(t1=0;t1<8;t1++)
- {
- if(temp&0x80)OLED_DrawPoint(x,y,mode);
- else OLED_DrawPoint(x,y,!mode);
- temp<<=1;
- y++;
- if((y-y0)==size)
- {
- y=y0;
- x++;
- break;
- }
- }
- }
- OLED_Refresh_Gram();
- }
- //m^noˉ
- u32 mypow(u8 m,u8 n)
- {
- u32 result=1;
- while(n--)result*=m;
- return result;
- }
- //??ê?2??êy
- //x,y :?eμ?×?±ê
- //len :êy×?μ???
- //size:×?ì?′ó
- //mode:?£ê? 0,ì?3??£ê?;1,μt?ó?£
- //num:êy?μ(0~4294967295);
- void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size)
- {
- u8 t,temp;
- u8 enshow=0;
- for(t=0;t<len;t++)
- {
- temp=(num/mypow(10,len-t-1))%10;
- if(enshow==0&&t<(len-1))
- {
- if(temp==0)
- {
- OLED_ShowChar(x+(size/2)*t,y,' ',size,1);
- continue;
- }else enshow=1;
-
- }
- OLED_ShowChar(x+(size/2)*t,y,temp+'0',size,1);
- }
- }
- //??ê?×?·?
- //x,y:?eμ?×?±ê
- //size:×?ì?′ó
- /
- void OLED_Init(void)
- {
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
- GPIO_InitTypeDef GPIO_InitStructure;
-
- GPIO_InitStructure.GPIO_Pin =GPIO_Pin_4 | GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
-
- GPIO_ResetBits(GPIOC,GPIO_Pin_4);
-
- LCD_SCL(1);
- LCD_RST(0);
- delay_ms(100);
- LCD_RST(1);
-
- OLED_WR_Byte(0xAE,OLED_CMD);
- OLED_WR_Byte(0xD5,OLED_CMD);
- OLED_WR_Byte(80,OLED_CMD);
- OLED_WR_Byte(0xA8,OLED_CMD);
- OLED_WR_Byte(0X3F,OLED_CMD);
- OLED_WR_Byte(0xD3,OLED_CMD);
- OLED_WR_Byte(0X00,OLED_CMD);
- OLED_WR_Byte(0x40,OLED_CMD);
-
- OLED_WR_Byte(0x8D,OLED_CMD);
- OLED_WR_Byte(0x14,OLED_CMD);
- OLED_WR_Byte(0x20,OLED_CMD);
- OLED_WR_Byte(0x02,OLED_CMD);
- OLED_WR_Byte(0xA1,OLED_CMD);
- OLED_WR_Byte(0xC0,OLED_CMD);
- OLED_WR_Byte(0xDA,OLED_CMD);
- OLED_WR_Byte(0x12,OLED_CMD);
-
- OLED_WR_Byte(0x81,OLED_CMD);
- OLED_WR_Byte(0xEF,OLED_CMD);
- OLED_WR_Byte(0xD9,OLED_CMD);
- OLED_WR_Byte(0xf1,OLED_CMD);
- OLED_WR_Byte(0xDB,OLED_CMD);
- OLED_WR_Byte(0x30,OLED_CMD);
- OLED_WR_Byte(0xA4,OLED_CMD);
- OLED_WR_Byte(0xA6,OLED_CMD);
- OLED_WR_Byte(0xAF,OLED_CMD);
- OLED_Clear();
- }
- 超聲波代碼
- #include "sonic.h"
- #include "timer.h"
- #include "delay.h"
- extern u8 TIM_Flag;
- void Sonic_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
-
-
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
- GPIO_Init(GPIOC,&GPIO_InitStructure);
-
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_Init(GPIOC,&GPIO_InitStructure);
- }
- void Sonic_Launch(void)
- {
- Trig_High;
- delay_ms(1);
- Trig_Low;
- }
- u16 Sonic_Measure(void)
- {
- u16 Distance;
- u16 TIM = 0;
-
- Sonic_Launch();
-
- TIM_Cmd(TIM2,ENABLE);
-
- while(Sonic_Status == 0 && TIM_Flag == 0);
- TIM2 -> CNT = 0;
- while(Sonic_Status == 1 && TIM_Flag == 0);
- TIM_Cmd(TIM2,DISABLE);
-
- if(TIM_Flag == 0)
- {
- TIM = TIM_GetCounter(TIM2);
- Distance = TIM * 0.015;
- }
- else
- {
- TIM_Flag = 0;
- Distance = 999;
- }
-
- return Distance;
-
- }
復(fù)制代碼
|
|