8051 Microcontroller, Embedded C Programming

Microcontroller is an entire computer manufactured on a single chip. They have all the features found in microprocessors. Built in ROM, RAM, input and output ports, serial ports, interrupts, timers and clock circuits are some of its basic features. 8051 microcontroller is the first basic microcontroller of the MCS-51 family and it was introduced by the Intel Corporation during the 1970s.

To know more about 8051 interfacing ,,,,, Refer https://www.mepits.com/tutorial/297/Programmable-Devices/Interfacing-On-Atmel-Microcontroller-AT89S52-Development-Board.......

8051 architecture  

8051 Architecture

8051 microcontroller is an 8 bit microcontroller that can Read, Process and Write data of 8 bit. This type of microcontroller has many applications in the field of robotics, home appliances etc. The basic architecture of 8051 is shown below. It consists of:

  • On-chip RAM: Random access memory of 128 byte is used for data storage in 8051. RAM as a non-volatile memory consists of register banks, stacks for temporary data storage and some special function registers.
  • On-chip ROM: 8051 consists of 4KB ROM for program storage. ROM as a volatile memory helps in permanent data storage.
  • Timers and Counters: Timer helps in providing delay between the events. In 8051, there are two timer pins T0, T1. If these pins are used in the counter mode, we can count the external pulses. In T0, it is possible to store 16 bit data. This is done by storing the lower 8 bit in TL0 and the upper 8 bit in TH0. Similarly, we can store 16 bit data in T1 also. TMOD and TCON helps in the timer operation.
  • Serial Port: Inorder, to perform the serial communication, TXD and RXD pins are used. TXD pin is used for transmitting the serial data and the RXD pin is used for the transmission of the data. SCON register is used to control the operation of the serial communication.
  • Input and Output Ports: P0, P1. P2. P3 form the four ports of 8051 microcontroller. Each of the port is 8 bit wide. Port P0 is used as a Lower Order Address bus. Port P2 can be used as I/O port and higher order bus A8 to A15. Port P3 can be used as I/O pin and each pin of port 3 has special functions.
  • Oscillator – This is used to provide clock to the 8051 microcontroller. The crystal frequency can vary from 4MHz to 30MHz.
  • Interrupts - Interrupts are requests which are used to handle special events or routines known as Interrupt Service Routines. INT0 and INT1 are the basic interrupt pins used in 8051.
  • Arithmetic Logic Unit - This unit is used for arithmetic calculations.
  • Accumulator (A register) – This register is used for arithmetic operations.
  • B register – This is an 8bit register that is bit addressable and is used for two instructions only like MUL AB and DIV AB.
  • Program Counter – is a 16 bit register that helps to access address from 0000H to FFFFH. Program Counter is used to address the next instruction to be executed from the ROM.
  • Flag Bits and PSW register – The flag bits are used to indicate the arithmetic condition of the ACC. Program Status Word (PSW) is the flag resister in 8051. This register consists of four flags like Carry, Auxiliary Carry Flag, Register Select 1, Register Select 0, Parity Flag, Overflow flag.
  1. Parity Flag (P) – If the accumulator registers consists of odd number of 1’s, then the parity flag will be set to 1. While, if the accumulator register consists of even number of 0’s, then the parity flag will be 0.
  2. Carry Flag (CY) – This flag is set when there is a carry out from the D7 bit.
  3. Auxiliary Carry (AC) – If there is a carry out after addition or subtraction operation from D4 bit, then the AC is set. Otherwise, AC is cleared.
  4. Overflow Flag (OV) – This flag is set when the result of the signed operation is very big.
  5. Register Select (RS1 and RS0) – They are used to change the bank registers.

Pin Diagram of 8051

8051 Pin Diagram

  • Vcc – 5V supply
  • Vss – Ground
  • XTAL2/XTAL1 – Oscillator Input
  • Port0 (Pins 32-39) – AD0/AD7 and P0.0 to P0.7
  • Port1 (Pins 1 to 8) – P1.0 to P1.7
  • Port2 – (Pins 21 to 28) – P2.0 to P2.7 and A8 to A15
  • Port3 – (Pins 10 to 17) – P3.0 to P3.7
  • RST – Restart 8051
  • ALE – Address Latch Enable
  • PSEN – Program Store Enable
  • P3.0 – is used as RXD pin for serial communication.
  • P3.1 – TXD pin for Serial Transmit Data.
  • P3.2 – External Interrupt0 Pin, INT0
  • P3.3 – External Interrupt1 Pin, INT1
  • P3.4 – T0 – Clock Input for counter0.
  • P3.5 - T1 – Clock Input for counter1.
  • P3.6 – WR – Signal to write to the external memory.
  • P3.7 – RD – Signal to read from the external memory.

 

8051 Instructions

The 8051 instruction set is divided into 5 groups like:

  • Arithmetic Instructions
  • Branch Instructions
  • Data Transfer Instructions
  • Logic Instructions
  • Bit Oriented Instruction

1.  Arithmetic Instructions

Subtraction, Addition, Multiplication and division operations are been performed by the arithmetic instructions of 8051 microcontroller. Some of the arithmetic instructions include:

Arithmetic instructions

2. Branch Instructions

Branch Instructions

3. Logic Instructions

Logic Instructions

4. Bit Oriented Instructions

Bit Oriented Instructions

Embedded C Programming Tutorial

Today, Embedded C language is used commonly to program microcontrollers. So it is important to know everything about Embedded C. Here we will be discussing the programming in Keil C compiler. Keil C51 compiler uses some keywords as given below for the ease of programming.

bdata: The variable chosen for the program will be stored in bit addressable memory of microcontroller.

unsigned char bdata v; 

data / idata: The variable chosen for the program will be stored in the internal data memory of the microcontroller.

unsigned char data t;

code: Its used to store a constant variable like string or large integer numbers in code memory.

unsigned char code str=” compiler program";

xdata:  The variable used in the program will be stored in the RAM memory of  the controller.

unsigned char xdata k;

_at_:  Helps to store a variable used in the program on a location in ram.

unsigned char data i _at_ 0x50; 

pdata: This will store the variable in the program in the paged data memory.

unsigned char pdata l;

sbit:  This keyword is used to define a special bit from SFR. It helps to set a particular variable to 1 in the program.

sbit Port1_1 = 0x90; 

using: is used to define register bank for a function.
sfr: It  is used to define a  8-bit SFR (special function register).

sfr P1 = 0x40; 

Memory Models
3 types of memory models are used in by the Keil C compiler for the programming.

  1. Small

  2. Compact

  3. Large

Pointers in Keil C

Generic and Memory-specific pointers are commonly used as pointers in Keil C.

Generic Pointers

  • char *a;     

Memory-Specific Pointers

  • The memory type and the pointer variable is indicated in the program statement.

          char data *c;

Functions

Function Declaration

[Return_type] Function_name ( [Arguments] ) [Memory_model] [reentrant] [interrupt n] [using n]

  • Arguments: Arguments passed to function.
  • Function_name: Name of function.
  • Return_type: It tells about the value type  returned from the function.


Interrupt Service Routines

Using the keyword interrupt and interrupt number, the interrupt service routine is specified.

void name (void) interrupt 2 {

/gggg
}

Reentrant Functions

They are recursive functions defined as follows:
void function_name (int argument) reentrant

{

        //function code
}

Real-time Function Tasks

Keil supports RTOS.  It is declared as follows:
void func (void) _task_ Number _priority_ Priority

{

        //code
}

Writing code

In hardware specific code, we use hardware peripherals like ports, timers etc. Lets write a simple code to Blink LED

#include <reg52.h>

void msdelay(unsigned int t)

{

unsigned int i,j;

for(i=0;i<t;i++)

for(j=0;j<1275;j++);

}

void main()

{

while(1)

{

P1=0X0F;

msdelay(250);

P1=0X00;

msdelay(250);

}

}

To  know more about 8051 interfacing ,,,,, Refer https://www.mepits.com/tutorial/297/Programmable-Devices/Interfacing-On-Atmel-Microcontroller-AT89S52-Development-Board.......

Related Items