C51 tutorial second 51 program: Marquee and running lights

Publisher:翩翩轻舞Latest update time:2022-01-27 Source: eefocusKeywords:C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

keil+proteus

The second 51 program: Marquee and running lights

This is a AT89c51 microcontroller tutorial. We will provide blog tutorials and accompanying materials for download every time.

A single-chip microcomputer is a small CPU that is widely used in the electronics field. It can be found in products such as televisions, refrigerators, washing machines, and air conditioners. It is a programmable MCU that can achieve many functions according to different codes.


Today we will enter the second program, the marquee. We will follow the steps, first the hardware, then the software, and draw the schematic diagram first.

insert image description here

After setting up, start writing code


#include


void delayms(unsigned int t)

{

unsigned int i,j;

for(i=0; i for(j=0; j<120; j++);

}

void main(void) //Main function

{

int n;

unsigned char a[8] = { //Get table array

0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};


while(1)

{

for(n=0; n<8; n++) // loop to get the table water lamp

{

P1 = a[n];

delayms(500);

}

}

}


Write the Keil project and compile to generate a hex file

insert image description here

Enter proteus simulation, burn the hex file, and click run...

The simulation starts running the ticker.

insert image description here

Keil engineering code and proteus simulation schematic package: http://download.csdn.net/download/mbs520/12118172


*Code interpretation

1,

#include

Include the header file . Why do we need to include the header file? Because we are going to write a 51 program, and the final code is to run in the 51 microcontroller, so we need to include a 51 header file. This file contains some definitions of all 51 registers. How to view it? It will appear in your project file when compiling. You can view it in the project file on the left side of Keil4.

insert image description here

2,

void delayms(unsigned int t)//delay function

{


int a,b;

for(a=0; a for(b=0; b<120; b++);


}

The delay function makes the CPU repeatedly execute an empty instruction to consume time. During this time, the CPU is still working and the values ​​of each register remain unchanged.


3.

void main(void)

This is the main function, the program starts here

In addition, the 51 single-chip microcomputer will also loop the contents of the main function without the while loop statement.


4.

int n;  

unsigned char a[8] = { //Get table array

0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};


int n; //Here we define n as the count for the for loop

unsigned char a[8] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};

What is this array for? I'll convert it to binary and you'll understand. They are

0000 0001

0000 0010

0000 0100

0000 1000

0001 0000

0010 0000

0100 0000

1000 0000

You will find that there is a 1 moving to the left. We think of this 1 as a light that is on, and 0 as a light that is off. If it flows according to this rule, it will become a running light.


5.

while(1)

{

for(n=0; n<8; n++) // loop to get the table water lamp

{

P1 = a[n];

delayms(500);

}

}


while(1) is an infinite loop, which will execute the contents in it repeatedly


for(n=0; n<8; n++) // loop to get the table water lamp

{undefined

P1 = a[n];

delayms(500);

}

This is the real code of the marquee. By using the for loop with n increasing by itself, the contents of the array can be output at once.

P1: defined in the header file, it is a microcontroller IO port register, 8 bits, each bit stores the high or low level 0 or 1 of P1_0-P1_7 from low to high.

Output a[0] a[1], a[2], a[3], a[4]... to port P1 in sequence to achieve a marquee effect


Baidu network disk link: https://pan.baidu.com/s/18cVVHyE6O1AzvUvZMsApZA

Extraction code: 22pi


(If you have any questions, please leave a message. This is my first post. I hope you can give me some pointers if I have any questions.)

(You can also send me a private message with questions. I will answer them patiently if you see them. I am happy to communicate with you about technical issues. Scope: circuit design, circuit simulation, PCB design, 51 microcontroller, msp430, arduino, stm32, luinx)

Keywords:C51 Reference address:C51 tutorial second 51 program: Marquee and running lights

Previous article:51 MCU - asm and C mixed programming
Next article:51 MCU tutorial: 51 MCU drives four 8*8 dot matrix

Latest Microcontroller Articles
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号