stm32f103c8t6与lcd1602连接的keil 程序
时间: 2023-08-01 21:11:12 浏览: 98
以下是 STM32F103C8T6 与 LCD1602 连接的 Keil 程序示例:
```
#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_4
#define LCD_D5 GPIO_Pin_5
#define LCD_D6 GPIO_Pin_6
#define LCD_D7 GPIO_Pin_7
void delay_ms(uint32_t ms) {
uint32_t i, j;
for (i = 0; i < ms; i++) {
for (j = 0; j < 2000; j++);
}
}
void LCD_Write4Bits(uint8_t data) {
GPIO_WriteBit(GPIOB, LCD_D4, (data & 0x01) ? Bit_SET : Bit_RESET);
GPIO_WriteBit(GPIOB, LCD_D5, (data & 0x02) ? Bit_SET : Bit_RESET);
GPIO_WriteBit(GPIOB, LCD_D6, (data & 0x04) ? Bit_SET : Bit_RESET);
GPIO_WriteBit(GPIOB, LCD_D7, (data & 0x08) ? Bit_SET : Bit_RESET);
}
void LCD_SendCommand(uint8_t cmd) {
GPIO_ResetBits(GPIOB, LCD_RS);
GPIO_ResetBits(GPIOB, LCD_RW);
LCD_Write4Bits(cmd >> 4);
GPIO_SetBits(GPIOB, LCD_EN);
GPIO_ResetBits(GPIOB, LCD_EN);
LCD_Write4Bits(cmd);
GPIO_SetBits(GPIOB, LCD_EN);
GPIO_ResetBits(GPIOB, LCD_EN);
delay_ms(2);
}
void LCD_SendData(uint8_t data) {
GPIO_SetBits(GPIOB, LCD_RS);
GPIO_ResetBits(GPIOB, LCD_RW);
LCD_Write4Bits(data >> 4);
GPIO_SetBits(GPIOB, LCD_EN);
GPIO_ResetBits(GPIOB, LCD_EN);
LCD_Write4Bits(data);
GPIO_SetBits(GPIOB, LCD_EN);
GPIO_ResetBits(GPIOB, LCD_EN);
delay_ms(2);
}
void LCD_Init(void) {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = LCD_RS | LCD_RW | LCD_EN | LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
delay_ms(50);
LCD_SendCommand(0x03);
delay_ms(5);
LCD_SendCommand(0x03);
delay_ms(5);
LCD_SendCommand(0x03);
delay_ms(5);
LCD_SendCommand(0x02);
delay_ms(5);
LCD_SendCommand(0x28);
LCD_SendCommand(0x08);
LCD_SendCommand(0x01);
LCD_SendCommand(0x06);
LCD_SendCommand(0x0C);
}
int main(void) {
LCD_Init();
while (1) {
LCD_SendCommand(0x80);
LCD_SendData('H');
LCD_SendData('e');
LCD_SendData('l');
LCD_SendData('l');
LCD_SendData('o');
LCD_SendData(',');
LCD_SendCommand(0xC0);
LCD_SendData('W');
LCD_SendData('o');
LCD_SendData('r');
LCD_SendData('l');
LCD_SendData('d');
delay_ms(1000);
}
}
```
这里使用的是 GPIOB 端口,连接方式如下:
LCD1602 | STM32F103C8T6
--------|--------------
RS | PB0
RW | PB1
EN | PB2
D4 | PB4
D5 | PB5
D6 | PB6
D7 | PB7
注意:这只是一个示例程序,实际上还需要根据具体的硬件连接和需求进行修改。
阅读全文