|
1設(shè)計(jì)要求
控制實(shí)驗(yàn)平臺(tái)的發(fā)光二極管LED1、LED2、LED3、LED4,使它們有規(guī)律的點(diǎn)亮,具體順序如下:
LED1亮->LED2亮->LED3亮->LED4亮,如此反復(fù),當(dāng)按下開發(fā)板上的B3按鈕時(shí),燈全部熄滅,
2秒鐘后發(fā)光二極管全部點(diǎn)亮,再過一秒鐘后返回被中斷前的狀態(tài),有規(guī)律的依次點(diǎn)亮。
2 硬件電路設(shè)計(jì)
在評(píng)估板上,LED1、LED2、LED3、LED4分別與PC6、PC7、PC8、PC9相連,按鍵Key與PB9相連。
3軟件程序設(shè)計(jì)
根據(jù)任務(wù)要求,程序內(nèi)容主要包括:
(1) 配置PB口第9個(gè)引腳作為外部中斷,下降延觸發(fā);
(2) 讀取端口數(shù)據(jù)輸出寄存器GPIOC_ODR[15:0]的值,因?yàn)镃口[9:6]位和四個(gè)LED燈連通。
(3) 初始化時(shí),LED依次點(diǎn)亮,當(dāng)按下Key鍵時(shí)產(chǎn)生中斷;
(4) 中斷服務(wù)子程序內(nèi)容:燈全部熄滅,2秒鐘后發(fā)光二極管全部點(diǎn)亮,再過1秒鐘后中斷返回。
整個(gè)工程包含3個(gè)源文件:STM32F10x.s、stm32f10x_it.c和main.c,其中STM32F10x.s為啟動(dòng)代碼,
所有中斷服務(wù)子程序均在stm32f10x_it.c中,其它函數(shù)則在main.c中。下面分別介紹相關(guān)的函數(shù),
具體程序清單見參考程序。
GPIO_Configuration函數(shù)負(fù)責(zé)配置GPIO端口,其中GPIO_InitStructure數(shù)據(jù)結(jié)構(gòu)包含所有GPIO端口
配置所需各項(xiàng),函數(shù)GPIO_Init則實(shí)現(xiàn)某個(gè)端口的配置。在STM32F10xR.LIB和STM3210xD.LIB中均已
定義,讀者可通過查閱5.3節(jié)所述的ST公司發(fā)布的《ARM?-based 32-bit MCU STM32F101xx and
STM32F103xx Firmware Library》手冊(cè)來了解該數(shù)據(jù)結(jié)構(gòu)以及相關(guān)驅(qū)動(dòng)函數(shù)。
NVIC_Configuration函數(shù)用于配置嵌入式中斷控制器,相關(guān)內(nèi)容將在后面章節(jié)介紹,此處需要將PB9
引腳配置為外部中斷。
EXTI9_5_IRQHandler函數(shù)是PB9的中斷服務(wù)子程序,當(dāng)按下Key按鈕之后觸發(fā)EXTI9_5中斷,在中斷服
務(wù)子程序中先將燈全部熄滅,延遲2秒后全部點(diǎn)亮,再延遲1秒之后退出中斷服務(wù)子程序。
SysTick_Configuration函數(shù)用于配置和允許系統(tǒng)時(shí)鐘中斷,系統(tǒng)時(shí)鐘中斷服務(wù)子程序SysTickHandler
函數(shù)則用于產(chǎn)生1毫秒的延時(shí),這樣Delay函數(shù)就可以通過開關(guān)系統(tǒng)時(shí)鐘計(jì)數(shù)器來實(shí)現(xiàn)精確延時(shí)了。
4 運(yùn)行過程
(1) 使用Keil uVision3 通過ULINK 2仿真器連接實(shí)驗(yàn)板,打開實(shí)驗(yàn)例程目錄GPIO_TEST子目錄下的
GPIO.Uv2例程,編譯鏈接工程;
(2) 選擇軟件調(diào)試模式,點(diǎn)擊MDK 的Debug菜單,選擇Start/Stop Debug Session項(xiàng)或Ctrl+F5鍵,
在邏輯分析儀中添加GPIOC_ODR.6、GPIOC_ODR.7、GPIOC_ODR.8、GPIOC_ODR.9,點(diǎn)擊Run按鈕即可在
邏輯分析儀中看到如圖7-11;
(3) 選擇硬件調(diào)試模式,選擇Start/Stop Debug Session項(xiàng)或Ctrl+F5鍵,下載程序并運(yùn)行,觀察LED
燈的變化情況;當(dāng)程序運(yùn)行在while循環(huán)體內(nèi)時(shí),按Key鍵,程序進(jìn)入中斷服務(wù)子程序EXTI9_5_IRQHandler(),
單步運(yùn)行,觀察LED的變化情況。
(4) 退出Debug模式,打開Flash菜單>Download,將程序下載到開發(fā)板的Flash中,按RESET鍵復(fù)位,
觀察LED燈的情況,正常情況應(yīng)如表7-6所列。
LED燈狀態(tài) 說 明
LED1 LED2 LED3 LED4
亮 滅 滅 滅 程序正常運(yùn)行,發(fā)光二極管依次點(diǎn)亮
滅 亮 滅 滅
滅 滅 亮 滅
滅 滅 滅 亮
滅 滅 滅 滅 外部信號(hào)輸入,發(fā)生中斷,執(zhí)行中斷處理程序
亮 亮 亮 亮
單片機(jī)源程序:
- /*******************************************************************************
- * File Name : main.c
- * Author : Wuhan R&D Center, Embest
- * Date First Issued : 08/08/2008
- * Description : Main program body
- ********************************************************************************/
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f10x_lib.h"
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- #define ADC1_DR_Address ((u32)0x4001244C)
- unsigned short int ADC_ConvertedValue;
- GPIO_InitTypeDef GPIO_InitStructure;
- ADC_InitTypeDef ADC_InitStructure;
- DMA_InitTypeDef DMA_InitStructure;
- EXTI_InitTypeDef EXTI_InitStructure;
- ErrorStatus HSEStartUpStatus;
- extern vu32 TimingDelay;
- uc8 table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, //數(shù)碼管編碼
- 0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
- /* Private function prototypes -----------------------------------------------*/
- void RCC_Configuration(void);
- void NVIC_Configuration(void);
- void GPIO_Configuration(void);
- void Delay(vu32 nTime);
- void SysTick_Configuration(void);
- void SetupLED (void) ;
- extern void SetupADC (void);
-
- /* Private functions ---------------------------------------------------------*/
- /*******************************************************************************
- * Function Name : main
- * Description : Main program.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- int main(void)
- {
- u8 i;
- #ifdef DEBUG
- debug();
- #endif
-
- /* Configure the system clocks */
- RCC_Configuration();
- SysTick_Configuration();
-
- /* NVIC Configuration */
- NVIC_Configuration();
- /* Configure the GPIO ports */
- GPIO_Configuration();
-
- for(i=0;i<8;i++)
- {
- GPIOE->ODR = 0xffffff00 | (1<<i);
- GPIOD->ODR = 0xffffffff ;
- Delay(100);
- }
- for(;;)
- {
- for(i=0;i<8;i++)
- {
- GPIOE->ODR = 0xffffffff & ~(1<<i);
- GPIOD->ODR = 0xffffff00 | table[i];
- Delay(800);
- //Delay(1);
- }
- }
- }
- /*******************************************************************************
- * Function Name : SysTick_Configuration
- * Description : Configures the SysTick to generate an interrupt each 1 millisecond.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void SysTick_Configuration(void)
- {
- /* Select AHB clock(HCLK) as SysTick clock source */
- SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
- /* Set SysTick Priority to 3 */
- NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 3, 0);
-
- /* SysTick interrupt each 1ms with HCLK equal to 72MHz */
- SysTick_SetReload(72000);
- /* Enable the SysTick Interrupt */
- SysTick_ITConfig(ENABLE);
- }
- /*******************************************************************************
- * Function Name : RCC_Configuration
- * Description : Configures the different system clocks.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void RCC_Configuration(void)
- {
- /* RCC system reset(for debug purpose) */
- RCC_DeInit();
- /* Enable HSE */
- RCC_HSEConfig(RCC_HSE_ON);
- /* Wait till HSE is ready */
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
- if(HSEStartUpStatus == SUCCESS)
- {
- /* HCLK = SYSCLK */
- RCC_HCLKConfig(RCC_SYSCLK_Div1);
-
- /* PCLK2 = HCLK */
- RCC_PCLK2Config(RCC_HCLK_Div1);
- /* PCLK1 = HCLK/2 */
- RCC_PCLK1Config(RCC_HCLK_Div2);
- /* Flash 2 wait state */
- FLASH_SetLatency(FLASH_Latency_2);
- /* Enable Prefetch Buffer */
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
- /* PLLCLK = 8MHz * 9 = 72 MHz */
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
- /* Enable PLL */
- RCC_PLLCmd(ENABLE);
- /* Wait till PLL is ready */
- while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
- {
- }
- /* Select PLL as system clock source */
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
- /* Wait till PLL is used as system clock source */
- while(RCC_GetSYSCLKSource() != 0x08)
- {
- }
- }
-
- /* Enable GPIOB, GPIOC and AFIO clocks */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |
- RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE , ENABLE);
- }
- /*******************************************************************************
- * Function Name : NVIC_Configuration
- * Description : Configures Vector Table base location.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
-
- #ifdef VECT_TAB_RAM
- /* Set the Vector Table base location at 0x20000000 */
- NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
- #else /* VECT_TAB_FLASH */
- /* Set the Vector Table base location at 0x08000000 */
- NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
- #endif
- /* Configure one bit for preemption priority */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
-
- /* Enable the EXTI9_5 Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- /*******************************************************************************
- * Function Name : GPIO_Configuration
- * Description : Configures the different GPIO ports.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- /* Configure PD. as Output push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- /* Configure PE. as Output push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOE, &GPIO_InitStructure);
-
-
- }
- /*******************************************************************************
- * Function Name : Delay
- * Description : Inserts a delay time.
- * Input : nTime: specifies the delay time length, in milliseconds.
- * Output : None
- * Return : None
- *******************************************************************************/
- void Delay(u32 nTime)
- {
- /* Enable the SysTick Counter */
- SysTick_CounterCmd(SysTick_Counter_Enable);
-
- TimingDelay = nTime;
- while(TimingDelay != 0);
- /* Disable the SysTick Counter */
- SysTick_CounterCmd(SysTick_Counter_Disable);
- /* Clear the SysTick Counter */
- SysTick_CounterCmd(SysTick_Counter_Clear);
- }
- /*******************************************************************************
- * Function Name : Delay
- * Description : Inserts a delay time.
- * Input : nCount: specifies the delay time length.
- * Output : None
- * Return : None
- *******************************************************************************/
- /*
- void Delay(vu32 nCount)
- {
- for(; nCount != 0; nCount--);
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
2.數(shù)碼管.rar
(52.23 KB, 下載次數(shù): 19)
2018-5-8 20:31 上傳
點(diǎn)擊文件名下載附件
數(shù)碼管代碼
|
|