本帖最后由 1653149838.791300 于 2023-9-26 14:14 编辑
本篇在上篇基础上,用 CSM32RV003模拟I2C驱动oled显示1.硬件搭建
硬件接图展示如下:
连接oled上电后动态显示时照片
背面图,展示MCU与oled接线
2.相关代码
/*
*main.c
*/
//R-PA12
#define LED_R_GPIO GPIOA
#define LED_R_PIN PIN12
//R-PA9
#define LED_G_GPIO GPIOA
#define LED_G_PIN PIN9
//R-PA8
#define LED_B_GPIO GPIOA
#define LED_B_PIN PIN8
#define LED_R_ON GPIO_Write(LED_R_GPIO,LED_R_PIN,GPIO_RESET)
#define LED_R_OFF GPIO_Write(LED_R_GPIO,LED_R_PIN,GPIO_SET)
#define LED_G_ON GPIO_Write(LED_G_GPIO,LED_G_PIN,GPIO_RESET)
#define LED_G_OFF GPIO_Write(LED_G_GPIO,LED_G_PIN,GPIO_SET)
#define LED_B_ON GPIO_Write(LED_B_GPIO,LED_B_PIN,GPIO_RESET)
#define LED_B_OFF GPIO_Write(LED_B_GPIO,LED_B_PIN,GPIO_SET)
#define LED_ALL_OFF {LED_R_OFF;LED_G_OFF;LED_B_OFF;}
#define LED_ALL_ON {LED_R_ON;LED_G_ON;LED_B_ON;}
int main(void)
{
RESET->NRST_EN =0; //PA15初始化位外部复位引脚,低电平复位;
Delay32M_ms(1000);
///---------- System Init ----------///
CLIC_Init();//系统中断配置
// SysClock_RC_16M_Init();//系统时钟初始化:RC模式
SysClock_Crystal_Mode_Init();//系统时钟初始化:晶振模式
NVIC_EnableIRQ(Systick_IRQn);
SysTick_mtime->MTIME = 0;
SysTick_mtimecmp->MTIMECMP = 3200000-1; //Sstick时钟已经在初始化里10分频
UART_eeprintf_Init(UART1,115200); //如果切换非32M时钟,需要更改 SystemCoreClock宏定义值
ee_printf("SystemClk:%d\r\n", SystemCoreClock);
///----Chapter 5 GPIO Test Init ---------------------------------------------------------------------------------
GPIO_MODE_Init(LED_R_GPIO,LED_R_PIN,GPIO_MODE_OUTPUT); //GPIO 模式
GPIO_MODE_Init(LED_G_GPIO,LED_G_PIN,GPIO_MODE_OUTPUT); //GPIO 模式
GPIO_MODE_Init(LED_B_GPIO,LED_B_PIN,GPIO_MODE_OUTPUT); //GPIO 模式
LED_ALL_ON;
oled_init();
#if 0
for(uint8_t i=0;i<10;i++)
{
GPIO_Toggle(GPIOA,PIN8); //GPIO 翻转
Delay16M_ms(100);
}
while(1);
#else
while(1)
{
LED_ALL_OFF;
LED_R_ON;
Delay32M_ms(3000);
LED_ALL_OFF;
LED_G_ON;
Delay32M_ms(3000);
LED_ALL_OFF;
LED_B_ON;
Delay32M_ms(3000);
LED_ALL_ON;
Delay32M_ms(3000);
}
#endif
}
/*
*oled.c
*/
//OLED的初始化、配置
void oled_init(void)
{
uint8_t len_total,len_sigle;
// SDA
GPIO_MODE_Init(OLED_GPIO,OLED_SDA_PIN,GPIO_MODE_OUTPUT); //GPIO 模式 //B
// SCL
GPIO_MODE_Init(OLED_GPIO,OLED_SCL_PIN,GPIO_MODE_OUTPUT); //GPIO 模式 //G
//delay_ms(1000);
Delay32M_ms(500);//500
OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel
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 Set Mapping RAM Display Start Line (0x00~0x3F)
OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register
OLED_WR_Byte(0xB4,OLED_CMD);// Set SEG Output Current Brightness //设置亮度0xCF
OLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常
OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常
OLED_WR_Byte(0xA6,OLED_CMD);//--set normal display
OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
OLED_WR_Byte(0x1f,OLED_CMD);//--1/64 duty
OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
OLED_WR_Byte(0x00,OLED_CMD);//-not offset
OLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency
OLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec
OLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge period
OLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
OLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configuration
OLED_WR_Byte(0x00,OLED_CMD);
OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomh
OLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect Level
OLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)
OLED_WR_Byte(0x02,OLED_CMD);//
OLED_WR_Byte(0x8D,OLED_CMD);//--set Charge Pump enable/disable
OLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disable
OLED_WR_Byte(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5)
OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7)
OLED_WR_Byte(0xAF,OLED_CMD);
OLED_Clear();
OLED_DisplayTurn(1);
OLED_ColorTurn(0);
//test
len_total=sizeof(hz_oled)/sizeof(hz_oled[0]);
len_sigle=sizeof(hz_oled[0])/sizeof(unsigned char);
ee_printf("data row:%d cloum:%d \r\n",len_total,len_sigle);
//OLED_DisPlay_On();
memset(OLED_GRAM,0,sizeof(OLED_GRAM));
OLED_set_buff(0,0,hz_oled,0,5,2,sizeof(hz_oled)/sizeof(hz_oled[0]),sizeof(hz_oled[0])/sizeof(unsigned char),1,0);
OLED_set_buff(0,16,yw_oled,0,10,2,sizeof(yw_oled)/sizeof(yw_oled[0]),sizeof(yw_oled[0])/sizeof(unsigned char),1,0);
OLED_set_buff(80,16,hz_oled,5,3,2,sizeof(hz_oled)/sizeof(hz_oled[0]),sizeof(hz_oled[0])/sizeof(unsigned char),1,0);
OLED_Refresh();
Delay32M_ms(5000);
#if 1
OLED_WR_Byte(0x2E,OLED_CMD); //停止水平滚动
Delay32M_ms(1000);
OLED_WR_Byte(0x2A,OLED_CMD); //左右水平滚
OLED_WR_Byte(0x00,OLED_CMD); //dummy
OLED_WR_Byte(0x00,OLED_CMD); //滚动起始页
OLED_WR_Byte(0x00,OLED_CMD); //滚动帧速
OLED_WR_Byte(0x01,OLED_CMD); //滚动结束页
OLED_WR_Byte(0x00,OLED_CMD); //水平
OLED_WR_Byte(0x2F,OLED_CMD); //启动滚动运动
#endif
}
3.视频效果
见本文末尾
4.开发心得
在调试过程中,遇到编译出现下面问题
找了很久也没找到语法错误,也不知啥问题。后面调整优化减小全局变量,亦即减小RAM使用,编译通过,功能也正常展示出来了。
后过头找了下map.txt文件,隐藏在如下,估摸是资源消耗情况
5.总结
整体试用来说,芯片功能基本上没啥问题。也给些小小的建议:
(1)有些资料可以进一步完善。如版本管控,这个在烧录接线方面 对外界刚接触的 不了解更新哪些 会按旧资料做踩坑。
(2)IDE编译错误结果尽可能详细点提示,便于拍错调试。编译固件资源消耗直接编译结果后展示出来 尤其会较好些。IDE功能介绍可以更详尽些。
(3)驱动这块可按寄存器LL层到HAL层,再到app层,做完整的封装和模块化接口。对应用有些demo在里面可以直接借用,在市场上研发推广好用也很加分
不尽详言,整体体验不错。远再接再厉,一起都更上一层楼。
效果视频贴在这下面了
display-show
|