hc32l13x lcd分别点亮每个灯示例代码
时间: 2023-07-03 10:09:03 浏览: 102
以下是一个使用hc32l13x控制LCD分别点亮每个灯的示例代码:
```
#include "hc32l13x.h"
#define LCD_DATA_PORT M4_PORT->P4OUT
#define LCD_CTRL_PORT M4_PORT->P5OUT
#define LCD_RS_PIN M4_PORT_Pin_5
#define LCD_RW_PIN M4_PORT_Pin_6
#define LCD_EN_PIN M4_PORT_Pin_7
#define LCD_DATA_PIN_4 M4_PORT_Pin_0
#define LCD_DATA_PIN_5 M4_PORT_Pin_1
#define LCD_DATA_PIN_6 M4_PORT_Pin_2
#define LCD_DATA_PIN_7 M4_PORT_Pin_3
void LCD_WriteCmd(uint8_t cmd)
{
LCD_CTRL_PORT &= ~(LCD_RS_PIN | LCD_RW_PIN);
LCD_DATA_PORT = cmd;
LCD_CTRL_PORT |= LCD_EN_PIN;
LCD_CTRL_PORT &= ~LCD_EN_PIN;
}
void LCD_WriteData(uint8_t data)
{
LCD_CTRL_PORT |= LCD_RS_PIN;
LCD_CTRL_PORT &= ~LCD_RW_PIN;
LCD_DATA_PORT = data;
LCD_CTRL_PORT |= LCD_EN_PIN;
LCD_CTRL_PORT &= ~LCD_EN_PIN;
}
void LCD_Init(void)
{
// Initialization sequence for HD44780 LCD controller
LCD_WriteCmd(0x38);
LCD_WriteCmd(0x0C);
LCD_WriteCmd(0x06);
LCD_WriteCmd(0x01);
}
int main(void)
{
// Enable GPIO clock
M4_PORT->PER |= (M4_PORT_Pin_0 | M4_PORT_Pin_1 | M4_PORT_Pin_2 | M4_PORT_Pin_3 | M4_PORT_Pin_5 | M4_PORT_Pin_6 | M4_PORT_Pin_7);
M4_PORT->PCR[M4_PORT_Pin_0] |= (PORT_FUNC_GPIO | PORT_DIR_OUT);
M4_PORT->PCR[M4_PORT_Pin_1] |= (PORT_FUNC_GPIO | PORT_DIR_OUT);
M4_PORT->PCR[M4_PORT_Pin_2] |= (PORT_FUNC_GPIO | PORT_DIR_OUT);
M4_PORT->PCR[M4_PORT_Pin_3] |= (PORT_FUNC_GPIO | PORT_DIR_OUT);
M4_PORT->PCR[M4_PORT_Pin_5] |= (PORT_FUNC_GPIO | PORT_DIR_OUT);
M4_PORT->PCR[M4_PORT_Pin_6] |= (PORT_FUNC_GPIO | PORT_DIR_OUT);
M4_PORT->PCR[M4_PORT_Pin_7] |= (PORT_FUNC_GPIO | PORT_DIR_OUT);
// Initialize LCD
LCD_Init();
// Turn on each light one by one
for (uint8_t i = 0; i < 8; i++)
{
LCD_WriteCmd(0x80 + i);
LCD_WriteData(1 << i);
}
while (1)
{
// Loop forever
}
}
```
该示例代码使用了hc32l13x的GPIO模块来控制LCD的数据和控制线。LCD初始化后,使用`LCD_WriteCmd`和`LCD_WriteData`函数来向LCD发送命令和数据。通过循环来逐个点亮每个灯。
阅读全文