- #include "oled.h"
- #include "oledfont.h"
- //向SSD1306写入一个字节。
- //dat:要写入的数据/命令
- //cmd:数据/命令标志 0,表示命令;1,表示数据;
- void OLED_WriteCmd(unsigned char cmd)
- {
- unsigned char i;
- DC(0);
- CS(0);
- for(i=0;i<8;i++)
- {
- SCL(0);
- if(cmd&0x80)
- {
- DIN(1);
- }
- else
- DIN(0);
- SCL(1);
- cmd<<=1;
- }
- CS(1);
- DC(1);
- }
- void OLED_Writebyte(unsigned char dat)
- {
- unsigned char i;
- DC(1);
- CS(0);
- for(i=0;i<8;i++)
- {
- SCL(0);
- if(dat&0x80)
- {
- DIN(1);
- }
- else
- DIN(0);
- SCL(1);
- dat<<=1;
- }
- CS(1);
- DC(1);
- }
- void OLED_Set_Pos(unsigned char x, unsigned char y)
- {
- OLED_WriteCmd(0xb0+y);
- OLED_WriteCmd(((x&0xf0)>>4)|0x10);
- OLED_WriteCmd((x&0x0f)|0x01);
- }
- //开启OLED显示
- void OLED_Display_On(void)
- {
- OLED_WriteCmd(0X8D); //SET DCDC命令
- OLED_WriteCmd(0X14); //DCDC ON
- OLED_WriteCmd(0XAF); //DISPLAY ON
- }
- //关闭OLED显示
- void OLED_Display_Off(void)
- {
- OLED_WriteCmd(0X8D); //SET DCDC命令
- OLED_WriteCmd(0X10); //DCDC OFF
- OLED_WriteCmd(0XAE); //DISPLAY OFF
- }
- //清屏函数,清完屏,整个屏幕是黑色的!和没点亮一样!!!
- void OLED_Clear(void)
- {
- unsigned char i,n;
- for(i=0;i<8;i++)
- {
- OLED_WriteCmd (0xb0+i); //设置页地址(0~7)
- OLED_WriteCmd (0x00); //设置显示位置—列低地址
- OLED_WriteCmd (0x10); //设置显示位置—列高地址
- for(n=0;n<128;n++)OLED_Writebyte(0);
- } //更新显示
- }
- //在指定位置显示一个字符,包括部分字符
- //x:0~127
- //y:0~63
- //mode:0,反白显示;1,正常显示
- //size:选择字体 16/12
- void OLED_WriteChar(unsigned char x,unsigned char y,unsigned char chr)
- {
- unsigned char c=0,i=0;
- c=chr-' ';//得到偏移后的值
- if(x>127)
- {
- x=0;
- y=y+2;
- }
- OLED_Set_Pos(x,y);
- for(i=0;i<8;i++)
- OLED_Writebyte(F8X16[c*16+i]);
- OLED_Set_Pos(x,y+1);
- for(i=0;i<8;i++)
- OLED_Writebyte(F8X16[c*16+i+8]);
- }
- //m^n函数
- unsigned int oled_pow(unsigned char m,unsigned char n)
- {
- unsigned int result=1;
- while(n--)result*=m;
- return result;
- }
- //显示2个数字
- //x,y :起点坐标
- //len :数字的位数
- //size:字体大小
- //mode:模式 0,填充模式;1,叠加模式
- //num:数值(0~4294967295);
- void OLED_WriteNum1(unsigned char x,unsigned char y,unsigned int num,unsigned char len,unsigned char size2)
- {
- unsigned char t,temp;
- unsigned char enWrite=0;
- for(t=0;t<len;t++)
- {
- temp=(num/oled_pow(10,len-t-1))%10;
- if(enWrite==0&&t<(len-1))
- {
- if(temp==0)
- {
- OLED_WriteChar(x+(size2/2)*t,y,' ');
- continue;
- }else enWrite=1;
- }
- OLED_WriteChar(x+(size2/2)*t,y,temp+'0');
- }
- }
- void OLED_WriteNum2(unsigned char x,unsigned char y,unsigned int num)
- {
- num=num+0x30;
- OLED_WriteChar(x,y,num);
- }
- //显示一个字符号串
- void OLED_WriteString(unsigned char x,unsigned char y,unsigned char *chr)
- {
- unsigned char j=0;
- while (chr[j]!='\0')
- { OLED_WriteChar(x,y,chr[j]);
- x+=8;
- if(x>120){x=0;y+=2;}
- j++;
- }
- }
- //显示汉字
- void OLED_WriteChinese(unsigned char x,unsigned char y,unsigned char no)
- {
- unsigned char t,adder=0;
- OLED_Set_Pos(x,y);
- for(t=0;t<16;t++)
- {
- OLED_Writebyte(Hzk[2*no][t]);
- adder+=1;
- }
- OLED_Set_Pos(x,y+1);
- for(t=0;t<16;t++)
- {
- OLED_Writebyte(Hzk[2*no+1][t]);
- adder+=1;
- }
- }
- /***********功能描述:显示显示BMP图片128×64起始点坐标(x,y),x的范围0~127,y为页的范围0~7*****************/
- void OLED_DrawBMP(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1,unsigned char BMP[])
- {
- unsigned int j=0;
- unsigned char x,y;
- if(y1%8==0) y=y1/8;
- else y=y1/8+1;
- for(y=y0;y<y1;y++)
- {
- OLED_Set_Pos(x0,y);
- for(x=x0;x<x1;x++)
- {
- OLED_Writebyte(BMP[j++]);
- }
- }
- }
- //初始化SSD1306
- void OLED_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;//定义GPIO结构体
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE); //使能PC,D,G端口时钟
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_7; //PD3,PD6推挽输出
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度50MHz
- GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOD3,6
- GPIO_SetBits(GPIOA,GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_7); //PA3,PA6 输出高
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; //PD3,PD6推挽输出
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度50MHz
- GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化GPIOD3,6
- RES(1);
- delay_ms(100);
- RES(0);
- delay_ms(100);
- RES(1);
- OLED_WriteCmd(0xAE);//--turn off oled panel
- OLED_WriteCmd(0x00);//---set low column address
- OLED_WriteCmd(0x10);//---set high column address
- OLED_WriteCmd(0x40);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
- OLED_WriteCmd(0x81);//--set contrast control register
- OLED_WriteCmd(0xCF); // Set SEG Output Current Brightness
- OLED_WriteCmd(0xA1);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常
- OLED_WriteCmd(0xC8);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常
- OLED_WriteCmd(0xA6);//--set normal display
- OLED_WriteCmd(0xA8);//--set multiplex ratio(1 to 64)
- OLED_WriteCmd(0x3f);//--1/64 duty
- OLED_WriteCmd(0xD3);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
- OLED_WriteCmd(0x00);//-not offset
- OLED_WriteCmd(0xd5);//--set display clock divide ratio/oscillator frequency
- OLED_WriteCmd(0x80);//--set divide ratio, Set Clock as 100 Frames/Sec
- OLED_WriteCmd(0xD9);//--set pre-charge period
- OLED_WriteCmd(0xF1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
- OLED_WriteCmd(0xDA);//--set com pins hardware configuration
- OLED_WriteCmd(0x12);
- OLED_WriteCmd(0xDB);//--set vcomh
- OLED_WriteCmd(0x40);//Set VCOM Deselect Level
- OLED_WriteCmd(0x20);//-Set Page Addressing Mode (0x00/0x01/0x02)
- OLED_WriteCmd(0x02);//
- OLED_WriteCmd(0x8D);//--set Charge Pump enable/disable
- OLED_WriteCmd(0x14);//--set(0x10) disable
- OLED_WriteCmd(0xA4);// Disable Entire Display On (0xa4/0xa5)
- OLED_WriteCmd(0xA6);// Disable Inverse Display On (0xa6/a7)
- OLED_WriteCmd(0xAF);//--turn on oled panel
- OLED_WriteCmd(0xAF); /*display ON*/
- OLED_Clear();
- OLED_Set_Pos(0,0);
- }
- void CS(unsigned char i)
- {
- if(i==0)
- {
- GPIO_ResetBits(GPIOA,GPIO_Pin_4); //CS(0)
- }
- if(i==1)
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_4);
- }
- }
- void DC(unsigned char i)
- {
- if(i==0)
- {
- GPIO_ResetBits(GPIOB,GPIO_Pin_1); //CS(0)
- }
- if(i==1)
- {
- GPIO_SetBits(GPIOB,GPIO_Pin_1);
- }
- }
- void RES(unsigned char i)
- {
- if(i==0)
- {
- GPIO_ResetBits(GPIOB,GPIO_Pin_0); //CS(0)
- }
- if(i==1)
- {
- GPIO_SetBits(GPIOB,GPIO_Pin_0);
- }
- }
- void DIN(unsigned char i)
- {
- if(i==0)
- {
- GPIO_ResetBits(GPIOA,GPIO_Pin_7); //CS(0)
- }
- if(i==1)
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_7);
- }
- }
- void SCL(unsigned char i)
- {
- if(i==0)
- {
- GPIO_ResetBits(GPIOA,GPIO_Pin_5); //CS(0)
- }
- if(i==1)
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_5);
- }
- }
复制代码
【必读】版权免责声明
1、本主题所有言论和内容纯属会员个人意见,与本论坛立场无关。2、本站对所发内容真实性、客观性、可用性不做任何保证也不负任何责任,网友之间仅出于学习目的进行交流。3、对提供的数字内容不拥有任何权利,其版权归原著者拥有。请勿将该数字内容进行商业交易、转载等行为,该内容只为学习所提供,使用后发生的一切问题与本站无关。 4、本网站不保证本站提供的下载资源的准确性、安全性和完整性;同时本网站也不承担用户因使用这些下载资源对自己和他人造成任何形式的损失或伤害。 5、本网站所有软件和资料均为网友推荐收集整理而来,仅供学习用途使用,请务必下载后两小时内删除,禁止商用。6、如有侵犯你版权的,请及时联系我们(电子邮箱1370723259@qq.com)指出,本站将立即改正。
|
|