?導(dǎo)讀:《藍(lán)橋杯嵌入式組》專欄文章是博主2019年參加藍(lán)橋杯的嵌入式組比賽所做的學(xué)習(xí)筆記,在當(dāng)年的比賽中,由于忙于準(zhǔn)備考研及保研相關(guān)工作,博主僅僅參加了當(dāng)年的省賽,并獲得了省賽一等獎的成績。成績雖談不上最好,但至少問心無愧。如今2021年回頭再看該系列文章,仍然感觸頗多。為了能更好地幫助到單片機(jī)初學(xué)者,今年特地抽出時間對當(dāng)年的文章邏輯和結(jié)構(gòu)進(jìn)行重構(gòu),以達(dá)到初學(xué)者快速上手的目的。需要指出的是,由于本人水平有限,如有錯誤還請讀者指出,非常感謝。那么,接下來讓我們一起開始愉快的學(xué)習(xí)吧。
一、主要代碼
main.c
/*******************************************************************************
* 文件名:main.c
* 描 述:
* 作 者:CLAY
* 版本號:v1.0.0
* 日 期: 2019年2月19日
* 備 注:修改后的LCD例程
* 定時器實現(xiàn)2ms定時中斷;按下B1,LED1狀態(tài)改變
*******************************************************************************
*/
#include "stm32f10x.h"
#include "lcd.h"
#include "e2prom.h"
#include "stdio.h"
#include "i2c.h"
#include "adc.h"
#include "rtc.h"
#include "usart2.h"
#include "pwm.h"
#include "pwm_oc.h"
#include "pwm_ic.h"
#include "timer.h"
#include "led.h"
#include "key.h"
u32 TimingDelay = 0;
u8 RxdCnt = 0;
u8 RxdOver = 0;
u8 RxdBuf[20];
void Delay_Ms(u32 nTime);
u8 RTC_Flag = 0;
//Main Body
int main(void)
{
STM3210B_LCD_Init();
LCD_Clear(Blue);
LCD_SetBackColor(Blue);
LCD_SetTextColor(White);
SysTick_Config(SystemCoreClock/1000);
//PWM_Init(500, 60);//500Hz 60%方波//PA1
PWM_OC_Init(500, 60);//500Hz 60%方波 PA1
PWM_IC_Init();//PA7
TIM4_Init(2000, 72);//定時2ms
LED_Init();//LED配置
KeyInit();
while(1)
{
KeyDriver();
}
}
void KeyAction(int code)
{
if(code == 1)//按下B1,LED1狀態(tài)改變
{
GPIOD->ODR |= (1<<2);//使能573
GPIOC->ODR ^= (1<<8);//LED1改變
GPIOD->ODR &= (1<<2);//失能573
}
}
//
void Delay_Ms(u32 nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
key.c
#include "stm32f10x.h"
#include "key.h"
extern void KeyAction(int code);
u8 KeySta[4] = {1, 1, 1, 1};
void KeyInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空輸入
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空輸入
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void KeyDriver(void)
{
u8 i;
static u8 backup[4] = {1, 1, 1, 1};
for(i=0; i<4; i++)
{
if(backup[i] != KeySta[i])
{
if(backup[i] != 0)
{
KeyAction(i+1);
}
backup[i] = KeySta[i];
}
}
}
void KeyScan(void)
{
u8 i;
static u8 keybuf[4] = {0xFF, 0xFF, 0xFF, 0xFF};
keybuf[0] = (keybuf[0] << 1) | KEY1;
keybuf[1] = (keybuf[1] << 1) | KEY2;
keybuf[2] = (keybuf[2] << 1) | KEY3;
keybuf[3] = (keybuf[3] << 1) | KEY4;
for(i=0; i<4; i++)
{
if(keybuf[i] == 0xFF)
{
KeySta[i] = 1;
}
else if(keybuf[i] == 0x00)
{
KeySta[i] = 0;
}
else
{}
}
}
key.h
#ifndef _KEY_H
#define _KEY_H
#define KEY1 GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)
#define KEY2 GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8)
#define KEY3 GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1)
#define KEY4 GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_2)
void KeyInit(void);
void KeyDriver(void);
void KeyScan(void);
#endif
stm32f10x_it.c
void TIM4_IRQHandler(void)
{
if(TIM_GetITStatus(TIM4, TIM_IT_Update) == SET)
{
TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
KeyScan();
}
}
二、注意事項
2.1、按鍵分別再PA0/PA8/PB1/PB2
2.2、宏定義按鍵值得在品味下
#define KEY1 GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)
2.3、別忘了在主函數(shù)中KeyInit
結(jié)語:以上就是本篇文章的全部內(nèi)容啦,希望大家可以多多支持我的原創(chuàng)文章。如有錯誤,請及時指正,非常感謝。