初级会员

- 积分
- 116
- 金钱
- 116
- 注册时间
- 2019-10-16
- 在线时间
- 18 小时
|
led.h
- #ifndef __LED_H
- #define __LED_H
- void LED_Init(void);
- #endif
- #define LED0 PBout(5)
复制代码
led.c
- #include "led.h"
- #include "stm32f10x.h"
- void LED_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE,ENABLE);//使能GPIOA和GPIOB
- //| 既是逻辑运算符也是位运算符;|| 只是逻辑运算符
- //| 不具有短路效果,即左边true,右边还会执行;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
- GPIO_SetBits(GPIOB,GPIO_Pin_5);
-
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOE,&GPIO_InitStructure);
- GPIO_SetBits(GPIOE,GPIO_Pin_5);
- }
复制代码
key.h
- #ifndef __KEY_H
- #define __key_h
- #include "sys.h"
- #define KEY0 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)
- #define KEY1 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3)
- #define WK_UP GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)//读取按键3(WK_UP)
- #define KEY0_PRES 1 //KEY0按下
- #define KEY1_PRES 2 //KEY1按下
- #define WKUP_PRES 3 //KEY_UP按下(即WK_UP/KEY_UP)
- void KEY_Init(void);//IO初始化
- u8 KEY_Scan(u8); //按键扫描函数
- #endif
复制代码
key.c
- #include "stm32f10x.h"
- #include "key.h"
- #include "sys.h"
- #include "delay.h"
- void KEY_Init(void) //IO初始化
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOE,ENABLE);//使能PORTA,PORTE时钟
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_3;//KEY0-KEY1
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入
- GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化GPIOE4,3
- //初始化 WK_UP-->GPIOA.0 下拉输入
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0设置成输入,默认下拉
- GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.0
- }
- //mode:0,不支持连续按;1,支持连续按
- u8 KEY_Scan(u8 mode)
- {
- static u8 key_up=1;//按键按松开标志
- if(mode)key_up=1; //支持连按
- if(key_up&&(KEY0==0||KEY1==0||WK_UP==1))
- {
- delay_ms(10);//去抖动
- key_up=0;
- if(KEY0==0)return KEY0_PRES;
- else if(KEY1==0)return KEY1_PRES;
- else if(WK_UP==1)return WKUP_PRES;
- }else if(KEY0==1&&KEY1==1&&WK_UP==0)key_up=1;
- return 0;// 无按键按下
- }
复制代码
main.c
- #include "sys.h"
- #include "usart.h"
- #include "led.h"
- #include "delay.h"
- #include "key.h"
- int main()
- {
- u16 t;
- u16 len;
- u16 times=0;
- delay_init();
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- uart_init(115200);
- LED_Init();
- KEY_Init();
- while(1)
- {
- if(USART_RX_STA&0x8000)
- {
- len=USART_RX_STA&0x3ff;
- printf("\r\n您发送的数据为:\r\n");
- for(t=0;t<len;t++)
- {
- USART_SendData(USART1, USART_RX_BUF[t]);
- while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//可以找它的defination
-
- }
- printf("\r\n\n");
- USART_RX_STA=0;
- }
- else//没有的话只会发送接收的数据的时候
- {
- times++;
- if(times%5000==0)
- {
- printf("\r\n串口实验\r\n");
- }
- if(times%200==0)
- {
- printf("输入数据,按回车键结束\n");//只写\n会换行和\r\n效果一样
- }
- if(times%30==0)
- {
- LED0=!LED0;
- }
- delay_ms(10);
- }
-
- }
- }
复制代码 以下是正点原子源程序代码
led.h
- #ifndef __LED_H
- #define __LED_H
- #include "sys.h"
- #define LED0 PBout(5)// PB5
- #define LED1 PEout(5)// PE5
- void LED_Init(void);//初始化
-
- #endif
复制代码
led.c
- #include "led.h"
- //初始化PB5和PE5为输出口.并使能这两个口的时钟
- //LED IO初始化
- void LED_Init(void)
- {
-
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE, ENABLE); //使能PB,PE端口时钟
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //LED0-->PB.5 端口配置
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
- GPIO_Init(GPIOB, &GPIO_InitStructure); //根据设定参数初始化GPIOB.5
- GPIO_SetBits(GPIOB,GPIO_Pin_5); //PB.5 输出高
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //LED1-->PE.5 端口配置, 推挽输出
- GPIO_Init(GPIOE, &GPIO_InitStructure); //推挽输出 ,IO口速度为50MHz
- GPIO_SetBits(GPIOE,GPIO_Pin_5); //PE.5 输出高
- }
-
复制代码
key.h
- #ifndef __KEY_H
- #define __KEY_H
- #include "sys.h"
- //#define KEY0 PEin(4) //PE4
- //#define KEY1 PEin(3) //PE3
- //#define WK_UP PAin(0) //PA0 WK_UP
- #define KEY0 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)//读取按键0
- #define KEY1 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3)//读取按键1
- #define WK_UP GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)//读取按键3(WK_UP)
-
- #define KEY0_PRES 1 //KEY0按下
- #define KEY1_PRES 2 //KEY1按下
- #define WKUP_PRES 3 //KEY_UP按下(即WK_UP/KEY_UP)
- void KEY_Init(void);//IO初始化
- u8 KEY_Scan(u8); //按键扫描函数
- #endif
复制代码
key.c
- #include "stm32f10x.h"
- #include "key.h"
- #include "sys.h"
- #include "delay.h"
-
- //按键初始化函数
- void KEY_Init(void) //IO初始化
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOE,ENABLE);//使能PORTA,PORTE时钟
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_3;//KEY0-KEY1
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入
- GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化GPIOE4,3
- //初始化 WK_UP-->GPIOA.0 下拉输入
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0设置成输入,默认下拉
- GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.0
- }
- //按键处理函数
- //返回按键值
- //mode:0,不支持连续按;1,支持连续按;
- //0,没有任何按键按下
- //1,KEY0按下
- //2,KEY1按下
- //3,KEY3按下 WK_UP
- //注意此函数有响应优先级,KEY0>KEY1>KEY_UP!!
- u8 KEY_Scan(u8 mode)
- {
- static u8 key_up=1;//按键按松开标志
- if(mode)key_up=1; //支持连按
- if(key_up&&(KEY0==0||KEY1==0||WK_UP==1))
- {
- delay_ms(10);//去抖动
- key_up=0;
- if(KEY0==0)return KEY0_PRES;
- else if(KEY1==0)return KEY1_PRES;
- else if(WK_UP==1)return WKUP_PRES;
- }else if(KEY0==1&&KEY1==1&&WK_UP==0)key_up=1;
- return 0;// 无按键按下
- }
复制代码
main.c
- #include "led.h"
- #include "delay.h"
- #include "key.h"
- #include "sys.h"
- #include "usart.h"
- int main(void)
- {
- u16 t;
- u16 len;
- u16 times=0;
- delay_init(); //延时函数初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
- uart_init(115200); //串口初始化为115200
- LED_Init(); //LED端口初始化
- KEY_Init(); //初始化与按键连接的硬件接口
- while(1)
- {
- if(USART_RX_STA&0x8000)
- {
- len=USART_RX_STA&0x3fff;//得到此次接收到的数据长度
- printf("\r\n您发送的消息为:\r\n\r\n");
- for(t=0;t<len;t++)
- {
- USART_SendData(USART1, USART_RX_BUF[t]);//向串口1发送数据
- while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
- }
- printf("\r\n\r\n");//插入换行
- USART_RX_STA=0;
- }else
- {
- times++;
- if(times%5000==0)
- {
- printf("\r\n精英STM32开发板 串口实验\r\n");
- printf("正点原子@ALIENTEK\r\n\r\n");
- }
- if(times%200==0)printf("请输入数据,以回车键结束\n");
- if(times%30==0)LED0=!LED0;//闪烁LED,提示系统正在运行.
- delay_ms(10);
- }
- }
- }
复制代码
|
|