在上一篇文章中介绍AT32F405RCT7-7之软件IIC驱动OLED通过软件模拟IIC通讯波形来实现数据传输。但是软件IIC传输速率不如硬件IIC。这次使用AT32自带的硬件IIC来驱动OLED。
使用AT Work Bench软件新建硬件IIC的测试工程,配置IIC的模式和参数以及对应IIC引脚的模式和参数。
同时需要开启DMA传输,因为在后面添加i2c_application.c和i2c_application.h文件中需要DMA的库函数,或者可以手动添加at32f402_405_dma.c库文件。
之后是系统时钟设置,可以选择使用外部时钟或者内部时钟。最后生成代码。
打开生成的MDK工程,添加i2c_application.c和i2c_application.h文件,这两个文件可以在生成代码的文件夹的HARD_IIC\middlewares\i2c_application_library路径下找到,跟上一篇同样新建OLED和delay的.c和.h文件。在at32f402_405_wk_config.c文件中系统自动生成IIC2初始化代码
void wk_i2c2_init(void)
{
/* add user code begin i2c2_init 0 */
/* add user code end i2c2_init 0 */
gpio_init_type gpio_init_struct;
gpio_default_para_init(&gpio_init_struct);
/* add user code begin i2c2_init 1 */
/* add user code end i2c2_init 1 */
/* configure the SCL pin */
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_OPEN_DRAIN;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
gpio_init_struct.gpio_pins = SCL_PIN;
gpio_init(SCL_GPIO_PORT, &gpio_init_struct);
gpio_pin_mux_config(SCL_GPIO_PORT, GPIO_PINS_SOURCE0, GPIO_MUX_4);
/* configure the SDA pin */
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_OPEN_DRAIN;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
gpio_init_struct.gpio_pins = SDA_PIN;
gpio_init(SDA_GPIO_PORT, &gpio_init_struct);
gpio_pin_mux_config(SDA_GPIO_PORT, GPIO_PINS_SOURCE1, GPIO_MUX_4);
i2c_init(I2C2, 0, 0x30D08080);
i2c_own_address1_set(I2C2, I2C_ADDRESS_MODE_7BIT, 0x0);
i2c_ack_enable(I2C2, TRUE);
i2c_clock_stretch_enable(I2C2, TRUE);
i2c_general_call_enable(I2C2, FALSE);
i2c_enable(I2C2, TRUE);
/* add user code begin i2c2_init 2 */
/* add user code end i2c2_init 2 */
}
我们只需要修改Write_IIC_Byte,Write_IIC_Data,Write_IIC_Command函数即可
void Write_IIC_Byte(uint8_t IIC_Byte)
{
i2c_status_type i2c_status;
uint8_t buff[2] = {0};
buff[0] = IIC_Byte;
i2c_status = i2c_master_transmit(&hi2cx, I2Cx_ADDRESS, buff, 1, 1000);
if(i2c_status != I2C_OK)
{
printf("erro send %d",i2c_status);
}
}
//==================================================================================================
// 函数功能: IIC外设驱动函数部分
// 函数标记: Write_IIC_Command
// 函数说明: 无
void Write_IIC_Command(uint8_t IIC_Command)
{
i2c_status_type i2c_status;
uint8_t buff[2] = {0};
buff[0] = 0x00;
buff[1] = IIC_Command;
i2c_status = i2c_master_transmit(&hi2cx, I2Cx_ADDRESS, buff, 2, 1000);
if(i2c_status != I2C_OK)
{
printf("erro send %d",i2c_status);
}
}
//==================================================================================================
// 函数功能: IIC外设驱动函数部分
// 函数标记: Write_IIC_Data
// 函数说明: 无
void Write_IIC_Data(uint8_t IIC_Data)
{
i2c_status_type i2c_status;
uint8_t buff[2] = {0};
buff[0] = 0x40;
buff[1] = IIC_Data;
i2c_status = i2c_master_transmit(&hi2cx, I2Cx_ADDRESS, buff, 2, 1000);
if(i2c_status != I2C_OK)
{
printf("erro send %d",i2c_status);
}
}
之后初始化OLED
<blockquote>static void i2c_config_init(void)
OLED的数字,字符,字符串以及汉字等的驱动函数保持不变
void OLED_Set_Pos(unsigned char x, unsigned char 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)
{
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);
} //更新显示
}
void OLED_On(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(1,OLED_DATA);
} //更新显示
}
//在指定位置显示一个字符,包括部分字符
//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);
}
之后在主函数添加必要的头文件和OLED_Init();OLED_Clear()函数,显示内容如下
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的刷新率比较快,下一篇将制作动画来比较软件IIC和硬件IIC的刷新效果
|
打赏榜单
ArteryMCU 打赏了 50.00 元 2024-06-07 理由:[F405开发板评测活动]内容优质
共1人点赞
|