|
單片機源程序如下:
- /* 頭文件 ------------------------------------------------------------------*/
- #include "stm32f10x_lib.h"
- #include "stdio.h"
- #include "lcd1602.h"
- /* 自定義同義關(guān)鍵字 --------------------------------------------------------*/
- /* 自定義參數(shù)宏 --------------------------------------------------------*/
- /* 自定義函數(shù)宏 --------------------------------------------------------*/
- /* 自定義全局變量 --------------------------------------------------------*/
-
- /* 自定義函數(shù)聲明 --------------------------------------------------------*/
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- void USART_Configuration(void);
- void ADC_Configuration(void);
- void ADC_Configuration1(void);
- /*******************************************************************************
- * 函數(shù)名 : main
- * 函數(shù)描述 : 主函數(shù)
- * 輸入?yún)?shù) : 無
- * 輸出結(jié)果 : 無
- * 返回值 : 無
- *******************************************************************************/
- int main(void)
- {
-
- /* 設(shè)置系統(tǒng)時鐘 */
- RCC_Configuration();
- /* 設(shè)置 GPIO 端口 */
- // GPIO_Configuration();
- Lcd_GPIO_init();
-
- /* 設(shè)置 USART */
- // USART_Configuration();
- /* 設(shè)置 ADC */
- // printf("\r\n The AD_value is:-------------------------- \r\n");
- Lcd_Init();
- delay_us(200);
- Lcd_Puts(0,0,"My Designer! ");
- while(1)
- {
- GPIO_SetBits(GPIOC, GPIO_Pin_13) ;
- Lcd_Puts(0,0,"My Designer! ");
- delay_us(20000);
- GPIO_ResetBits(GPIOC, GPIO_Pin_13) ;
- delay_us(20000);
- }
- }
- /*******************************************************************************
- * 函數(shù)名 : RCC_Configuration
- * 函數(shù)描述 : 設(shè)置系統(tǒng)各部分時鐘
- * 輸入?yún)?shù) : 無
- * 輸出結(jié)果 : 無
- * 返回值 : 無
- *******************************************************************************/
- void RCC_Configuration(void)
- {
- /* 定義枚舉類型變量 HSEStartUpStatus */
- ErrorStatus HSEStartUpStatus;
- /* 復(fù)位系統(tǒng)時鐘設(shè)置 */
- RCC_DeInit();
- /* 開啟 HSE */
- RCC_HSEConfig(RCC_HSE_ON);
- /* 等待 HSE 起振并穩(wěn)定 */
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
- /* 判斷 HSE 起是否振成功,是則進入if()內(nèi)部 */
- if(HSEStartUpStatus == SUCCESS)
- {
- /* 選擇 HCLK(AHB)時鐘源為SYSCLK 1分頻 */
- RCC_HCLKConfig(RCC_SYSCLK_Div1);
- /* 選擇 PCLK2 時鐘源為 HCLK(AHB)1分頻 */
- RCC_PCLK2Config(RCC_HCLK_Div1);
- /* 選擇 PCLK1 時鐘源為 HCLK(AHB)2分頻 */
- RCC_PCLK1Config(RCC_HCLK_Div2);
- /* 設(shè)置 FLASH 延時周期數(shù)為2 */
- FLASH_SetLatency(FLASH_Latency_2);
- /* 使能 FLASH 預(yù)取緩存 */
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
- /* 選擇鎖相環(huán)(PLL)時鐘源為 HSE 1分頻,倍頻數(shù)為9,則PLL輸出頻率為 8MHz * 9 = 72MHz */
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
- /* 使能 PLL */
- RCC_PLLCmd(ENABLE);
- /* 等待 PLL 輸出穩(wěn)定 */
- while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
- /* 選擇 SYSCLK 時鐘源為 PLL */
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
- /* 等待 PLL 成為 SYSCLK 時鐘源 */
- while(RCC_GetSYSCLKSource() != 0x08);
- }
- /* 使能各個用到的外設(shè)時鐘 */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOC |
- RCC_APB2Periph_GPIOB, ENABLE);
- }
- /*******************************************************************************
- * 函數(shù)名 : GPIO_Configuration
- * 函數(shù)描述 : 設(shè)置各GPIO端口功能
- * 輸入?yún)?shù) : 無
- * 輸出結(jié)果 : 無
- * 返回值 : 無
- *******************************************************************************/
- void GPIO_Configuration(void)
- {
- /* 定義 GPIO 初始化結(jié)構(gòu)體 GPIO_InitStructure */
- GPIO_InitTypeDef GPIO_InitStructure;
- /* 設(shè)置 USART1 的Tx腳(PA.9)為第二功能推挽輸出功能 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA , &GPIO_InitStructure);
-
- /* 設(shè)置 USART1 的Rx腳(PA.10)為浮空輸入腳 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA , &GPIO_InitStructure);
- /* 將 PB.0 設(shè)置為模擬輸入腳 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
- GPIO_Init(GPIOB , &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
- GPIO_Init(GPIOB , &GPIO_InitStructure);
- }
- /*******************************************************************************
- * 函數(shù)名 : ADC_Configuration
- * 函數(shù)描述 : 初始化并啟動ADC轉(zhuǎn)換
- * 輸入?yún)?shù) : 無
- * 輸出結(jié)果 : 無
- * 返回值 : 無
- *******************************************************************************/
- void ADC_Configuration(void)
- {
- /* 定義 ADC 初始化結(jié)構(gòu)體 ADC_InitStructure */
- ADC_InitTypeDef ADC_InitStructure;
-
- /* 配置ADC時鐘分頻 */
- RCC_ADCCLKConfig(RCC_PCLK2_Div4);
- /*
- * 獨立工作模式;
- * 多通道掃描模式;
- * 連續(xù)模數(shù)轉(zhuǎn)換模式;
- * 轉(zhuǎn)換觸發(fā)方式:轉(zhuǎn)換由軟件觸發(fā)啟動;
- * ADC 數(shù)據(jù)右對齊 ;
- * 進行規(guī)則轉(zhuǎn)換的 ADC 通道的數(shù)目為1;
- */
- ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
- ADC_InitStructure.ADC_ScanConvMode = ENABLE;
- ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
- ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
- ADC_InitStructure.ADC_NbrOfChannel = 1;
- ADC_Init(ADC1, &ADC_InitStructure);
-
- /* 設(shè)置 ADC1 使用8轉(zhuǎn)換通道,轉(zhuǎn)換順序1,采樣時間為 55.5 周期 */
- ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 1, ADC_SampleTime_71Cycles5);
- /* 使能 ADC1 */
- ADC_Cmd(ADC1, ENABLE);
- /* 復(fù)位 ADC1 的校準(zhǔn)寄存器 */
- ADC_ResetCalibration(ADC1);
- /* 等待 ADC1 校準(zhǔn)寄存器復(fù)位完成 */
- while(ADC_GetResetCalibrationStatus(ADC1));
- /* 開始 ADC1 校準(zhǔn) */
- ADC_StartCalibration(ADC1);
- /* 等待 ADC1 校準(zhǔn)完成 */
- while(ADC_GetCalibrationStatus(ADC1));
- /* 啟動 ADC1 轉(zhuǎn)換 */
- ADC_SoftwareStartConvCmd(ADC1, ENABLE);
- }
- void ADC_Configuration1(void)
- {
- /* 定義 ADC 初始化結(jié)構(gòu)體 ADC_InitStructure */
- ADC_InitTypeDef ADC_InitStructure;
-
- /* 配置ADC時鐘分頻 */
- RCC_ADCCLKConfig(RCC_PCLK2_Div4);
- /*
- * 獨立工作模式;
- * 多通道掃描模式;
- * 連續(xù)模數(shù)轉(zhuǎn)換模式;
- * 轉(zhuǎn)換觸發(fā)方式:轉(zhuǎn)換由軟件觸發(fā)啟動;
- * ADC 數(shù)據(jù)右對齊 ;
- * 進行規(guī)則轉(zhuǎn)換的 ADC 通道的數(shù)目為1;
- */
- ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
- ADC_InitStructure.ADC_ScanConvMode = ENABLE;
- ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
- ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
- ADC_InitStructure.ADC_NbrOfChannel = 1;
- ADC_Init(ADC1, &ADC_InitStructure);
-
- /* 設(shè)置 ADC1 使用8轉(zhuǎn)換通道,轉(zhuǎn)換順序1,采樣時間為 55.5 周期 */
- ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_55Cycles5);
- /* 使能 ADC1 */
- ADC_Cmd(ADC1, ENABLE);
- /* 復(fù)位 ADC1 的校準(zhǔn)寄存器 */
- ADC_ResetCalibration(ADC1);
- /* 等待 ADC1 校準(zhǔn)寄存器復(fù)位完成 */
- while(ADC_GetResetCalibrationStatus(ADC1));
- /* 開始 ADC1 校準(zhǔn) */
- ADC_StartCalibration(ADC1);
- /* 等待 ADC1 校準(zhǔn)完成 */
- while(ADC_GetCalibrationStatus(ADC1));
- /* 啟動 ADC1 轉(zhuǎn)換 */
- ADC_SoftwareStartConvCmd(ADC1, ENABLE);
- }
- /*******************************************************************************
- * 函數(shù)名 : USART_Configuration
- * 函數(shù)描述 : 設(shè)置USART1
- * 輸入?yún)?shù) : 無
- * 輸出結(jié)果 : 無
- * 返回值 : 無
- *******************************************************************************/
- void USART_Configuration(void)
- {
- /* 定義 USART 初始化結(jié)構(gòu)體 USART_InitStructure */
- USART_InitTypeDef USART_InitStructure;
- /* 波特率為115200bps;
- * 8位數(shù)據(jù)長度;
- * 1個停止位,無校驗;
- * 禁用硬件流控制;
- * 禁止USART時鐘;
- * 時鐘極性低;
- * 在第2個邊沿捕獲數(shù)據(jù)
- * 最后一位數(shù)據(jù)的時鐘脈沖不從 SCLK 輸出;
- */
- USART_InitStructure.USART_BaudRate = 115200;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No ;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(USART1 , &USART_InitStructure);
-
- /* 使能 USART1 */
- USART_Cmd(USART1 , ENABLE);
- }
-
- /*******************************************************************************
- * 函數(shù)名 : fputc
- * 函數(shù)描述 : 將printf函數(shù)重定位到USATR1
- * 輸入?yún)?shù) : 無
- * 輸出結(jié)果 : 無
- * 返回值 : 無
- *******************************************************************************/
- int fputc(int ch, FILE *f)
- {
- USART_SendData(USART1, (u8) ch);
- while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
- return ch;
- }
復(fù)制代碼
所有資料51hei提供下載:
Stm32驅(qū)動LCD1602.rar
(208.75 KB, 下載次數(shù): 69)
2019-2-13 16:30 上傳
點擊文件名下載附件
|
評分
-
查看全部評分
|