- // LCD1602.h
- #ifndef _BSP_LCD1602_H
- #define _BSP_LCD1602_H
- #include "stm32f10x.h"
- #include "stm32f10x_gpio.h"
- #include "bsp_SysTick.h"
- #define LCD1602_CLK RCC_APB2Periph_GPIOB
- #define LCD1602_GPIO_PORT GPIOB
- #define LCD1602_E GPIO_Pin_10 //定義使能引腳
- #define LCD1602_RW GPIO_Pin_11 //定義讀寫引腳
- #define LCD1602_RS GPIO_Pin_12 //定義數據、命名引腳
- #define EO(X) X? (GPIO_SetBits(LCD1602_GPIO_PORT,LCD1602_E)):(GPIO_ResetBits(LCD1602_GPIO_PORT,LCD1602_E))
- #define RWO(X) X? (GPIO_SetBits(LCD1602_GPIO_PORT,LCD1602_RW)):(GPIO_ResetBits(LCD1602_GPIO_PORT,LCD1602_RW))
- #define RSO(X) X? (GPIO_SetBits(LCD1602_GPIO_PORT,LCD1602_RS)):(GPIO_ResetBits(LCD1602_GPIO_PORT,LCD1602_RS))
- //只能是某個GPIO口的低八位
- #define DB0 GPIO_Pin_0
- #define DB1 GPIO_Pin_1
- #define DB2 GPIO_Pin_2
- #define DB3 GPIO_Pin_3
- #define DB4 GPIO_Pin_4
- #define DB5 GPIO_Pin_5
- #define DB6 GPIO_Pin_6
- #define DB7 GPIO_Pin_7
- void LCD1602_Init(void); //初始化LCD602;
- void LCD1602_ShowStr(uint8_t x, uint8_t y, uint8_t *str,uint8_t len);
- void LCD_ShowNum(uint8_t x, uint8_t y,uint8_t num);
- void LCD_ShowChar(uint8_t x, uint8_t y,uint8_t dat);
- #endif //_BSP_LCD1602_H
復制代碼
#include "adc.h"
void ADC1_GPIO_Config(void){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_ADC1, ENABLE); //使能ADC1,GPIOC時鐘
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//為什么沒有配置這個????
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //模擬輸入
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PC4
}
void ADC_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;//ADC結構體變量//注意在一個語句快內變量的聲明要放在可執行語句的前面,否則出錯,因此要放在ADC1_GPIO_Config();前面
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;//ADC1和ADC2工作在獨立模式
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //使能掃描
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//ADC轉換工作在連續模式
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//由軟件控制轉換,不使用外部觸發
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//轉換數據右對齊
ADC_InitStructure.ADC_NbrOfChannel = 1;//轉換通道為1
ADC_Init(ADC1, &ADC_InitStructure); //初始化ADC
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_55Cycles5);
//ADC1選擇信道14,音序等級1,采樣時間55.5個周期
// ADC_DMACmd(ADC1, ENABLE);//使能ADC1模塊DMA
ADC_Cmd(ADC1, ENABLE);//使能ADC1
ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
// ADC_ResetCalibration(ADC1); //重置.(復位).ADC1校準寄存器
// while(ADC_GetResetCalibrationStatus(ADC1));//等待ADC1校準重置完成
// ADC_StartCalibration(ADC1);//開始ADC1校準
// while(ADC_GetCalibrationStatus(ADC1));//等待ADC1校準完成
// ADC_SoftwareStartConvCmd(ADC1, ENABLE); //使能ADC1軟件開始轉換
}
- //adc.h
- #ifndef __ADC_H
- #define __ADC_H
- #include "sys.h"
- void Adc_Init(void);
- void ADC1_GPIO_Config(void);
- void ADC_Config(void);
- #endif
復制代碼- #include "bsp-lcd1602.h"
- void LCD1602_GPIO_Config(void)
- {
- RCC_APB2PeriphClockCmd(LCD1602_CLK, ENABLE);
- GPIO_InitTypeDef LCD1602_GPIOStruct;
- LCD1602_GPIOStruct.GPIO_Mode = GPIO_Mode_Out_PP;
- LCD1602_GPIOStruct.GPIO_Speed = GPIO_Speed_10MHz;
- LCD1602_GPIOStruct.GPIO_Pin = LCD1602_E | LCD1602_RS | LCD1602_RW ;
- GPIO_Init(LCD1602_GPIO_PORT,&LCD1602_GPIOStruct);
- LCD1602_GPIOStruct.GPIO_Mode = GPIO_Mode_Out_OD;
- LCD1602_GPIOStruct.GPIO_Pin = DB0 | DB1 | DB2 |DB3 | DB4 | DB5|
- DB6 | DB7 ; //設置為開漏輸出
- GPIO_Init(LCD1602_GPIO_PORT,&LCD1602_GPIOStruct);
- }
- void LCD1602_WaitReady(void) //檢測忙狀態
- {
- uint8_t sta;
- GPIOB->ODR =0x00FF;
- RSO(0);
- RWO(1);
- EO(1);
- SysTick_Delay_Us(1);
- do{
- sta=GPIO_ReadInputDataBit(LCD1602_GPIO_PORT,GPIO_Pin_7);
- EO(0);
- }while(sta);
- }
- void LCD1602_WriteCmd(uint8_t cmd) //寫指令
- {
- LCD1602_WaitReady();
- RSO(0);
- RWO(0);
- EO(0);
- SysTick_Delay_Us(1);
- EO(1);
- LCD1602_GPIO_PORT->ODR &= (cmd|0xFF00);
- EO(0);
- SysTick_Delay_Us(400);
- }
- void LCD1602_WriteDat(uint8_t dat) //寫數據
- {
- LCD1602_WaitReady();
- RSO(1);
- RWO(0);
- SysTick_Delay_Us(30);
- EO(1);
- LCD1602_GPIO_PORT->ODR &=(dat|0xFF00);
- EO(0);
- SysTick_Delay_Us(400);
- }
- void LCD1602_SetCursor(uint8_t x, uint8_t y)
- {
- uint8_t addr;
-
- if (y == 0) //由輸入的屏幕坐標計算顯示RAM的地址
- addr = 0x00 + x; //第一行字符地址從0x00起始
- else
- addr = 0x40 + x; //第二行字符地址從0x40起始
- LCD1602_WriteCmd(addr|0x80); //設置RAM地址
- }
- void LCD1602_ShowStr(uint8_t x, uint8_t y, uint8_t *str, uint8_t len)
- {
- LCD1602_SetCursor(x, y); //設置起始地址
- while (len--) //連續寫入len個字符數據
- {
- LCD1602_WriteDat(*str++);
- }
- }
- //??1???
- //x,y :????
- //num:??(0~99)
- //-----------------------------*/
- void LCD_ShowNum(uint8_t x, uint8_t y,uint8_t num)
- {
- LCD1602_SetCursor(x, y); //設置起始地址
- LCD_ShowChar(x,y,num+'0');
-
- }
- void LCD_ShowChar(uint8_t x, uint8_t y,uint8_t dat)
- {
- LCD1602_SetCursor(x, y); //設置起始地址
- LCD1602_WriteDat(dat);
- }
- void LCD1602_Init(void)
- {
- LCD1602_GPIO_Config(); //開啟GPIO口
- LCD1602_WriteCmd(0X38); //16*2顯示,5*7點陣,8位數據接口
- LCD1602_WriteCmd(0x0C); //顯示器開,光標關閉
- LCD1602_WriteCmd(0x06); //文字不動,地址自動+1
- LCD1602_WriteCmd(0x01); //清屏
- }
-
-
復制代碼- int main()
- #include "stm32f10x.h"
- #include "bsp-lcd1602.h"
- #include "delay.h"
- #include "sys.h"
- #include "adc.h"
- int main(void)
- {
- int a,b,c,d;
- float temp;
-
- delay_init(); //延時函數初始化
- LCD1602_Init();
- ADC1_GPIO_Config();
- ADC_Config();
- LCD1602_ShowStr(1,0,"adcvalue=0.0V",13);
- LCD1602_ShowStr(1,1,"LIUTONGBO 32",13);
-
- while(1)
- {
- b=ADC_GetConversionValue(ADC1);
- temp=(float)b*(3.4/4096);
- a=temp/0.675;
- c=temp*10;
- d=c%10;
- LCD_ShowNum(10,0,a);
- LCD_ShowNum(12,0,d);
- }
- }
復制代碼
|