0% found this document useful (0 votes)
18 views43 pages

Embedded Contro Systems

A Smart DC motor drive using microcontroller atmega 2560 (PWM-ADC-LCD-UART-Keypad) Software + hard ware

Uploaded by

vpyyqjrb7p
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views43 pages

Embedded Contro Systems

A Smart DC motor drive using microcontroller atmega 2560 (PWM-ADC-LCD-UART-Keypad) Software + hard ware

Uploaded by

vpyyqjrb7p
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Embedded Control Systems

Lab Final Project


‫‪Made by:‬‬

‫محمد أحمد محمد عالم‬


‫عبدهللا سيد منصور نصر‬

‫‪Supervised by:‬‬
‫‪Dr/ Emad El-Sheikh‬‬
Content

 Project Description
 Components
 Hardware Description
 Firmware
-ADC header and C file
-KeyPad header and C file
-LCD header and C file
-PWM header and C file
-UART header and C file
-Main Code
Project
Description
Project Description

 A DC motor to Control its Speed Using a potentiometer Connected in ADC pin


 The motor speed is varied by PWM DAC
 But , Not any one can have the access to Control the speed
 To access you have to enter the password correctly using Keypad
 The password and a feedback word is displayed on LCD
 If the password is entered incorrect 3 time in row the system stops and doesn’t
work again until 10 mins
 If password is correct the user can control the speed
 When the user finishes ,He/She pushes finish Button to save changes and return to
Lock Mode
Project Description
Communication Module
 There is another Microcontroller that need the state
of the first microcontroller to display it on LCD
 The second just displays word “open mode” when the
user have the accessibility to control the motor
speed
 And display word “lock mode” when the first system
can’t be accessed
 This communication between them uses UART
Protocol.
Components
Components
Component Number
Atmel Microcontroller (ATMEGA 2560) 2
LCD Display (16x2) 2
Keypad (4x4) 1
Dc motor 1
Bush buttons 3
Potentiometer(10k) 3
Resistors(10k) 8
Resistors(1k) 1
Diodes 5
Crystal Oscillator (16 MHZ) 2
Capacitors (22 pf) 4
Power Supply (5v) 1
Hard Ware
Description
Hardware Description

 A 4x4 Keypad is used as an input for the password


 It’s Connected to port J
 A Ones and Zero is sent to the rows and detect columns
Hardware Description

 An LCD at the size of 16x2 is used to display the password


 It’s used in 8bit mode to have more speed
 Data port is connected to Port K
 Status bits is connected to pins(G0,G1,G2)
Hardware Description

• The user can choose the speed he/she wants using a


potentiometer connected in ADC channel 0 (pin F0).
Hardware Description

 The interface Circuit of controlling the speed of the DC motor is using a transistor 2N2222A
with a series resistance of 1 k ohms.
 It is connected to a PWM pin (PB6)
Hardware Description (System-1)
Hardware Description (System-2)
Firm Ware
ADC header file
#ifndef ADC_H_
#define ADC_H_
#include <avr/io.h>
void init_ADC(void);
uint16_t ADC_read(uint8_t channel);
#endif /* ADC_H_ */
ADC C file
#include "ADC.h"
void init_ADC(void)
{
ADMUX |=(1<<REFS0); //Voltage reference VCC
ADCSRA |=(1<<ADEN); //Enable ADC
//Set division factor to max 128 , frequency=125kHZ
ADCSRA |=(1<<ADPS2);
ADCSRA |=(1<<ADPS1);
ADCSRA |=(1<<ADPS0);
}
uint16_t ADC_read(uint8_t channel)
{
ADMUX &=0xf0; //Clear the last conversion
ADMUX |=channel; //Select the channel to convert
ADCSRA |=(1<<ADSC); //Start Conversion
while(ADCSRA &(1<<ADSC)){;} //wait until the conversion is finished
return ADC; //return the ADC Value
}
Keypad header file
#include <avr/io.h>
#include <avr/delay.h>
#ifndef KEYPAD4_H_
#define KEYPAD4_H_
#define Row1 PJ0
#define Row2 PJ1
#define Row3 PJ2
#define Row4 PJ3
#define Col1 PJ4
#define Col2 PJ5
#define Col3 PJ6
#define Col4 PJ7

char KeyPad(void);

#endif /* KEYPAD4_H_ */
Keypad C file
#include "KeyPad4.h“
char KeyPad(void)
{
while(1)
{
PORTJ &=~(1<<Row1); PORTJ |=(1<<Row2); PORTJ |=(1<<Row3); PORTJ |=(1<<Row4);

_delay_ms(10);

if(!(PINJ & (1<<Col1))) {return '7';}

if(!(PINJ & (1<<Col2))) {return '8';}

if(!(PINJ& (1<<Col3))) {return '9';}

if(!(PINJ & (1<<Col4))) {return '/';}


Keypad C file
PORTJ |=(1<<Row1); PORTJ &=~(1<<Row2); PORTJ |=(1<<Row3); PORTJ |=(1<<Row4);

_delay_ms(10);

if(!(PINJ & (1<<Col1))) {return '4';}

if(!(PINJ& (1<<Col2))) {return '5';}

if(!(PINJ & (1<<Col3))) {return '6';}

if(!(PINJ & (1<<Col4))) {return 'x';}


Keypad C file
PORTJ |=(1<<Row1); PORTJ |=(1<<Row2); PORTJ &=~(1<<Row3); PORTJ |=(1<<Row4);

_delay_ms(10);

if(!(PINJ & (1<<Col1))) {return '1';}

if(!(PINJ & (1<<Col2))) {return '2';}

if(!(PINJ & (1<<Col3))) {return '3';}

if(!(PINJ & (1<<Col4))) {return '-';}


Keypad C file
PORTJ |=(1<<Row1); PORTJ |=(1<<Row2); PORTJ |=(1<<Row3); PORTJ &=~(1<<Row4);

_delay_ms(10);

if(!(PINJ & (1<<Col1))) {return 'c';}

if(!(PINJ & (1<<Col2))) {return '0';}

if(!(PINJ & (1<<Col3))) {return '=';}

if(!(PINJ & (1<<Col4))) {return '+';}

else continue;

}
LCD header file
#include <avr/io.h>
#include <avr/delay.h>
#ifndef LCD_H_
#define LCD_H_
#define LCD_DATA PORTK
#define rs PG0
#define rw PG1
#define en PG2
LCD header file
void init_lcd(void);
void lcd_cmd(unsigned char);
void lcd_write(char);
void Cursor_pos(unsigned char x_pos ,unsigned char y_pos);
void display_integer(int x);
unsigned char cmd;
unsigned char ch[3];
char data;
#endif /* LCD_H_ */
LCD C file
#include "LCD.h"
void init_lcd(void)
{
lcd_cmd(0x38); //Initialize 8bit mode
_delay_ms(1);
lcd_cmd(0x01); //Clear LCD
_delay_ms(1);
lcd_cmd(0x02); //Return home
_delay_ms(1);
lcd_cmd(0x06); //Increment Cursor
_delay_ms(1);
lcd_cmd(0x80); //Go to origin
_delay_ms(1);
}
LCD C file
void lcd_cmd(unsigned char cmd)
{
LCD_DATA=cmd; //send command to data port
PORTG &=~(1<<rs); //RS=0
PORTG &=~(1<<rw); //RW=0
PORTG |=(1<<en); //EN=1
_delay_ms(2);
PORTG &=~(1<<en); //EN=0
}

void lcd_write(char data)


{
LCD_DATA=data;//send data to data port
PORTG |=(1<<rs); //RS=1
PORTG &=~(1<<rw); //RW=0
PORTG |=(1<<en); //EN=1
_delay_ms(2);
PORTG &=~(1<<en); //EN=0
}
LCD C file
void Cursor_pos(unsigned char x_pos ,unsigned char y_pos)
{
uint8_t the_address=0;
if(x_pos==0) the_address=0x80; //1st row 1st column
else if(x_pos==1) the_address=0xc0; //2nd row 1st column
if(y_pos<16)
the_address += y_pos;
lcd_cmd(the_address);
}
LCD C file
void display_integer(int x)
{
for(int j=0;j<3;j++)
{
ch[j]=' '; //empty the array
}
itoa(x,ch,10); //integer to ASCII
for(int j=0;j<3;j++)
{
if(ch[j] <'0' || ch[j] >'9')
lcd_write(' '); //write space for non integer values
else
lcd_write(ch[j]); //display the character
}
}
PWM header file
#ifndef PWM_H_
#define PWM_H_
#include <avr/io.h>
#include <avr/delay.h>
void PWM_init(uint16_t frequency);
void PWM_write(uint16_t value);

#endif /* PWM_H_ */
PWM header file
#include "PWM.h"
void PWM_init(uint16_t frequency)
{
TCCR1A |=(1<<COM1B1); //PWM at compare Match Mode
TCCR1A |=(1<<WGM11)|(1<<WGM10); //Fast PWM with 10 bits duty cycle
TCCR1B |=(1<<WGM12); //Fast PWM with 10 bits duty cycle
TCCR1B |=(1<<CS10); //No Pre Scale
OCR1A=(16000000/(2*frequency))-1; //Send the frequency
}
void PWM_write(uint16_t value)
{
OCR1B=value; //Send value 0:1023
}
UART header file
#ifndef UART_H_
#define UART_H_
#include <avr/io.h>
#include <avr/delay.h>
void UART_init(unsigned int baud_rate);
void UART_write(unsigned char character);
unsigned char UART_read(void);
unsigned int baud;

#endif /* UART_H_ */
UART C file
#include "UART.h"
void UART_init(unsigned int baud_rate)
{
baud=(16000000/(16*baud_rate))-1; //Calculate The Value of UBRR
UBRR0L=(unsigned char)baud; //Set baud rate lower bits Register
UBRR0H=(unsigned char)(baud>>8); //Set baud rate higher bits Register
UCSR0B |=(1<<RXEN0)|(1<<TXEN0); // Turn on transmission and reception
UCSR0C |=(1<<UCSZ00)|(1<<UCSZ01); //8 bit Character Mode
}
void UART_write(unsigned char character)
{
while (!(UCSR0A&(1<<UDRE0))); //wait for data register to be empty
UDR0=character; //Send Data to Data Register
}
unsigned char UART_read(void)
{
while (!(UCSR0A&(1<<RXC0))); //Wait for Data until it received
return UDR0; //return the read Data;
}
System_1
Main Code
#include <avr/io.h>
#include <avr/delay.h>
#include "ADC.h"
#include "KeyPad4.h"
#include "LCD.h"
#include "PWM.h"
#include "UART.h"
#define Finish PL0
char check=1;
char num=0;
char password[9]={'P','a','s','s','w','o','r','d',':'};
char pass[3]={'5','6','7'};
char incorrect[9]={'I','n','c','o','r','r','e','c','t'};
char trials[15]={'T','r','i','a','l','s',' ','F','i','n','i','s','h','e','d'};
char speed[6]={'s','p','e','e','d','='};
int m_speed=0;
int adc_value=0;
int main(void)
{
DDRK=0xff;
DDRG=0x07;
DDRJ=0x0f;
DDRB |=(1<<DDB6);
DDRL &=~(1<<DDL0);
init_lcd();
_delay_ms(100);
lcd_cmd(0x0c); //display on
PWM_init(5000);
UART_init(9600);
init_ADC();
_delay_ms(100);
repeat:
UART_write(0);
lcd_cmd(0x01); //lcd Clear
Cursor_pos(0,1);
for(int i=0;i<9;i++)
{
lcd_write(password[i]);
}
while (1)
{
for(int i=0;i<3;i++)
{
unsigned char key =KeyPad();
lcd_write(key);
if (key !=pass[i]) check=0;
while(!(PINJ &(1<<Col1) && PINJ &(1<<Col2) && PINJ &(1<<Col3) && PINJ &(1<<Col4))){;}
}
if (!check)
{
num++;
if(num==3)
{
Cursor_pos(1,1);
for(int i=0;i<9;i++)
{
lcd_write(incorrect[i]);
}
_delay_ms(2000);
lcd_cmd(0x01); //clear lcd
Cursor_pos(0,1);
for(int i=0;i<15;i++)
{
lcd_write(trials[i]);
}
for(int i=0;i<60;i++)
{
_delay_ms(10000); //wait 10 minutes
}
check=1;
lcd_cmd(0x01); //lcd Clear
Cursor_pos(0,1);
for(int i=0;i<9;i++)
{
lcd_write(password[i]);
}
}
else
{
Cursor_pos(1,1);
for(int i=0;i<9;i++)
{
lcd_write(incorrect[i]);
}
_delay_ms(3000);
lcd_cmd(0x01); //lcd Clear
Cursor_pos(0,1);
for(int i=0;i<9;i++)
{
lcd_write(password[i]);
}
check=1;
}
}
else
{
num=0;
Cursor_pos(1,1);
for(int i=2;i<9;i++)
{
lcd_write(incorrect[i]);
}
_delay_ms(2000);
lcd_cmd(0x01); //lcd Clear
Cursor_pos(0,1);
for(int i=0;i<6;i++)
{
lcd_write(speed[i]);
}
UART_write('1');
while(1)
{
adc_value=ADC_read(0);
float x=(adc_value/1023.0)*100;
m_speed=x;
PWM_write(adc_value);
Cursor_pos(1,5);
display_integer(m_speed);
if(PINL &(1<<Finish)) goto repeat;
}
}
}
}
System_2
Main Code
#include <avr/io.h>
#include <avr/delay.h>
#include "UART.h"
#include "LCD.h"
char open[5]={'o','p','e','n',' '};
char lock[5]={'l','o','c','k',' '};
char mode[4]={'m','o','d','e'};
int main(void)
{
DDRF=0xff;
DDRG=0x07;
init_lcd();
UART_init(9600);
_delay_ms(100);
lcd_cmd(0x0c); //display on
while (1)
{
if(UART_read()>0)
{
Cursor_pos(0,1);
for(int i=0;i<5;i++)
{
lcd_write(open[i]);
}
for(int i=0;i<4;i++)
{
lcd_write(mode[i]);
}
}
else
{
Cursor_pos(0,1);
for(int i=0;i<5;i++)
{
lcd_write(lock[i]);
}
for(int i=0;i<4;i++)
{
lcd_write(mode[i]);
}
}
}
}
Thank you

You might also like