用单片机第一个并行口接8个发光二极管采用共阴极接线变成实现 1 依次

2024-11-14 15:42:46
推荐回答(1个)
回答1:

#include


unsigned int t = 0;

unsigned char cnt = 0;


unsigned char a = 0;


void ConfigTimer0()

{

TMOD = 0x02;

TH0 = 0x9C;

TL0 = 0x9C;

ET0 = 1;

TR0 = 1;

EA = 1;

}

void main()

{

ConfigTimer0();

while(1)

{

switch(a)

{

case 0: P0 = 0xFe; break;

case 1: P0 = 0xFD; break;

case 2: P0 = 0xFB; break;

case 3: P0 = 0xF7; break;

case 4: P0 = 0xEF; break;

case 5: P0 = 0xDF; break;

case 6: P0 = 0xBF; break;

case 7: P0 = 0x7F; break;

default: break;

}

}

}

void interruptTimer0() interrupt 1

{

t++;

if(t >= 10000)

{

t = 0;

a++;

if(a >= 8)

{

a = 0;

}

}

}


2.

#include


unsigned int t = 0;

unsigned char cnt = 0;


unsigned char a = 0;


void ConfigTimer0()

{

TMOD = 0x02;

TH0 = 0x9C;

TL0 = 0x9C;

ET0 = 1;

TR0 = 1;

EA = 1;

}

void main()

{

ConfigTimer0();

while(1)

{

switch(a)

{

case 0: P0 = 0xE7; break;

case 1: P0 = 0xC3; break;

case 2: P0 = 0x81; break;

case 3: P0 = 0x00; break;

case 4: P0 = 0x18; break;

case 5: P0 = 0x3C; break;

case 6: P0 = 0x7E; break;

case 7: P0 = 0xFF; break;

default: break;

}

}

}

void interruptTimer0() interrupt 1

{

t++;

if(t >= 5000)

{

t = 0;

a++;

if(a >= 8)

{

a = 0;

}

}

}