This post was last edited by Mr. Jiang and Chuyi on 2019-11-19 11:52
This content is originally created by EEWORLD forum users Jiang Xiansheng and Chuyi . If you want to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source
Topic requirements: Do not use arrays and delay functions , use timers
#include <STC12C5A60S2.H> //Header file of this microcontroller
unsigned int Count_disturb = 0; //Record the number of interruptions
unsigned int model = 0; // event variable in switch statement
unsigned char i = 0, j = 0; // double loop control variable
void Int_T0(void) interrupt 1 //interrupt function
{
TH0 = -9;
Count_disturb++;
}
void main() //Main function
{
P0M0 = 0xff; //Push-pull output, increase current
TMOD = 0x01; //Timer 0 selects working mode 1, 16-bit software reload initial value mode
TH0 = -9; //Default TL0 = 0, note: registers can only store non-negative numbers, after compilation, it is automatically converted to TH0 = 65536 - 9
ET0 = 1; //The following three lines are fixed operations to start the timer
EA = 1;
TR0 = 1;
while (1) //Main loop
{
switch (model)
{
case 0:
if (Count_disturb == 100) //The number can be changed arbitrarily, the interruption time is 2.5ms
{
P0 = 0x01 << i++; //General running light
i = i & 7; //There are 8 lights in total, & 7 is used to make the 8 lights light up in sequence and then repeat the running
Count_disturb = 0; //Assign 0 and enter the next timing
j++; //The condition for entering the next loop j == 7 is established, and the value can be changed arbitrarily
}
break;
//The following event processing is similar to event 0
case 1:
if (Count_disturb == 100)
{
P0 = 0x80 | (0x01 << i++);
if (i == 6)
{
j = 7;
}
i = i & 7;
Count_disturb = 0;
}
break;
case 2 :
if (Count_disturb == 100)
{
P0 = 0xC0 | (0x01 << i++);
if (i == 5)
{
j = 7;
}
i = i & 7;
Count_disturb = 0;
}
break;
case 3:
if (Count_disturb == 100)
{
P0 = 0xE0 | (0x01 << i++);
if (i == 4)
{
j = 7;
}
i = i & 7;
Count_disturb = 0;
}
break;
case 4:
if (Count_disturb == 100)
{
P0 = 0xF0 | (0x01 << i++);
if (i == 3)
{
j = 7;
}
i = i & 7;
Count_disturb = 0;
}
break;
case 5:
if (Count_disturb == 100)
{
P0 = 0xF8 | (0x01 << i++);
i = i & 7;
Count_disturb = 0;
if (i == 2)
{
j = 7;
}
}
break;
case 6:
if (Count_disturb == 100)
{
P0 = 0xFC | (0x01 << i++);
i = i & 7;
Count_disturb = 0;
if (i == 1)
{
j = 7;
}
}
break;
case 7:
if (Count_disturb == 100)
{
P0 = 0xFE;
Count_disturb = 0;
j = 7;
}
break;
case 8:
if (Count_disturb == 100)
{
P0 = 0xFF;
Count_disturb = 0;
j = 7;
}
break;
case 9:
do
{
if (Count_disturb == 100)
{
P0 = ~P0;
Count_disturb = 0;
i++;
}
} while (i <= 4);
j = 7;
i = 0;
break;
}
//Second loopif
(j == 7)
{
model++;
j = 0;
if (model == 10)
{
model = 0;
}
}
}
}