LED.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
void LED_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//打开GPIOA时钟
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1|GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_Write(GPIOB, ~0x000B);
}
void LED_LSD()
{
GPIO_Write(GPIOB, ~0x0001); //0000 0000 0000 0001
Delay_ms(60000);
GPIO_Write(GPIOB, ~0x0002); //0000 0000 0000 0010
Delay_ms(60000);
GPIO_Write(GPIOB, ~0x008); //0000 0000 0000 0100
Delay_ms(60000);
}
Key.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
void Key_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//打开GPIOA时钟
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
uint8_t Key_GetNum(void)
{
uint8_t KeyNum = 0;
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0) == 1)//检测是否被按下
{
Delay_ms(100);//消抖
while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0) == 1);
Delay_ms(100);
KeyNum = 1;
}
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 2)
{
Delay_ms(100);
while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 2);
Delay_ms(100);
KeyNum = 2;
}
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_2) == 3)
{
Delay_ms(100);
while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_2) == 3);
Delay_ms(100);
KeyNum = 3;
}
return KeyNum;
}
motor.c
#include "stm32f10x.h" // Device header
void Motor_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);//打开GPIOB时钟
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_SetBits(GPIOC, GPIO_Pin_0);
GPIO_SetBits(GPIOC, GPIO_Pin_1);
}
void SetZhengZhuan()
{
GPIO_SetBits(GPIOC, GPIO_Pin_0);
GPIO_ResetBits(GPIOC, GPIO_Pin_1);
}
void SetFanZhuan()
{
GPIO_ResetBits(GPIOC, GPIO_Pin_0);
GPIO_SetBits(GPIOC, GPIO_Pin_1);
}
void SetTingZhi()
{
GPIO_ResetBits(GPIOC, GPIO_Pin_0);
GPIO_ResetBits(GPIOC, GPIO_Pin_1);
}
main.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "Key.h"
#include "LED.h"
#include "motor.h"
uint16_t num = 0;
uint16_t key;
int main(void)
{
LED_LSD();
Key_Init();//独立按键初始化
LED_Init();//LED初始化
while (1)
{
LED_LSD();//流水灯
key = Key_GetNum(); //扫描按键是否被按下,如果按下返回键码
if(key)//如果按键被按下
{
switch(key)
{
case 1:
SetZhengZhuan();
break;
case 2:
SetFanZhuan();
break;
case 3:
SetTingZhi();
break;
}
}
}
}
仿真图连线