一、程序结构
二、仿真效果
三、程序源码
1、main.c2、buzzer.c- #include <iom16v.h>
- #include <macros.h>
- //7种声音,由低到高
- void buzzer01()
- {
- for(OCR1A= 1000;OCR1A<1700;OCR1A+=100)
- {
- TCCR1A = 0x40;
- TCCR1B = 0x09;
- DelayMs(30); //蜂鸣器鸣叫30mS钟,定时器一开始工作蜂鸣器即开始鸣叫
- TCCR1A = 0x00; //定时器停止工作,无频率产生,蜂鸣器停止鸣叫
- }
- }
- //7种声音,由高到低
- void buzzer02()
- {
- for(OCR1A= 1600;OCR1A>900;OCR1A-=100)
- {
- TCCR1A = 0x40;
- TCCR1B = 0x09;
- DelayMs(30); //蜂鸣器鸣叫30mS钟,定时器一开始工作蜂鸣器即开始鸣叫
- TCCR1A = 0x00; //定时器停止工作,无频率产生,蜂鸣器停止鸣叫
- }
- }
复制代码 3、led.c- #include <iom16v.h>
- #include <macros.h>
- void LED_on() //打开所有LED
- {
- PORTB =0X00;
- Delay();
- }
- void LED_off() //关闭所有LED
- {
- PORTB = 0xFF;
- Delay();
- }
- void LED_01(int i) //LED亮灭控制
- {
- PORTB = ~BIT(i); //输出低电平
- DelayMs(100); //调用延时程序
- }
- void LED_02(int i) //间隔点亮
- {
- PORTB=~(BIT(i)|BIT(i-2));
- DelayMs(100);
- }
- void LED_03(int i) //相临点亮
- {
- PORTB=~(BIT(i)|BIT(i-1)); //~后内容需用括号括起来
- DelayMs(100);
- }
- void LED_04(int i) //发散聚集点亮
- {
- switch(i)
- {
- case 0:PORTB=0xE7;DelayMs(100);break; //延时100ms
- case 1:PORTB=0xDB;DelayMs(100);break;
- case 2:PORTB=0xBD;DelayMs(100);break;
- case 3:PORTB=0x7E;DelayMs(100);break;
- default:break;
- }
- }
- void LED_05(int i)
- {
- switch(i)
- {
- case 0:PORTB=0XAA;DelayMs(100);break;
- case 1:PORTB=0X55;DelayMs(100);break;
- }
- }
复制代码 4、int0_inerrupt.c- #include <iom16v.h>
- #include <macros.h>
- #define uchar unsigned char
- #define uint unsigned int
- #pragma interrupt_handler Int0:2 //外部中断INT0
- void Int0(void)
- {
- uint i,z;
- for (z=0;z<5;z++)
- {
- buzzer01(); //7种声音,由低到高
- for (i = 0; i < 8; i ++)
- {
- PORTB = ~(1 << i);
- DelayMs(100);
- }
- PORTB=0XFF; //关闭所有LED
- buzzer02();//7种声音,由高到低
- for (i = 0; i < 8; i ++)
- {
- PORTB = ~(1 << i);
- DelayMs(200);
- }
- PORTB=0XFF; //关闭所有LED
- }
- }
复制代码 5、delay.c- /*******************************
- Platform : AVR mega16学习板(www.iccavr.com)
- function :延时函数
- Clock F : 3.6864M
- Software : ICCAVR7.14C
- Author : 林夕依然
- Version : 09.02.25
- comments :
- 1、两种方式实现延时
- ********************************/
- /*---------------------------------------------------------------------------------
- 延时程序计算方法
- 计数个数j = 延时时间/6*晶振频率 - 1
- ---------------------------------------------------------------------------------*/
- #define uchar unsigned char
- #define uint unsigned int
- //方式一:
- void Delay()
- {
- uchar a, b, c;
- for (a = 1; a; a++)
- for (b = 1; b; b++)
- for (c = 0; c<10; c++) //循环次数=255*255*10
- ;
- }
- //方式二:1ms延时,准确性较Delay();高
- void DelayMs(uint i)
- {
- while(i--)
- {
- uint j;
- for(j=1;j<=613;j++)
- ;
- }
- }
复制代码 四、完整项目文件下载
实验8:7种LED亮灭模式+INT0中断+蜂鸣器(ICC).rar (83.07 KB)
|