嵌入式STM32程序编写基本流程(LED流水灯)

LED流水灯程序初始化流程

[cpp] view plain copy

  1. //1.定义结构体变量  
  2. GPIO_InitTypeDef GPIO_InitStructure;  
  3. //2.开启GPIOC的外部时钟,不同的外设开启不同的时钟,IO口复用时两个时钟都要开启。stmf10x_rcc.h  
  4. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);  
  5. //3.设置要控制的GPIO管脚  
  6. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;  
  7. //4.设置管脚模式,推挽输出   
  8. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;  
  9. //5.设置GPIOC的引脚速度为50MHz  
  10. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;  
  11. //6.调用库函数初始化GPIOC,初始化IO口   
  12. GPIO_Init(GPIOC,&GPIO_InitStructure);   
  13. //7.关闭所有LED等灯,GPIO_ResetBits(,);GPIO_SetBits(,);置位和清0函数  
  14. GPIO_SetBits(GPIOC,GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5);  

寄存器方式

[cpp] view plain copy

  1. GPIO_TypeDef * GPIOx;    
  2. GPI0x=GPIOA;    
  3. //开启GPIOA外设时钟  
  4. GPIOx->APB2ENR|=1<<2;   
  5. //配置GPIOA.3 4 5为推挽输出50MHZ   
  6. GPIOx->CRL|=0X03<<12|0X03<<16|0X03<<20;    
  7. //GPIOA.3 4 5输出0xff。  
  8. GPIOx->ODR=0XFF;    

 

LED.H

[cpp] view plain copy

  1. #ifndef __LED_H    
  2. #define __LED_H    
  3. #include "stm32f10x.h"    
  4. #define LED1(a) if(a)     
  5. GPIO_SetBits(GPIOC,GPIO_Pin_3);    
  6. else        
  7. GPIO_ResetBits(GPIOC,GPIO_Pin_3);    
  8. #define LED2(a) if(a)     
  9. GPIO_SetBits(GPIOC,GPIO_Pin_4);    
  10. else        
  11. GPIO_ResetBits(GPIOC,GPIO_Pin_4);    
  12. #define LED3(a) if(a)     
  13. GPIO_SetBits(GPIOC,GPIO_Pin_5);    
  14. else        
  15. GPIO_ResetBits(GPIOC,GPIO_Pin_5);    
  16. void GPIO_Config(void);    
  17. #endif  

""为行连接符,表示本行与上一行属于同一代码行.  

 

端口设置可以使用“sys.h“下的PXout(n)=0/1;PXin(n);来进行设置

原文地址:https://www.cnblogs.com/huan-huan/p/8296299.html