STM32位带实现蜂鸣器

fmq.c

#include"fmq.h"
#include"bitband.h"
void Fmq_Init(void)
{
	GPIO_InitTypeDef GPIO_VALUE; 
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
	GPIO_VALUE.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_VALUE.GPIO_Pin=GPIO_Pin_7;
	GPIO_VALUE.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOC,&GPIO_VALUE);	
}
void Fmq_On(void)
{
	//GPIO_SetBits(GPIOC,GPIO_Pin_7);
	PCOut(7)=1;//λ´ø
	
}
void Fmq_Off(void)
{
	//GPIO_ResetBits(GPIOC,GPIO_Pin_7);
	PCOut(7)=0;//λ´ø
}

fmq.h

#ifndef __FMQ_H
#define __FMQ_H
#include "stm32f10x_conf.h"
extern void Fmq_Init(void);
extern void Fmq_On(void);
extern void Fmq_Off(void);
#endif 

bitband.h

#ifndef __BITBAND_H
#define __BITBAND_H

#include "stm32f10x_conf.h"


#define BIT_BAND(ADDR,BITNUM) ((ADDR & 0xF0000000) + 0x2000000 + ((ADDR & 0xFFFFF) << 5) + (BITNUM << 2))

#define MEM_ADDR(ADDR) *(volatile unsigned int *)(ADDR)


#define BITBAND(ADDR,BITNUM) MEM_ADDR(BIT_BAND(ADDR,BITNUM))

#define GPIOA_IDR_ADDR (GPIOA_BASE + 0x08)
#define GPIOA_ODR_ADDR (GPIOA_BASE + 0x0C)

#define GPIOB_IDR_ADDR (GPIOB_BASE + 0x08)
#define GPIOB_ODR_ADDR (GPIOB_BASE + 0x0C)

#define GPIOC_IDR_ADDR (GPIOC_BASE + 0x08)
#define GPIOC_ODR_ADDR (GPIOC_BASE + 0x0C)

#define GPIOD_IDR_ADDR (GPIOD_BASE + 0x08)
#define GPIOD_ODR_ADDR (GPIOD_BASE + 0x0C)

#define PAOut(n) BITBAND(GPIOA_ODR_ADDR, n)
#define PAIn(n)  BITBAND(GPIOA_IDR_ADDR, n)

#define PBOut(n) BITBAND(GPIOB_ODR_ADDR, n)
#define PBIn(n)  BITBAND(GPIOB_IDR_ADDR, n)

#define PCOut(n) BITBAND(GPIOC_ODR_ADDR, n)
#define PCIn(n)  BITBAND(GPIOC_IDR_ADDR, n)

#define PDOut(n) BITBAND(GPIOD_ODR_ADDR, n)
#define PDIn(n)  BITBAND(GPIOD_IDR_ADDR, n)

#endif

main.c

#include"fmq.h"
int main(void)
{
	int i=0,j=0;
	Fmq_Init();
	for(i=0;i<3;i=(i+1)%3)
	{			
		Fmq_On();
		for(j=0;j<1000000;j++);
		Fmq_Off();
		for(j=0;j<1000000;j++);
		
	}
	return 0;
}

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值