本帖最后由 jinglixixi 于 2021-12-9 16:09 编辑
本打算先解决GPIO口输入的问题,从而以按键来控制LED,但没有解决任何读取按键的状态,于是只好继续用GPIO的输出能力来驱动一个LCD5110显示屏。相较于I2C接口的OLED屏,它显示的信息会更多一些。
该显示屏是以SPI接口来工作的,它与开发板的连接关系如下: SCE ---P301
RST ---P302
D/C ---P201
SDIN---P401
SCLK---P400
使用GPIO口输出更多电平的语句定义为:
#define LCD_RST_Clr() R_PORT3->PODR &= ~(1 <<1); //301
#define LCD_RST_Set() R_PORT3->PODR |= (1 << 1);
#define LCD_CE_Clr() R_PORT3->PODR &= ~(1 <<2); //302
#define LCD_CE_Set() R_PORT3->PODR |= (1 << 2);
#define LCD_DC_Clr() R_PORT2->PODR &= ~(1 <<1); //201
#define LCD_DC_Set() R_PORT2->PODR |= (1 << 1);
#define SCLK_Clr() R_PORT4->PODR &= ~(1 <<0); //400
#define SCLK_Set() R_PORT4->PODR |= (1 << 0);
#define SDIN_Clr() R_PORT4->PODR &= ~(1 <<1); //401
#define SDIN_Set() R_PORT4->PODR |= (1 << 1);
实现字节数据发送的函数为:
- void LCD_write_byte(unsigned char dat, unsigned char command)
- {
- unsigned char i;
- LCD_CE_Clr();
- rt_hw_us_delay(2);
- if(command==0)
- {
- LCD_DC_Clr();
- }
- else
- {
- LCD_DC_Set();
- }
- rt_hw_us_delay(2);
- for(i=0;i<8;i++)
- {
- if(dat&0x80)
- {
- SDIN_Set();
- }
- else
- {
- SDIN_Clr();
- }
- rt_hw_us_delay(2);
- SCLK_Clr();
- dat=dat<<1;
- rt_hw_us_delay(2);
- SCLK_Set();
- rt_hw_us_delay(2);
- }
- LCD_CE_Set();
- }
显示屏的初始化函数为:
- void LCD_init(void)
- {
- LCD_RST_Clr();
- rt_hw_us_delay(1);
- LCD_RST_Set();
- LCD_CE_Clr();
- rt_hw_us_delay(1);
- LCD_CE_Set();
- rt_hw_us_delay(1);
- LCD_write_byte(0x21, 0);
- LCD_write_byte(0xa0, 0);
- LCD_write_byte(0x07, 0);
- LCD_write_byte(0x17, 0);
- LCD_write_byte(0x20, 0);
- LCD_clear();
- LCD_write_byte(0x0c, 0);
- LCD_CE_Clr();
- }
显示屏的清屏函数为:
- void LCD_clear(void)
- {
- unsigned int i;
- LCD_write_byte(0x0c, 0);
- LCD_write_byte(0x80, 0);
- for (i=0; i<504; i++)
- LCD_write_byte(0, 1);
- }
实现显示效果的主程序为:
- int main(void)
- {
- R_PORT1->PDR |= (1 << 3);
- R_PORT1->PDR |= (1 << 4);
- R_PORT4->PDR |= (1 << 0);
- R_PORT4->PDR |= (1 << 1);
- R_PORT2->PDR |= (1 << 1);
- R_PORT3->PDR |= (1 << 1);
- R_PORT3->PDR |= (1 << 2);
- LCD_init();
- LCD_clear();
- LCD_write_english_string(0,0," Welcome to ");
- LCD_write_english_string(0,1," Amy studio ");
- LCD_write_english_string(0,2,"amy-studio.com");
- LCD_write_english_string(0,3," Nokia5110 LCD");
- LCD_write_chinese_string(12,4,12,4,0,5);
- while(1)
- {
- R_PORT1->PODR |= (1 << 3);
- R_PORT1->PODR |= (1 << 4);
- delay();
- R_PORT1->PODR &= ~(1 << 3);
- R_PORT1->PODR &= ~(1 << 4);
- delay();
- }
- }
经编译和下载,其运行效果如图2所示。
图1 下载过程
图2 运行效果
图3 运行效果
图4 运行效果