上一篇介绍AT Work Bench软件的使用情况,并成功实现流水灯效果,这一节介绍使用AT Work Bench配置软件IIC来驱动OLED。首先打开AT Work Bench软件,配置MCU系列和型号,新建一个工程,之后选择GPIO引脚配置输出模式和参数,时钟配置选择外部高速时钟和外部低速时钟。之后便是生成代码。
打开MDK工程,先全局编译一遍看看有没有报错,之后在user路径下新建两个.c文件,分别是OLED.c和delay.c,用来写OLED驱动程序和延时函数,同时相应的.h也需要一并添加到user路径下面。因为系统中用到ASCII码和汉字需要一个特殊的oledfont.h文件来存放取模软件生成的字符数组。完成这些准备工作之后,便可以放心的编写IIC代码了。
软件IIC是通过GPIO引脚,软件模拟寄存器的方式来控制管脚的状态,从而模拟IIC通讯的波形,最终达到一个IIC通讯的过程。下面详细介绍IIC软件模拟通讯的4个过程,分别是1、起始信号与停止信号;2、应答信号;3、数据的有效性;4、数据传输。
1、起始信号与停止信号
起始信号:当SCL为高电平期间,SDA由高到低的跳变,起始信号是一种电平跳变的时序信号而不是一个电平信号。
停止信号:当SCL为高电平期间,SDA由低到高的跳变,停止信号也是一种电平跳变的时序信号而不是一个电平信号。起始信号和停止信号一般由主机产生。
//IIC Start
**********************************************/
void OLED_IIC_Start()
{
OLED_SCLK_Set();
delay_us(3);
OLED_SDIN_Set();
delay_us(3);
OLED_SDIN_Clr();
delay_us(3);
OLED_SCLK_Clr();
delay_us(3);
}
/**********************************************
//IIC Stop
**********************************************/
void OLED_IIC_Stop()
{
OLED_SCLK_Set();
delay_us(3);
OLED_SDIN_Clr();
delay_us(3);
OLED_SDIN_Set();
delay_us(3);
}
为了更加直观方便的编写,在oled.h文件中添加宏定义
#define OLED_MODE 0
#define SIZE 8
#define XLevelL 0x00
#define XLevelH 0x10
#define Max_Column 128
#define Max_Row 64
#define Brightness 0xFF
#define X_WIDTH 128
#define Y_WIDTH 64
#define OLED_SCLK_Clr() gpio_bits_reset(GPIOB,GPIO_PINS_8)//SCL
#define OLED_SCLK_Set() gpio_bits_set(GPIOB,GPIO_PINS_8)
#define OLED_SDIN_Clr() gpio_bits_reset(GPIOB,GPIO_PINS_9)//SDA
#define OLED_SDIN_Set() gpio_bits_set(GPIOB,GPIO_PINS_9)
#define OLED_CMD 0 //写命令
#define OLED_DATA 1 //写数据
2、从机应答信号
主机每发送一个字节吗,就在时钟脉冲9期间释放数据线,由从机反馈一个应答信号。应答信号为低电平时规定为有效应答位(ACK简称应答位)表示从机已经成功接收到该字节。应答信号为高电平时,规定为非应答位,一般表示从机接收该字节没有成功。对于反馈有效应答位ACK的要求是从机在第9个时钟脉冲之前的低电平期间将SDA线拉低,并且确保在该时钟的高电平期间为稳定的低电平。在图中红色可以看到,SCL为低电平后SDA再升为高电平,再SCL变为高电平,由此可以进行一个软件模拟IIC波形
void OLED_IIC_Wait_Ack()
{
OLED_SCLK_Set();
delay_us(3);
OLED_SCLK_Clr();
delay_us(3);
}
3、数据有效性
IIC总线进行数据传输时,时钟信号为高电平期间,数据线上的数据必须保持稳定,只有在时钟线上的信号为低电平期间,数据线上的高电平或低电平状态才允许变化。SDA数据线在SCL的每个时钟周期传输一位数据。即数据在SCL的上升沿到来之前就需要准备好,并在下降沿到来之前必须稳定。
4、数据传输
在数据有效性中,数据位的传输是边沿触发的,在IIC总线上数据传输,传送的每一位数据都有一个时钟脉冲相对应,SCL为高电平时SDA表示数据有效,即此时的SDA为高电平时表示数据“1”,为低电平时表示数据“0”.即在SCL串行时钟的配合下,SDA上逐位地串行传送每一位数据。传输数据地时候,将SCL置低,然后设置SDA总线对应地引脚电平为高/低,SDA电平确定后再将SCL置高,将8位由高到低依次发送出去。
/**********************************************
// IIC Write byte
**********************************************/
void OLED_Write_IIC_Byte(unsigned char IIC_Byte)
{
unsigned char i;
unsigned char m,da;
da=IIC_Byte;
OLED_SCLK_Clr();
for(i=0;i<8;i++)
{
m=da;
// OLED_SCLK_Clr();
m=m&0x80;
if(m==0x80)
{
OLED_SDIN_Set();
}
else
OLED_SDIN_Clr();
da=da<<1;
delay_us(3);
OLED_SCLK_Set();
delay_us(3);
OLED_SCLK_Clr();
delay_us(3);
}
}
/**********************************************
// IIC Write Command
**********************************************/
void OLED_Write_IIC_Command(unsigned char IIC_Command)
{
OLED_IIC_Start();
OLED_Write_IIC_Byte(0x78); //Slave address,SA0=0
OLED_IIC_Wait_Ack();
OLED_Write_IIC_Byte(0x00); //write command
OLED_IIC_Wait_Ack();
OLED_Write_IIC_Byte(IIC_Command);
OLED_IIC_Wait_Ack();
OLED_IIC_Stop();
}
/**********************************************
// IIC Write Data
**********************************************/
void OLED_Write_IIC_Data(unsigned char IIC_Data)
{
OLED_IIC_Start();
OLED_Write_IIC_Byte(0x78); //D/C#=0; R/W#=0
OLED_IIC_Wait_Ack();
OLED_Write_IIC_Byte(0x40); //write data
OLED_IIC_Wait_Ack();
OLED_Write_IIC_Byte(IIC_Data);
OLED_IIC_Wait_Ack();
OLED_IIC_Stop();
}
void OLED_WR_Byte(unsigned dat,unsigned cmd)
{
if(cmd)
{
OLED_Write_IIC_Data(dat);
}
else
{
OLED_Write_IIC_Command(dat);
}
}
本次实验用的是OLED模块(0.96寸4针IIC接口OLED显示屏)SSD1306,电源电压3.3-5.5V,总共四个接口,GND,VCC,SCL(IIC总线时钟信号),SDA(IIC总线数据信号)。SSD1306是一个为映射静态 RAM 保存位模式来显示。该 RAM 的为 128 * 64 bit大小,RAM 分为 8 页,从 PAFE0 到 PAGE7,用于单色 128 * 64 点阵显示,
void OLED_Init(void)
{
wk_gpio_config();
OLED_WR_Byte(0xAE,OLED_CMD);//--display off
OLED_WR_Byte(0x00,OLED_CMD);//---set low column address
OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
OLED_WR_Byte(0x40,OLED_CMD);//--set start line address
OLED_WR_Byte(0xB0,OLED_CMD);//--set page address
OLED_WR_Byte(0x81,OLED_CMD); // contract control
OLED_WR_Byte(0xFF,OLED_CMD);//--128
OLED_WR_Byte(0xA1,OLED_CMD);//set segment remap
OLED_WR_Byte(0xA6,OLED_CMD);//--normal / reverse
OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
OLED_WR_Byte(0x3F,OLED_CMD);//--1/32 duty
OLED_WR_Byte(0xC8,OLED_CMD);//Com scan direction
OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset
OLED_WR_Byte(0x00,OLED_CMD);//
OLED_WR_Byte(0xD5,OLED_CMD);//set osc division
OLED_WR_Byte(0x80,OLED_CMD);//
OLED_WR_Byte(0xD8,OLED_CMD);//set area color mode off
OLED_WR_Byte(0x05,OLED_CMD);//
OLED_WR_Byte(0xD9,OLED_CMD);//Set Pre-Charge Period
OLED_WR_Byte(0xF1,OLED_CMD);//
OLED_WR_Byte(0xDA,OLED_CMD);//set com pin configuartion
OLED_WR_Byte(0x12,OLED_CMD);//
OLED_WR_Byte(0xDB,OLED_CMD);//set Vcomh
OLED_WR_Byte(0x30,OLED_CMD);//
OLED_WR_Byte(0x8D,OLED_CMD);//set charge pump enable
OLED_WR_Byte(0x14,OLED_CMD);//
OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
OLED_Clear();
OLED_Set_Pos(0,0);
}
写入起始坐标
//设置数据写入的起始坐标(行和列)
//x:列的起始低地址与起始高地址
//y:页的起始页的地址 0-7
void OLED_Set_Pos(unsigned char x,unsigned char y)
{
OLED_Write_Byte(0xb0+y,OLED_CMD); //写入页地址
OLED_Write_Byte((x&0x0f),OLED_CMD); //写入列的地址,低半个字节
OLED_Write_Byte((x&0xf0)>>4 | 0x10,OLED_CMD); //写入列地址,高半个字节
}
清屏函数
//清屏函数,清完屏,整个屏幕是黑色的!和没点亮一样!!!
void OLED_Clear(void)
{
uint8_t 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);
} //更新显示
}
开启&关闭OLED显示
//开启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
}
显示字符,汉字,数字,字符串等函数
//在指定位置显示一个字符,包括部分字符
//x:0~127
//y:0~63
//mode:0,反白显示;1,正常显示
//size:选择字体 16/12
void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t 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函数
uint32_t oled_pow(uint8_t m,uint8_t n)
{
uint32_t result=1;
while(n--)result*=m;
return result;
}
//显示2个数字
//x,y :起点坐标
//len :数字的位数
//size:字体大小
//mode:模式 0,填充模式;1,叠加模式
//num:数值(0~4294967295);
void OLED_ShowNumber(uint8_t x,uint8_t y,uint32_t num,uint8_t len,uint8_t size2)
{
uint8_t t,temp;
uint8_t enshow=0;
for(t=0;t<len;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(uint8_t x,uint8_t y,uint8_t *chr,uint8_t Char_Size)
{
while (*chr!='\0')
{ OLED_ShowChar(x,y,*chr,Char_Size);
x+=8;
if(x>120){x=0;y+=2;}
chr++;
}
}
//显示汉字
void OLED_ShowCHinese(uint8_t x,uint8_t y,uint8_t no)
{
uint8_t t,adder=0;
OLED_Set_Pos(x,y);
for(t=0;t<16;t++)
{
OLED_WR_Byte(Hzk[2*no][t],OLED_DATA);
adder+=1;
}
OLED_Set_Pos(x,y+1);
for(t=0;t<16;t++)
{
OLED_WR_Byte(Hzk[2*no+1][t],OLED_DATA);
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_WR_Byte(BMP[j++],OLED_DATA);
}
}
}
void OLED_Float(unsigned char Y,unsigned char X,double real,unsigned char N)
{
unsigned char i_Count=1;
unsigned char n[12]={0};
long j=1;
int real_int=0;
double decimal=0;
unsigned int real_decimal=0;
if(real<0)
{
real_int=(int)(-real);
}
else
{
real_int=(int)real;
}
decimal=real-real_int;
real_decimal=decimal*1e4;
while(real_int/10/j!=0)
{
j=j*10;i_Count++;
}
n[0]=(real_int/10000)%10;
n[1]=(real_int/1000)%10;
n[2]=(real_int/100)%10;
n[3]=(real_int/10)%10;
n[4]=(real_int/1)%10;
n[5]='.';
n[6]=(real_decimal/1000)%10;
n[7]=(real_decimal/100)%10;
n[8]=(real_decimal/10)%10;
n[9]=real_decimal%10;
n[6+N]='\0';
for(j=0;j<10;j++) n[j]=n[j]+16+32;
if(real<0)
{
i_Count+=1;
n[5-i_Count]='-';
}
n[5]='.';
n[6+N]='\0';
OLED_ShowString(X,Y,&n[5-i_Count],12);
}
void OLED_Float2(unsigned char Y,unsigned char X,double real,unsigned char N1,unsigned char N2)
{
unsigned char i_Count=1;
unsigned char n[12]={0};
long j=1;
unsigned int real_int=0;
double decimal=0;
unsigned int real_decimal=0;
X=X*8;
real_int=(int)real;
//Dis_Num(2,0,real_int,5);
decimal=real-real_int;
real_decimal=decimal*1e4;
//Dis_Num(2,6,real_decimal,4);
while(real_int/10/j!=0)
{
j=j*10;i_Count++;
}
n[0]=(real_int/10000)%10;
n[1]=(real_int/1000)%10;
n[2]=(real_int/100)%10;
n[3]=(real_int/10)%10;
n[5]='.';
n[6]=(real_decimal/1000)%10;
n[7]=(real_decimal/100)%10;
n[8]=(real_decimal/10)%10;
n[9]=real_decimal%10;
n[6+N2]='\0';
for(j=0;j<10;j++) n[j]=n[j]+16+32;
n[5]='.';
n[6+N2]='\0';
OLED_ShowString(X,Y,&n[5-N1],12);
}
void OLED_Num2(unsigned char x,unsigned char y, int number)
{
unsigned char shi,ge;
int num =number;
if(num<0)
{
num=-num;
shi=num%100/10;
ge=num%10;
OLED_fuhao_write(x,y,13);
OLED_Num_write(x+1,y,shi);
OLED_Num_write(x+2,y,ge);
}
else
{
shi=num%100/10;
ge=num%10;
OLED_fuhao_write(x,y,11);
OLED_Num_write(x+1,y,shi);
OLED_Num_write(x+2,y,ge);
}
}
void OLED_Num3(unsigned char x,unsigned char y,int number)
{
unsigned char ge,shi,bai;
int num =number;
if(num<0)
{
num=-num;
OLED_fuhao_write(x,y,13); //显示-号
ge = num %10;
shi = num/10%10;
bai = num/100;
OLED_Num_write(x+3,y,ge);
OLED_Num_write(x+2,y,shi);
OLED_Num_write(x+1,y,bai);
}
else
{
OLED_fuhao_write(x,y,11);
ge = num %10;
shi = num/10 %10;
bai = num/100;
OLED_Num_write(x+3,y,ge);
OLED_Num_write(x+2,y,shi);
OLED_Num_write(x+1,y,bai);
}
}
void OLED_Num4(unsigned char x,unsigned char y, int number)
{
unsigned char qian,bai,shi,ge;
int num =number;
if(num<0)
{
num=-num;
}
qian=num/1000;
bai=num%1000/100;
shi=num%100/10;
ge=num%10;
OLED_Num_write(x,y,qian);
OLED_Num_write(x+1,y,bai);
OLED_Num_write(x+2,y,shi);
OLED_Num_write(x+3,y,ge);
}
void OLED_Num_write(unsigned char x,unsigned char y,unsigned char asc)
{
int i=0;
OLED_Set_Pos(x*6,y);
for(i=0;i<6;i++)
{
OLED_WR_Byte(F6x8[asc+16][i],OLED_DATA);
}
}
void OLED_fuhao_write(unsigned char x,unsigned char y,unsigned char asc)
{
int i=0;
OLED_Set_Pos(x*6,y);
for(i=0;i<6;i++)
{
OLED_WR_Byte(F6x8[asc][i],OLED_DATA);
}
}
void OLED_Num5(unsigned char x,unsigned char y,unsigned int number)
{
unsigned char wan,qian,bai,shi,ge;
wan=number/10000;
qian = number%10000/1000;
bai=number%1000/100;
shi=number%100/10;
ge=number%10;
OLED_Num_write(x,y,wan);
OLED_Num_write(x+1,y,qian);
OLED_Num_write(x+2,y,bai);
OLED_Num_write(x+3,y,shi);
OLED_Num_write(x+4,y,ge);
}
这里面要提到取模软件01-PCtoLCD2002完美版,可以实现汉字、图片、英文的取模。首先双击打开
点击模式,选择字符模式
点击选项,进行配置,点阵内的数字手动输入999,然后点击确定
以生成16*16的汉字为例,字宽和字高选择16,在输入栏输入需要取模的汉字,且只能是汉字,然后点击生成字模。
将生成的代码复制到oledfont.h的数组中。
char Hzk[][32]={
{0x00,0xC2,0xBA,0x82,0x82,0xFE,0xA2,0x90,0xFC,0x4B,0x48,0xF9,0x4A,0x48,0x08,0x00},
{0x20,0x10,0x08,0x46,0x81,0x7F,0x00,0x00,0xFF,0x22,0x22,0x3F,0x22,0x22,0x20,0x00},/*"雅",0*/
{0x40,0x3C,0x10,0xFF,0x10,0x10,0x40,0x48,0x48,0x48,0x7F,0x48,0xC8,0x48,0x40,0x00},
{0x02,0x06,0x02,0xFF,0x01,0x01,0x00,0x02,0x0A,0x12,0x42,0x82,0x7F,0x02,0x02,0x00},/*"特",1*/
{0x00,0x10,0x10,0x10,0x10,0x10,0xFF,0x10,0x10,0x10,0x10,0x10,0xF0,0x00,0x00,0x00},
{0x00,0x80,0x40,0x20,0x18,0x06,0x01,0x00,0x20,0x40,0x80,0x40,0x3F,0x00,0x00,0x00},/*"力",2*/
{0x24,0x24,0xA4,0xFE,0xA3,0x22,0x00,0x22,0xCC,0x00,0x00,0xFF,0x00,0x00,0x00,0x00},
{0x08,0x06,0x01,0xFF,0x00,0x01,0x04,0x04,0x04,0x04,0x04,0xFF,0x02,0x02,0x02,0x00},/*"科",3*/
{0x10,0x10,0x10,0xFF,0x10,0x90,0x08,0x88,0x88,0x88,0xFF,0x88,0x88,0x88,0x08,0x00},
{0x04,0x44,0x82,0x7F,0x01,0x80,0x80,0x40,0x43,0x2C,0x10,0x28,0x46,0x81,0x80,0x00},/*"技",4*/
{0x00,0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x00,0x00,0x00},
{0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x00},/*"二",5*/
{0x10,0xF0,0x1F,0x10,0xF0,0x04,0xD4,0x54,0x54,0xFF,0x54,0x54,0x74,0x04,0x00,0x00},
{0x41,0x22,0x14,0x0C,0x33,0x80,0x41,0x21,0x19,0x07,0x19,0x25,0x49,0x87,0x80,0x00},/*"姨",6*/
{0x00,0x00,0xF8,0x88,0x88,0x88,0x88,0xFF,0x88,0x88,0x88,0x88,0xF8,0x00,0x00,0x00},
{0x00,0x00,0x1F,0x08,0x08,0x08,0x08,0x7F,0x88,0x88,0x88,0x88,0x9F,0x80,0xF0,0x00},/*"电",7*/
{0x80,0x82,0x82,0x82,0x82,0x82,0x82,0xE2,0xA2,0x92,0x8A,0x86,0x82,0x80,0x80,0x00},
{0x00,0x00,0x00,0x00,0x00,0x40,0x80,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"子",8*/
{0x00,0xFE,0x02,0x22,0x42,0x82,0x72,0x02,0x22,0x42,0x82,0x72,0x02,0xFE,0x00,0x00},
{0x00,0xFF,0x10,0x08,0x06,0x01,0x0E,0x10,0x08,0x06,0x01,0x4E,0x80,0x7F,0x00,0x00},/*"网",9*/
{0x10,0x60,0x02,0x8C,0x00,0xFE,0x02,0xF2,0x02,0xFE,0x00,0xF8,0x00,0xFF,0x00,0x00},
{0x04,0x04,0x7E,0x01,0x80,0x47,0x30,0x0F,0x10,0x27,0x00,0x47,0x80,0x7F,0x00,0x00},/*"测",10*/
{0x40,0x40,0x42,0xCC,0x00,0x90,0x90,0x90,0x90,0x90,0xFF,0x10,0x11,0x16,0x10,0x00},
{0x00,0x00,0x00,0x3F,0x10,0x28,0x60,0x3F,0x10,0x10,0x01,0x0E,0x30,0x40,0xF0,0x00},/*"试",11*/
};
完成上述过程之后,在main函数添加必要的头文件和初始化代码后,可以在while中循环执行以下显示代码。
OLED_ShowCHinese(0,2,0);
OLED_ShowCHinese(27,2,1);
OLED_ShowCHinese(55,2,2);
OLED_ShowCHinese(83,2,3);
OLED_ShowCHinese(111,2,4);
OLED_ShowCHinese(23,0,5);
OLED_ShowCHinese(39,0,6);
OLED_ShowCHinese(55,0,7);
OLED_ShowCHinese(71,0,8);
OLED_ShowCHinese(87,0,9);
OLED_ShowString(8,4,"AT32F405RCT7-7",16);
OLED_ShowString(31,6,"OLED",16);
OLED_ShowCHinese(64,6,10);
OLED_ShowCHinese(80,6,11);
效果如下图所示。
后续将使用硬件IIC驱动OLED。
|
|