PIC中档单片机蜂鸣器原理图:
使用示例:
/******************************************************************************** 跳线接法:短接P2的2、3* 功能描述:无源蜂鸣器发声*******************************************************************************/#include <htc.h>#include <math.h>#define uint8 unsigned char#define uint16 unsigned int__CONFIG(WDTDIS & LVPDIS & HS & PWRTDIS & BORDIS);//设置配置位//WDTDIS:disable watchdog timer//LVPDIS:low voltage PRogramming disabled//HS:high speed crystal/resonator//PWRTDIS:disable power up timer//BORDIS:disable brown out reset/***************************声明函数*******************************************/void DelayUS(uint8 delay);/******************************************************************************** 函 数 名: DelayUS(uint8 delay)* 函数功能: 微秒延时 for 20MHZ* 入口参数: delay* 返 回: 无*******************************************************************************/void DelayUS(uint8 delay){ while(--delay);}/******************************************************************************* 函 数 名: mian(void)* 函数功能: 驱动无源蜂鸣器发声* 入口参数: 无* 返 回: 无*******************************************************************************/void main(void){ ADCON1 = 0x86; //PORTA设置为数字IO口 TRISA0 = 0; //RA0设置为输出模式 while(1) { RA0 = 1; DelayUS(400); RA0 = 0; DelayUS(400); }}新闻热点
疑难解答