8種LED閃爍燈控制
基于AVR單片機(jī)ATmega16的8種LED閃爍燈控制轉(zhuǎn)載標(biāo)簽:
#include iom16v.h>
#include macros.h>
#define uchar unsigned char
#define uint unsigned int
void Delay() //延時(shí)函數(shù)
{
uchar a, b, c;
for (a = 1; a; a++)
for (b = 1; b; b++)
for (c = 0; c10; c++) //循環(huán)次數(shù)=255*255*10
;
}
void DelayMs(uint i) //1ms延時(shí),準(zhǔn)確性較Delay();高
{
while(i--)
{
uint j;
for(j=1;j=613;j++)
;
}
}
void LED_on() //打開(kāi)所有LED
{
PORTB =0X00;
Delay();
}
void LED_off() //關(guān)閉所有LED
{
PORTB = 0xFF;
Delay();
}
void LED_01(int i) //LED亮滅控制
{
PORTB = ~BIT(i); //輸出低電平
DelayMs(100); //調(diào)用延時(shí)程序
}
void LED_02(int i) //間隔點(diǎn)亮
{
PORTB=~(BIT(i)|BIT(i-2));
DelayMs(100);
}
void LED_03(int i) //相臨點(diǎn)亮
{
PORTB=~(BIT(i)|BIT(i-1)); //~后內(nèi)容需用括號(hào)括起來(lái)
DelayMs(100);
}
void LED_04(int i) //發(fā)散聚集點(diǎn)亮
{
switch(i)
{
case 0:PORTB=0xE7;DelayMs(100);break; //延時(shí)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) //00,0F,F0,FF方式顯示
{
switch(i)
{
case 0:PORTB=0x00;DelayMs(100);break; //延時(shí)100ms
case 1:PORTB=0x0F;DelayMs(100);break;
case 2:PORTB=0xF0;DelayMs(100);break;
case 3:PORTB=0xFF;DelayMs(100);break;
default:break;
}
}
void LED_06(int i)
{
switch(i)
{
case 0:PORTB=0XAA;DelayMs(100);break;
case 1:PORTB=0X55;DelayMs(100);break;
}
}
void main()
{
int l,m,n,o,p,q,r,s,i,j;
DDRA =0X00; //端口上拉輸入
PORTA=0XFF;
DDRB =0xFF; //端口輸出
PORTB="0xFF"; //輸出高電平,LED熄滅
DDRC =0X00;
PORTC=0XFF;
DDRD =0X00;
PORTD=0XFF;
for (r=0;r5;r++)
{
for(l=0;l5;l++) //模式1:順序點(diǎn)亮
{
for (i = 0; i 8; i++) //順序單個(gè)點(diǎn)亮LED
LED_01(i);
for (i = 6; i > 0; i--) //逆序單個(gè)點(diǎn)亮LED
LED_01(i);
}
LED_off();
for(m=0;m5;m++) //模式2:順序單個(gè)間隔點(diǎn)亮
{
for (i = 0; i 8; i += 2) //順序間隔點(diǎn)亮LED
LED_01(i);
for (i = 7; i > 0; i -= 2) //逆序間隔點(diǎn)亮LED
LED_01(i);
}
LED_off();
for(n=0;n5;n++) //模式3:間隔點(diǎn)亮
{
for (i = 2; i 8; i++) //間隔順序同時(shí)點(diǎn)亮
LED_02(i);
for (i = 6; i > 2; i--) //間隔逆序同時(shí)點(diǎn)亮
LED_02(i);
}
LED_off();
for(o=0;o5;o++) //模式4:相臨點(diǎn)亮
{
for (i = 1; i 8; i++) //相臨順序同時(shí)點(diǎn)亮
LED_03(i);
for (i = 6; i > 1; i--) //相臨逆序同時(shí)點(diǎn)亮
LED_03(i);
}
LED_off();
for(p=0;p5;p++) //模式5:發(fā)散聚集點(diǎn)亮
{
for(i=0;i4;i++) //發(fā)散點(diǎn)亮
LED_04(i);
for(i=2;i>0;i--) //聚集點(diǎn)亮
LED_04(i);
}
LED_off();
for(q=0;q5;q++) //模式6:四四點(diǎn)亮
{
for(i=0;i4;i++) //四四順序點(diǎn)亮
LED_05(i);
for(i=2;i>0;i--) //四四逆序點(diǎn)亮
LED_05(i);
}
LED_off();
for(s=0;s5;s++) //模式7:四四點(diǎn)亮
{
for(i=0;i2;i++) //四四順序點(diǎn)亮
LED_06(i);
}
LED_off();
for(j=0;j10;j++) //模式8:全部點(diǎn)亮熄滅
{
LED_on();
LED_off();
}
}
//MCUCR=0x40; //空閑模式,CPU占用100%
//MCUCR=0x50; //ADC噪聲抑制模式,CPU占用100%
//MCUCR=0x60; //掉電模式,CPU占用80%
//MCUCR=0x70; //省電模式,CPU占用4%
//MCUCR=0xE0; //Standby模式,CPU占用80%
MCUCR=0xF0; //擴(kuò)展Standby模式,CPU占用4%
asm("sleep"); //CPU休眠指令
}
c++相關(guān)文章:c++教程
led燈相關(guān)文章:led燈原理
評(píng)論