代码是很久之前写的了,仅是记录一下
流水灯:
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //使能GPIOA的时钟
GPIO_InitTypeDef GPIO_InitStructure; //定义GPIO初始化结构体
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2; //选择GPIO引脚
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化GPIOA端口
GPIO_WriteBit(GPIOA, GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2,Bit_SET);
while(1)
{
GPIO_WriteBit(GPIOA, GPIO_Pin_0,Bit_RESET);
Delay_ms(500);
GPIO_WriteBit(GPIOA, GPIO_Pin_0,Bit_SET);
Delay_ms(500);
GPIO_WriteBit(GPIOA, GPIO_Pin_1,Bit_RESET);
Delay_ms(500);
GPIO_WriteBit(GPIOA, GPIO_Pin_1,Bit_SET);
Delay_ms(500);
GPIO_WriteBit(GPIOA, GPIO_Pin_2,Bit_RESET);
Delay_ms(500);
GPIO_WriteBit(GPIOA, GPIO_Pin_2,Bit_SET);
Delay_ms(500);
}
}
代码很简单,就是通过GPIO_WriteBit来控制对应引脚的灯的状态,当然,也可以用GPIO_ResetBits(GPIOx, GPIO_Pin_n)和GPIO_SetBits(GPIOx, GPIO_Pin_n);来控制。因为代码量不大我也没有分模块
附效果:
然后是
按键控制点灯:
button.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "led.h"
void BUTTON_Init(void){
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_11;
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_1)==0){//判断是否被按下
Delay_ms(20);//消抖
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0);
Delay_ms(20);
Keynum=1;
}
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0){
Delay_ms(20);
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0);
Delay_ms(20);
Keynum=2;
}
return Keynum;
}
button.h
#include "stm32f10x.h" // Device header
#ifndef __button_H
#define __button_H
void BUTTON_Init();
uint8_t Key_GetNum(void);
#endif
led.c
#include "stm32f10x.h" // Device header
void LED_Init(void){
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
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(GPIOA,&GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2);
}
void l0_on(void){
GPIO_ResetBits(GPIOA,GPIO_Pin_0);
}
void l0_off(void){
GPIO_SetBits(GPIOA,GPIO_Pin_0);
}
void l0_Turn(void){
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==0){
GPIO_SetBits(GPIOA,GPIO_Pin_0);
}
else{
GPIO_ResetBits(GPIOA,GPIO_Pin_0);
}
}
void l1_on(void){
GPIO_ResetBits(GPIOA,GPIO_Pin_1);
}
void l1_off(void){
GPIO_SetBits(GPIOA,GPIO_Pin_1);
}
void l1_Turn(void){
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_1)==0){
GPIO_SetBits(GPIOA,GPIO_Pin_1);
}
else{
GPIO_ResetBits(GPIOA,GPIO_Pin_1);
}
}
void l2_on(void){
GPIO_ResetBits(GPIOA,GPIO_Pin_2);
}
void l2_off(void){
GPIO_SetBits(GPIOA,GPIO_Pin_2);
}
void l2_Turn(void){
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_2)==0){
GPIO_SetBits(GPIOA,GPIO_Pin_2);
}
else{
GPIO_ResetBits(GPIOA,GPIO_Pin_2);
}
}
这些都没什么好说的,唯一比较值得说一下的就是led_turn,灯亮再按就灭,灯灭再按就亮
main.c
#include "stm32f10x.h" // Device headerc
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "Delay.h"
#include "led.h"
#include "button.h"
uint8_t KeyNum;
uint8_t lsnum;
int main(void)
{
LED_Init();
BUTTON_Init();
while(1)
{
KeyNum=Key_GetNum();
if(KeyNum==1){
l1_Turn();
}
if(KeyNum==2){
l2_Turn();
}
}
}