Single-chip Microcomputer example learning

Source: Internet
Author: User

1. Light the first LED light

#include <reg52.h>
sbit led = p2^0;


void Main ()
{
	while (1)
	{
		led = 0;
	}
}

2.LED Blinking

#include <reg52.h>
typedef unsigned char U8;
typedef unsigned int u16;

Sbit led = p2^0; D1

void Delay (U16 i)
{
	while (i--);
}

void Main ()
{
	while (1)
	{
     led = 0;
     Delay (50000);
     LED = 1;
		 Delay (50000);
	}
}

3.LED Running Lights

#include <reg52.h>
#include "intrins.h"

typedef unsigned char U8;
typedef unsigned int u16;

#define LED P2

void delay (U16 i)
{
	while (i--);
}

void Main ()
{
	U8 i= 0;
	LED = 0xFE;
	Delay (50000);
	
	while (1)
	{
		U8 i = 0;
		LED =0xfe;
		Delay (50000);
		for (i = 0; i<7; i++)
		{
		   led = _crol_ (led,1);
			 Delay (50000);
		}
		
		for (i = 0;i<7;i++)
		{
			led = _cror_ (led,1);
			Delay (50000);}}}

Relays

#include <reg51.h>	
sbit RELAY = p1^4;
Sbit K1    = p2^7;
Main ()
{
if (! K1) RELAY = 0;
else 	RELAY = 1;
}

4. Buzzer

#include <reg52.h>
#include "intrins.h"

typedef unsigned char U8;
typedef unsigned int u16;

Sbit beep = p1^5;

void Delay (U16 i)
{
	while (i--);
}

void Main ()
{
	while (1)
	{
		 beep = ~beep;
		 Delay (ten);
	}
}

5. Timers

#include "reg52.h"
typedef unsigned int u16;	  
typedef unsigned char U8;
Sbit led=p2^0;
void Timer0init ()
{
tmod|=0x01;
th0=0xfc;//timing 1ms
tl0=0x18;	
Et0=1;
ea=1;
tr0=1;
}

void Main ()
{	
	timer0init ();  
	while (1);		
}

void Timer0 () Interrupt 1
{
	static u16 I;
	TH0=0XFC;	
	tl0=0x18;
	i++;
	if (i==1000)
	{
		i=0;
		led=~led;	
	}	
}

6. Press the button

#include "reg52.h"			

typedef unsigned int u16;	 
typedef unsigned char U8;

Sbit k1=p3^1;	 
Sbit led=p2^0;	

void Delay (U16 i)
{
	while (i--);	
}

void Keypros ()
{
	if (k1==0)		  
	{	
		delay ();   
		if (k1==0)	 
		{
			led=~led;	  
		}
		while (!K1);	 
	}		
}

void Main ()
{	
	while (1)
	{	
		Keypros ()  	
	}		
}

7. External interrupts

Interrupt 1

#include "reg52.h"			

typedef unsigned int u16;	  
typedef unsigned char U8;

Sbit k3=p3^2;  
Sbit led=p2^0;	 

void Delay (U16 i)
{
	while (i--);	
}

void Int0init ()
{
	it0=1;
	Ex0=1;
	ea=1;
}

void Main ()
{	
	int0init ();  
	while (1);		
}



void Int0 ()	interrupt 0		
{
	delay (+);	
	if (k3==0)
	{
		led=~led;
	}
}

Interrupt 2

#include "reg52.h"			 

typedef unsigned int u16;	
typedef unsigned char U8;

Sbit k3=p3^3;  
Sbit led=p2^1;	 

void Delay (U16 i)
{
	while (i--);	
}

void Int1init ()
{
	it1=1;
	ex1=1;
	ea=1;	
}


void Main ()
{	
	int1init ();  
	while (1);		
}



void Int1 ()	interrupt 2		
{
	delay (+);	
	if (k3==0)
	{
		led=~led;
	}
}

8, Digital tube display






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.