NOTE: This post is only for beginners in 8051 programming. It explains basic ways to program the registers of the uC.
WARNING: THIS CODES ARE BEEN TESTED AND WORKING WELL. I'M NOT RESPONSIBLE FOR ANY DAMAGE CAUSED BY YOU.
To start the circuit diagram. Please follow my older post for basic circuits: Click here
There are various ways you can program uC8051 controllers.
The following examples are below:
PLEASE NOTE: USE ONLY ONE #INCLUDE FILE I MENTIONED BELOW ACCORDING TO THE CONTROLLER NAME I MENTIONED BELOW. COMMENT THE ONE WHICH DOES NOT HAVE YOUR CONTROLLER NAME CATEGORY BELOW. THE LINES TO BE COMMENTED ARE COLORED IN GREEN. I HAVE ALREADY COMMENTED REGX52.H BY DEFAULT.
1: To program any port to be set as output on 89cxx series microcontroller.
////////////main.c start////////////////
#include "REGX51.h" // for 89c2051,89c4051,89c51, 89s51 controller
//#include "REGX52.h" //for 89c52,89s52 controller
void main( )
{
P0=0x00; //Set port 0 to 0v logic
P1=0x00; //Set port 1 to 0v logic
P2=0x00; //Set port 2 to 0v logic
P3=0x00; //Set port 3 to 0v logic
P1=0x01; //set port 1 = 1. That is 00000001 on 8bit Port 1
P2_1=0x01; // set port 2 bit 1 =1. That is 00000010 on 8bit Port 2
P3=P1; //Set port 3 same as port 1. That is 00000001 on 8bit port 3.
while(1)
{
//Go to infinity to stop the process here. If there is no infinity loop the code gets restarted again.
}
}
///////////main.c end//////////////
2: To blink a pin or port (the below code will turn on and off port 1 pin 0)
PLEASE NOTE: USE ONLY ONE #INCLUDE FILE I MENTIONED BELOW ACCORDING TO THE CONTROLLER NAME I MENTIONED BELOW. COMMENT THE ONE WHICH DOES NOT HAVE YOUR CONTROLLER NAME CATEGORY BELOW. THE LINES TO BE COMMENTED ARE COLORED IN GREEN. I HAVE ALREADY COMMENTED REGX52.H BY DEFAULT.
//////////main.c start/////////
#include "REGX51.h" // for 89c2051,89c4051,89c51, 89s51 controller
//#include "REGX52.h" //for 89c52,89s52 controller
void delay(int n); //delay routine
void delay(int n)
{
int i,j;
for(i=0;i<=100;i++)
{
for(j=0;j<=n;j++); //the loop will be occurring at n * 100 times
}
}
void main( )
{
P0=0x00; //Set port 0 to 0v logic
P1=0x00; //Set port 1 to 0v logic
P2=0x00; //Set port 2 to 0v logic
P3=0x00; //Set port 3 to 0v logic
while(1)
{
P1=0x01; //set port 1 = 1. That is 00000001 on 8bit Port 1
delay(1000); //Call delay routine to pause port 1 state
P1 = 0x00; //set port 1 = 0. That is 00000000 on 8bit port 1
delay(1000); // Call delay routine to pause port 1 state
}
}
///////////main.c end//////////////
3: How to work with timers? Timers work on interrupt routines. You must follow this basic way below to make timer work.
PLEASE NOTE: USE ONLY ONE #INCLUDE FILE I MENTIONED BELOW ACCORDING TO THE CONTROLLER NAME I MENTIONED BELOW. COMMENT THE ONE WHICH DOES NOT HAVE YOUR CONTROLLER NAME CATEGORY BELOW. THE LINES TO BE COMMENTED ARE COLORED IN GREEN. I HAVE ALREADY COMMENTED REGX52.H BY DEFAULT.
//////////main.c start/////////
#include "REGX51.h" // for 89c2051,89c4051,89c51, 89s51 controller
//#include "REGX52.h" //for 89c52,89s52 controller
void timer0_ISR (void) interrupt 1 // for timer 1 if implemented use void timer1_ISR(void)interrupt 2
{
P2_1=!P2_1; // SET PORT 2 PIN 1 = 1 AND THEN 0 ON NEXT INTERRUPT AND VICE VERSA
}
void main( )
{
TMOD = 0xe0; // Set T/C0 Mode 16bit timer
ET0 = 1; // Enable Timer 0 Interrupts
TR0 = 1; // Start Timer 0 Running
EA = 1; // Global Interrupt Enable
TH0 = 0; //Timer count register H byte
TL0 = 0; //Timer count register L byte
P0=0x00; //Set port 0 to 0v logic
P1=0x00; //Set port 1 to 0v logic
P2=0x00; //Set port 2 to 0v logic
P3=0x00; //Set port 3 to 0v logic
while(1)
{
// GO INFINITE LOOP AND WAIT FOR TIMER INTERRUPT
}
}
///////////main.c end//////////////
I will be uploading more basic programming soon. If you want specific applications you got to mix things up and learn. Go to this link here for more codes : ( Click Here )
Download those codes and test your controller. Learn how they have done implement them your way. That is the only way you can learn 8051 microcontrollers.
Thank you for reading.
Macjan Camilo Fernandes ( R & D )
WARNING: THIS CODES ARE BEEN TESTED AND WORKING WELL. I'M NOT RESPONSIBLE FOR ANY DAMAGE CAUSED BY YOU.
To start the circuit diagram. Please follow my older post for basic circuits: Click here
There are various ways you can program uC8051 controllers.
The following examples are below:
PLEASE NOTE: USE ONLY ONE #INCLUDE FILE I MENTIONED BELOW ACCORDING TO THE CONTROLLER NAME I MENTIONED BELOW. COMMENT THE ONE WHICH DOES NOT HAVE YOUR CONTROLLER NAME CATEGORY BELOW. THE LINES TO BE COMMENTED ARE COLORED IN GREEN. I HAVE ALREADY COMMENTED REGX52.H BY DEFAULT.
1: To program any port to be set as output on 89cxx series microcontroller.
////////////main.c start////////////////
#include "REGX51.h" // for 89c2051,89c4051,89c51, 89s51 controller
//#include "REGX52.h" //for 89c52,89s52 controller
void main( )
{
P0=0x00; //Set port 0 to 0v logic
P1=0x00; //Set port 1 to 0v logic
P2=0x00; //Set port 2 to 0v logic
P3=0x00; //Set port 3 to 0v logic
P1=0x01; //set port 1 = 1. That is 00000001 on 8bit Port 1
P2_1=0x01; // set port 2 bit 1 =1. That is 00000010 on 8bit Port 2
P3=P1; //Set port 3 same as port 1. That is 00000001 on 8bit port 3.
while(1)
{
//Go to infinity to stop the process here. If there is no infinity loop the code gets restarted again.
}
}
///////////main.c end//////////////
2: To blink a pin or port (the below code will turn on and off port 1 pin 0)
PLEASE NOTE: USE ONLY ONE #INCLUDE FILE I MENTIONED BELOW ACCORDING TO THE CONTROLLER NAME I MENTIONED BELOW. COMMENT THE ONE WHICH DOES NOT HAVE YOUR CONTROLLER NAME CATEGORY BELOW. THE LINES TO BE COMMENTED ARE COLORED IN GREEN. I HAVE ALREADY COMMENTED REGX52.H BY DEFAULT.
//////////main.c start/////////
#include "REGX51.h" // for 89c2051,89c4051,89c51, 89s51 controller
//#include "REGX52.h" //for 89c52,89s52 controller
void delay(int n); //delay routine
void delay(int n)
{
int i,j;
for(i=0;i<=100;i++)
{
for(j=0;j<=n;j++); //the loop will be occurring at n * 100 times
}
}
void main( )
{
P0=0x00; //Set port 0 to 0v logic
P1=0x00; //Set port 1 to 0v logic
P2=0x00; //Set port 2 to 0v logic
P3=0x00; //Set port 3 to 0v logic
while(1)
{
P1=0x01; //set port 1 = 1. That is 00000001 on 8bit Port 1
delay(1000); //Call delay routine to pause port 1 state
P1 = 0x00; //set port 1 = 0. That is 00000000 on 8bit port 1
delay(1000); // Call delay routine to pause port 1 state
}
}
///////////main.c end//////////////
3: How to work with timers? Timers work on interrupt routines. You must follow this basic way below to make timer work.
PLEASE NOTE: USE ONLY ONE #INCLUDE FILE I MENTIONED BELOW ACCORDING TO THE CONTROLLER NAME I MENTIONED BELOW. COMMENT THE ONE WHICH DOES NOT HAVE YOUR CONTROLLER NAME CATEGORY BELOW. THE LINES TO BE COMMENTED ARE COLORED IN GREEN. I HAVE ALREADY COMMENTED REGX52.H BY DEFAULT.
//////////main.c start/////////
#include "REGX51.h" // for 89c2051,89c4051,89c51, 89s51 controller
//#include "REGX52.h" //for 89c52,89s52 controller
void timer0_ISR (void) interrupt 1 // for timer 1 if implemented use void timer1_ISR(void)interrupt 2
{
P2_1=!P2_1; // SET PORT 2 PIN 1 = 1 AND THEN 0 ON NEXT INTERRUPT AND VICE VERSA
}
void main( )
{
TMOD = 0xe0; // Set T/C0 Mode 16bit timer
ET0 = 1; // Enable Timer 0 Interrupts
TR0 = 1; // Start Timer 0 Running
EA = 1; // Global Interrupt Enable
TH0 = 0; //Timer count register H byte
TL0 = 0; //Timer count register L byte
P0=0x00; //Set port 0 to 0v logic
P1=0x00; //Set port 1 to 0v logic
P2=0x00; //Set port 2 to 0v logic
P3=0x00; //Set port 3 to 0v logic
while(1)
{
// GO INFINITE LOOP AND WAIT FOR TIMER INTERRUPT
}
}
///////////main.c end//////////////
I will be uploading more basic programming soon. If you want specific applications you got to mix things up and learn. Go to this link here for more codes : ( Click Here )
Download those codes and test your controller. Learn how they have done implement them your way. That is the only way you can learn 8051 microcontrollers.
Thank you for reading.
Macjan Camilo Fernandes ( R & D )
Basic C Programming for 89c2051, 89c4051, 89c51, 89c52, 89s51, 89s52