STM32开发板点灯

来源:互联网 发布:java怎么做图形界面 编辑:程序博客网 时间:2024/04/28 19:39

本来计划下午是在家写个AVR的点灯程序玩的,结果。。。。。AVR jtag下载器一直一直都连不上AVR studio,上网查了好久,没有头绪。只能说明自己人品差,估计那玩意坏了。。。等过两个月后买个回来,在把AVR的补起来。。。

好了,所以玩玩stm32,Jlink是个好东西,坏了自己可以烧写固件,只要不是彻底崩溃就好。。。

同样看图

现在只控制一个,实现最最最简单的led控制。

这里的延时程序我调用stm32的内部SysTick定时器,这个东西好。。。呵呵,比自己写延时方便多了。。。。也比较准确,STM32的固件库也是比较好玩的东西,当然网上好多人都对此抱不一样的态度,我还好,自己玩开心就行,反正是自己喜欢的。。。。

总是不喜欢排版,慢慢来改 上代码:

main.c:


#include <stdio.h>
#include "stm32f10x.h"
#include "Delay.h"
#include "Usart.h"
#include "led.h"


void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void EXTI_Configuration(void);




/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)

    RCC_Configuration();
GPIO_Configuration();
NVIC_Configuration();



SysTick_Configuration();
USART_Configuration();


    printf("New Lib Start!\n\r");
printf("this is test led\n\r");
while(1){
led_init();

}
}


/*------函数名:void RCC_Configuration(void)-----------*/
//功能:配置stm32端口时钟,这个端口时钟参考固件库RCC的配置。
//因为led灯要用到GPIOD所以此处就要将GPIOD的端口时钟开放。更详细的硬件说明可以看看32的数据手册


void RCC_Configuration(void){
/* Enable peripheral clocks --------------------------------------------------*/ 
//RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC|RCC_AHBPeriph_SRAM|RCC_AHBPeriph_FLITF, ENABLE);
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|
  RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOE|RCC_APB2Periph_USART1|
  RCC_APB2Periph_AFIO,ENABLE);


}


void GPIO_Configuration(void){
GPIO_InitTypeDef GPIO_InitStructure;


GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  //这个是端口速度。一般IO口无特殊要求就选择50MHz



//-------------       USART1      ----------------//
//=============   USART1_Tx PA9   ================//
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //配置成端口复用推挽输出,这里是串口暂时不考虑
  GPIO_Init(GPIOA, &GPIO_InitStructure);
    
  //=============   USART1_Rx PA10   ================//
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //配置成浮空输入,这里是串口可以不考虑
  GPIO_Init(GPIOA, &GPIO_InitStructure);


//============PD3,PD6====================//
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
  GPIO_Init(GPIOD, &GPIO_InitStructure);




}


void NVIC_Configuration(void){
//NVIC_InitTypeDef NVIC_InitStructure;


#ifdef  VECT_TAB_RAM  
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); 
#else  /* VECT_TAB_FLASH  */
    NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);   
#endif
   
/*NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);


NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;  
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; 
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
NVIC_Init(&NVIC_InitStructure);*/

}


void EXTI_Configuration(void){
/*EXTI_InitTypeDef EXTI_InitStructure;


    GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource0);
  
  EXTI_InitStructure.EXTI_Line = EXTI_Line0 ;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);*/ 
}










#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


delay.c:

#include "stm32f10x.h"
#include "Delay.h"


vu32 TimingDelay;

void SysTick_Configuration(void){
SysTick_Config(72000); 
}


void Delay_ms(u16 nMs){
  TimingDelay = nMs;
  while(TimingDelay != 0);
}


led.c:

#include "stm32f10x.h"
#include "led.h"
#include "delay.h"


void led_init(void)
{
led2_ON;  //灯亮
Delay_ms(2000); //调用stm32内部systick秒延时.这里是延时2秒。。。Delay_ms(1000)就是延时1秒,修改此处调节灯的闪烁时间
led2_OFF;  //灯灭
Delay_ms(2000);

}



原创粉丝点击