工程可见Github<传送门>
main.c
/*******************************************************************************
* 文件名:main.c
* 描 述:
* 作 者:CLAY
* 版本号:v1.0.0
* 日 期: 2019年2月19日
* 备 注:修改后的LCD例程
* 按键B1切换LED1状态并且蜂鸣器鸣叫0.1s
* 按键B2按下发生长鸣;按键B3停止鸣叫
*******************************************************************************
*/
#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"
#include "beep.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();
BeepInit();
while(1)
{
KeyDriver();
}
}
void KeyAction(int code)
{
if(code == 1)//按下B1,LED1状态改变
{
GPIOD->ODR |= (1<<2);//使能573
GPIOC->ODR ^= (1<<8);//LED1改变
GPIOD->ODR &= (1<<2);//失能573
Beep(100);//伴随滴的一声
}
else if(code == 2)//B2
{
Beep(-1);//长鸣
}
else if(code == 3)
{
Beep(0);//停止鸣叫
}
}
//
void Delay_Ms(u32 nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
beep.c
#include "stm32f10x.h"
#include "beep.h"
s32 BeepTimer = 0;//注意是有符号类型
void BeepInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);//注意这里要开启AFIO时钟
GPIO_PinRemapConfig(GPIO_Remap_SWJ_NoJTRST, ENABLE);//PB4引脚的重映射成普通GPIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//强推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
BeepOff();//上电初始化关闭蜂鸣器
}
void BeepScan(u8 ms)//ms对应的定时器扫描时间
{
if(BeepTimer > 0)
{
BeepTimer -= ms;
if(BeepTimer <= 0)
{
BeepOff();
BeepTimer = 0;
}
}
}
void Beep(s32 time)
{
BeepTimer = time;
if(BeepTimer == 0)
{
BeepOff();
}
else
{
BeepOn();
}
}
beep.h
#ifndef _BEEP_H
#define _BEEP_H
#define BeepOff() GPIO_SetBits(GPIOB, GPIO_Pin_4)
#define BeepOn() GPIO_ResetBits(GPIOB, GPIO_Pin_4)
void BeepInit(void);
void BeepScan(u8 ms);
void Beep(s32 time);
#endif
stm32f10x_it.c
void TIM4_IRQHandler(void)
{
if(TIM_GetITStatus(TIM4, TIM_IT_Update) == SET)
{
TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
KeyScan();
BeepScan(2);
}
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);//注意这里要开启AFIO时钟
GPIO_PinRemapConfig(GPIO_Remap_SWJ_NoJTRST, ENABLE);//PB4引脚的重映射成普通GPIO
s32 BeepTimer = 0;
//注意是有符号类型#define BeepOff() GPIO_SetBits(GPIOB, GPIO_Pin_4)