The Three Realms of Marquee

Publisher:快乐时刻Latest update time:2022-05-24 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Shift blocking


#include

#define uint unsigned int

#define uchar unsigned char

void delayms(uint xms)

{

uint i,j;

for(i=xms;i>0;i--)

for(j=110;j>0;j--);

}

void main()

{

fly Su8Data=0x01,Su8Cnt=0;

while(1)

{

P1=Su8Data; //8 pins of P1 port connect to 8 LED lights

delayms(500); //delay 0.5S, blocking delay

Su8Data=Su8Data<<1;//Shift left one bit

Su8Cnt++;

if(Su8Cnt>=8)//8th reassignment to continue from the first position

{

Su8Cnt=0;

Su8Data=0x01;

}

}

}


Because of the "blocking delay", the whole program seems mechanical and rigid, lacking a multi-task parallel framework.


2. Non-blocking shift


#include

#define BLINK_TIME 500

unsigned char timeFlag=0,Su8Data=0x01,Su8Cnt=0;

unsigned int count=0;

void main()

{

TMOD=0x01;

TH0=0xfc; //initial value of timing 1ms

TL0=0x66;

EA=1;

ET0=1;

TR0=1;

while(1)

{

if(count==0) //non-blocking

{

timeFlag=0;//mutex

count=BLINK_TIME; //500ms delay

timeFlag=1;

P1=Su8Data;

Su8Data=Su8Data<<1;

Su8Cnt++;

if(Su8Cnt>=8)//Reassign value for next cycle

{

Su8Cnt=0;

Su8Data=0x01;

}

}

}

}

void T0timer() interrupt 1

{

TH0=0xfc;

TL0=0x66;

if(timeFlag==1 && count>0) // can generate multiple software timers for parallel processing

{

count--;

}

}


Although the basic element of multi-tasking parallel processing, "software timer", is used, it still remains at the shifting stage and is just a marquee, and has not gone beyond the marquee itself.


3. State switching is non-blocking

Two core frameworks: (four zones and one line; switch plus timed interrupt)

The "four zones and one line" structure below is mainly for understanding the approximate "spatial partitioning" of the microcontroller program.

"Zone 1" is a system initialization function, which is specifically used to initialize the microcontroller's own registers and some peripheral output devices that require fast response speed, to prevent the peripheral devices from malfunctioning due to the uncertain level state of the output IO port after power-on, such as malfunction of the driving relay.


"One line" is a delay function, which is mainly prepared for the peripheral initialization function, because the peripheral initialization function is specially used to initialize peripheral chips and modules that do not require immediate processing after power-on. For example, LCD modules, AT24C02 memory chips, etc., these chips need a little time to reset themselves internally at the moment of power-on, and it also takes a little time for the external voltage to stabilize. Only after this time, these chips are in working state, and the microcontroller can communicate with them normally.


The "second area" is the peripheral initialization function, which is specifically used to initialize peripheral chips and modules that do not require immediate processing upon power-up.


"The third area" is the task function that is continuously scanned in the main function.


"Area 4", timing interrupt function. Provides the system's beat time, and processes and scans some functions related to IO port debounce and buzzer driver.


#include

#define BLINK_TIME 500

void SystemInit();

void delayms(unsigned int xms);

void PeripheralInitial();

void LedTask();

sbit led1=P1^0;

sbit led2=P1^1;

sbit led3=P1^2;

sbit led4=P1^3;

sbit led5=P1^4;

sbit led6=P1^5;

sbit led7=P1^6;

sbit led8=P1^7;

unsigned char Gu8Step=0,timeFlag=0;

unsigned int count=0;

void main()

{

SystemInit(); //"First District" of "Four Districts and One Line", system initialization function

delayms(1000); //"One line" of "four zones and one line", the dividing line between "system initialization function" and "peripheral initialization function", delay function

PeripheralInitial(); // "Second zone" of "four zones and one line", peripheral initialization function

while(1)

{

LedTask(); //The "third zone" of "four zones and one line", a task function that is continuously scanned in the main function

}

}


void T0timer() interrupt 1 //The fourth zone of "four zones and one line", the timer interrupt function, provides the system's beat time

{

TH0=0xfc;

TL0=0x66;

if(1==timeFlag && count>0)//Software timer

{

count--;

}

}


void SystemInit()

{

TMOD=0x01;

TH0=0xfc;

TL0=0x66;

EA=1;

ET0=1;

TR0=1;

}


void delayms(unsigned int xms)

{

unsigned int i,j;

for(i=xms;i>0;i--)

for(j=110;j>0;j--);

}


void PeripheralInitial()

{}


void LedTask()

{

switch(Gu8Step)

{

case 0:

if(0==count) //500ms time to execute command

{

led1=0; //The first light is on, the others are off

led2=1;

led3=1;

led4=1;

led5=1;

led6=1;

led7=1;

led8=1;

timeFlag=0;

count=BLINK_TIME; //Reload time

timeFlag=1;

Gu8Step=1; //Switch steps to achieve non-blocking purpose

}

break;

case 1:

if(0==count)

{

led1=1;

led2=0;

led3=1;

led4=1;

led5=1;

led6=1;

led7=1;

led8=1;

timeFlag=0;

count=BLINK_TIME;

timeFlag=1;

Gu8Step=2;

}

break;

case 2:

if(0==count)

{

led1=1;

led2=1;

led3=0;

led4=1;

led5=1;

led6=1;

led7=1;

led8=1;

timeFlag=0;

count=BLINK_TIME;

timeFlag=1;

Gu8Step=3;

}

break;

case 3:

if(0==count)

{

led1=1;

led2=1;

led3=1;

led4=0;

led5=1;

led6=1;

led7=1;

led8=1;

timeFlag=0;

count=BLINK_TIME;

timeFlag=1;

Gu8Step=4;

}

break;

   case 4:

if(0==count)

{

led1=1;

led2=1;

led3=1;

led4=1;

led5=0;

led6=1;

led7=1;

led8=1;

timeFlag=0;

count=BLINK_TIME;

timeFlag=1;

Gu8Step=5;

}

break;

case 5:

if(0==count)

{

led1=1;

led2=1;

led3=1;

led4=1;

led5=1;

led6=0;

led7=1;

led8=1;

timeFlag=0;

count=BLINK_TIME;

timeFlag=1;

Gu8Step=6;

}

break;

case 6:

if(0==count)

{

led1=1;

led2=1;

led3=1;

led4=1;

led5=1;

led6=1;

led7=0;

led8=1;

timeFlag=0;

count=BLINK_TIME;

timeFlag=1;

Gu8Step=7;

}

break;

case 7:

if(0==count)

{

led1=1;

led2=1;

led3=1;

led4=1;

led5=1;

led6=1;

led7=1;

led8=0;

timeFlag=0;

count=BLINK_TIME;

timeFlag=1;

Gu8Step=0;

}

break;

}

}


Although the code size of this program has increased, it does not affect the running efficiency. (If the 8 LED lights are on the P1 and P0 ports respectively or if the two marquees are processed in parallel by multi-tasking, the above two shifting methods will not work.) The 8 LED lights are separated and displayed separately because the 8 pins represent not only LED lights but 8 output signals. These 8 output signals may drive different relays, motors, cannons, missiles and various ever-changing combinational logics in the future. After the separation, the program framework has unlimited scalability.

Reference address:The Three Realms of Marquee

Previous article:Single and double click with separate buttons like a mouse
Next article:One timer interrupt generates N software timers

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号