流水灯主函数

程序员成长之旅 · 程序员成长之旅/嵌入式学习/SMT32F4/第一天作业 · 380 字

```c
/**
 ******************************************************************************
 * @file    Project\USER\main.c
 * @author  WEN
 * @version V1.0
 * @date    2019-01-01
 * @brief   基于 STM32F4xx 标准外设固件库工程模板主函数
 ******************************************************************************
 * @comment
 *    CoreLED1 -- PH12
 *    CoreLED2 -- PH13
 *    CoreLED3 -- PH14
 *    CoreLED4 -- PH15
 ******************************************************************************
 */


/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private define ------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
static __IO uint32_t uwTimingDelay;
RCC_ClocksTypeDef RCC_Clocks;


/* Private function prototypes -----------------------------------------------*/
static void Delay(__IO uint32_t nTime);


/* Private functions ---------------------------------------------------------*/


/**
 * @brief  Main program
 * @param  None
 * @retval None
 */
uint32_t getGPIOPin(int targetPin){
   uint32_t result;
   switch(targetPin){
       case 12:
           result = GPIO_Pin_12;
           break;
       case 13:
           result = GPIO_Pin_13;
           break;
       case 14:
           result = GPIO_Pin_14;
           break;
       case 15:
           result = GPIO_Pin_15;
           break;
       default:
           result = GPIO_Pin_12;
           break;
   }
   return result;
}


uint32_t getLedPin(int targetLedId){// 0 <= targetLedId <= 3
   return getGPIOPin(12 + targetLedId);
}






void setLedStatus(int targetLedId, int isEnable){
   GPIO_WriteBit(GPIOH, getLedPin(targetLedId),(BitAction)isEnable);
}
  
void initGPIOH(){
   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOH, ENABLE);
}
void initAllLed(){
   for(int i = 0; i < 4; i++){
       GPIO_InitTypeDef GPIOInitStructure;
       GPIOInitStructure.GPIO_Pin = getLedPin(i);
       GPIOInitStructure.GPIO_Mode = GPIO_Mode_OUT;
       GPIOInitStructure.GPIO_OType = GPIO_OType_PP;
       GPIOInitStructure.GPIO_Speed = GPIO_Speed_100MHz;
       GPIOInitStructure.GPIO_PuPd = GPIO_PuPd_UP;
       GPIO_Init(GPIOH, &GPIOInitStructure);
       GPIO_ResetBits(GPIOH, getGPIOPin(12 + i));
   }
}
void init(){
   /* 获取当前芯片时钟频率值 */
   RCC_GetClocksFreq(&RCC_Clocks);
   /* SysTick 定时器 10ms 延时配置*/
   SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000);
   initGPIOH();
   initAllLed();
}


int main(void)
{
   init();
   Delay(5);// 延时5ms
   while(1){
       for(int i = 0; i < 4; i++){
           setLedStatus(i, 1);
           Delay(500);
       }
       // Delay(1000);
       for(int i = 0; i < 4; i++){
           setLedStatus(3 - i, 0);
           Delay(500);
       }
   }
}
/**
 * @brief  延时函数.
 * @param  nTime: 指定延迟时间长度,以毫秒为单位
 * @retval None
 */
void Delay(__IO uint32_t nTime)
{
   uwTimingDelay = nTime;


   while(uwTimingDelay != 0);
}


/**
 * @brief  SysTick 定时器延时变量递减函数
 * @param  None
 * @retval None
 */
void TimingDelay_Decrement(void)
{
   if (uwTimingDelay != 0x00)
   {
       uwTimingDelay--;
   }
}


#ifdef  USE_FULL_ASSERT


/**
 * @brief  Reports the name of the source file and the source line number
 *         where the assert_param error has occurred.
 * @param  file: pointer to the source file name
 * @param  line: assert_param error line source number
 * @retval None
 */
void assert_failed(uint8_t* file, uint32_t line)
{
   /* User can add his own implementation to report the file name and line number,
    ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */


   /* Infinite loop */
   while (1)
   {
   }
}
#endif


/**
 * @}
 */




/************************ (C) COPYRIGHT WEN *****END OF FILE*****************/