The first single-chip microcomputer program

Source: Internet
Author: User
Tags sleep

Today learned a bit of the basics of single-chip microcomputer, wrote a simple LED running light program, to achieve 8 LEDs and so on from right to left cycle flashing.

Paste the code First:

#include <reg51.h>
#include <intrins.h>

void sleep (int t)
{
	while (t--);
}
	
int main ()
{
	int i;	
	P1 = 254;
	while (1)
	{for
		(i = 7; I >= 0; i--)
		{	
			sleep (z);	
			P1=_crol_ (p1,1);}}}


I use is a treasure above the study board, single-chip computer model is STC89C52.

At first I was prepared to use a sbit array to save 8 led addresses, and later found that 51 monolithic computer does not support bit arrays, the following query found this method.

The Reg51.h header file of the program defines the SRF P1 = 0x80, and I now understand that this initialization tells the address of the register P1, and then the operation of the P1 is the operation of this address memory (not quite the same as the usual OS-based initialization), then the following P1 = 254, is the P1 corresponding address of the eight-bit memory to 11111110 (for 0 o'clock corresponding LED light, this number of decimal is 254), and then use the _crol_ () function to achieve the P1 cycle left, so that 0 LED lights cycle change. The sleep () function implements the wait effect through multiple self-subtraction operations.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.