Friday, May 25, 2012

Reading Port Status

From the previous examples, we seen how easy to output values to the ports of the 8051. Now we will learn how to read port status. For the following examples, beside from the LEDs connected at Port2, we will connect two switches on P3.6 and P3.7 as seen on the diagram below.



 In SDCC, ports can directly read. For instance if we want to read the status of the switch connected at P3.6, we can detect the if it was pressed using the following line of codes.

if(!P3_6)
{
  ...your code here;
}
The if statement above will test the value of P3.6 if it is  zero since we have the port pulled to VCC, so when the switch is pressed, it will be pulled to ground, so the value of P3.6 becomes zero. The value of P3.6 can also be read by reading whole 8 bits of P3 and masking its value to separate P3.6 from others. It can be done by the following line of codes.

unsigned char switch_value = 0; // a variable where port value will be stored.

value = P3 & 0x40;

if(value == 0x00)
  {
      ..your code here;
  }
We mask the value of the port such that other bits are read as zero, and the bit position of P3.6 is 1 so that we can detect the change if its status changes from 1 to 0.

Example Application :
 if P3.6 is pressed , all LEDs connected to PORT2 will light, else LEDs will be off.

#include <at89x52.h>

void main (void)
{
   unsigned char sw_value = 0;
   P2 = 0;

      while(1)
         {
            sw_value = P3 & 0x40;
           
            if(sw_value == 0){
                P2 = 0xFF;
             }
            else{
               P2 = 0x00;
             }
         }
}
Demo of the above code:


With the basic knowledge of writing and reading the ports is the basic building blocks in more advance projects.



















 

Sunday, May 20, 2012

More LED's: Binary Counter

In this example we will display the binary values to LEDs connected to PORT2. P2_0 represents the least significant bit (LSB) and P2_3 represents the most significant bit (MSB).

For example, if we want to display binary count from 0(0x00) to 15(0x0F), we can hard code the values or another approach is use a variable and increment its value by 1 and display its value to PORT2.


Example 1: Hard Coded values

#include <at89x52.h>

void delay (void)
{
    unsigned int i;
     for(i=0; i<0x7FFF; i++);
}

void main (void)
{
   P2 = 0; //initially turn LEDS off

      while(1)
         {
                P2 = 0x00;
                delay();
                P2 = 0x01;
                delay();
                P2 = 0x02;
                delay();
                P2 = 0x03;
                delay();
                P2 = 0x04;
                delay();
                P2 = 0x05;
                delay();
                P2 = 0x06;
                delay();
                P2 = 0x07;
                delay();
                P2 = 0x08;
                delay();
                P2 = 0x09;
                delay();
                P2 = 0x0A;
                delay();
                P2 = 0x0B;
                delay();
                P2 = 0x0C;
                delay();
                P2 = 0x0D;
                delay();
                P2 = 0x0E;
                delay();
                P2 = 0x0F;
                delay();
         } 
}

Example 2: Improved version

#include <at89x52.h>

void delay (void)
{
    unsigned int i;
     for(i=0; i<0x7FFF; i++);

}

void main (void)
{
   unsigned char val = 0  //the value of this variable will be transferred to P2
   P2 = 0; //initially turn off all LEDs


    while(1)
       {
            P2 = val;  //transfer val to P2
                 val++; //increment val by 1
            if(val>=0x0F) //if we reach 0x0F then reset value
               val = 0;

             delay();

       }

}


Saturday, May 19, 2012

Working With GPIO

Now that we have our tools, we are now ready to start! For our first lesson we will learn how to turn an LED on and off. Writing and reading a port in SDCC can be directly done. So to write to a PORT is simply writing 1 to turn it on or 0 to turn it off.

To write to the whole 8 bits of the PORT, its simply "Px = value;" where x is the port number and value is an 8 bit data, and to write to a specific pin its "Px_y", where x is the port number and y is bit number.

   Examples:

     Write 0x05 to Port 1
  
                    P1 = 0x05;

     Make Port3.4 high

                     P3_4 = 1;


Now that we know the basic syntax, Lets keep going!

For the following examples, we will be using the following connections to the microcontroller.


Example 1:

   Turn on LED connected at Port 2-0.

  1. Open M-IDE and create a new file called led.c and save to your working folder.
  2. Copy and paste the following codes:
 #include <at89x52.h>

void main (void)
{

  P2 = 0; //initially turn off PORT2
 
         while(1)
         {
            P2_0 = 1;
         }
}
 3. Build the program and load the hex to the microcontroller to see the result.

 Example 2:

  1. Create a new file called blink.c and save to your working folder.
  2. Copy paste the following codes:

      To blink a LED we turn it on and off, but since the microcontroller is running fast that we cannot see, we will introduce a delay in between.We will introduce a software delay. This delay routine is just a counter that will waster CPU process before doing another instruction. Now lets make the LED connected to P2_0 blink.

#include <at89x52.h>

void delay (void)
{
    unsigned int i;                 //this for loop will just count up to 0xFFFF

     for(i=0; i<0xFFFF; i++)
     
}

void main (void)
{
    P2 = 0; //initially turn off all LEDs

    while(1)
       {
              P2_0 = 1;
              delay();
              P2_0 = 0;
              delay();
      }
}
 3. Build the program and load the hex to the microcontroller to see the result.


Led Blink Demo









Getting Started: The Tools

In order to learn, we need tools, to learn a microcontroller we need the microcontroller itself, some I/O modules, a programmer and an IDE to compile our program.

The IDE

      M-IDE for the 8051 can be downloaded freely from opcube packed with ASEM-51 assembler and SDCC C compiler. This IDE is easy to use, free and also comes with a simulator. It can be downloaded from here


Input/Output Modules

    This is a microcontroller independent Microcontroller Training Module proudly designed and made in the Philippines by e-Gizmo Mechatronix Central. It features almost all the peripherals needed for learning such as Buttons, LED's, Seven Segment, LCD, RS232, Analog sources, relay, motor drivers, Temp Sensor, RTC, matrix keypad, I/O expander and a Rotary Encoder. Any arduino compatible board can be plugged-in.



The Microcontroller
   For use with my trainer, I made an Arduino compatible board for the 8051, though the small breadboard on the center can also be used for plugging in any MCU for experiment.



Programmer
  This is my DIY programmer for programming 8051 and AVR via ISP. It is based on USBASP with modified firmware to support the 8051. Project files for the programmer can be found at 8051projects.info 




Friday, May 18, 2012

Introduction: The 8051 microcontroller




The 8051 microcontroller is basically a tiny computer in a chip, it comes in different variety nowadays initially developed by Intel in the 80's. The 8051 family is also known as MCS-51 family. In my page, I will be using the AT89S52 from Atmel.

The AT89S52 features a 8KB flash memory, 256 bytes internal RAM, clock support up to 33MHz, 32 I/O lines, full duplex serial port, three 16 bit timers, eight interrupts sources and In-System Programmable and 12 clock architecture, other newer 8051 derivatives are enhanced that instructions run in 1 clock cycle.