Written By : ASIST. PROF. MUHAMMAD FARHAN KHAN
BE (Industrial Electronics from IIEE/ NEDUET) MS (Communications from NEDUET)
14 Years of Teaching and Industrial Experience
STAR ENGINEERING (Training, Research and Development Unit) A-249, Block-3, Gulshan-e-Iqbal, Karachi-75300 WEB: http://star-engg.com E-Mail: [email protected] Phone: +92 21 34242284, +92 333 2156027
(2)
Table of Contents 1 MICROCONTROLLER TRANING KIT ATK V11-162 ......................................................... 4
1.1 Schematic Diagram: ........................................................................................................ 4 1.2 P. C. B. Diagram:............................................................................................................. 5 1.3 Assembled Diagram of P. C. B.: ..................................................................................... 6
2 Hardware Description ................................................................................................................ 7
2.1 Power Supply .................................................................................................................. 7 2.2 Controller Socket ............................................................................................................ 8
2.2.1 Supported AVR Microcontroller ................................................................................ 8
2.3 Reset AND In-System Programming (ISP) Support .................................................. 8 2.4 Bar Graph Display .......................................................................................................... 9
2.5 Crystel ........................................................................................................................... 10 2.6 Analog to Digital Convertor (ADC) ............................................................................. 11 2.7 Communication PORT (COMM PORT) ...................................................................... 11 2.8 Off Board Numeric Keypad ......................................................................................... 12
2.9 4-Digit Multiplxe 7-Segment Display .......................................................................... 13 2.10 Alphanumeric LCD Interface ....................................................................................... 14 2.11 Relays ........................................................................................................................... 16 2.12 Push Button.................................................................................................................... 17
3 Practical................................................................................................................................... 18
3.1 PRACTICAL NO. 1 (Check Signal Key) Increase/Decrease a single binary count on the
LEDs at PortB when a button at PD6 or PD7 is pressed…………………………………............. 17 3.1.1 Apparatus: .................................................................................................................. 17
3.1.2 Algorithm & Flow Chart:........................................................................................... 17 3.1.3 Avr Program C Code: ................................................................................................ 18
3.1.4 Exercise: ..................................................................................................................... 19 3.1.5 Conclusion: ................................................................................................................ 19
3.2 PRACTICAL NO. 2 (Bottle Filling) Control a conveyer based bottle filling station. Use
PD2 to read the signal from bottle detector, PE0 to drive conveyer and PA5 for filling valve.
Run the system and wait for bottle, use the filling delay of 10 Second and show the count of
filled bottles on LCD. (Inputs are active low and outputs are active high).. ............................... 19 3.2.1 Apparatus: .................................................................................................................. 19
3.2.2 Avr Program C Code: ................................................................................................ 19 3.2.3 Exercise: ..................................................................................................................... 20
3.2.4 Conclusion: ................................................................................................................ 21 3.3 PRACTICAL NO. 3 (Stepper Motor Control System) Run a stepper motor in clockwise
direction with maximum speed, when a button at PD6 is pressed in cw, when a button at PD7 is
pressed in ccw and motor should continue to run till key is pressed………………………………21
3.3.1 Apparatus: .................................................................................................................. 21 3.3.2 Avr Program C Code: ................................................................................................ 21
3.3.3 Exercise: ..................................................................................................................... 22 3.3.4 Conclusion: ................................................................................................................ 22
3.4 PRACTICAL NO. 4 (Frequency Counter) Write C language program for ATmega162
to display the frequency of analog input signal (Use 220V mains supply as input). Hint external
interrupt may be used for zero crossing counting and the value of count will show the frequency
after each second. ......................................................................................................................... 23 3.4.1 Apparatus: .................................................................................................................. 23 3.4.2 Avr Program C Code: ................................................................................................ 23
3.4.3 Exercise: ..................................................................................................................... 23 3.4.4 Conclusion: ................................................................................................................ 24
3.5 PRACTICAL NO. 5 (Servo Motor Control System) Use UART with baud rate of 9600
to increase or decrease the angular position of the shaf of a Servo Motor. User will press „+‟ or
(3)
„-‟ to increase or decrease the positions. Write down C language program for ATmega162 to
drive the Servo Motor through PB3. Hint (Use timer zero for 20 ms interrupt and timer two for
pulse width). ................................................................................................................................. 24
3.5.1 Apparatus: .................................................................................................................. 25 3.5.2 Avr Program C Code: ................................................................................................ 24 3.5.3 Exercise: ..................................................................................................................... 26 3.5.4 Conclusion: ................................................................................................................ 26
3.6 PRACTICAL NO. 6 (ADC_UART_LCD) Display the equivalent decimal of ADC
input on the LCD module and also send this value through UART with Baud Rate=9600bps. . 26 3.6.1 Apparatus: .................................................................................................................. 27 3.6.2 Avr Program C Code: ................................................................................................ 27 3.6.3 Exercise: ..................................................................................................................... 27 3.6.4 Conclusion: ................................................................................................................ 28
3.7 PRACTICAL NO. 7 (UART control DC motor) Use UART with baud rate of 9600bps
to receive the speed level (0-9) for a DC Motor from Hyper Terminal. Write C Language
program for ATmega162 to drive a DC Motor through PWM using timer zero according to
received speed level. Also acknowledge by reflecting the received character. ........................... 28 3.7.1 Apparatus: .................................................................................................................. 28 3.7.2 Avr Program C Code: ................................................................................................ 29 3.7.3 Exercise: ..................................................................................................................... 29
3.7.4 Conclusion: ................................................................................................................ 29 3.8 PRACTICAL NO. 8(4x4 Multiplexed KeyPad) Use 4x4 multiplexed Keypad an LCD
Module with ATmega162 to display key codes (On interrupt basis) when a key is pressed on
keypad. Key codes should range from '0' to 'F'. Also pass the key pressed indication to main
program using a flag bit and generate a half second pulse on a relay in main program.. ............ 29 3.8.1 Apparatus: .................................................................................................................. 29 3.8.2 Avr Program C Code: ................................................................................................ 29
3.8.3 Exercise: ..................................................................................................................... 30
3.8.4 Conclusion: ................................................................................................................ 30 3.9 PRACTICAL NO. 9(Single Digit 7 Segment Display) Display the Hexadecimal
counting in increasing on Button PD6 is press and decreasing on Button PD7 is press Set the
counting delay of 300ms. ............................................................................................................. 31 3.9.1 Apparatus: .................................................................................................................. 31
3.9.2 Algorithm & Flow Chart:........................................................................................... 31 3.9.3 Avr Program C Code: ................................................................................................ 31 3.9.4 Exercise: ..................................................................................................................... 32 3.9.5 Conclusion: ................................................................................................................ 32
3.10 PRACTICAL NO. 10 (4-Digit Multiplexed 7-Segment LED Driver) Generate the
binary pattern in increasing/decreasing order on 4digit multiplexed 7 segment display at
PORTB, when a button at PD6 or PD7is pressed. Set the counting speed of 2 counts per second.
33 3.10.1 Apparatus: .................................................................................................................. 33 3.10.2 Avr Program C Code: ................................................................................................ 33 3.10.3 Exercise: ..................................................................................................................... 35
3.10.4 Conclusion: ................................................................................................................ 35 3.11 PRACTICAL NO. 11(Real Time Clock) Use RTC (D71307) with ATmega162 to
update the value of time and date after each second on LCD screen. Use PA1 and PA2 with
clock and data of RTC for I2C interface.. .................................................................................... 35 3.11.1 Apparatus: .................................................................................................................. 35
3.11.2 Avr Program C Code: ................................................................................................ 35 3.11.3 Exercise: ..................................................................................................................... 37 3.11.4 Conclusion: ................................................................................................................ 37
(4)
1 Microcontroller Training Kit ATK V11-162
1.1 Schematic Diagram:
(5)
1.2 P. C. B. Picture:
(6)
1.3 Assembled Diagram of P. C. B.:
(7)
2 Hardware Description
2.1 Power Supply:
9V AC/DC input supply is required for the board. Bridge rectifier converts the input AC supply
into DC or it can correct the polarity when we apply DC input. The input supply must be capable
to provide up to 1A supply current. A filter capacitor and a voltage regulator LM7805 is used to
provide the regulated +5V supply for the components of trainer board. Also the unregulated DC
supply of 9/12V is taken from bridge rectifier for relays and external use.
(8)
2.2 Microcontroller Socket:
A forty pin socket (IC Base) is placed to hold a microcontroller at the trainer board. The notch of
microcontroller will be kept upward according to notch of socket. The trainer board supports a
range of 8051 based and AVR Microcontroller.
2.2.1 Supported 8051 Microcontrollers:
AT89S51
AT89S52
AT89S53
AT89S54
AT89S55
AT89S58
AT89S8252
AT89S8252
2.2.2 Supported AVR Microcontrollers:
AT90S8515
ATmega8515
ATmega161
ATmega162
ATmega163
2.3 Reset and In-System Programming (ISP) Support:
ISP cable may be used on the development for program downloading from PC to Microcontroller.
For this purpose a 5pin Crimp shell male connector is placed with pin # 6 to pin # 10 of the
microcontroller base socket. To download the machine code (HEX file) connect 5 pin female
connector of ISP cable at this male crimp shell connector and use downloading software to burn
the code from computer.
Pin configuration of ISP connector is:
AT89S51/52
9
1819
20
293031
40
12345
678
2122232425262728
1011121314151617
3938373635343332
RST/RST
XTAL2XTAL1 G
ND
PSEN/PE2ALE/PE1
EA/PE0
VC
CP1.0/PB0P1.1/PB1P1.2/PB2P1.3/PB3P1.4/PB4
P1.5/PB5P1.6/PB6P1.7/PB7
P2.0/PC0P2.1/PC1P2.2/PC2P2.3/PC3P2.4/PC4P2.5/PC5P2.6/PC6P2.7/PC7
P3.0/PD0/RXP3.1/PD1/TXP3.2/PD2/INT0P3.3/PD3/INT1P3.4/PD4P3.5/PD5P3.6/PD6P3.7/PD7
P0.0/PA0P0.1/PA1P0.2/PA2P0.3/PA3P0.4/PA4P0.5/PA5P0.6/PA6P0.7/PA7
ATmega162
ATmega8515AT89S8252/53
(9)
Pin # 1 …. GND
Pin # 2 …. Reset
Pin # 3 …. SCK
Pin # 4 …. MISO
Pin # 5 …. MOSI
2.3.1 Reset Button:
Since the power ON reset is not in-built in 8051 based controllers therefore a jumper is given to
connect external power ON reset RC circuit for using 8051 based Microcontrollers. Supported
8051 controllers require external power on reset circuitry therefore an RC circuit to the reset pin of
microcontroller may be connected through a jumper as shown in above figure.
2.4 Bar Graph Display: A package of 10 LEDs is placed on the trainer board as bar graph display. 8 middle LEDs of this
display are connected to PORT1/ PORTB of microcontroller in active low state. These LEDs are
connected in series with 1K resistors (9 pin SIP). SIP is a package of 8 resistors (1Kohm). One
terminal of each resistor is commonly connected to the supply voltage and other terminals of
resistors are connected in series with LEDs. Two LEDs (top and bottom) are open circuited (not in
use). The PORTB of microcontroller may also be disconnected (for external use) from these LEDs
by extracting the LED array from board. Therefore we can use the PORTB for external hardware
interfacing as well as for ISP and stepper motor.
(10)
2.5 Crystal:
The board is equipped with a crystal of 11.0592 MHz. 11.0592 MHz crystal is especially preferred
for serial communication applications. This crystal provides accurate values standard baud rates.
The manufacturer (Atmel) provides their all AVR microcontrollers with internal RC oscillator as
the default CPU clock source. Therefore it is necessary to first configure a fresh microcontroller on
external crystal.
AT
mega 1
62
15
14
13
20
23
16
40
9
1819
101112
3938373635343332
313029
2827262524
12345678
2122
17
P3.5/PD5
P3.4/PD4
P3.3/PD3
GN
D
PC2/P2.2
P3.6/PD6
VC
C
Reset
XTAL 2XTAL 1
P3.0/PD0P3.1/PD1P3.2/PD2
PA0/P0.0PA1/P0.1PA2/P0.2PA3/P0.3PA4/P0.4PA5/P0.5PA6/P0.6PA7/P0.7
PE0/EAPE1/ALE
PE2/PSEN
PC7/P2.7PC6/P2.6PC5/P2.5PC4/P2.4PC3/P2.3
P1.0/PB0P1.1/PB1P1.2/PB2P1.3/PB3P1.4/PB4P1.5/PB5P1.6/PB6P1.7/PB7
PC0/P2.0PC1/P2.1
P3.7/PD7
11.0592 MHz
AT
89S
8252
Crystal
22pf
GND
22pf
(11)
2.6 Analog to Digital Converter (ADC):
An ADC0831, 8bit serial ADC is available on the trainer board. The multi turn potentiometer is
used to provide the reference voltage to the ADC and a three pin connector is given for analog data
to the ADC. The corner pins of three pin connector provide 5V supply to the sensor while the
central pin is used as analog input. The ADC is interfaced with microcontroller IO bits P0.2/PA2 at
ADC data, P0.0/PA0 at ADC chip select and P0.1/PA1 at ADC serial clock.
2.7 Communication Port (Comm. Port):
A comm. port is provided for serial communication at the board. DB-9 female connector is used as
the comm. port. The In-built UART can receive or transmit the serial data (using this connector)
through a line drive MAX-232. This line driver IC converts the incoming RS-232 signals into the
TTL for microcontroller. Similarly it converts the TTL signals coming from microcontroller to RS-
232.
(12)
2.8 Off Board Numeric Keypad:
Numeric 4×4 multiplex keypad is available off the trainer board. The rows and columns of key
matrix are connected with a keypad driver IC 74C922 which sends the key press interrupt to PD2
and then data may be received from higher nibble of PORTD. Output enable (OE) of keypad driver
is connected with PD3. Keypad may be plug in or plug out at crimp shell Connector for PORTD.
(13)
Important: PORTB may be shared by Stepper motor driver and 7 Segment display.
2.9 4-Digit Multiplexed 7-Segment Display:
The data bus of multiplexed 7-segment display is connected to PORTB of microcontroller (PB0
for segment „a‟, PB1 for segment „b‟ and so on). Eight common anode terminals of digits are
driven with the help of PNP transistors. Only four digits are used with microcontroller programs
on the kit. The least significant digit of display is controlled by PA0 and most significant digit by
PA3. The 7-segment display may also be disconnected to PORTB of microcontroller at CR
Connector for PORT B.
(14)
Important: PORTB may be shared by Stepper motor driver and Keypad.
2.10 Alphanumeric LCD Interface: 12 pin Crimp Shell connector is used to interface an LCD module with the microcontroller. PC of
Microcontroller is used at this connector for data bus of LCD module, PA6 for RS, PA7 for enable
(E of LCD). The contrast pin of LCD module should be connected to ground. The Backlight of
LCD module is connected to the supply to turn on backlight permanently.
(15)
Important: PORTC is shared by LCD, 7 Segment display therefore it is necessary to plug out
7segment display.
(16)
2.11 Relays: Four Relays are interfaced through PE2 to PE0 and PA3.
(17)
2.12 Push Buttons:
Six push buttons may be used at PD2 to PD7 as shown below.
3 Practical
3.1 Practical No. 1 (Check Signal Key)
Increase/Decrease a single binary count on the LEDs at PORTB when a button at PD6 or
PD7 is pressed.
*Soft copy of practical is available in the folder “Check Signal Key”
3.1.1 Apparatus:
Computer System
Microcontroller ATmega162
ISP cable for Program downloading
Star Engineering Training kit ATK V11-162 with accessories
3.1.2 Algorithm & Flow Chart:
We have to scan two push buttons continuously till any one is pressed. On
pressing the button we will execute the task only one time (Increment/Decrement
of binary count on LEDs). The flow Chart is given below:
(18)
A => CHK_PB1
B => CHK_PB2
Initial value of CNT is
00H
LEDs are connected at P1
LEDs are active low
3.1.3 AVR Program C Code:
#include <mega162.h>
#include <delay.h>
#define SW1 PIND.6
#define SW2 PIND.7
void main(void) // Main loop
{
unsigned char cnt=0; // Declare your local variables here
PORTB=0xFF; // To enable internal pull-up resister
DDRB=0xFF; // For outward direction
PORTD.6=1; // To enable internal pull-up resister
PORTD.7=1; // To enable internal pull-up resister
while (1)
{
while (SW1==0) // Loop while key is pressed at PD6, Active low
{
delay_ms(15);
START
Y
N
Y
N
PB1
Pressed
PB2
Pressed
A
B
PBI
Pressed
CNT++
NOT (CNT)
P1=CNT
Delay 15ms
Delay 15ms
PB2
Pressed
CNT--
NOT (CNT)
P1=CNT
Delay 15ms
Delay 15ms
(19)
cnt++; // For Increment
PORTB=~cnt; // Invert the variable value to drive active low LED's
while (SW1==0) ; // Empty loop, Wait to release the key
delay_ms(15);
};
while (SW2==0) // Loop while key is pressed at PD7, Active low
{
delay_ms(15);
cnt--; // For Decrement
PORTB=~cnt; // Invert the variable value to drive active low LED's
while (SW2==0) ; // Empty loop, Wait to release the key
delay_ms(15);
};
};
}
3.1.4 Exercise:
Type the program, assemble/compile it, and make its object file and hex
file.
Now download the HEX file in Microcontroller’s code memory
Use star trainer to run/execute the program and check results
Now make a program and its flowchart with the same objective but for gray
codes pattern (counting)
Submit the flowchart, program code, and results neatly.
3.1.5 Conclusion:
We can use the program format for scanning/checking multiple push
buttons.
We can also use the program format for executing tasks on key down or key
up events.
3.2 Practical No. 2 (Bottle Filling)
Control a conveyer based bottle filling station. Use PD2 to read the signal from bottle detector,
PE0 to drive conveyer and PA5 for filling valve. Run the system and wait for bottle, use the filling
delay of 10 Second and show the count of filled bottles on LCD. (Inputs are active low and outputs
are active high).
*Soft copy of practical is available in the folder “filling_bottle”
3.2.1 Apparatus:
Computer System
Microcontroller ATmega162
ISP cable for Program downloading
Star Engineering Training kit ATK V11-162 with accessories
3.2.2 Algorithm
Control a conveyer based bottle filling station. Use PD2 to read the signal from bottle detector,
PE0 to drive conveyer and PA5 for filling valve. Run the system and wait for bottle, use the filling
delay of 10 Second and show the count of filled bottles on LCD. (Inputs are active low and outputs
are active high).
(20)
3.2.3 AVR Program C Code:
#include <mega162.h>
#include <delay.h>
#include <ATKv11_162.h>
#define BOT_Detector PIND.2
#define UP_LVL PIND.3
#define DWN_LVL PIND.4
#define CONVAYOR PORTE.0
#define Nozzle PORTE.1
#define PUMP PORTE.2
void main (void) // Main loop
{
unsigned int cnt=0; // Declare your local variables here
DDRE.0=1; // For outward direction
DDRE.1=1; // For outward direction
DDRE.2=1; // For outward direction
Init_LCD(); // To initialized the LCD
PORTD.2=1; // To enable internal pull-up resister
PORTD.3=1; // To enable internal pull-up resister
PORTD.4=1; // To enable internal pull-up resister
while(1)
{
CONVAYOR=1; //(Active High - Turn on Conveyer)
while(BOT_Detector==1) //(Active Low - Bottle Detector)
{
}
CONVAYOR=0; //(Turn off Conveyer)
delay_ms(1000);
if(DWN_LVL==1) //(Active Low - Down Level Detector)
{
wrLCDcmd(0x80); // To write the Command byte on LCD
LCD_msg("Tank Filling started"); // To write a massage on LCD
PUMP=1; //(Active High - Turn on Pump)
while(UP_LVL==1) //(Active Low - Up Level Detector)
{
}
PUMP=0; //(Active High - Turn off Pump)
wrLCDcmd(0x80); // To write the Command byte on LCD
LCD_msg("Tank has been filled"); // To write a massage on LCD
delay_ms(1000);
wrLCDcmd(0x80); // To write the Command byte on LCD
LCD_msg("Wait for Bottle"); // To write a massage on LCD
delay_ms(1000);
}
wrLCDcmd(0x80); // To write the Command byte on LCD
LCD_msg("Bottle Filling Started"); // To write a massage on LCD
Nozzle=1; //(Active High - Filling Nozzle Open)
delay_ms(10000);
wrLCDcmd(0x80); // To write the Command byte on LCD
LCD_msg("Filling Completed"); // To write a massage on LCD
Nozzle=0; //Active High - Filling Nozzle Close
delay_ms(1000);
wrLCDcmd(0xC0); // To write the Command byte on LCD
UpdateDigits(++cnt); // Counting of bottle
}
}
3.2.4 Exercise:
(21)
Type the program, assemble/ compile it, make its object file and hex
file.
Now download the HEX file in Microcontroller’s code memory
Use star trainer to run/ execute the program and check results
Write a program code for the same objective with parallel operation of
tank filling and bottle filling.
Submit the flowchart, program code, and results neatly.
3.2.5 Conclusion:
We can use the program format for scanning/ checking multiple push
buttons.
We can also use the program format for executing tasks on key down or key
up events.
3.3 Practical No. 3 (Stepper Motor Control System) Run a stepper motor in clockwise direction with maximum speed, when a button at PD6 is pressed
in cw, when a button at PD7 is pressed in ccw and motor should continue to run till key is pressed.
*Soft copy of practical is available in the folder “Stepper_motor”
3.3.1 Apparatus:
Computer System
Microcontroller ATmega162
ISP cable for Program downloading
Star Engineering Training kit ATK V11-162 with accessories
3.3.2 Algorithm
Run a stepper motor in clockwise direction with maximum speed, when a button at
PD6 is pressed in cw, when a button at PD7 is pressed in ccw and motor should
continue to run till key is pressed.
3.3.3 AVR Program C Code:
#include <mega162.h>
#include <delay.h>
#define SW1 PIND.6
#define SW2 PIND.7
void main(void) // Main Loop
{
DDRB=0x0F; // For outward direction
PORTD=0x0F; // To enable internal pull-up resisters
while (1)
{
while(SW1==0)
{
if(PORTB.0==1)
{
PORTB.0=0;
PORTB.1=1;
}
else if(PORTB.1==1)
{
PORTB.1=0;
(22)
PORTB.2=1;
}
else if(PORTB.2==1)
{
PORTB.2=0;
PORTB.3=1;
}
else
{
PORTB.3=0;
PORTB.0=1;
}
delay_ms(10);
}
while(SW2==0)
{
if(PORTB.3==1)
{
PORTB.3=0;
PORTB.2=1;
}
else if(PORTB.2==1)
{
PORTB.2=0;
PORTB.1=1;
}
else if(PORTB.1==1)
{
PORTB.1=0;
PORTB.0=1;
}
else
{
PORTB.0=0;
PORTB.3=1;
}
delay_ms(50);
}
};
}
3.3.4 Exercise:
Type the program, assemble/ compile it, make its object file and hex
file.
Now download the HEX file in Microcontroller’s code memory
Use star trainer to run/ execute the program and check results.
Run a stepper motor in anti clockwise direction with maximum speed, when
a button at PD6 or PD7 is pressed, motor should continue to run till key
is pressed rather cw or ccw.
Submit the flowchart, program code, and results neatly.
3.3.5 Conclusion:
We can also use the program format for executing the tasks after certain
duration of key press.
The program is also giving us the concept of giving a little security on
the operations executed by pressing a key.
(23)
3.4 Practical No. 4 (Frequency Counter)
Write C language program for ATmega162 to display the frequency of analog input signal
(Use 220V mains supply as input). Hint external interrupt may be used for zero crossing counting
and the value of count will show the frequency after each second.
*Soft copy of practical is available in the folder “zero_crossing”
3.4.1 Apparatus:
Computer System
Microcontroller ATmega162
ISP cable for Program downloading
Star Engineering Training kit ATK V11-162 with accessories
3.4.2 Algorithm
Read 220 volt AC frequency and display On LCD using external interrupt
3.4.3 AVR Program C Code:
#include <mega162.h>
#include <delay.h>
#include <ATKv11_162.h>
unsigned int cnt=0;
interrupt [EXT_INT0] void ext_int0_isr(void)
{
cnt++; // For Increment
}
void main(void) // Main Program
{
GICR=0x40;
MCUCR=0x02;
Init_LCD(); // To initialized the LCD
SREG.7=1; // To enable global interrupt
LCD_msg("Frequency="); // To write a message on LCD
while (1)
{
delay_ms(1000);
wrLCDcmd(0x8A); // To write the Command byte on LCD
UpdateDigits(cnt);// To display the implemented count
cnt=0;
};
}
3.4.4 Exercise:
Type the program, assemble/compile it, and make its object file and hex
file.
Now download the HEX file in Microcontroller’s code memory
Use star trainer to run/ execute the program and check results
Now write a C language program for ATmega162 to display the phase shift
(Phase Difference in micro second) of two analog inputs. Hint: External
interrupts may be used for zero crossing detections and a counter will
run during two zero crossings. Counter value show the phase shift.
Submit the flowchart, program code, and results neatly.
(24)
3.4.5 Conclusion:
We can also use the program format for executing the tasks after certain
duration of key press.
The program is also giving us the concept of giving a little security on
the operations executed by pressing a key.
3.5 Practical No. 5 (Servo Motor Control System)
Use UART with baud rate of 9600 to increase or decrease the angular position of the shaft
of a Servo Motor. User will press „+‟ or „-‟ to increase or decrease the positions. Write
down C language program for ATmega162 to drive the Servo Motor through PB3. Hint (Use timer
zero for 20 ms interrupt and timer two for pulse width).
*Soft copy of practical is available in the folder “Servo_motor”
3.5.1 Apparatus:
Computer System
Microcontroller ATmega162
ISP cable for Program downloading
Star Engineering Training kit ATK V11-162 with accessories
3.5.2 Algorithm
Microcontroller interface with UART and servo motor when a user press + button
on ascii key board on hyper terminal servo motor change it angle or press –
button on ascii key board on hyper terminal servo motor change it angle to
another direction.
3.5.3 AVR Program C Code:
#include <mega162.h>
#include <delay.h>
#include <ATKv11_162.h>
#define Servo PORTB.3
interrupt [TIM0_COMP] void timer0_comp_isr(void)
// to generate reference time(20 ms) for servo control signal
{
Servo=1; // To set servo m.c. signal(Width of signal is define by timer#2)
TCCR2=0x0D; // To enable the Timer#2, for No PWM, CTC Mode and Ftimer = Fcpu/128
}
interrupt [TIM2_COMP] void timer2_comp_isr(void)
{
Servo=0; // To Clear servo m.c. signal(Width of signal is define by
//timer#2)
TCCR2=0x00; // To disable Timer#2
}
void main(void)
{
PORTB=0xFF; // To enable internal pull-up resister
DDRB=0xFF; // For outward direction
Servo=0;
TCCR0=0x0D; // To enable the Timer#2, for No PWM, CTC Mode and Ftimer=Fcpu/1024
OCR0=216; // To configure interrupt time=20ms
(25)
OCR2=90; // To initialize pulse width=1ms
TIMSK=0x82; // To enable compare match intrrupt#0 and #2
SREG.7=1; // To enable global interrupt by Assembly program
UCSR0B=0x18; // To enable the TxD and RxD
UBRR0L=71; // To set the baud rate=9600 with XTAL=11.0952 MHz
UBRR0H=0;
Init_LCD(); // To initialized the LCD
wrLCDcmd(0x80); // To write the Command byte on LCD
LCD_msg(" Servo Test "); // To write a message on LCD
wrLCDcmd(0xC0); // To write the Command byte on LCD
LCD_msg(" Press + or - "); // To write a message on LCD
while (1)
{
Wait4RxC();
if(UDR=='+')
{
if(OCR2<180)
{
OCR2++;
Wait4TxC('+');
}
else
{
wrLCDcmd(0xC0);
LCD_msg("Extreme Reached ");
}
}
else if(UDR=='-')
{
if(OCR2>90)
{
OCR2--;
Wait4TxC('-');
}
else
{
wrLCDcmd(0xC0);
LCD_msg("Extreme Reached ");
}
}
else
{
wrLCDcmd(0xC0);
LCD_msg(" Press + or - ");
}
}
}
3.5.4 Exercise:
Type the program, assemble/compile it, and make its object file and hex
file.
Now download the HEX file in Microcontroller’s code memory
Use star trainer to run/ execute the program and check results.
Now make a program to use UART with baud rate of 9600 to adjust the
angular position of servo. User will press ‘1’ for step1 and ‘2‘for
step2. Step1 move the motor shaft at 30 degree and step2 move the motor
at 150 degree, if shaft is already at one of the mentioned angle display
(26)
a message on LCD. Hint (Use timer zero for 20 ms interrupt and timer two
for pulse width).
Submit the flowchart, program code, and results neatly.
3.5.5 Conclusion:
We can also use the program format for executing the tasks after certain
duration of key press.
The program is also giving us the concept of giving a little security on
the operations executed by pressing a key.
3.6 Practical No. 6 (ADC_UART_LCD) Display the equivalent decimal of ADC input on
the LCD module and also send this value through UART with Baud Rate=9600bps.
*Soft copy of practical is available in the folder “ADC_UART_LCD_162”
3.6.1 Apparatus:
Computer System
Microcontroller ATmega162
ISP cable for Program downloading
Star Engineering Training kit ATK V11-162 with accessories
3.6.2 Algorithm
Using ADC 0831 display equivalent decimal value on lcd and also send it on UART transmission.
3.6.3 AVR Program C Code:
#include <mega162.h>
#include <delay.h>
#include <ATKv11_162.h>
#define rs PORTA.6
#define LOW 0
#define HIGH 1
#define ADC_CS PORTA.0
#define ADC_SK PORTA.1
#define ADC_DT PINA.2
flash unsigned char Name[20]="****Rashid****";
typedef unsigned char byte;
typedef unsigned int word;
byte dig1=0,dig2=0,dig3=0,ADC;
void UART_LCD_SendVar(void) //Function define for send ADC value on UART and LCD.
{
UDR0=dig3; //Send digit 3 value on UART
wrLCDchr(dig3); //Send digit 3 value on LCD
(27)
UDR0=dig2; //Send digit 2 value on UART
wrLCDchr(dig2); //Send digit 2 value on LCD
UDR0=dig1; //Send digit 1 value on UART
wrLCDchr(dig1); //Send digit 1 value on LCD
UDR0=0x0D;
}
void pulseADC(void) //Function for serial clock
{
ADC_SK=1; //serial clock high
delay_ms(1);
ADC_SK=0; //serial clock low
delay_ms(1);
}
void ADCdata(void) //
{
unsigned char j=0x80;
ADC=0;
for (j=0x80; j> 0x01; j>>=1)
{
pulseADC();
if(ADC_DT==1)
{
ADC=ADC+j;
}
}
pulseADC();
if(ADC_DT==1)
{
ADC=ADC+j;
}
}
void readADC(void) //Function for read data from serial ADC
{
ADC_CS=0; //ADC chip select to low
delay_ms(1);
pulseADC(); //Call function for serial clock
ADCdata(); //
delay_ms(1);
ADC_CS=1; //ADC chip select to high
}
void main(void)
{
Init_LCD(); //Call LCD initialize function
UCSR0B=0x08;
UBRR0L=71;
UBRR0H=0;
PORTD.2=1; // To enable internal pull-up resister
PORTD.3=1; // To enable internal pull-up resister
(28)
PORTD.5=1; // To enable internal pull-up resister
DDRD.4=1; // For outward direction
DDRC.0=1; // For outward direction
DDRA.0=1; // For outward direction
DDRA.1=1; // For outward direction
PORTA.0=1; // To enable internal pull-up resister
wrLCDcmd(0x82); //Command for cursor
LCD_msg("My First Message"); //Display massage on LCD
delay_ms(2000);
wrLCDcmd(0xC3); //Command for cursor
LCD_msg(Name); //Display massage on LCD
delay_ms(2000);
while (1)
{
readADC();
UpdateDigits(ADC);
UART_LCD_SendVar(); //
delay_ms(1000);
wrLCDcmd(0x01); //clear LCD command
}
}
3.6.4 Exercise:
Type the program, assemble/compile it, make its object file and hex file.
Now download the HEX file in Microcontroller’s code memory
Use star trainer to run/execute the program and check results.
Now write a C language program to control the temperature of a boiler.
Use LM35 with 8 bit in ADC 0831 interface with ATmega162 to get
temperature feedback and a potentiometer to get set point through channel
one and channel two.
Now make a program and its flowchart with the same objective but you have
to toggle the LED if the key press duration lies between 4 to 6 seconds.
Submit the flowchart, program code, and results neatly.
3.6.5 Conclusion:
We can also use the program format for executing the tasks after certain
duration of key press.
The program is also giving us the concept of giving a little security on
the operations executed by pressing a key.
3.7 Practical No. 7 (UART control DC motor). Use UART with baud rate of 9600bps to
receive the speed level (0-9) for a DC Motor from Hyper Terminal. Write C Language
program for ATmega162 to drive a DC Motor through PWM using timer zero according to
received speed level. Also acknowledge by reflecting the received character.
*Soft copy of practical is available in the folder “uart_dc_motor”.
3.7.1 Apparatus:
(29)
Computer System
Microcontroller ATmega162
ISP cable for Program downloading
Star Engineering Training kit ATK V11-162 with accessories
3.7.2 Algorithm
Control DC motor with RS232(UART Communication),when user press any numeric button
On key board from 0-9 then speed of motor increases/deceases.
3.7.3 AVR Program C Code:
#include <mega162.h>
#include <delay.h>
#include <ATKv11_162.h>
void main(void) // Main loop
{
unsigned char x=0; // Declare your local variables here
PORTB=0xFF; // To enable internal pull-up resister
DDRB=0xFF; // For outward direction
TCCR0=0x6D; // PWM
OCR0=128;
UCSR0B=0x18; // To enable the TxD and RxD
UBRR0H=0;
UBRR0L=71; // To set the baud rate=9600 with XTAL=11.0952 MHz
Init_LCD(); // To initialized the LCD
while (1)
{
LCD_msg("Enter Speed Level for DC Motor (0 to 9): ");
Wait4RxC();
x=UDR0;
if((x>='0')&&(x<='9')) // Checking valid value
{
OCR0=(x-0x30)*28; // For Scaling value 0 to 255
Wait4TxC(x);
}
Wait4TxC(0x0D);
};
}
3.7.4 Exercise:
Type the program, assemble/compile it, and make its object file and hex
file.
Now download the HEX file in Microcontroller’s code memory
Use star trainer to run/ execute the program and check results.
Now make a program to use UART with baud rate of 9600bps to receive the
speed level (0-9) for a DC Motor from Hyper Terminal. Write C Language
program for ATmega162 to drive a DC Motor through PWM using timer zero
according to received speed level. Also acknowledge by reflecting the
received character.
Submit the flowchart, program code, and results neatly.
3.7.5 Conclusion:
We can also use the program format for executing the tasks after certain
duration of key press.
(30)
The program is also giving us the concept of giving a little security on
the operations executed by pressing a key.
3.8 Practical No. 8(4x4 Multiplexed Keypad) Use 4x4 multiplexed Keypad an LCD Module
with ATmega162 to display key codes (On interrupt basis) when a key is pressed on keypad.
Key codes should range from '0' to 'F'. Also pass the key pressed indication to main program
using a flag bit and generate a half second pulse on a relay in main program.
*Soft copy of practical is available in the folder “4X4 Keypad”
3.8.1 Apparatus:
Computer System
Microcontroller ATmega162
ISP cable for Program downloading
Star Engineering Training kit ATK V11-162 with accessories
3.8.2 Algorithm
This function waits for a key press on Keypad. This function will make CPU busy
Till the key pressed by user. 4-bit key code is returned through a character
Variable. Execution time of this function is user dependent.
3.8.3 AVR Program C Code:
#include <mega162.h>
#include <delay.h>
#include <ATKv11_162.h>
#define Keypad_INT PIND.2
#define Keypad_data PIND
#define Relay PORTA.3
bit KeyFlag = 0;
unsigned char KeyCode=0; // Declare global variables here
/*_____________________________________________________________________________
This function waits for a key press on Keypad. This function will make CPU busy
till the key pressed by user. 4-bit key code is returned through a character
variable. Execution time of this function is user dependent.
_____________________________________________________________________________*/
interrupt[EXT_INT2] void wait4key(void)
{
delay_ms(5);
KeyCode = Keypad_data; // Read Key data from PortD
KeyCode = KeyCode & 0xF0; // Removing undesired lower nibble
KeyCode = KeyCode >> 4; // Shift data to lower nibble
KeyCode = KeyCode | 0x30; // Converting to ASCII
if(KeyCode>0x039)
KeyCode = KeyCode + 7; // For shifting the ASCII code after
// ABCDEF of Hex Dec.
wrLCDcmd(0xC0); // Set cursor position
LCD_msg("Key: "); //show this message on LCD
wrLCDchr(KeyCode); // Display the key code
KeyFlag=1;
(31)
}
void main(void)
{
Init_LCD(); // Initialize LCD Module
LCD_msg(" LCD Keypad "); // To write a message on LCD
GICR = 0x20;
DDRA.3 = 1; // For outward direction
SREG.7 = 1;
while (1)
{
while(KeyFlag==0);
Relay=1; // To turn on the Relay
delay_ms(500);
Relay=0;
KeyFlag=0;
};
}
3.8.4 Exercise:
Type the program, assemble/compile it, and make its object file and hex
file.
Now download the HEX file in Microcontroller’s code memory
Use star trainer to run/execute the program and check results
Repeat Practical No.07 by giving speed level (0-9) from keypad.
Submit the flowchart, program code, and results neatly.
3.8.5 Conclusion:
We can also use the program format for executing the tasks after certain
duration of key press.
The program is also giving us the concept of giving a little security on
the operations executed by pressing a key.
3.9 Practical No. 9(Single Digit 7 Segment Display)
Display the hexadecimal counting in increasing on Button PD6 is press and decreasing on Button
PD7 is press set the counting delay of 300ms.
*Soft copy of practical is available in the folder “single_digit_7seg”
3.9.1 Apparatus:
Computer System
Microcontroller ATmega162
ISP cable for Program downloading
Star Engineering Training kit ATK V11-162 with accessories
3.9.2 Algorithm & Flow Chart:
We have to generate the pattern of 7-segment codes for hexadecimal
numbers at P1 to display. The flow Chart is given below:
PA1 = 1 P A2 = 1
P A3 = 1
Initialize pointer at segment codes
START
(32)
3.9.3 AVR Program C Code:
#include <mega162.h>
#include <delay.h>
#define SW1 PORTD.6
#define SW2 PORTD.7
void main(void)
{
unsigned char i; // Declaration variables
unsigned char SegCod[16]={0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F,
0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71};
DDRA=0x0F; // for outward direction
DDRB=0xFF; // for outward direction
PORTD.6=1; // To enable internal pull-up resister
PORTD.7=1; // To enable internal pull-up resister
start:
while (1) // infinite loop
{
while (SW1==0)
{
for(i=0x00;i<=0x0F;i++)
{
PORTB=~SegCod[i]; // to find 7 segment code from array
If(SW1==1) // break if key released
goto start;
delay_ms(300);
};
};
while (SW2==0)
{
for(i=0x0F;i>=0x00;i--)
{
(33)
PORTB=~SegCod[i]; // to find 7 segment code from array
if(SW2==1) // break if key released
goto start;
delay_ms(300);
};
};
};
}
3.9.4 Exercise:
Type the program, assemble/compile it, and make its object file and hex
file.
Now download the HEX file in Microcontroller’s code memory
Use star trainer to run/ execute the program and check results
Display the decimal counting in increasing order on the least significant
digit of 7-segment display. Set the counting delay of 300ms.
Submit the flowchart, program code, and results neatly.
3.9.5 Conclusion:
We can also use the program format for executing the repetitive tasks
with constant speed.
The program is also giving us the concept of deterministic pattern
generation (7 segment codes etc.)
3.10 Practical NO. 10 (4-digit multiplexed 7-segment led driver) generate the binary pattern
in increasing/decreasing order on 4-digit multiplexed 7-segment displays at PORTB, when a
button at PD6 or PD7is pressed. Set the counting speed of 2 counts per second.
*Soft copy of practical is available in the folder “single_digit_7seg”.
3.10.1 Apparatus:
Computer System
Microcontroller ATmega162
ISP cable for Program downloading
Star Engineering Training kit ATK V11-162 with accessories
3.10.2 Algorithm
Display decimal pattern on 4-Digit 7-Segment display with increasing/decreasing order when
button on PORTD, PD6 or PD7 is pressed.
3.10.3 AVR Program C Code:
#include <mega162.h>
#include <delay.h>
#include <ATKv11_162.h>
#define SW1 PIND.6 //Define PD6 as a sw1
#define SW2 PIND.7 //Define PD7 as a sw2
unsigned int cnt;
flash unsigned char SegCod[16]={0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07,
0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71};
(34)
unsigned char GetSegDig16Bit(unsigned char Number, unsigned char digit)
{
unsigned char dig1, dig2, dig3, dig4;
dig1 =SegCod[Number%10];
Number/=10;
dig2 =SegCod[Number%10];
Number/=10;
dig3 =SegCod[Number%10];
Number/=10;
dig4 =0x3F;
if(digit==1)
return dig1;
else if(digit==2)
return dig2;
else if(digit==3)
return dig3;
else if(digit==4)
return dig4;
else
return 0;
}
interrupt [TIM0_COMP] void RefreshDisplay(void) //interrupt for timer 0
{
if (PORTA.0==0) //Check PA0 output
{
PORTA.0=1; //Put high in PA0 for drive a transistor
PORTA.1=0; //Put low in PA1 for drive a transistor
PORTB=GetSegDig16Bit(cnt,2); //Send 4-Digit 7-Segment code on PORTB
}
else if (PORTA.1==0) //Check PA1 output
{
PORTA.1=1; //Put high in PA1 for drive a transistor
PORTA.2=0; //Put low in PA2 for drive a transistor
PORTB=GetSegDig16Bit(cnt,3); //Send 4-Digit 7-Segment code on PORTB
}
else if (PORTA.2==0) //Check PA2 output
{
PORTA.2=1; //Put high in PA2 for drive a transistor
PORTA.3=0; //Put low in PA3 for drive a transistor
PORTB=GetSegDig16Bit(cnt,4); //Send 4-Digit 7-Segment code on PORTB
}
else
{
PORTA.3=1; //Put high in PA3 for drive a transistor
PORTA.0=0; //Put low in PA0 for drive a transistor
PORTB=GetSegDig16Bit(cnt,1); //Send 4-Digit 7-Segment code on PORTB
}
}
void main(void) // Main loop
{
DDRB=0xFF; // For outward direction
PORTA=0xFE;
DDRA=0xFF; // For outward direction
PORTC.0=1; // To enable internal pull-up resister
PORTC.1=1; // To enable internal pull-up resister
TCCR0=0x0D; // To enable the Timer#2, for No PWM, CTC Mode and
Ftimer=Fcpu/1024
(35)
OCR0=39; // To initialize pulse width=
TIMSK=0x02;
SREG.7=1;
while (1)
{
while (SW1==0) //(Active Low - wait for button on pd6 is pressed)
{
cnt++; //increament in cnt verible
delay_ms(50); //delay
};
while (SW2==0) //(Active Low - wait for button on pd6 is pressed)
{
cnt--; //decreament in cnt verible
delay_ms(50);
};
};
}
3.10.4 Exercise:
Type the program, assemble/compile it, and make its object file and hex
file.
Now download the HEX file in Microcontroller’s code memory
Use star trainer to run/ execute the program and check results
Generate the binary pattern in increasing/decreasing order on 4digit
multiplexed 7 segment display at PORTB, when a button at PD6 or PD7 is
pressed. Set the counting speed of 1 count per second.
Now make a program and its flowchart with the same objective but for
decimal pattern (counting)
Submit the flowchart, program code, and results neatly.
3.10.5 Conclusion:
We can also use the program format for executing the repetitive tasks
with constant speed.
The program is also giving us the concept of deterministic pattern
generation (7 segment codes etc.)
3.11 Practical NO. 11(Real Time Clock) Use RTC (D71307) with ATmega162 to update the
value of time and date after each second on LCD screen. Use PA1 and PA2 with clock and
data of RTC for I2C interface.
*Soft copy of practical is available in the folder “RTC”
3.11.1 Apparatus:
Computer System
Microcontroller ATmega162
ISP cable for Program downloading
Star Engineering Training kit ATK V11-162 with accessories
(36)
3.11.2 Algorithm
Display time and date on LCD using RTC interface with microcontroller using tow wire
interfacing.
3.11.3 AVR Program C Code:
#include <mega162.h>
#include <delay.h>
// I2C Bus functions
#asm // To Enter Assembly programming mode
.equ __i2c_port=0x15 ;PORTC
.equ __sda_bit=1
.equ __scl_bit=0
#endasm // To Exit Assembly programming mode
#include <i2c.h>
// DS1307 Real Time Clock functions
#include <ds1307.h>
#include <ATKv11_162.h>
// Declare your global variables here
void main(void) // Main loop
{
unsigned char *RTChour, *RTCmin, *RTCsec, RTCdig1, RTCdig2, *RTCday, *RTCmon,
*RTCyer;
PORTB=0xFF;
DDRB=0xFF; // For outward direction
// I2C Bus initialization
Init_LCD(); // To initialized the LCD
i2c_init();
// DS1307 Real Time Clock initialization
// Square wave output on pin SQW/OUT: On
// Square wave frequency: 1Hz
rtc_init(0,0,0);
wrLCDcmd(0x80); // To write the Command byte on LCD
LCD_msg("Star Engineering"); // To write a message on LCD
wrLCDcmd(0xC0); // To write the Command byte on LCD
LCD_msg(" RTC DS1307 "); // To write a message on LCD
delay_ms(2000);
wrLCDcmd(0x01); // To write the Command byte on LCD
rtc_set_time(3, 6, 0); //RTChour, RTCmin, RTCsec
delay_ms(2000);
rtc_set_date(23, 12, 01);
while (1)
{
wrLCDcmd(0x80); // To write the Command byte on LCD
rtc_get_time(RTChour, RTCmin, RTCsec); //RTChour, RTCmin, RTCsec
RTCdig1=(*RTChour%10)+0x30;
RTCdig2=(*RTChour/10)+0x30;
wrLCDchr(RTCdig2);
wrLCDchr(RTCdig1);
wrLCDchr(':');
RTCdig1=(*RTCmin%10)+0x30;
RTCdig2=(*RTCmin/10)+0x30;
wrLCDchr(RTCdig2);
wrLCDchr(RTCdig1);
wrLCDchr(':');
(37)
RTCdig1=(*RTCsec%10)+0x30;
RTCdig2=(*RTCsec/10)+0x30;
wrLCDchr(RTCdig2);
wrLCDchr(RTCdig1);
wrLCDchr(' ');
PORTB=~*RTCsec;
wrLCDcmd(0xC0);
rtc_get_date(RTCday, RTCmon, RTCyer);
RTCdig1=(*RTCday%10)+0x30;
RTCdig2=(*RTCday/10)+0x30;
wrLCDchr(RTCdig2);
wrLCDchr(RTCdig1);
wrLCDchr('-');
RTCdig1=(*RTCmon%10)+0x30;
RTCdig2=(*RTCmon/10)+0x30;
wrLCDchr(RTCdig2);
wrLCDchr(RTCdig1);
wrLCDchr('-');
RTCdig1=(*RTCyer%10)+0x30;
RTCdig2=(*RTCyer/10)+0x30;
wrLCDchr(RTCdig2);
wrLCDchr(RTCdig1);
delay_ms(1000);
};
}
3.11.4 Exercise:
Type the program, assemble/compile it, and make its object file and hex
file.
Now download the HEX file in Microcontroller’s code memory
Use star trainer to run/execute the program and check results
Use RTC (DS1307) with ATmega162 to update the value of time and date
after each second on LCD screen.
Submit the flowchart, program code, and results neatly.
3.11.5 Conclusion:
We can also use the program format for executing the repetitive tasks
with constant speed.
The program is also giving us the concept of deterministic pattern
generation (7 segment codes etc.)