5401 views|6 replies

3

Posts

0

Resources
The OP
 

STC12C5A60S2 MCU pattern water light (birds returning home) C program [Copy link]

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;
}
}
}
}

This post is from 51mcu

Latest reply

Great to learn this!   Details Published on 2020-6-6 11:14
 

58

Posts

0

Resources
2
 

Thank you for sharing. It would be better if you added a simple annotation to help beginners learn from it.

This post is from 51mcu
 
 
 

3

Posts

0

Resources
3
 
zwk34 posted on 2019-11-19 08:20 Thank you for sharing. It would be better if you added a simple comment to facilitate beginners to learn from.

OK, thanks for the suggestion, it has been added

This post is from 51mcu
 
 
 

58

Posts

0

Resources
4
 
Mr. Jiang, Chuyi posted on 2019-11-19 11:52 OK, thank you for your suggestion, it has been added

Haha, what a warm-hearted person.

This post is from 51mcu
 
 
 

2

Posts

0

Resources
5
 

Just learning this, great. Thanks.

This post is from 51mcu
 
 
 

4005

Posts

0

Resources
6
 

It will be much simpler to do it with logic, as simple as 5 lines at most

This post is from 51mcu
 
 
 

39

Posts

0

Resources
7
 

Great to learn this!

This post is from 51mcu
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list