初级会员

- 积分
- 82
- 金钱
- 82
- 注册时间
- 2020-10-10
- 在线时间
- 23 小时
|
1金钱
///////////////////////main.c///////////////////
#include "stm32f10x.h" // Device header
#include "delay.h"
#include "sys.h"
#include "key.h"
#include "stepper.h"
#include "led.h"
uint8_t KeyNum;
int main(void)
{
// OLED_Init();
KEY_Init();
// Stepper_GPIOInit();
LED_Init();
// LED0 = 0;
// LED1 = 0;
//delay_ms(20);
// Stepper_RotateByStep(Foreward, 512, 3);
// Stepper_RotateByStep(Reversal, 512, 3);
// Stepper_RotateByLoop(Foreward, 1, 3);
while(1)
{
KeyNum = Key_GetNum();
if (KeyNum == 1)
{
//Stepper_RotateByLoop(Foreward, 1, 3);
LED0 =0;
}
if (KeyNum == 2)
{
//Stepper_RotateByLoop(Reversal, 1, 3);
LED1 = 0;
}
}
return 0;
}
////////////////////key.c////////////////
#include "stm32f10x.h" // Device header
#include "delay.h"
#include "key.h"
#include "led.h"
void KEY_Init(void) //IO³õʼ»¯
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);//ʹÄÜ ORTA,PORTEʱÖÓ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //ÉèÖóÉÉÏÀ­ÊäÈë
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;//KEY0-KEY1
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
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
}
/**
* @brief ·µ»Ø°´Ï°´¼üµÄÖµ£¬Èô²»°´Ï°´¼üĬÈÏ·µ»»0
* @param ÎÞ
* @retval KeyNum °´¼ü¶ÔÓ¦µÄÖµ,°´Ï B1°´¼ü·µ»Ø1,°´Ï B11·µ»Ø2
*/
uint8_t Key_GetNum(void)
{
uint8_t KeyNum = 0;
if(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_4) == 0) // ¶ÁÈ¡1¶Ë¿ÚµÄÖµ
{
delay_ms(20);
while (GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_4) == 0); // Èç¹û²»ËÉÊÖ,³ÌÐò½«Ôڴ˵ȴý
delay_ms(20);
KeyNum = 1;
}
if(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_3) == 0) // ¶ÁÈ¡11¶Ë¿ÚµÄÖµ22222
{
delay_ms(20);
while (GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_3) == 0); // Èç¹û²»ËÉÊÖ,³ÌÐò½«Ôڴ˵ȴý
delay_ms(20);
KeyNum = 2;
}
// if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4) == 1) // ¶ÁÈ¡11¶Ë¿ÚµÄÖµ
// {
// delay_ms(20);
// while (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4) == 1); // Èç¹û²»ËÉÊÖ,³ÌÐò½«Ôڴ˵ȴý
// delay_ms(20);
// KeyNum = 3;
// }
return KeyNum;
}
//////////////////////////////led.c////////////////
#include "stm32f10x.h"
#include "led.h"
//LED IO³õʼ»¯
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE, ENABLE); //ʹÄÜ B,PE¶Ë¿ÚʱÖÓ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //LED0--> B.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--> E.5 ¶Ë¿ÚÅäÖÃ, ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO¿ÚËÙ¶ÈΪ50MHz
GPIO_Init(GPIOE, &GPIO_InitStructure); //ÍÆÍìÊä³ö £¬IO¿ÚËÙ¶ÈΪ50MHz
GPIO_SetBits(GPIOE,GPIO_Pin_5); //PE.5 Êä³ö¸ß
}
|
最佳答案
查看完整内容[请看2#楼]
程序里没有关LED灯的
KeyNum = Key_GetNum();
if (KeyNum == 1)
{
LED0 =0;
}
if (KeyNum == 2)
{
LED1 = 0;
}
|