stm32f103c8t6驱动1602 LCD代码
时间: 2023-05-26 21:07:31 浏览: 100
以下是stm32f103c8t6驱动1602 LCD的代码示例:
```
#include "stm32f10x.h"
#include "delay.h"
#define LCD_Port GPIOB
#define LCD_RS_Pin GPIO_Pin_12
#define LCD_E_Pin GPIO_Pin_13
#define LCD_D4_Pin GPIO_Pin_14
#define LCD_D5_Pin GPIO_Pin_15
#define LCD_D6_Pin GPIO_Pin_6
#define LCD_D7_Pin GPIO_Pin_7
void LCD_E(unsigned char is_high)
{
if(is_high)
GPIO_SetBits(LCD_Port, LCD_E_Pin);
else
GPIO_ResetBits(LCD_Port, LCD_E_Pin);
}
void LCD_RS(unsigned char is_data)
{
if(is_data)
GPIO_SetBits(LCD_Port, LCD_RS_Pin);
else
GPIO_ResetBits(LCD_Port, LCD_RS_Pin);
}
void LCD_Write(unsigned char data)
{
GPIO_WriteBit(LCD_Port, LCD_D7_Pin, (data & 0x80) >> 7);
GPIO_WriteBit(LCD_Port, LCD_D6_Pin, (data & 0x40) >> 6);
GPIO_WriteBit(LCD_Port, LCD_D5_Pin, (data & 0x20) >> 5);
GPIO_WriteBit(LCD_Port, LCD_D4_Pin, (data & 0x10) >> 4);
LCD_E(1);
delay_ms(1);
LCD_E(0);
delay_ms(1);
GPIO_WriteBit(LCD_Port, LCD_D7_Pin, (data & 0x08) >> 3);
GPIO_WriteBit(LCD_Port, LCD_D6_Pin, (data & 0x04) >> 2);
GPIO_WriteBit(LCD_Port, LCD_D5_Pin, (data & 0x02) >> 1);
GPIO_WriteBit(LCD_Port, LCD_D4_Pin, (data & 0x01) >> 0);
LCD_E(1);
delay_ms(1);
LCD_E(0);
delay_ms(1);
}
void LCD_Init()
{
//设置GPIOB的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
//设置LCD的引脚为输出
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin = LCD_RS_Pin | LCD_E_Pin | LCD_D4_Pin | LCD_D5_Pin | LCD_D6_Pin | LCD_D7_Pin;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LCD_Port, &GPIO_InitStruct);
//初始化LCD
delay_ms(20);
LCD_RS(0);
LCD_Write(0x03);
delay_ms(5);
LCD_Write(0x03);
delay_ms(1);
LCD_Write(0x03);
delay_ms(1);
LCD_Write(0x02);
delay_ms(1);
LCD_Write(0x28); //设置4位数据,2行,5x8点阵的字符
delay_ms(1);
LCD_Write(0x08); //关闭显示器
delay_ms(1);
LCD_Write(0x01); //清除显示
delay_ms(1);
LCD_Write(0x06); //设置文字在屏幕左边,文字不动
delay_ms(1);
LCD_Write(0x0C); //打开显示器,关闭光标
delay_ms(1);
}
void LCD_Print(unsigned char row, unsigned char col, char *str)
{
unsigned char addr;
if(row == 1)
addr = col - 1;
else
addr = col - 1 + 0x40;
LCD_RS(0);
LCD_Write(0x80 | addr); //设置写入地址
delay_ms(1);
LCD_RS(1);
while(*str)
{
LCD_Write(*str);
str++;
}
}
int main(void)
{
LCD_Init();
char *str1 = "Hello";
char *str2 = "World";
while(1)
{
LCD_Print(1, 1, str1);
LCD_Print(2, 1, str2);
}
}
```
请注意,此代码示例使用GPIOB口来控制1602 LCD显示屏,您需要修改代码中的端口号和引脚号以适应您的使用情况。此外,还可能需要根据您的实际情况修改其他参数及函数调用。
阅读全文