OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 1849|回复: 2

STM32F1串口实验自己学习写的代码

[复制链接]

28

主题

28

帖子

0

精华

初级会员

Rank: 2

积分
116
金钱
116
注册时间
2019-10-16
在线时间
18 小时
发表于 2019-11-16 20:56:26 | 显示全部楼层 |阅读模式
led.h
  1. #ifndef __LED_H
  2. #define __LED_H

  3. void LED_Init(void);


  4. #endif

  5. #define LED0 PBout(5)

复制代码



led.c
  1. #include "led.h"
  2. #include "stm32f10x.h"

  3. void LED_Init(void)
  4. {
  5.         GPIO_InitTypeDef GPIO_InitStructure;
  6.         
  7.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE,ENABLE);//使能GPIOA和GPIOB
  8.                                                                                                                                                                                                                                                                                                          //| 既是逻辑运算符也是位运算符;|| 只是逻辑运算符
  9.                                                                                                                                                                                                                                                                                                                 //| 不具有短路效果,即左边true,右边还会执行;
  10.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  11.         GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
  12.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  13.         GPIO_Init(GPIOB,&GPIO_InitStructure);
  14.   GPIO_SetBits(GPIOB,GPIO_Pin_5);        
  15.         
  16.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  17.         GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
  18.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  19.         GPIO_Init(GPIOE,&GPIO_InitStructure);        
  20.   GPIO_SetBits(GPIOE,GPIO_Pin_5);               
  21. }

复制代码



key.h
  1. #ifndef __KEY_H
  2. #define __key_h
  3. #include "sys.h"

  4. #define KEY0  GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)
  5. #define KEY1  GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3)
  6. #define WK_UP GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)//读取按键3(WK_UP)

  7. #define KEY0_PRES         1        //KEY0按下
  8. #define KEY1_PRES          2        //KEY1按下
  9. #define WKUP_PRES   3        //KEY_UP按下(即WK_UP/KEY_UP)


  10. void KEY_Init(void);//IO初始化
  11. u8 KEY_Scan(u8);          //按键扫描函数               


  12. #endif

复制代码



key.c
  1. #include "stm32f10x.h"
  2. #include "key.h"
  3. #include "sys.h"
  4. #include "delay.h"

  5. void KEY_Init(void) //IO初始化
  6. {
  7.          GPIO_InitTypeDef GPIO_InitStructure;

  8.          RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOE,ENABLE);//使能PORTA,PORTE时钟

  9.         GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_4|GPIO_Pin_3;//KEY0-KEY1
  10.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入
  11.          GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化GPIOE4,3

  12.         //初始化 WK_UP-->GPIOA.0          下拉输入
  13.         GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_0;
  14.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0设置成输入,默认下拉         
  15.         GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.0

  16. }

  17. //mode:0,不支持连续按;1,支持连续按

  18. u8 KEY_Scan(u8 mode)
  19. {         
  20.         static u8 key_up=1;//按键按松开标志
  21.         if(mode)key_up=1;  //支持连按                  
  22.         if(key_up&&(KEY0==0||KEY1==0||WK_UP==1))
  23.         {
  24.                 delay_ms(10);//去抖动
  25.                 key_up=0;
  26.                 if(KEY0==0)return KEY0_PRES;
  27.                 else if(KEY1==0)return KEY1_PRES;
  28.                 else if(WK_UP==1)return WKUP_PRES;
  29.         }else if(KEY0==1&&KEY1==1&&WK_UP==0)key_up=1;            
  30.          return 0;// 无按键按下
  31. }

复制代码



main.c

  1. #include "sys.h"
  2. #include "usart.h"
  3. #include "led.h"
  4. #include "delay.h"
  5. #include "key.h"

  6. int main()
  7. {
  8.         u16 t;
  9.         u16 len;
  10.         u16 times=0;
  11.         delay_init();
  12.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  13.         uart_init(115200);
  14.         LED_Init();
  15.         KEY_Init();
  16.         while(1)
  17.         {
  18.                 if(USART_RX_STA&0x8000)
  19.                 {
  20.                         len=USART_RX_STA&0x3ff;
  21.                         printf("\r\n您发送的数据为:\r\n");
  22.                         for(t=0;t<len;t++)
  23.                         {
  24.                                 USART_SendData(USART1, USART_RX_BUF[t]);
  25.                                 while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//可以找它的defination
  26.                                
  27.                         }
  28.                         printf("\r\n\n");
  29.                         USART_RX_STA=0;
  30.                 }
  31.                 else//没有的话只会发送接收的数据的时候
  32.                 {                       
  33.                         times++;
  34.                         if(times%5000==0)
  35.                         {
  36.                                 printf("\r\n串口实验\r\n");
  37.                         }
  38.                         if(times%200==0)
  39.                         {
  40.                                 printf("输入数据,按回车键结束\n");//只写\n会换行和\r\n效果一样
  41.                         }
  42.                         if(times%30==0)
  43.                         {
  44.                                 LED0=!LED0;
  45.                         }
  46.                         delay_ms(10);
  47.                 }
  48.                
  49.         }
  50. }
复制代码
以下是正点原子源程序代码
led.h
  1. #ifndef __LED_H
  2. #define __LED_H         
  3. #include "sys.h"

  4. #define LED0 PBout(5)// PB5
  5. #define LED1 PEout(5)// PE5       

  6. void LED_Init(void);//初始化

  7.                                                      
  8. #endif
复制代码


led.c
  1. #include "led.h"

  2. //初始化PB5和PE5为输出口.并使能这两个口的时钟                    
  3. //LED IO初始化
  4. void LED_Init(void)
  5. {

  6. GPIO_InitTypeDef  GPIO_InitStructure;
  7.        
  8. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE, ENABLE);         //使能PB,PE端口时钟
  9.        
  10. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;                                 //LED0-->PB.5 端口配置
  11. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;                  //推挽输出
  12. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;                 //IO口速度为50MHz
  13. GPIO_Init(GPIOB, &GPIO_InitStructure);                                         //根据设定参数初始化GPIOB.5
  14. GPIO_SetBits(GPIOB,GPIO_Pin_5);                                                 //PB.5 输出高

  15. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;                             //LED1-->PE.5 端口配置, 推挽输出
  16. GPIO_Init(GPIOE, &GPIO_InitStructure);                                           //推挽输出 ,IO口速度为50MHz
  17. GPIO_SetBits(GPIOE,GPIO_Pin_5);                                                  //PE.5 输出高
  18. }

复制代码


key.h
  1. #ifndef __KEY_H
  2. #define __KEY_H         
  3. #include "sys.h"


  4. //#define KEY0 PEin(4)           //PE4
  5. //#define KEY1 PEin(3)        //PE3
  6. //#define WK_UP PAin(0)        //PA0  WK_UP

  7. #define KEY0  GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)//读取按键0
  8. #define KEY1  GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3)//读取按键1
  9. #define WK_UP   GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)//读取按键3(WK_UP)



  10. #define KEY0_PRES         1        //KEY0按下
  11. #define KEY1_PRES          2        //KEY1按下
  12. #define WKUP_PRES   3        //KEY_UP按下(即WK_UP/KEY_UP)


  13. void KEY_Init(void);//IO初始化
  14. u8 KEY_Scan(u8);          //按键扫描函数                                            
  15. #endif
复制代码


key.c
  1. #include "stm32f10x.h"
  2. #include "key.h"
  3. #include "sys.h"
  4. #include "delay.h"
  5.                                                                     
  6. //按键初始化函数
  7. void KEY_Init(void) //IO初始化
  8. {
  9.         GPIO_InitTypeDef GPIO_InitStructure;

  10.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOE,ENABLE);//使能PORTA,PORTE时钟

  11.         GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_4|GPIO_Pin_3;//KEY0-KEY1
  12.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入
  13.         GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化GPIOE4,3

  14.         //初始化 WK_UP-->GPIOA.0          下拉输入
  15.         GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_0;
  16.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0设置成输入,默认下拉          
  17.         GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.0

  18. }
  19. //按键处理函数
  20. //返回按键值
  21. //mode:0,不支持连续按;1,支持连续按;
  22. //0,没有任何按键按下
  23. //1,KEY0按下
  24. //2,KEY1按下
  25. //3,KEY3按下 WK_UP
  26. //注意此函数有响应优先级,KEY0>KEY1>KEY_UP!!
  27. u8 KEY_Scan(u8 mode)
  28. {         
  29.         static u8 key_up=1;//按键按松开标志
  30.         if(mode)key_up=1;  //支持连按                  
  31.         if(key_up&&(KEY0==0||KEY1==0||WK_UP==1))
  32.         {
  33.                 delay_ms(10);//去抖动
  34.                 key_up=0;
  35.                 if(KEY0==0)return KEY0_PRES;
  36.                 else if(KEY1==0)return KEY1_PRES;
  37.                 else if(WK_UP==1)return WKUP_PRES;
  38.         }else if(KEY0==1&&KEY1==1&&WK_UP==0)key_up=1;             
  39.         return 0;// 无按键按下
  40. }
复制代码


main.c
  1. #include "led.h"
  2. #include "delay.h"
  3. #include "key.h"
  4. #include "sys.h"
  5. #include "usart.h"
  6. int main(void)
  7. {               
  8.         u16 t;  
  9.         u16 len;       
  10.         u16 times=0;
  11.         delay_init();                     //延时函数初始化          
  12.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
  13.         uart_init(115200);         //串口初始化为115200
  14.         LED_Init();                             //LED端口初始化
  15.         KEY_Init();          //初始化与按键连接的硬件接口
  16.         while(1)
  17.         {
  18.                 if(USART_RX_STA&0x8000)
  19.                 {                                          
  20.                         len=USART_RX_STA&0x3fff;//得到此次接收到的数据长度
  21.                         printf("\r\n您发送的消息为:\r\n\r\n");
  22.                         for(t=0;t<len;t++)
  23.                         {
  24.                                 USART_SendData(USART1, USART_RX_BUF[t]);//向串口1发送数据
  25.                                 while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
  26.                         }
  27.                         printf("\r\n\r\n");//插入换行
  28.                         USART_RX_STA=0;
  29.                 }else
  30.                 {
  31.                         times++;
  32.                         if(times%5000==0)
  33.                         {
  34.                                 printf("\r\n精英STM32开发板 串口实验\r\n");
  35.                                 printf("正点原子@ALIENTEK\r\n\r\n");
  36.                         }
  37.                         if(times%200==0)printf("请输入数据,以回车键结束\n");  
  38.                         if(times%30==0)LED0=!LED0;//闪烁LED,提示系统正在运行.
  39.                         delay_ms(10);   
  40.                 }
  41.         }         
  42. }

复制代码



正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

31

主题

2183

帖子

0

精华

资深版主

Rank: 8Rank: 8

积分
14410
金钱
14410
注册时间
2018-8-3
在线时间
1156 小时
发表于 2019-11-17 09:55:23 | 显示全部楼层
啊哈~加油!!
回复 支持 反对

使用道具 举报

6

主题

1127

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
1656
金钱
1656
注册时间
2019-8-15
在线时间
102 小时
发表于 2019-11-20 17:19:27 | 显示全部楼层
帮顶      
成功没有捷径
回复 支持 反对

使用道具 举报

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

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-5-25 15:20

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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