STM32單片機學習(10) 數(shù)碼管輸出實驗補 靜態(tài)(共陽)+動態(tài)(共陰)
參考手冊、電路圖等參見STM32單片機學習(1) 總記 學習資料+參考手冊+LED燈
本文引用地址:http://m.butianyuan.cn/article/201611/318252.htm靜態(tài)數(shù)碼管實驗
/** * 軟件功能: 靜態(tài)數(shù)碼管實驗(軟件延時方式)* */#include "stm32f10x.h"#include "delay.h"void RCC_Configuration(void);void GPIO_Configuration(void);u8 Count=0;// 此表為 LED 的字模 0-f,"." unsigned char LED7Code[] = {~0x3F,~0x06,~0x5B,~0x4F,~0x66,~0x6D,~0x7D,~0x07,~0x7F,~0x6F,~0x77,~0x7C,~0x39,~0x5E,~0x79,~0x71,~0x80};/*函數(shù): int main(void)功能: main主函數(shù)參數(shù): 無返回: 無/int main(void){RCC_Configuration();GPIO_Configuration();delay_init(72);while(1){GPIO_Write(GPIOB,LED7Code[Count%17]<<8); //將Count數(shù)據(jù)送到GPIOB高八位Count++;delay_ms(500);//延時} }/*函數(shù): void RCC_Configuration(void)功能: 復位和時鐘控制 配置參數(shù): 無返回: 無/void RCC_Configuration(void){ErrorStatus HSEStartUpStatus; //定義外部高速晶體啟動狀態(tài)枚舉變量RCC_DeInit(); //復位RCC外部設備寄存器到默認值RCC_HSEConfig(RCC_HSE_ON); //打開外部高速晶振HSEStartUpStatus = RCC_WaitForHSEStartUp(); //等待外部高速時鐘準備好if(HSEStartUpStatus == SUCCESS) //外部高速時鐘已經(jīng)準別好{FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //開啟FLASH預讀緩沖功能,加速FLASH的讀取。所有程序中必須的用法.位置:RCC初始化子函數(shù)里面,時鐘起振之后FLASH_SetLatency(FLASH_Latency_2); //flash操作的延時RCC_HCLKConfig(RCC_SYSCLK_Div1); //配置AHB(HCLK)時鐘等于==SYSCLKRCC_PCLK2Config(RCC_HCLK_Div1); //配置APB2(PCLK2)鐘==AHB時鐘RCC_PCLK1Config(RCC_HCLK_Div2); //配置APB1(PCLK1)鐘==AHB1/2時鐘RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); //配置PLL時鐘 == 外部高速晶體時鐘 * 9 = 72MHzRCC_PLLCmd(ENABLE); //使能PLL時鐘while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) //等待PLL時鐘就緒{}RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //配置系統(tǒng)時鐘 = PLL時鐘while(RCC_GetSYSCLKSource() != 0x08) //檢查PLL時鐘是否作為系統(tǒng)時鐘{}}RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //允許 GPIOB、AFIO時鐘}/*函數(shù): void GPIO_Configuration(void)功能: GPIO配置參數(shù): 無返回: 無/void GPIO_Configuration(void){GPIO_InitTypeDef GPIO_InitStructure; //定義GPIO初始化結構體GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); //PB用于輸出控制LED燈}
動態(tài)數(shù)碼管實驗(74LS138譯碼器)
/** * 軟件功能: 動態(tài)數(shù)碼管實驗(軟件延時方式)* */#include "stm32f10x.h"#include "delay.h"/硬件接口定義*/#define LS138A GPIO_Pin_0 //=P2^7; //138譯碼器管腳定義#define LS138B GPIO_Pin_1 //=P2^6;#define LS138C GPIO_Pin_2 //=P2^5; /宏定義*/#define LS138a(x) x ? GPIO_SetBits(GPIOB, LS138A): GPIO_ResetBits(GPIOB, LS138A)#define LS138b(x) x ? GPIO_SetBits(GPIOB, LS138B): GPIO_ResetBits(GPIOB, LS138B)#define LS138c(x) x ? GPIO_SetBits(GPIOB, LS138C): GPIO_ResetBits(GPIOB, LS138C)u8 i=0,j;//此表為 LED 的字模, 共陰數(shù)碼管 0-9 小數(shù)點 不顯示 "0." 0x3F|0x80unsigned char Digit[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x80,0x00};void RCC_Configuration(void);void GPIO_Configuration(void);/*函數(shù): int main(void)功能: main主函數(shù)參數(shù): 無返回: 無/int main(void){RCC_Configuration();GPIO_Configuration();delay_init(72);while(1){for(i=0; i<8; i++) { switch(i) { //138譯碼case 0: //0 0 0 LS138a(0); LS138b(0); LS138c(0);GPIO_Write(GPIOA,0x00);break; case 1: //1 0 0 CBA 001LS138a(1); LS138b(0); LS138c(0);GPIO_Write(GPIOA,Digit[i]);break; case 2: //0 1 0LS138a(0); LS138b(1); LS138c(0);GPIO_Write(GPIOA,Digit[10]); //小數(shù)點 break; case 3: //1 1 0 //注意是倒過來的 CBA 011LS138a(1); LS138b(1); LS138c(0);GPIO_Write(GPIOA,Digit[i]);break;case 4: //0 0 1LS138a(0); LS138b(0); LS138c(1); GPIO_Write(GPIOA,Digit[i]);break; case 5: //1 0 1LS138a(1); LS138b(0); LS138c(1);GPIO_Write(GPIOA,Digit[i]);break; case 6: //0 1 1LS138a(0); LS138b(1); LS138c(1);GPIO_Write(GPIOA,Digit[i]);break;case 7: //1 1 1LS138a(1); LS138b(1); LS138c(1);GPIO_Write(GPIOA,Digit[i]|0x80); //數(shù)字附帶小數(shù)點break; }delay_us(10); GPIO_Write(GPIOA,0x00); //不顯示} } }/*函數(shù): void RCC_Configuration(void)功能: 復位和時鐘控制 配置參數(shù): 無返回: 無/void RCC_Configuration(void){ErrorStatus HSEStartUpStatus; //定義外部高速晶體啟動狀態(tài)枚舉變量RCC_DeInit(); //復位RCC外部設備寄存器到默認值RCC_HSEConfig(RCC_HSE_ON); //打開外部高速晶振HSEStartUpStatus = RCC_WaitForHSEStartUp(); //等待外部高速時鐘準備好if(HSEStartUpStatus == SUCCESS) //外部高速時鐘已經(jīng)準別好{FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //開啟FLASH預讀緩沖功能,加速FLASH的讀取。所有程序中必須的用法.位置:RCC初始化子函數(shù)里面,時鐘起振之后FLASH_SetLatency(FLASH_Latency_2); //flash操作的延時RCC_HCLKConfig(RCC_SYSCLK_Div1); //配置AHB(HCLK)時鐘等于==SYSCLKRCC_PCLK2Config(RCC_HCLK_Div1); //配置APB2(PCLK2)鐘==AHB時鐘RCC_PCLK1Config(RCC_HCLK_Div2); //配置APB1(PCLK1)鐘==AHB1/2時鐘RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); //配置PLL時鐘 == 外部高速晶體時鐘 * 9 = 72MHzRCC_PLLCmd(ENABLE); //使能PLL時鐘while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) //等待PLL時鐘就緒{}RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //配置系統(tǒng)時鐘 = PLL時鐘while(RCC_GetSYSCLKSource() != 0x08) //檢查PLL時鐘是否作為系統(tǒng)時鐘{}}RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //允許 GPIOA GPIOB、AFIO時鐘}/*函數(shù): void GPIO_Configuration(void)功能: GPIO配置參數(shù): 無返回: 無/void GPIO_Configuration(void){GPIO_InitTypeDef GPIO_InitStructure; //定義GPIO初始化結構體GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); //PA用于輸出控制數(shù)碼管GPIO_Init(GPIOB, &GPIO_InitStructure); //PB用于輸出控制138譯碼器}
評論