0% found this document useful (0 votes)
31 views18 pages

ATMEGA32 Device Interfacing

Uploaded by

Free Use
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
31 views18 pages

ATMEGA32 Device Interfacing

Uploaded by

Free Use
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 18

ATMEGA32 Device

Interfacing
Prof. Pratik Gohel
Assistant Professor
Electronics & Communication Department
Government Engineering College Bhavnagar
ATMega32 how to set and reset relay
via digital buttons
• In this example, I use digital inputs outputs of ATMega32 to
control a relay. Relay has an advantage of driving alternative
that the microcontroller could do that directly.
• The CPU clocks at 16 MHz, supplied at +5 V DC. PC0 of PortC
is a digital output, driving a relay. The relay coil work at +5V.
You can choose another voltage rating.
• PA0 is an input, connected to a push button sw1. When the
button is pressed, PC0 become set.
• PA7 is an input, connected to a push button sw2. When the
button is pressed, PC0 become reset.
Program
#include <avr/io.h>
#define setButton (PINA&0x01)
#define resetButton (PINA&0x80)
int main(void)
{
DDRA=0x7E; //PA0 and PA7 are input
PORTA=0x81; //Set PA0 and PA7 High
DDRC=0xFF;
/* Replace with your application code */
while (1)
{
if (setButton==0) PORTC=0x01;
if (resetButton==0) PORTC=0x00;
}
}
ATMega32 AVR digital input output example in Atmel Studio 7 (youtube.com)
ATMega32 controlling DC Motor
direction via buttons
• In this example I use a single Port of PORTC accepting the
digital input from button, and output the control to DC motor.
• I use L293 for controlling the direction, and also driving the
motor at higher voltage and higher current at +12 V.
while (1)
#include <avr/io.h>
{
#define F_CPU 16000000UL
if (forward==0)
#include "util/delay.h"
{
#define forward (PINC&0b10000000)
PORTC=stop_running;
#define stop (PINC&0b01000000)
_delay_ms(250);
#define backward (PINC&0b00100000)
PORTC=run_forward;
//OUTPUT DATA FOR MOTOR
}
#define run_forward 0b11100001
else if (stop==0)
#define run_backward 0b11100010
{
#define stop_running 0b11100000
PORTC=stop_running;
_delay_ms(250);
PORTC=stop_running;
int main(void)
}
{
else if (backward==0)
DDRC=0b00011111; //PINC7.5 INPUT
{
PORTC=0b11100000; //PC7.5 SET TO HIGH
PORTC=stop_running;
_delay_ms(250);
PORTC=run_backward;
}
}
}
Interfacing ATMega32 To Seven Segment Display
• The programming for is very simple. It could represent numbers
and some English letters.
• It come with two terminology, common cathode type and common
anode type. But deciding which type to use depends on the
application. In this example, I take only common cathode type.
• Since it made of seven LED segment, we just turn on any LED to
display digit or letter. For example, turning on LED segment B
and C make digit "1" to display.
• In programming, they commonly use hexadecimal equivalent to
the SSD output.
• For the common cathode type display the hex code represent the
digit from 0 to 9 and from A to F is shown below:
• Each LED segment made of from LED, requiring around 2 V
and ~10 mA of current. Typically, the digital output from
microcontroller drives a voltage of +5V TTL. So we need to cut
down the voltage from microcontroller to ~2 V via resistor.
#include <avr/io.h> while (1)
#define F_CPU 16000000UL {
#include "util/delay.h" if (btnPressed) //CHECK IF PD0 IS
PRESSED
//SET THE INPUT BUTTON FOR COUNTING
{
#define btnPressed ((PIND&0x01)==0) btnCount+=1; //Increase The Counter
int main(void) _delay_ms(250);
{ if(btnCount>15) btnCount=0;
//RESET COUNTER WHEN IT IS OVER 15
//SEVEN SEGMENT MAP FOR COMMON
}
//CATHODE TYPE SEVEN SEGMENT DISPLAY PORTC=ssd[btnCount]; //OUTPUT TO SSD
unsigned char }
ssd[16]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x }
7D,
0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0
x71};
unsigned char btnCount=0;
Interfacing ATMega32 To Seven Segment D
DDRC=0xFF; //PORTC IS FOR OUTPUT TO SEVEN isplay (youtube.com)
SEGMENT
DDRD=0xFE; //PORTD PD0 IS USED FOR INPUT
COUNTING
PORTD=0x01; //SET PD0 TO HIGH FOR PULLING
UP
Controlling the uni-polar stepper
motor with ATMega32
• Uni-polar stepper motor is kind of brush-less DC motor that convert electrical pulse to mechanical
movement. Comparing to bipolar stepper motor, it's easy to control but with lower torque. Stepper
motor give a higher precision stepping than simple brushed DC motor. It's commonly use in
photocopier machine, numerical control etc.
• It is made of two distinct coils. Each coil divided into two pieces by a common centered tap.
• We can rotate by two method, half stepping and full stepping.
Half Stepping
• Half stepping is very easy to implement. Since the motor made
of four coil, we just active each coil one by one. For Example,
we activate A1 Coil for a given millisecond and then turn A1 off.
After that we do this to the next A2 coil.
• This stepping method could be easily implemented using a
synchronous digital circuit.
• The timing diagram below used for stepping counter clock wise:
• The time diagram below used for stepping clock wise:
#include <avr/io.h> int main(void)
#define F_CPU 16000000UL {
#include "util/delay.h" DDRC=0x0F; //PC0.3 ARE OUTPUT
#define stepTime 50
PORTC=0x00; //Clear PortC
void stepBackward() DDRD=0x7F; //PD7 IS INPUT
{ PORTD=0x80; //PULLUP PD7 HIGH
PORTC=0x01; while (1)
_delay_ms(stepTime); {
PORTC=0x02; if(PIND&0x80)
_delay_ms(stepTime); stepBackward();
PORTC=0x04; else
_delay_ms(stepTime);
stepForward();
PORTC=0x08;
_delay_ms(stepTime); }
} }
void stepForward()
{
PORTC=0x08;
_delay_ms(stepTime);
PORTC=0x04;
_delay_ms(stepTime);
PORTC=0x02;
_delay_ms(stepTime);
PORTC=0x01;
_delay_ms(stepTime);
}
Full Stepping
• Full stepping give the motor to maximize its output torque. For
this stepping, we must active two coil at the same time to make
one step.
• The timing diagram below gives the full step clock wise rotation:

• The timing diagram below gives the full step counter clock wise
rotation:
#include <avr/io.h> int main(void)
{
#define F_CPU 16000000UL DDRC=0x0F; //PC0.3 ARE OUTPUT
#include "util/delay.h"
PORTC=0x00; //Clear PortC
#define stepTime 50
void stepForward() DDRD=0x7F; //PD7 IS INPUT
{ PORTD=0x80; //PULLUP PD7 HIGH
PORTC=0b1001; while (1)
_delay_ms(stepTime); {
PORTC=0x0011; If(PIND&0x80)
_delay_ms(stepTime); stepBackward();
PORTC=0b0110; else
_delay_ms(stepTime);
stepForward();
PORTC=0b1100;
_delay_ms(stepTime); }
} }
void stepBackward()
{
PORTC=0b1100;
_delay_ms(stepTime);
PORTC=0b0110;
_delay_ms(stepTime);
PORTC=0b0011;
_delay_ms(stepTime);
PORTC=0b1001;
_delay_ms(stepTime);
}

You might also like