找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 4357|回復(fù): 0
收起左側(cè)

STM32控制LED代碼

[復(fù)制鏈接]
ID:325383 發(fā)表于 2018-5-8 20:32 | 顯示全部樓層 |閱讀模式
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ī)源程序:
  1. /*******************************************************************************
  2. * File Name          : main.c
  3. * Author             : Wuhan R&D Center, Embest
  4. * Date First Issued  : 08/08/2008
  5. * Description        : Main program body
  6. ********************************************************************************/

  7. /* Includes ------------------------------------------------------------------*/
  8. #include "stm32f10x_lib.h"


  9. /* Private typedef -----------------------------------------------------------*/
  10. /* Private define ------------------------------------------------------------*/
  11. /* Private macro -------------------------------------------------------------*/
  12. /* Private variables ---------------------------------------------------------*/
  13. #define ADC1_DR_Address    ((u32)0x4001244C)

  14. unsigned short int ADC_ConvertedValue;
  15. GPIO_InitTypeDef GPIO_InitStructure;
  16. ADC_InitTypeDef   ADC_InitStructure;
  17. DMA_InitTypeDef   DMA_InitStructure;
  18. EXTI_InitTypeDef EXTI_InitStructure;
  19. ErrorStatus HSEStartUpStatus;
  20. extern vu32 TimingDelay;



  21. uc8 table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,                                //數(shù)碼管編碼
  22.                         0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

  23. /* Private function prototypes -----------------------------------------------*/
  24. void RCC_Configuration(void);
  25. void NVIC_Configuration(void);
  26. void GPIO_Configuration(void);
  27. void Delay(vu32 nTime);
  28. void SysTick_Configuration(void);
  29. void SetupLED (void) ;        
  30. extern void SetupADC    (void);
  31.         
  32. /* Private functions ---------------------------------------------------------*/

  33. /*******************************************************************************
  34. * Function Name  : main
  35. * Description    : Main program.
  36. * Input          : None
  37. * Output         : None
  38. * Return         : None
  39. *******************************************************************************/
  40. int main(void)
  41. {
  42. u8 i;
  43. #ifdef DEBUG
  44.   debug();
  45. #endif
  46.    
  47.   /* Configure the system clocks */
  48.   RCC_Configuration();
  49.   SysTick_Configuration();
  50.    
  51.   /* NVIC Configuration */
  52.   NVIC_Configuration();

  53.   /* Configure the GPIO ports */
  54.   GPIO_Configuration();
  55.                                                       



  56.         for(i=0;i<8;i++)
  57.            {
  58.                             GPIOE->ODR = 0xffffff00 | (1<<i);
  59.                          GPIOD->ODR = 0xffffffff ;
  60.                          Delay(100);
  61.                 }
  62.    for(;;)
  63.    {
  64.               for(i=0;i<8;i++)
  65.            {
  66.                             GPIOE->ODR = 0xffffffff & ~(1<<i);
  67.                          GPIOD->ODR = 0xffffff00 | table[i];
  68.                          Delay(800);
  69.                          //Delay(1);
  70.                 }
  71.         }   

  72. }

  73. /*******************************************************************************
  74. * Function Name  : SysTick_Configuration
  75. * Description    : Configures the SysTick to generate an interrupt each 1 millisecond.
  76. * Input          : None
  77. * Output         : None
  78. * Return         : None
  79. *******************************************************************************/
  80. void SysTick_Configuration(void)
  81. {
  82.   /* Select AHB clock(HCLK) as SysTick clock source */
  83.   SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);

  84.   /* Set SysTick Priority to 3 */
  85.   NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 3, 0);
  86.    
  87.   /* SysTick interrupt each 1ms with HCLK equal to 72MHz */
  88.   SysTick_SetReload(72000);

  89.   /* Enable the SysTick Interrupt */
  90.   SysTick_ITConfig(ENABLE);
  91. }



  92. /*******************************************************************************
  93. * Function Name  : RCC_Configuration
  94. * Description    : Configures the different system clocks.
  95. * Input          : None
  96. * Output         : None
  97. * Return         : None
  98. *******************************************************************************/
  99. void RCC_Configuration(void)
  100. {
  101.   /* RCC system reset(for debug purpose) */
  102.   RCC_DeInit();

  103.   /* Enable HSE */
  104.   RCC_HSEConfig(RCC_HSE_ON);

  105.   /* Wait till HSE is ready */
  106.   HSEStartUpStatus = RCC_WaitForHSEStartUp();

  107.   if(HSEStartUpStatus == SUCCESS)
  108.   {
  109.     /* HCLK = SYSCLK */
  110.     RCC_HCLKConfig(RCC_SYSCLK_Div1);
  111.   
  112.     /* PCLK2 = HCLK */
  113.     RCC_PCLK2Config(RCC_HCLK_Div1);

  114.     /* PCLK1 = HCLK/2 */
  115.     RCC_PCLK1Config(RCC_HCLK_Div2);

  116.     /* Flash 2 wait state */
  117.     FLASH_SetLatency(FLASH_Latency_2);
  118.     /* Enable Prefetch Buffer */
  119.     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

  120.     /* PLLCLK = 8MHz * 9 = 72 MHz */
  121.     RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

  122.     /* Enable PLL */
  123.     RCC_PLLCmd(ENABLE);

  124.     /* Wait till PLL is ready */
  125.     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  126.     {
  127.     }

  128.     /* Select PLL as system clock source */
  129.     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  130.     /* Wait till PLL is used as system clock source */
  131.     while(RCC_GetSYSCLKSource() != 0x08)
  132.     {
  133.     }
  134.   }
  135.    
  136.   /* Enable GPIOB, GPIOC and AFIO clocks */
  137.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |
  138.                          RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE , ENABLE);
  139. }

  140. /*******************************************************************************
  141. * Function Name  : NVIC_Configuration
  142. * Description    : Configures Vector Table base location.
  143. * Input          : None
  144. * Output         : None
  145. * Return         : None
  146. *******************************************************************************/
  147. void NVIC_Configuration(void)
  148. {
  149. NVIC_InitTypeDef NVIC_InitStructure;
  150.   
  151. #ifdef  VECT_TAB_RAM  
  152.   /* Set the Vector Table base location at 0x20000000 */
  153.   NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  154. #else  /* VECT_TAB_FLASH  */
  155.   /* Set the Vector Table base location at 0x08000000 */
  156.   NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);   
  157. #endif

  158.   /* Configure one bit for preemption priority */
  159.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  160.   
  161.   /* Enable the EXTI9_5 Interrupt */
  162.   NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel;
  163.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  164.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  165.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  166.   NVIC_Init(&NVIC_InitStructure);
  167. }


  168. /*******************************************************************************
  169. * Function Name  : GPIO_Configuration
  170. * Description    : Configures the different GPIO ports.
  171. * Input          : None
  172. * Output         : None
  173. * Return         : None
  174. *******************************************************************************/
  175. void GPIO_Configuration(void)
  176. {
  177.   GPIO_InitTypeDef GPIO_InitStructure;

  178.   /* Configure PD. as Output push-pull */
  179.   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;
  180.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  181.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  182.   GPIO_Init(GPIOD, &GPIO_InitStructure);

  183.   /* Configure PE. as Output push-pull */
  184.   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;
  185.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  186.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  187.   GPIO_Init(GPIOE, &GPIO_InitStructure);
  188.    

  189. }


  190. /*******************************************************************************
  191. * Function Name  : Delay
  192. * Description    : Inserts a delay time.
  193. * Input          : nTime: specifies the delay time length, in milliseconds.
  194. * Output         : None
  195. * Return         : None
  196. *******************************************************************************/
  197. void Delay(u32 nTime)
  198. {
  199.   /* Enable the SysTick Counter */
  200.   SysTick_CounterCmd(SysTick_Counter_Enable);
  201.   
  202.   TimingDelay = nTime;

  203.   while(TimingDelay != 0);

  204.   /* Disable the SysTick Counter */
  205.   SysTick_CounterCmd(SysTick_Counter_Disable);
  206.   /* Clear the SysTick Counter */
  207.   SysTick_CounterCmd(SysTick_Counter_Clear);
  208. }

  209. /*******************************************************************************
  210. * Function Name  : Delay
  211. * Description    : Inserts a delay time.
  212. * Input          : nCount: specifies the delay time length.
  213. * Output         : None
  214. * Return         : None
  215. *******************************************************************************/
  216. /*
  217. void Delay(vu32 nCount)
  218. {
  219.   for(; nCount != 0; nCount--);
  220. ……………………

  221. …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼

所有資料51hei提供下載:
2.數(shù)碼管.rar (52.23 KB, 下載次數(shù): 19)


回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表