1設計要求
控制實驗平臺的發光二極管LED1、LED2、LED3、LED4,使它們有規律的點亮,具體順序如下:
LED1亮->LED2亮->LED3亮->LED4亮,如此反復,當按下開發板上的B3按鈕時,燈全部熄滅,
2秒鐘后發光二極管全部點亮,再過一秒鐘后返回被中斷前的狀態,有規律的依次點亮。
2 硬件電路設計
在評估板上,LED1、LED2、LED3、LED4分別與PC6、PC7、PC8、PC9相連,按鍵Key與PB9相連。
3軟件程序設計
根據任務要求,程序內容主要包括:
(1) 配置PB口第9個引腳作為外部中斷,下降延觸發;
(2) 讀取端口數據輸出寄存器GPIOC_ODR[15:0]的值,因為C口[9:6]位和四個LED燈連通。
(3) 初始化時,LED依次點亮,當按下Key鍵時產生中斷;
(4) 中斷服務子程序內容:燈全部熄滅,2秒鐘后發光二極管全部點亮,再過1秒鐘后中斷返回。
整個工程包含3個源文件:STM32F10x.s、stm32f10x_it.c和main.c,其中STM32F10x.s為啟動代碼,
所有中斷服務子程序均在stm32f10x_it.c中,其它函數則在main.c中。下面分別介紹相關的函數,
具體程序清單見參考程序。
GPIO_Configuration函數負責配置GPIO端口,其中GPIO_InitStructure數據結構包含所有GPIO端口
配置所需各項,函數GPIO_Init則實現某個端口的配置。在STM32F10xR.LIB和STM3210xD.LIB中均已
定義,讀者可通過查閱5.3節所述的ST公司發布的《ARM?-based 32-bit MCU STM32F101xx and
STM32F103xx Firmware Library》手冊來了解該數據結構以及相關驅動函數。
NVIC_Configuration函數用于配置嵌入式中斷控制器,相關內容將在后面章節介紹,此處需要將PB9
引腳配置為外部中斷。
EXTI9_5_IRQHandler函數是PB9的中斷服務子程序,當按下Key按鈕之后觸發EXTI9_5中斷,在中斷服
務子程序中先將燈全部熄滅,延遲2秒后全部點亮,再延遲1秒之后退出中斷服務子程序。
SysTick_Configuration函數用于配置和允許系統時鐘中斷,系統時鐘中斷服務子程序SysTickHandler
函數則用于產生1毫秒的延時,這樣Delay函數就可以通過開關系統時鐘計數器來實現精確延時了。
4 運行過程
(1) 使用Keil uVision3 通過ULINK 2仿真器連接實驗板,打開實驗例程目錄GPIO_TEST子目錄下的
GPIO.Uv2例程,編譯鏈接工程;
(2) 選擇軟件調試模式,點擊MDK 的Debug菜單,選擇Start/Stop Debug Session項或Ctrl+F5鍵,
在邏輯分析儀中添加GPIOC_ODR.6、GPIOC_ODR.7、GPIOC_ODR.8、GPIOC_ODR.9,點擊Run按鈕即可在
邏輯分析儀中看到如圖7-11;
(3) 選擇硬件調試模式,選擇Start/Stop Debug Session項或Ctrl+F5鍵,下載程序并運行,觀察LED
燈的變化情況;當程序運行在while循環體內時,按Key鍵,程序進入中斷服務子程序EXTI9_5_IRQHandler(),
單步運行,觀察LED的變化情況。
(4) 退出Debug模式,打開Flash菜單>Download,將程序下載到開發板的Flash中,按RESET鍵復位,
觀察LED燈的情況,正常情況應如表7-6所列。
LED燈狀態 說 明
LED1 LED2 LED3 LED4
亮 滅 滅 滅 程序正常運行,發光二極管依次點亮
滅 亮 滅 滅
滅 滅 亮 滅
滅 滅 滅 亮
滅 滅 滅 滅 外部信號輸入,發生中斷,執行中斷處理程序
亮 亮 亮 亮
單片機源程序:
- /*******************************************************************************
- * 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, //數碼管編碼
- 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--);
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
2.數碼管.rar
(52.23 KB, 下載次數: 19)
2018-5-8 20:31 上傳
點擊文件名下載附件
數碼管代碼 下載積分: 黑幣 -5
|