找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1404|回复: 0
打印 上一主题 下一主题
收起左侧

基于stm32f407的跑马灯程序

[复制链接]
跳转到指定楼层
楼主
基于stm32f407的跑马灯这是我昨天做的基于stm32f407的跑马灯,欢迎交流
  1. #include "led.h"
  2. #include "usart1.h"


  3. /*********************************************************************************
  4. **********************启明欣欣 STM32F407应用开发板(高配版)************************
  5. **********************************************************************************
  6. * 文件名称: 例程1 LED跑马灯主函数main()                                          *
  7. * 文件简述:LED跑马灯                                                            *
  8. **********************************************************************************
  9. *********************************************************************************/

  10. /*******************下面代码是通过位带操作实现IO口控制***************************/
  11. int main(void)
  12. {
  13.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置系统中断优先级分组2
  14.         delay_init();                  //初始化延时函数
  15.         LED_Init();                    //初始化LED端口
  16.         uart1_init(115200);
  17.   while(1)
  18.         {
  19.     LED0=0;     //LED0亮
  20.     LED1=1;     //LED1灭
  21.     LED2=1;     //LED2灭
  22.            printf("LED0 is light!\r\n");
  23.     delay_ms(500);
  24.                 LED0=1;     //LED0灭
  25.     LED1=0;     //LED1亮
  26.     LED2=1;     //LED2灭
  27.     printf("LED1 is light!\r\n");
  28.                 delay_ms(500);
  29.                 LED0=1;     //LED0灭
  30.     LED1=1;     //LED1灭
  31.     LED2=0;     //LED2亮
  32.            printf("LED2 is light!\r\n");
  33.     delay_ms(500);
  34.         }
  35. }
复制代码
  1. #include "usart1.h"
  2. #include "string.h"
  3. #include "stdlib.h"  
  4. #include "led.h"


  5. /*********************************************************************************
  6. ************************启明欣欣 STM32F407核心开发板******************************
  7. **********************************************************************************
  8. * 文件名称: usart1.c                                                             *
  9. * 文件简述:USART1使用                                                           *
  10. * 创建日期:2015.03.06                                                           *
  11. * 版    本:V1.0                                                                 *
  12. * 作    者:Clever                                                               *
  13. * 说    明:利用串口调试助手经过USART1控制LED亮灭与蜂鸣器响闭                    *
  14. **********************************************************************************
  15. *********************************************************************************/        

  16. u8 receive_str[USART1_REC_NUM];     //接收缓存数组,最大USART_REC_LEN个字节
  17. u8 uart_byte_count=0;

  18. /****************************************************************************
  19. * 名    称: void uart1_init(u32 bound)
  20. * 功    能:USART1初始化
  21. * 入口参数:bound:波特率
  22. * 返回参数:无
  23. * 说    明:
  24. ****************************************************************************/
  25. void uart1_init(u32 bound)
  26. {   //GPIO端口设置
  27.   GPIO_InitTypeDef GPIO_InitStructure;
  28.         USART_InitTypeDef USART_InitStructure;
  29.         NVIC_InitTypeDef NVIC_InitStructure;
  30.         
  31.         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //使能GPIOA时钟
  32.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//使能USART1时钟
  33.         //串口1对应引脚复用映射
  34.         GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);  //GPIOA9复用为USART1
  35.         GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); //GPIOA10复用为USART1
  36.         //USART1端口配置
  37.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; //GPIOA9与GPIOA10
  38.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;      //复用功能
  39.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;        //速度50MHz
  40.         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
  41.         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;   //上拉
  42.         GPIO_Init(GPIOA,&GPIO_InitStructure);          //初始化PA9,PA10
  43.    //USART1 初始化设置
  44.         USART_InitStructure.USART_BaudRate = bound;//波特率设置
  45.         USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
  46.         USART_InitStructure.USART_StopBits = USART_StopBits_1;  //一个停止位
  47.         USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
  48.         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
  49.         USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;        //收发模式
  50.   USART_Init(USART1, &USART_InitStructure); //初始化串口1        
  51.   USART_Cmd(USART1, ENABLE);  //使能串口1
  52.         
  53.         USART_ClearFlag(USART1, USART_FLAG_TC);
  54.         
  55.         USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);         //开启相关中断
  56.         //Usart1 NVIC 配置
  57.   NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;      //串口1中断通道
  58.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3
  59.         NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;                   //子优先级3
  60.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                           //IRQ通道使能
  61.         NVIC_Init(&NVIC_InitStructure);          //根据指定的参数初始化VIC寄存器、
  62. }

  63. //串口1发送一个字符
  64. void uart1SendChar(u8 ch)
  65. {      
  66.         while((USART1->SR&0x40)==0);  
  67.     USART1->DR = (u8) ch;      
  68. }

  69. /****************************************************************************
  70. * 名    称: int fputc(int ch, FILE *f)
  71. * 功    能:重定向,让printf输出到串口  
  72. * 入口参数:
  73. * 返回参数:
  74. * 说    明:因printf()之类的函数,使用了半主机模式。使用标准库会导致程序无法
  75.             运行,以下是解决方法:使用微库,因为使用微库的话,不会使用半主机模式.
  76.             请在工程属性的“Target“-》”Code Generation“中勾选”Use MicroLIB“这
  77.             样以后就可以使用printf,sprintf函数了  
  78. ****************************************************************************/
  79. int fputc(int ch, FILE *f)   //重定向,让printf输出到串口  
  80. {
  81.     uart1SendChar(ch);
  82.     while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  83.         
  84.     return ch;
  85. }

  86. /****************************************************************************
  87. * 名    称: void uart1SendChars(u8 *str, u16 strlen)
  88. * 功    能:串口1发送一字符串
  89. * 入口参数:*str:发送的字符串
  90.             strlen:字符串长度
  91. * 返回参数:无
  92. * 说    明:
  93. ****************************************************************************/
  94. void uart1SendChars(u8 *str, u16 strlen)
  95. {
  96.           u16 k= 0 ;
  97.    do { uart1SendChar(*(str + k)); k++; }   //循环发送,直到发送完毕   
  98.     while (k < strlen);
  99. }

  100. //串口1中断服务程序
  101. void USART1_IRQHandler(void)  
  102. {
  103.         u8 rec_data;
  104.         if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)  //接收中断
  105.                 {
  106.                                 rec_data =(u8)USART_ReceiveData(USART1);         //(USART1->DR) 读取接收到的数据
  107.         if(rec_data=='S')                                                   //如果是S,表示是命令信息的起始位
  108.                                 {
  109.                                         uart_byte_count=0x01;
  110.                                 }

  111.                         else if(rec_data=='E')                                         //如果E,表示是命令信息传送的结束位
  112.                                 {
  113.                                         if(strcmp("Light_led0",(char *)receive_str)==0)        LED0=0;        //点亮LED1
  114.                                         else if(strcmp("Close_led0",(char *)receive_str)==0)   LED0=1;        //关灭LED1
  115.                                         if(strcmp("Light_led1",(char *)receive_str)==0)        LED1=0;        //点亮LED1
  116.                                         else if(strcmp("Close_led1",(char *)receive_str)==0)   LED1=1;        //关灭LED1
  117.                                         if(strcmp("Light_led2",(char *)receive_str)==0)        LED2=0;        //点亮LED1
  118.                                         else if(strcmp("Close_led2",(char *)receive_str)==0)   LED2=1;        //关灭LED1
  119. //                                        else if(strcmp("Open_beep",(char *)receive_str)==0)    BEEP=1;         //蜂鸣器响
  120. //                                        else if(strcmp("Close_beep",(char *)receive_str)==0)   BEEP=0;         //蜂鸣器不响
  121.                                        
  122.                                         for(uart_byte_count=0;uart_byte_count<32;uart_byte_count++)receive_str[uart_byte_count]=0x00;
  123.                                         uart_byte_count=0;   
  124.                                 }                                 
  125.                         else if((uart_byte_count>0)&&(uart_byte_count<=USART1_REC_NUM))
  126.                                 {
  127.                                    receive_str[uart_byte_count-1]=rec_data;
  128.                                    uart_byte_count++;
  129.                                 }                                 
  130.    }
  131. }
复制代码
LED1.7z (290.83 KB, 下载次数: 12)

51hei图片20201003165458.png (69.62 KB, 下载次数: 37)

51hei图片20201003165458.png
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表