/* LCD1602引脚2(VDD)必须接5V电压, 不可以接3.3V电压。其他引脚(包括引脚15-背光电源正)都可以接3.3V电压 */
/* 必须把J-Link设为不供电(更改内部跳线), 然后另插上USB线,才能得到5V的电压 */
#include <stm32f10x.h>
#define RS_0 (GPIOA->BRR = GPIO_BRR_BR0)
#define RS_1 (GPIOA->BSRR = GPIO_BSRR_BS0)
#define RW_0 (GPIOA->BRR = GPIO_BRR_BR1)
#define RW_1 (GPIOA->BSRR = GPIO_BSRR_BS1)
#define E_0 (GPIOA->BRR = GPIO_BRR_BR2)
#define E_1 (GPIOA->BSRR = GPIO_BSRR_BS2)
void delay(void)
{
uint16_t i;
for (i = 0; i < 10; i++);
}
void LCD1602_BusyWait(void)
{
RS_0;
RW_1;
E_1;
GPIOC->CRL = 0x44444444; // 读端口
while (GPIOC->IDR & GPIO_IDR_IDR7);
GPIOC->CRL = 0x33333333;
E_0;
}
void LCD1602_WriteCmd(uint8_t cmd)
{
LCD1602_BusyWait();
RS_0;
RW_0;
GPIOC->ODR = cmd;
E_1;
delay();
E_0;
}
void LCD1602_WriteData(uint8_t data)
{
LCD1602_BusyWait();
RS_1;
RW_0;
GPIOC->ODR = data;
E_1;
delay();
E_0;
}
void LCD1602_WriteString(const char *s)
{
while (*s)
LCD1602_WriteData(*s++);
}
void LCD1602_Init(void)
{
LCD1602_WriteCmd(0x38);
LCD1602_WriteCmd(0x01);
LCD1602_WriteCmd(0x0c);
}
int main(void)
{
RCC->APB1ENR = RCC_APB1ENR_DACEN;
RCC->APB2ENR = RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPCEN;
GPIOA->CRL = 0x00000333; // RS, RW, E设为输出, PA4~5设为模拟输出, 其中PA4接LCD_3(对比度端口), PA5接电压表正极
GPIOC->CRL = 0x33333333; // 数据端口设为输出
// 设置背光电压
DAC->CR = DAC_CR_EN1 | DAC_CR_EN2;
DAC->DHR8R1 = 69; // 通道1为液晶提供对比度电压, 约1.0V
DAC->DHR8R2 = DAC->DHR8R1; // 通道2供电压表显示电压
LCD1602_Init();
LCD1602_WriteString("Hello World!");
LCD1602_WriteCmd(0xc0);
LCD1602_WriteString("STM32F103RCT6");
while (1);
}