clock

Saturday, 25 May 2013

Basic C Programming for 89c2051, 89c4051, 89c51, 89c52, 89s51, 89s52

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//////////////

No comments :

Post a Comment