/* 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);
}