–Beep.h
#ifndef __Beep_h
#define __Beep_h
#define Beep_ON (GPIOB->ODR |= 0x0020)
// GPIO_SetBits(GPIOB,GPIO_Pin_5)
#define Beep_OFF (GPIOB->ODR &= 0xFFDF)
// GPIO_ResetBits(GPIOB,GPIO_Pin_5)
void Beep_Init(void);
void Beep_breath(void);
void Beep_delay(int ms);
#endif
–Beep.c
#include <stdio.h>
#include "stm32f10x.h"
#include "Beep.h"
#define T 300
static int i = 0;
static int j = 0;
void Beep_Init(void)
{
RCC->APB2ENR |= 0x0008;
GPIOB->CRL = (GPIOB->CRL&0xFF0FFFFF)|0x00300000;
//需要用到PB5端口
}
void Beep_breath(void)
{
int time;
for( time = 1 ; time < T ; time += 1)
{
Beep_ON;
Beep_delay(15000);
Beep_OFF;
Beep_delay(5000);
}
}
void Beep_delay(int ms)
{
for( i = 0 ; i < 100 ; i++)
for(j = 0 ; j < ms ; j++);
}