码迷,mamicode.com
首页 > 其他好文 > 详细

AVR第5课:蜂鸣器

时间:2015-01-21 13:17:31      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

下面是蜂鸣器的电路图。

技术分享


代码:蜂鸣器代码。

<span style="font-size:18px;">/*
*info:buzzer
*author:chenlu
*date:2014.11.20
*/

//input the head file so that the program can work normally
//iom16v---know the register
//macros---know the BIT(x)
#include<iom16v.h>
#include<macros.h>
//use those can make your study very conveninet
#define uint unsigned int
#define uchar unsigned char

//display methods 
void initSystem();
void delay();

//the main function
void main()
{
 	//init your system
	initSystem();
	while(1)
	{
	  	//start your function,and this is core solution
		PORTA = PORTA & (~BIT(1));
		delay();
		delay();
		delay();
		delay();
		PORTA = PORTA | BIT(1);
		delay();
		delay();
		delay();
		delay();
	}
}

//the method of init system
void initSystem()
{
 	//to make PA1 port output 
	DDRA = DDRA | BIT(1);
	//to make PA1 port output high level
	PORTA = PORTA | BIT(1);
}

//the sub method of delay
void delay()
{
 	uchar i,j;
 	for(i=0;i<250;i++)
	  for(j=0;j<250;j++);
}</span>
让蜂鸣器响一下,接着停一下,如此循环。

AVR第5课:蜂鸣器

标签:

原文地址:http://www.cnblogs.com/gcczhongduan/p/4238579.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!