帮我下一个点亮LCD的stm32代码
时间: 2023-10-05 10:09:07 浏览: 48
下面是一个使用STM32的GPIO口点亮LCD的示例代码,你可以根据自己的需要进行修改。
```c
#include "stm32f10x.h"
#define LCD_RS GPIO_Pin_0
#define LCD_RW GPIO_Pin_1
#define LCD_EN GPIO_Pin_2
#define LCD_D4 GPIO_Pin_3
#define LCD_D5 GPIO_Pin_4
#define LCD_D6 GPIO_Pin_5
#define LCD_D7 GPIO_Pin_6
void delay(uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
void LCD_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = LCD_RS | LCD_RW | LCD_EN | LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA, LCD_RS | LCD_RW | LCD_EN | LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7);
delay(10000);
GPIO_ResetBits(GPIOA, LCD_RS | LCD_RW | LCD_EN);
GPIO_Write(GPIOA, 0x02 << 3);
GPIO_SetBit(GPIOA, LCD_EN);
delay(10000);
GPIO_ResetBit(GPIOA, LCD_EN);
delay(10000);
GPIO_Write(GPIOA, 0x08 << 3);
GPIO_SetBit(GPIOA, LCD_EN);
delay(10000);
GPIO_ResetBit(GPIOA, LCD_EN);
delay(10000);
GPIO_Write(GPIOA, 0x01 << 3);
GPIO_SetBit(GPIOA, LCD_EN);
delay(10000);
GPIO_ResetBit(GPIOA, LCD_EN);
delay(10000);
GPIO_Write(GPIOA, 0x06 << 3);
GPIO_SetBit(GPIOA, LCD_EN);
delay(10000);
GPIO_ResetBit(GPIOA, LCD_EN);
delay(10000);
GPIO_Write(GPIOA, 0x0C << 3);
GPIO_SetBit(GPIOA, LCD_EN);
delay(10000);
GPIO_ResetBit(GPIOA, LCD_EN);
delay(10000);
}
void LCD_SendByte(uint8_t dat, uint8_t cmd)
{
GPIO_Write(GPIOA, (dat & 0xF0) << 3);
if(cmd == 0)
GPIO_ResetBits(GPIOA, LCD_RS);
else
GPIO_SetBits(GPIOA, LCD_RS);
GPIO_ResetBits(GPIOA, LCD_RW);
GPIO_SetBits(GPIOA, LCD_EN);
delay(10000);
GPIO_ResetBits(GPIOA, LCD_EN);
GPIO_Write(GPIOA, (dat & 0x0F) << 7);
if(cmd == 0)
GPIO_ResetBits(GPIOA, LCD_RS);
else
GPIO_SetBits(GPIOA, LCD_RS);
GPIO_ResetBits(GPIOA, LCD_RW);
GPIO_SetBits(GPIOA, LCD_EN);
delay(10000);
GPIO_ResetBits(GPIOA, LCD_EN);
delay(10000);
}
void LCD_DisplayString(uint8_t x, uint8_t y, uint8_t *str)
{
uint8_t i;
if(x < 16)
{
x |= 0x80;
switch(y)
{
case 0: x |= 0x00; break;
case 1: x |= 0x40; break;
case 2: x |= 0x10; break;
case 3: x |= 0x50; break;
}
LCD_SendByte(x, 0);
}
i = 0;
while(str[i] != 0)
{
LCD_SendByte(str[i], 1);
i++;
}
}
int main(void)
{
LCD_Init();
LCD_DisplayString(0, 0, "Hello, World!");
while(1);
}
```
这个代码使用了GPIO口来控制LCD的各个引脚,首先对GPIO口进行了初始化,然后通过`LCD_SendByte`函数来发送数据和命令到LCD,最后通过`LCD_DisplayString`函数来在LCD上显示字符串。你可以根据自己的需要进行修改和扩展。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)