完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
主函数: #include "STM32f4xx.h" #include "delay.h" #include "sys.h" #include "oled.h" #include "bmp.h" int main(void) { u8 t; delay_init(200); NVIC_Configuration(); OLED_Init(); OLED_Clear(); t=' '; OLED_ShowCHinese(0,0,0);//ÖÐ OLED_ShowCHinese(18,0,1);//¾° OLED_ShowCHinese(36,0,2);//Ô° OLED_ShowCHinese(54,0,3);//µç OLED_ShowCHinese(72,0,4);//×Ó OLED_ShowCHinese(90,0,5);//¿Æ OLED_ShowCHinese(108,0,6);//¼¼ while(1) { OLED_Clear(); OLED_ShowCHinese(0,0,0); OLED_ShowCHinese(18,0,1); OLED_ShowCHinese(36,0,2); OLED_ShowCHinese(54,0,3); OLED_ShowCHinese(72,0,4); OLED_ShowCHinese(90,0,5); OLED_ShowCHinese(108,0,6); OLED_ShowString(6,3,"0.96' OLED TEST",16); OLED_ShowString(0,6,"ASCII:",16); OLED_ShowString(63,6,"CODE:",16); OLED_ShowChar(48,6,t,16);//ÏÔʾASCII×Ö·û t++; if(t>'~')t=' '; OLED_ShowNum(103,6,t,3,16);//ÏÔʾASCII×Ö·ûµÄÂëÖµ delay_ms(8000); delay_ms(8000); delay_ms(8000); delay_ms(8000); delay_ms(8000); OLED_DrawBMP(0,0,128,8,BMP1) delay_ms(8000); delay_ms(8000); delay_ms(8000); delay_ms(8000); OLED_DrawBMP(0,0,128,8,BMP1); delay_ms(8000); delay_ms(8000); delay_ms(8000); delay_ms(8000); } } OLED驱动函数: #include "oled.h" #include "stdlib.h" #include "oledfont.h" #include "delay.h" void GPIO_INIT(void)//¶Ë¿Ú³õʼ»¯ { GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE); GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType=GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP; GPIO_Init(GPIOB,&GPIO_InitStructure); } void IIC_Start() { OLED_SCLK_Set(); OLED_SDIN_Set(); OLED_SDIN_Clr(); OLED_SCLK_Clr(); } void IIC_Stop() { OLED_SCLK_Set() ; OLED_SCLK_Clr(); OLED_SDIN_Clr(); OLED_SDIN_Set(); } void IIC_Wait_Ack() { OLED_SCLK_Set() ; OLED_SCLK_Clr(); } void Write_IIC_Byte(u8 IIC_Byte) { u8 i; u8 m,da; da=IIC_Byte; OLED_SCLK_Clr(); for(i=0;i<8;i++) { m=da; m=m&0x80; if(m==0x80) { OLED_SDIN_Set(); } else OLED_SDIN_Clr(); da=da<<1; OLED_SCLK_Set(); OLED_SCLK_Clr(); } } void Write_IIC_Command(u8 IIC_Command) { IIC_Start(); Write_IIC_Byte(0x78); //Slave address,SA0=0 IIC_Wait_Ack(); Write_IIC_Byte(0x00); //write command IIC_Wait_Ack(); Write_IIC_Byte(IIC_Command); IIC_Wait_Ack(); IIC_Stop(); } void Write_IIC_Data(u8 IIC_Data) { IIC_Start(); Write_IIC_Byte(0x78); //D/C#=0; R/W#=0 IIC_Wait_Ack(); Write_IIC_Byte(0x40); //write data IIC_Wait_Ack(); Write_IIC_Byte(IIC_Data); IIC_Wait_Ack(); IIC_Stop(); } void OLED_WR_Byte(unsigned dat,unsigned cmd) { if(cmd) { Write_IIC_Data(dat); } else { Write_IIC_Command(dat); } } void fill_picture(u8 fill_Data) { u8 m,n; for(m=0;m<8;m++) { OLED_WR_Byte(0xb0+m,0); //page0-page1 OLED_WR_Byte(0x00,0); //low column start address OLED_WR_Byte(0x10,0); //high column start address for(n=0;n<128;n++) { OLED_WR_Byte(fill_Data,1); } } } void Delay_50ms(unsigned int Del_50ms) { unsigned int m; for(;Del_50ms>0;Del_50ms--) for(m=6245;m>0;m--); } void Delay_1ms(unsigned int Del_1ms) { unsigned char j; while(Del_1ms--) { for(j=0;j<123;j++); } } //×ø±êÉèÖà void OLED_Set_Pos(u8 x, u8 y) { OLED_WR_Byte(0xb0+y,OLED_CMD); OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD); OLED_WR_Byte((x&0x0f),OLED_CMD); } //¿ªÆôOLEDÏÔʾ void OLED_Display_On(void) { OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDCÃüÁî OLED_WR_Byte(0X14,OLED_CMD); //DCDC ON OLED_WR_Byte(0XAF,OLED_CMD); //DISPLAY ON } //¹Ø±ÕOLEDÏÔʾ void OLED_Display_Off(void) { OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDCÃüÁî OLED_WR_Byte(0X10,OLED_CMD); //DCDC OFF OLED_WR_Byte(0XAE,OLED_CMD); //DISPLAY OFF } //ÇåÆÁº¯Êý,ÇåÍêÆÁ,Õû¸öÆÁÄ»ÊǺÚÉ«µÄ!ºÍûµãÁÁÒ»Ñù!!! void OLED_Clear(void) { u8 i,n; for(i=0;i<8;i++) { OLED_WR_Byte (0xb0+i,OLED_CMD); //ÉèÖÃÒ³µØÖ·£¨0~7£© OLED_WR_Byte (0x00,OLED_CMD); //ÉèÖÃÏÔʾλÖáªÁе͵ØÖ· OLED_WR_Byte (0x10,OLED_CMD); //ÉèÖÃÏÔʾλÖáªÁиߵØÖ· for(n=0;n<128;n++)OLED_WR_Byte(0,OLED_DATA); } //¸üÐÂÏÔʾ } void OLED_On(void) { u8 i,n; for(i=0;i<8;i++) { OLED_WR_Byte (0xb0+i,OLED_CMD); //ÉèÖÃÒ³µØÖ·£¨0~7£© OLED_WR_Byte (0x00,OLED_CMD); //ÉèÖÃÏÔʾλÖáªÁе͵ØÖ· OLED_WR_Byte (0x10,OLED_CMD); //ÉèÖÃÏÔʾλÖáªÁиߵØÖ· for(n=0;n<128;n++)OLED_WR_Byte(1,OLED_DATA); } //¸üÐÂÏÔʾ } //ÔÚÖ¸¶¨Î»ÖÃÏÔʾһ¸ö×Ö·û,°üÀ¨²¿·Ö×Ö·û //x:0~127 //y:0~63 //mode:0,·´°×ÏÔʾ;1,Õý³£ÏÔʾ //size:Ñ¡Ôñ×ÖÌå 16/12 void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 Char_Size) { unsigned char c=0,i=0; c=chr-' ';//µÃµ½Æ«ÒƺóµÄÖµ if(x>Max_Column-1){x=0;y=y+2;} if(Char_Size ==16) { OLED_Set_Pos(x,y); for(i=0;i<8;i++) OLED_WR_Byte(F8X16[c*16+i],OLED_DATA); OLED_Set_Pos(x,y+1); for(i=0;i<8;i++) OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA); } else { OLED_Set_Pos(x,y); for(i=0;i<6;i++) OLED_WR_Byte(F6x8[c][i],OLED_DATA); } } //m^nº¯Êý u32 oled_pow(u8 m,u8 n) { u32 result=1; while(n--)result*=m; return result; } //ÏÔʾ2¸öÊý×Ö //x,y :Æðµã×ø±ê //len :Êý×ÖµÄλÊý //size:×ÖÌå´óС //mode:ģʽ 0,Ìî³äģʽ;1,µþ¼Óģʽ //num:ÊýÖµ(0~4294967295); void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size2) { u8 t,temp; u8 enshow=0; for(t=0;t temp=(num/oled_pow(10,len-t-1))%10; if(enshow==0&&t<(len-1)) { if(temp==0) { OLED_ShowChar(x+(size2/2)*t,y,' ',size2); continue; }else enshow=1; } OLED_ShowChar(x+(size2/2)*t,y,temp+'0',size2); } } //ÏÔʾһ¸ö×Ö·ûºÅ´® void OLED_ShowString(u8 x,u8 y,u8 *chr,u8 Char_Size) { unsigned char j=0; while (chr[j]!=' |