IndianEngineer

ProjectsectsBazaar.in

Microcontrollers & Use in Embedded Technology

Its fun programming and working with microcontrollers. With microcontrollers you can create a piece of hardware which acts according to your wish (obviously with some limitations !). Examples are you can flash a LED, drive a 7 segment display, print text on a LCD, receive signals from a remote control, control electrical appliances of your room, build a robot that follows a line on the floor and avoids obstacles coming in between, frequency meter and infinitely many more..

BSNL JTO

BSNL JTO COACHING

Basically a Microcontroller is a mini computer with CPU, Memory, I/O lines etc. Microcontrollers are much better for smaller embedded systems than their ancestors i.e. microprocessors, if you are developing a system using microprocessor, then your hardware or circuit will be more complex whereas if you choose a microcontroller, your hardware becomes simple because all the necessary peripherals such as RAM, ROM, TIMERS, I/O ports etc are embedded in a microcontroller. Therefore it is also called a mini computer on a chip. Now we will start with interfacing various devices to microcontrollers (Assuming you have basic knowledge of 8051 architecture and assembly programming and also C). I have chosen 8051 microcontroller due to its simplicity in architecture and assembly language, and learning 8051 chip is much easier than other microcontrollers if you don’t have any prior knowledge on microcontrollers. For more detailed information of hardware architecture and assembly language of 8051 microcontroller you can refer hardware manual and instruction set from ATMEL’s homepage.

Most of the projects and tutorials published here are based on Atmel’s AT89S52 microcontroller, it is a 8- bit microcontroller with 8051 architecture, comes in a 40 pin DIP, contains all necessary peripherals required for a hobby project. Now for a microcontroller to be operational the minimum hardware you need is a crystal oscillator, power supply, power on reset etc. So I have come up with a very simple development board to carry on the projects and interfacing tutorials published here. (Note: This development board is not compulsory; you can also rig up the circuits in breadboard since most of the circuits are simple). You can construct the development board on a general purpose circuit board. (Soon I will be publishing PCB layout for this).

8051 Development Board.

We will start with interfacing microcontroller with LED’s, Displays, Motors, Relays etc.

· Interfacing LED(s)

· Interfacing 7 segment Display

· Interfacing multiple 7 segment displays

· Basics of PWM

· Interfacing Relays

· Interfacing 4×4 keyboard

· 8051 Serial port

· many more to come…

In order set up your simple home lab, you need

· keil or any other assembler/compiler.

· ISP programmer circuit

· ISP programmer software

· Target hardware (your development board or circuit).

Advertisements

April 11, 2010 Posted by | Microcontroller's | , | 2 Comments

8051 Development Board

I have designed a development board for practicing 8051 microcontrollers. The development board is simple one containing minimum hardware required for a 8051 chip to function. The 8051 chip used in this development board is 89S52. The AT89S52 comes in a 40 pin PDIP package. It consists 8kB in system programmable flash memory for code storage, 256 bytes of RAM, 32 I/O lines, three 16 bit timer/counters, one full duplex serial port and many more. For further details you can refer the datasheet.

Figure below shows the circuit diagram of the development board. Click on the image to enlarge. I have connected pull up resistors for all the ports in the circuit diagram, but pull ups are compulsory only for port 0. You can neglect pull ups connected for other ports since they are connected to pull ups internally.

This circuit can be easily constructed in a general purpose circuit board. One can neglect the seven segment display and 8 LEDs if they wish to build it separately. I will be publishing the PCB files for this board soon. Keep visiting.

Some Images of my 8051 development board built on breadboard. This is my first version of my board so the 7 segment display is not present.

Image notes: 1] The marvel chip Atmel’s 89S52    2] ISP port for downloading programs    3] Pull up resistor network    4] connectors to connect I/O pins to any other circuits/interface boards    5] 11.0592 Mhz crystal    6] Power connector    7] 7805 voltage regulator    8] Power indication LED    9] 8 LEDs connected to Port 0

April 11, 2010 Posted by | Microcontroller's | , , | Leave a comment

Microcontroller 7 Segment Interfacing

http://www.shree-electronics.com/images/DSC00209.JPG

Seven segment display is a basic type of display which can display numbers from 0 to 9. The circuit for interfacing single 7 segment display is shown below.

http://www.shree-electronics.com/images/7_segment_small.gif

Driving a 7 segment display is as simple as flashing LEDs, but here we are flashing 7+1 LEDs. The 7 segment display module has 8 LEDs (7 segments to display number and one segment for decimal point or dot) arranged in a particular manner as shown in image below.

http://www.shree-electronics.com/images/7_segment_display.svg.png

http://www.shree-electronics.com/images/commom_cathode1.gif http://www.shree-electronics.com/images/commom_anode1.gif

By driving (in the sense controlling ON and OFF conditions) these LEDs in various combinations, we can display the numbers 0 to 9. There are basically two types of 7 segment displays, they are common cathode and common anode. In common cathode, the cathodes of all the LED segments are connected together, we should apply a logic 1 or high input to a segment pin to light up that particular segment, and in common cathode the case is opposite. Table below shows the combinations of inputs to be applied to 7 segment display for digits 0 to 9.

For common cathode displays
Digit binary input value hexadecimal input value
0 11111100 FC
1 01100000 60
2 11011010 DA
3 11110010 F2
4 01100110 66
5 10110110 B6
6 10111110 BE
7 11100000 E0
8 11111110 FE
9 11110110 F6

For common anode displays
Digit binary input value hexadecimal input value
0 00000011 03
1 10011111 9F
2 00100101 25
3 00001101 0D
4 10011001 99
5 01001001 49
6 01000001 41
7 00011111 1F
8 00000001 01
9 00001001 09

The images shown below are the photographs of 7 segment add-on board for my microcontroller board, I have included one 7 segment in the current version of my development board, so if you are using that circuit then you don’t need an external 7 segment display board. Click on the image to enlarge.

http://www.shree-electronics.com/images/DSC00045_small.JPG http://www.shree-electronics.com/images/DSC00261_small.JPG

The assembly language source code for interfacing 7 segment display is given below.

;*************************************************
;
;Program: Driving seven segment display
;Author: Srikanth
;Website: http://shree-electronics.com/
;Description: Displays numbers 0 to 9 on
;the LED seven segment display continuously
;
;*************************************************

;Declarations
port equ P0

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

;Main program
org 0000h
ljmp main
org 30h

main:mov r0,#08h
mov a,#00000001b ;test all segments of disp
up: rr a
mov port,a
acall delay
djnz r0,up

again:mov port,#11111100b ;’0′
acall delay
mov port,#01100000b ;’1′
acall delay
mov port,#11011010b ;’2′
acall delay
mov port,#11110010b ;’3′
acall delay
mov port,#01100110b ;’4′
acall delay
mov port,#10110110b ;’5′
acall delay
mov port,#10111110b ;’6′
acall delay
mov port,#11100000b ;’7′
acall delay
mov port,#11111110b ;’8′
acall delay
mov port,#11110110b ;’9′
acall delay
sjmp again

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

delay:mov r2,#0ffh ;delay subroutine
up3: mov r4,#03fh
up2: mov r3,#0fh
up1: djnz r3,up1
djnz r4,up2
djnz r2,up3
ret

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

end


April 11, 2010 Posted by | Microcontroller Interfacing | | 2 Comments

Microcontroller LEd InterFacing

http://www.shree-electronics.com/images/DSC00288.JPG

The first thing usually done while learning any microcontroller or embedded system is blinking an LED. The circuit below shows the circuit for Interfacing an LED.

http://www.shree-electronics.com/images/led_small.gif

note: Since the circuit diagram dimensions are big, your browser may fit the image to window size, if you are using Internet explorer, then an expander tool will be displayed on the bottom right corner, if you are using firefox , then when you move the mouse pointer over the image a magnifier tool will be displayed.

Here an LED is connected to the first pin of port0 (P0.0). The assembly program given below is simple and self explanatory.

;*************************************************
;
;Program: Blinking LED.
;Author: Srikanth
;Website: http://shree-electronics.com/
;Description: Blinks an LED connected to P0.0
;continuously
;
;*************************************************

led equ P0.0

org 00h
up: setb led ;Turn ON the LED
acall delay ;call delay subroutine
clr led ;Turn OFF the LED
acall delay ;call delay subroutine
sjmp up ;Loop

delay:mov r7,#0ffh ;delay subroutine
loop:mov r6,#0ffh
djnz r6,$
djnz r7,loop
ret

end

The program is purposely made lengthy in order to explain some programming techniques such as subroutine, loop etc.


April 11, 2010 Posted by | Microcontroller Interfacing | | 2 Comments

Interfacing Relays

http://www.shree-electronics.com/images/DSC00305.JPG  http://www.shree-electronics.com/images/DSC00305.JPG

Relays allow us to control the high power circuits or instruments using low power circuits and voltage levels such as output of microcontrollers. Interfacing relays to 8051 or any other microcontroller is just simple as controlling a LED.

A relay consists of a coil which is driven by low power circuits such as microcontrollers in order to control high power ac circuits through output switches. Normally there are two types of switches in a relay, they are Normally Open (NO) and Normally Closed (NC) contacts. ‘NO’ contacts are open when relay is not activated where as the ‘NC’ contacts are closed, when relay is activated by supplying power to the input coil, the ‘NO’ contacts are now closed and the ‘NC’ contacts are open.

The circuit diagram for interfacing relay to a 8051 microcontroller is shown below.

http://www.shree-electronics.com/images/uc_relay_small.gif  http://www.shree-electronics.com/images/uc_relay_small.gif

The assembly program for interfacing relay is given below.

;*************************************************
;
;Program: Interfacing relay
;Author: Srikanth
;Website: http://shree-electronics.com/
;Description: Relay turns ON when the switch
;connected to P1.0 and turns OFF when pressed
;again
;
;*************************************************

relay equ P0.0
sw equ P1.0

;*************************************************
org 0000h

Main:clr relay        ;Configure inp and outp
setb sw
up:  jnb sw,on        ;wait for switch to be pressed
clr relay
acall delay
sjmp up
on:  setb relay       ;Turn ON relay
acall delay
acall delay
here:jb sw,here       ;wait for switch to be released
clr relay        Turn OFF relay
acall delay
acall delay
sjmp up          ;Loop

;*************************************************
delay:mov r7,#0ffh    ;delay subroutine
again:mov r6,#0ffh
djnz r6,$
djnz r7,again
ret

;*************************************************
end

BACK


April 11, 2010 Posted by | Microcontroller Interfacing | | Leave a comment

Interfacing Keybord With Microcontroller

Keyboard is a basic and essential element of an embedded or microcontroller system. For small and hobby projects a 4×4 (hex) matrix keyboard is sufficient. The keys in the matrix keyboard are arranged in a matrix arrangement in order to utilize the port pins of the microcontrollers efficiently. Here we can interface 16 keys by using just 8 pins of microcontroller. This is shown in the circuit diagram.

As shown in the circuit diagram, the rows are connected to 4 pins of a port, the columns are connected to other four pins, we configure rows as input and columns as output. By grounding one column and setting high all other columns, now by observing the status of the rows, we can come to know which button is pressed. For example, if we ground the first column and if the first row is low, then the first button is pressed. We know that microcontrollers are really fast, therefore they can detect the key press in microseconds, if we hold the switch for long time (for a microcontroller long time is in milliseconds), the microcontroller is triggered more than once, also there is a problem of switch debounce because of the spring action of the switch. To eliminate these problems we should introduce some amount of delay after every key press. The assembly source code for the circuit is given in the table below.

Circuit diagram for interfacing 4×4 keyboard to 8051(89s52):

http://www.shree-electronics.com/images/4x4_keypad_small.gif  http://www.shree-electronics.com/images/4x4_keypad_small.gif

My prototype for connecting to the main development board:

http://www.shree-electronics.com/images/DSC00044_small.JPG  http://www.shree-electronics.com/images/DSC00044_small.JPG

The assembly program for interfacing 4×4 keyboard:

;*************************************************
;Program: Blinking LED.
;Author: Srikanth
;Website: http://shree-electronics.com/
;Description: Displays the button pressed on the
;keyboard on a 7 segment display.
;
;keyboard format: 0 1 2 3
;                 4 5 6 7
;                 8 9 A B
;                 C D E F
;*************************************************
;Declarations
rw0 equ P2.0;
rw1 equ P2.1;
rw2 equ P2.2;
rw3 equ P2.3;
cl0 equ P2.4;
cl1 equ P2.5;
cl2 equ P2.6;
cl3 equ P2.7;
;*************************************************
;Main program

org 00h
ljmp main

org 30h
main: mov P2,#0ffh       ;Configure input
acall scan_key     ;Scan for keypress
acall display      ;display the key pressed
sjmp main          ;Loop

;*************************************************
;Subroutine to scan keys
scan_key:mov P2,#0ffh
clr cl0
mov a, P2
anl a,#00001111b
cjne a,#00001111b,Row0
setb cl0
clr cl1
mov a, P2
anl a,#00001111b
cjne a,#00001111b,Row1
setb cl1
clr cl2
mov a, P2
anl a,#00001111b
cjne a,#00001111b,Row2
setb cl2
clr cl3
mov a, P2
anl a,#00001111b
cjne a,#00001111b,Row3
setb cl3
ret

row0: mov dptr,#led_data
mov r6,#04h
clr c
rww0: rrc a
jc next0
sjmp over
next0:inc dptr
djnz r6,rww0
sjmp scan_key

row1: mov dptr,#led_data+4h
mov r6,#04h
clr c
rww1: rrc a
jc next1
sjmp over
next1:inc dptr
djnz r6,rww1
sjmp scan_key

row2: mov dptr,#led_data+8h
mov r6,#04h
clr c
rww2: rrc a
jc next2
sjmp over
next2:inc dptr
djnz r6,rww2
sjmp scan_key

row3: mov dptr,#led_data+0ch
mov r6,#04h
clr c
rww3: rrc a
jc next3
sjmp over
next3:inc dptr
djnz r6,rww3
sjmp scan_key

over: ret
;*************************************************
;Display subroutine
display:clr a
movc a,@a+dptr
mov P0,a
ret

;*************************************************
;lookup table
led_data:db 0fch,066h,0feh,09dh; For row1:0 1 2 3
db 060h,0b6h,0f6h,0fdh; For row2:4 5 6 7
db 0dah,0beh,0efh,09fh; For row3:8 9 A B
db 0f2h,0e0h,0ffh,08fh; For row4:C D E F

;*************************************************
end;


April 11, 2010 Posted by | Microcontroller Interfacing | | 1 Comment

8051 serial port

The only one communication hardware present in atmel 89S52 version of 8051 microcontroller is the serial port. The serial port or UART transfers data bit by bit. In mode 1, 8 bits can be sent with one start and stop bit in a frame.

Communicating with the PC:

We can communicate with PC by using the serial port available in 8051. In order to communicate with the PC, we need a level converter because the serial port of the pc uses RS232 voltage levels which is much higher voltage than the TTL logic used by the microcontroller (+12v for logic 0 and -12v for logic 1). A dedicated IC from MAXIM i.e. Max232 is readily available in the market. It converts the TTL signals from the microcontroller to RS232 level and the other way round. Max232 contains 2 channels, works with single 5V supply and uses the principle of charge pump to convert the voltage levels.

http://www.shree-electronics.com/images/rs232_level_conv.gif  http://www.shree-electronics.com/images/rs232_level_conv.gif

The schematic :

http://www.shree-electronics.com/images/max232_pins.gif  http://www.shree-electronics.com/images/max232_pins.gif

http://www.shree-electronics.com/images/max232_ckt.gif  http://www.shree-electronics.com/images/max232_ckt.gif

Configuring HyperTerminal:

HyperTerminal is the tool usually used to test and communicate with the PC’s serial port. To set up HyperTerminal follow the steps given below.

1. Open HyperTerminal.

Start > All programs > Accessories > Communications > Hyper Terminal

2.Specify a name for the connection

Click OK

http://www.shree-electronics.com/images/hyper2.gif  http://www.shree-electronics.com/images/hyper2.gif

3.Configure the connection parameters

Select the port of your PC i.e.COM1, COM2 etc., click OK

http://www.shree-electronics.com/images/hyper3.gif  http://www.shree-electronics.com/images/hyper3.gif

Set baud rate as required, and change Flow control to none

http://www.shree-electronics.com/images/hyper4.gif  http://www.shree-electronics.com/images/hyper4.gif

The connected in the bottom shows the status of the connection.

http://www.shree-electronics.com/images/hyper6.gif  http://www.shree-electronics.com/images/hyper6.gif

Now your HyperTerminal is ready to communicate. You can disconnect or connect the connection by clicking on the icons shown in the image.

http://www.shree-electronics.com/images/hyper5.gif  http://www.shree-electronics.com/images/hyper5.gif

HyperTerminal displays only the character or data received, not the one you typed to send.

Testing the Level converter:

You are ready with level converter hardware and HyperTerminal, now its time for testing your level converter hardware. For testing purpose, just interconnect the Tx and Rx pins of the level converter, and type something in the HyperTerminal, you will see the echo.

Programming the 8051:

8051 USART uses dedicated buffer register SBUF, the mode and other settings of the serial port such as number of stop bits etc. is determined by the contents of SCON register. Timer 1 should be initialized to 8-bit auto reload, and its content determines the Baud rate of the 8051 serial port. Read datasheet to know more about different modes of UART.

Connecting the Microcontroller:

Connect the Tx and Rx pins of the 8051 (pin no. 10 and 11 i.e. P3.0 and P3.1 in 89S52)microcontroller to the level converter. Provide power for the 8051 microcontroller target board.

;*************************************************
;
;Program: UART_test
;Author: Srikanth
;Website: http://shree-electronics.com/
;Description: Prints string on the hyperterminal window
;
;*************************************************

org 0000h
ljmp main

main:
mov tmod,#20h      ;Timer 1 configured to 8 bit
;auto reload
mov th1,#-6        ;Baud rate set to 9600
mov scon,#50h      ;UART configure as 8 data bits
setb tr1           ;with 1 start and stop bit
up:
mov sbuf,#’s’      ;Store the char to be
;transmitted in SBUF refister
acall transmit     ;call transmit subroutine
mov sbuf,#’h’
acall transmit
mov sbuf,#’r’
acall transmit
mov sbuf,#’e’
acall transmit
mov sbuf,#’e’
acall transmit
mov sbuf,#’-‘
acall transmit
mov sbuf,#’e’
acall transmit
mov sbuf,#’l’
acall transmit
mov sbuf,#’e’
acall transmit
mov sbuf,#’c’
acall transmit
mov sbuf,#’t’
acall transmit
mov sbuf,#’r’
acall transmit
mov sbuf,#’o’
acall transmit
mov sbuf,#’n’
acall transmit
mov sbuf,#’i’
acall transmit
mov sbuf,#’c’
acall transmit
mov sbuf,#’s’
acall transmit
mov sbuf,#’.’
acall transmit
mov sbuf,#’c’
acall transmit
mov sbuf,#’o’
acall transmit
mov sbuf,#’m’
acall transmit

up1:
ajmp up1           ;Loop here

transmit:            ;Transmit subroutine
jnb ti, transmit   ;Wait for completion of
;transmission
clr ti             ;Clear transmit flag
ret

end

;*************************************************
;
;Program: UART_test_1
;Author: Srikanth
;Website: http://shree-electronics.com/
;Description: Prints the next char of the typed
; character on the hyperterminal window
;
;*************************************************

org 0000h
ljmp main

main:
mov tmod,#20h      ;Timer 1 configured to 8 bit
;auto reload
mov th1,#-6       ;Baud rate set to 9600
mov scon,#50h     ;UART configure as 8 data bits
setb tr1          ;with 1 start and stop bit
up:
acall recieve     ;First recieve the char typed on
;hyperterminal
cjne a,#’z’,next  ;If typed char is not Z, then
mov a,#’a’-1      ;send next char of the typed one
next:
inc a
acall transmit
sjmp up           ;Loop

transmit:           ;Transmit subroutine
mov sbuf,a        ;Move the char to be transmitted
;into SBUF reg
jnb ti,$          ;Wait for transmission to be
;complete
clr ti            ;clear transmit flag
ret

recieve:            ;Recieve subroutine
jnb ri,$          ;wait for a char to be recieved
mov a,sbuf        ;save the recieved char
clr ri            ;clear recieve flag
ret

end

April 11, 2010 Posted by | Microcontroller's | | Leave a comment