头文件:
#ifndef __SP_H
#define __SP_H
typedef struct {
unsigned int mode;
unsigned int otyper;
unsigned int ospeed;
unsigned int pupdr;
unsigned int idr;
unsigned int odr;
} GPIO_T;
#define RCC (*(volatile unsigned int*)0x50000A28)
#define GPIOB ((GPIO_T*)0x50003000)
#define GPIOE ((GPIO_T*)0x50006000)
void all_sert();
void output_sp();
void dear_5s(int ms);
#endif
main:
// #include "led.h"
#include "sp.h"
int main()
{
// all_led_init();
all_sert();
while (1) {
// all_led_flash();
output_sp();
}
return 0;
}
函数:
#include "sp.h"
void dear_5s(int ms)
{
for (int i = 0; i < ms; i++)
for (int i = 0; i < 1800; i++)
;
}
void all_sert()
{
// 蜂鸣器
// 设置:RCC时钟
RCC |= (0x1 << 2);
// 设置:输出
GPIOB->mode &= (~(0x3 << 12));
GPIOB->mode |= (0x1 << 12);
// 设置:输出类型
GPIOB->otyper &= (~(0x1 << 6));
// 设置:输出速度
GPIOB->ospeed &= (~(0x3 << 12));
// 设置:取消上拉、下拉
GPIOB->pupdr &= (~(0x3 << 12));
// 风扇
RCC |= (0x1 << 4);
// 设置:输出
GPIOE->mode &= (~(0x3 << 18));
GPIOE->mode |= (0x1 << 18);
// 设置:输出类型
GPIOE->otyper &= (~(0x1 << 9));
// 设置:输出速度
GPIOE->ospeed &= (~(0x3 << 18));
// 设置:取消上拉、下拉
GPIOE->pupdr &= (~(0x3 << 18));
}
void output_sp()
{
// 设置:高电平
GPIOB->odr &= (~(0x1 << 6));
GPIOB->odr |= (0x1 << 6);
dear_5s(5000);
// 设置:低电平
GPIOB->odr &= (~(0x1 << 6));
// 设置:高电平
GPIOE->odr &= (~(0x1 << 9));
GPIOE->odr |= (0x1 << 9);
dear_5s(5000);
GPIOE->odr &= (~(0x1 << 9));
}