#include <stdio.h> #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) { uint32_t i; for (i = 0; i < 8000000; i++); }
void delay_short(void) { uint8_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_short(); E_0; }
void LCD1602_WriteData(uint8_t data) { LCD1602_BusyWait(); RS_1; RW_0; GPIOC->ODR = data; E_1; delay_short(); E_0; }
void LCD1602_Init(void) { LCD1602_WriteCmd(0x38); LCD1602_WriteCmd(0x01); LCD1602_WriteCmd(0x0c); DAC->CR = DAC_CR_EN1 | DAC_CR_EN2; DAC->DHR8R1 = 69; // 对比度电压: 约1.0V, 端口: PA4 DAC->DHR8R2 = DAC->DHR8R1; // PA5输出同样大小的电压 }
// printf内容往液晶上显示 // 工程属性里的Use MicroLIB必须打勾 int fputc(int ch, FILE *fp) { LCD1602_WriteData(ch); return ch; }
int main(void) { uint8_t n = 0; RCC->APB1ENR = RCC_APB1ENR_DACEN; RCC->APB2ENR = RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPCEN | RCC_APB2ENR_ADC1EN; GPIOA->CRL = 0x00000333; // RS, RW, E设为输出, PA4~5为模拟输出, PA6为模拟输入 GPIOC->CRL = 0x33333333; // 1602DATA LCD1602_Init(); ADC1->SQR1 &= ~ADC_SQR1_L; // L=0000, 1 conversion ADC1->SQR3 = 6; // SQ1=PA6=ADC12_IN6 ADC1->CR2 = ADC_CR2_ADON; while (1) { ADC1->CR2 |= ADC_CR2_ADON; // 再次写1开始AD转换 while ((ADC1->SR & ADC_SR_EOC) == 0); // 等待转换完毕 LCD1602_WriteCmd(0x80); // 转到第一行 printf("ADC1->DR=%d ", ADC1->DR); LCD1602_WriteCmd(0xc0); // 转到第二行 printf("n=%d ", n++); delay(); } }
|