Monday, March 19, 2012

Things you should know before working with Microcontroller Programming

There are a lot of things you need to know before working with your microcontroller project code. I would like to explain some of the important ones. They are:


1.  PROGRAMMING LANGUAGE: A microcontroller program can be written in 3 languages – assembly, c and hex (with the help of a computer). The first two are the practical and most widely used languages. The third method is correct but impractical for large programs.
Each language has its own advantages and disadvantages. In assembly language the program is written using the microcontroller instruction set only. This makes programming in assembly, somewhat complex. The main advantage of using assembly is its fast execution. Because of this reason assembly language is highly preferred in real time systems, for which the time constraint is a serious matter. The ‘c’ language (actually it is micro-c), as its name indicates, is written in the same syntax of the original computer ‘c’ language. The advantage of writing in ‘c’ is the availability of a large number of functions that comes in handy while programming. But the ‘c’ programs are comparatively slower than the assembly programs. In either case you need the simulator software with assembler/compiler. I recommend using the latest version of ‘Keil uVision’ if you are working on 8051 microcontroller, ‘PIC Simulator IDE’ for PIC microcontroller and ‘AVR Studio’ for the AVR microcontroller.


2.  HEX FILE: Though you are initially writing the program code in assembly or ‘c’ language you will have to convert it to hex file before you can put it (program/burn) into a microcontroller flash memory. Usually, the conversion is done by the assembler program associated with the programming (simulator) software. Then the hex file can be loaded into the microcontroller using a suitable microcontroller programmer (hardware).


3.  MICROCONTROLLER PCB CIRCUIT: I recommend that you create a basic microcontroller circuit on a General PCB with the ‘Lock/Eject type’ IC base for the microcontroller. This will help a lot during the microcontroller programming by avoiding the uneasiness and various connection problems while using the breadboard circuit and it also eliminates the need for those costly and bulky development boards.


4.  CHECK YOUR MICROCONTROLLER: Before you test your program code on a microcontroller, you should check whether the microcontroller is functional or not. This can be done by burning a simple program in the microcontroller and checking the result (output). For example, the following test code is written for the said purpose. It is written for 8051 microcontroller in the assembly language:


ORG 00H          ; initialize microcontroller

LOP:

SETB P1.0        ; make p1.0 pin high

MOV R5, #0FH     ; approx. 1s delay

LOP2:

MOV R6, #0FFH

CALL DELAY

DJNZ R5, LOP2

CLR P1.0         ; make p1.0 pin low

MOV R5, #0FH     ; approx. 1s delay

LOP3:

MOV R6, #0FFH

CALL DELAY

DJNZ R5, LOP3

JMP LOP          ; go to ‘lop’ label


;****************************************

;           1s DELAY ROUTINE

;****************************************

DELAY:

DELAYL1: MOV R7, #0FFH

DELAYL2: DJNZ R7, DELAYL2

DJNZ R6, DELAYL1

RET

END


The above code will toggle the 8051 P1.0 pin between high (+5V) and low (0V) with a delay of approximately 1 second. You can see this state switching by means of an LED (with driver-recommended) circuit connected to the P1.0 pin.

No comments:

Post a Comment