我用三个按键加上三个led灯模拟赛车换挡程序,三个led灯分别表示“离合”,“升档”,“降档”;三个按键分别表示“离合按键”(按下之后摩擦片松开),“升档按键”(按下之后车自动升档),“降档”(按下之后车自动降档)。key1、led1---离合;key2、led2---升档;key3、led3---降档。主要过程如下:
1.只要key1按下,led1一直亮着,key1松开,led1随之熄灭; ------我用的是中断里嵌入按键扫描
2.当key2按下,led1 点亮,一段时间后,led2点亮,一段时间后led1熄灭 ,一段时间后,led2熄灭; ------我用的中断
3.当key3按下的时候,led1 点亮,一段时间后,led3点亮,一段时间后led1熄灭 ,一段时间后,led3熄灭。 ------我用的中断
4.当key2与key3同时按下的时候,led1点亮
ps.我写好了程序,连接好了电路,可是却达不到预期的效果,一点反应都没有,我用的是mini板,请大神赐教,
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
<key.h>
#ifndef __KEY_H
#define __KEY_H
#include "sys.h"
//#define KEY0 PAin(2)
//#define KEY1 PAin(3)
//#define KEY2 PAin(4)
#define lihekey GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_1)//读取按键0
#define shengkey GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_2)//读取按键1
#define jiangkey GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_3)//读取按键2
#define lihekey_PRES 1 //KEY0
#define shengkey_PRES 2 //KEY1
#define jiangkey_PRES 3 //KEY2
void KEY_Init(void); //IO初始化
u8 KEY_Scan(u8 mode); //按键扫描函数
#endif
<key.c>
#include "key.h"
#include "sys.h"
#include "delay.h"
void KEY_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);// 使能PORTA,时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //PA1
GPIO_InitStructure .GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz ;
GPIO_Init (GPIOA , &GPIO_InitStructure);
GPIO_InitStructure .GPIO_Pin = GPIO_Pin_2; //PA2
GPIO_InitStructure .GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure .GPIO_Speed = GPIO_Speed_50MHz ;
GPIO_Init (GPIOA ,&GPIO_InitStructure );
GPIO_InitStructure .GPIO_Pin = GPIO_Pin_3 ; //PA3
GPIO_InitStructure .GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure .GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA ,&GPIO_InitStructure );
}
u8 KEY_Scan(u8 mode)
{
static u8 key_up=1;//按键按松开标志
if(mode==1)key_up=1; //支持连按
if(key_up&&(lihekey ==0||shengkey==0||jiangkey==1))
{
delay_ms(10);//去抖动
key_up=0;
if(lihekey ==0)return lihekey_PRES;
else if(shengkey==0)return shengkey_PRES;
else if(jiangkey==1)return jiangkey_PRES;
}else if(lihekey ==1&&shengkey ==1&&jiangkey ==0)key_up=1;
return 0;// 无按键按下
}
<led.h>
#ifndef __LED_H
#define __LED_H
#include "sys.h"
#define lihe PBout(1) // PB1
#define shengdang PBout(2) // PB2
#define jiangdang PBout(3) // PB3
#define LED0 PAout(8) // PA8
#define LED1 PDout(2) // PD2
void LED_Init(void);//初始化
void shoudong (void);
void SD (void);
void JD (void);
void LH (void);
#endif
<led.c>
#include"led.h"
#include"delay.h"
#include"key.h"
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能PB端口时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //LED0-->PB.1端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure); //根据设定参数初始化GPIOB.1
GPIO_SetBits(GPIOB,GPIO_Pin_1); //PB.1 输出高
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //LED1-->PB.2 端口配置
GPIO_InitStructure .GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure .GPIO_Speed = GPIO_Speed_50MHz ; //io口速度50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure); //IO口速度为50MHz
GPIO_SetBits(GPIOB,GPIO_Pin_2); //PB.2 输出高
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //led2-->PB.3端口设置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度为50MHz
GPIO_Init (GPIOB,&GPIO_InitStructure ); //根据设定参数初始化
GPIO_SetBits (GPIOB,GPIO_Pin_3 ); //PB3 输出高
}
void shoudong (void )
{
if(shengkey==1&&jiangkey==0) //升档
{
LED0=1;
delay_ms(200);
if(shengkey==1&&jiangkey==0)
{
SD() ;
}
while(shengkey==1&&jiangkey==0)
{
shengdang=0;
jiangdang=0;
lihe=0;
}
delay_ms(200);
}
if(shengkey==0&&jiangkey==1) //降档
{
delay_ms(200);
if(shengkey==0&&jiangkey==1)
{
JD() ;
}
while(shengkey==0&&jiangkey==1)
{
shengdang=0;
jiangdang=0;
lihe=0;
}
delay_ms(200);
}
if(shengkey==1&&jiangkey==1) //紧急中断动力
{
delay_ms(200);
if(shengkey==1&&jiangkey==1)
{
lihe = 1;
}
while(shengkey==1&&jiangkey==1)
{
shengdang=0;
jiangdang=0;
lihe=0;
}
delay_ms(200);
}
if(lihekey==1) //离合
{
delay_ms(10);
if(lihekey==1)
{
LH();
}
while(lihekey==1)
{
shengdang=0;
jiangdang=0;
lihe=0;
}
delay_ms(10);
}
}
void SD ( void )
{
lihe = 1;
delay_ms(200);
shengdang = 1;
delay_ms(200);
lihe = 0;
delay_ms(200);
shengdang = 0;
}
void JD ( void )
{
lihe = 1;
delay_ms(200);
jiangdang = 1;
delay_ms(200);
lihe = 0;
delay_ms(200);
jiangdang = 0;
}
void LH ( void )
{ vu8 t;
lihe = 1;
while(1)
{
t=KEY_Scan(0); //得到键值
switch(t)
{
case lihekey_PRES:
lihe = 0;
break;
}
break ;
}
}
<exti.h>
#ifndef __EXTI_H
#define __EXTI_H
#include "sys.h"
void EXTIX_Init(void);//IO初始化
#endif
<exti.c>
#include "exti.h"
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "key.h"
#include "led.h"
void EXTIX_Init(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//外部中断,需要使能AFIO时钟
KEY_Init();//初始化按键对应io模式
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource1); //PA1 离合
EXTI_InitStructure.EXTI_Line = EXTI_Line1;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
GPIO_EXTILineConfig (GPIO_PortSourceGPIOA,GPIO_PinSource2 ); //PA2 升档
EXTI_InitStructure.EXTI_Line = EXTI_Line2;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling ;
EXTI_InitStructure.EXTI_LineCmd = ENABLE ;
GPIO_EXTILineConfig (GPIO_PortSourceGPIOA,GPIO_PinSource3 ); //PA3 降档
EXTI_InitStructure.EXTI_Line = EXTI_Line3;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling ;
EXTI_InitStructure.EXTI_LineCmd = ENABLE ;
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn; //PA1 离合
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE ;
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn; //PA2 升档
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn; //PA3 降档
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE ;
}
void EXTI1_IRQhandler (void ) // 离合
{ vu8 t;
delay_ms(100);
if (lihekey == 1)
{
LH();
}
EXTI_ClearITPendingBit(EXTI_Line1 );
}
void EXTI2_IRQhandler (void ) //升档
{
delay_ms(100);
if (shengkey == 1)
{
SD() ;
}
EXTI_ClearITPendingBit(EXTI_Line2);
}
void EXTI3_IRQhandler (void ) //降档
{
delay_ms(100);
if (jiangkey == 1)
{
JD ();
}
EXTI_ClearITPendingBit(EXTI_Line3 );
}
<main.c>
#include "stm32f10x.h"
#include "sys.h"
#include "usart.h"
#include "delay.h"
#include "led.h"
#include "key.h"
#include "exti.h"
#include "misc.h"
void Delay(u32 count)
{
u32 i=0;
for(;i<count;i++);
}
int main(void)
{ vu8 t;
delay_init ();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //优先级
uart_init(9600); //
while(1)
{
shoudong ();
}
}
不好意思啊,我记得我是添加了附件的,可是不知道为什么没有传上去,附上了代码,麻烦看看
一周热门 更多>