08 - Lecture 6-Embedded
08 - Lecture 6-Embedded
0 0 I No Tri-State (High Z)
Output High
1 1 O No
(Source)
int main(void)
{
DDRD = DDRD | (1<<3); /* Make PD3 as output pin */
DDRD = DDRD & (~(1<<2)); /* Make PD2 as input pin */
PORTD = PORTD | (1<<2); /* Enable pull-up on PD2 by writing 1 to it */
int pin_status;
while(1)
{
pin_status = PIND & (1<<2); /*Read status of pin PD2 */
if(pin_status) /* Transmit status of pin PD2 on to pin PD3 to drive LED. */
{
PORTD = PORTD | (1<<3); /* Switch is open, pin_status = 1, LED is ON */
}
else
{
PORTD = PORTD & (~(1<<3)); /* Switch is closed, pin_status = 0, LED is OFF */
}
}
return 0;
}
Interfacing seven segment with Atmega32
• A seven-segment display is a set of seven bar-shaped LED (light-emitting
diode) elements, arranged to form a squared-off figure 8. It’s also the
most common, simple-to-use and cheap display.
int main(void)
{
char array[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
DDRA=0xff;
/* define LED port direction is output */
for(int i=0;i<10;i++)
{
PORTA= array[i]; /* write data on to the LED port */
_delay_ms(1000); /* wait for 1 second */
}
}
}
Program to Display 0 to 99 on Seven Segment
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
/* Replace with your application code */
int arr [10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
while (1)
{
int i;
int j;
DDRA=0xFF;
DDRB=0xFF;
for(i=0;i<=9;i++)
{
PORTA=arr[i];
{
for(j=0;j<=9;j++)
{
PORTB=arr[j];
_delay_ms(300);
}
}
}
}
}
7-segment with BCD 7-segment decoder
• Why to use driver (BCD decoder)
• Display driver requires only 4 pins of microcontroller to drive the 7-segment
display.
• We do not need to convert decimal numbers into 7 segment display format
(from 0-9).We just need to provide BCD (Binary Coded Decimal) numbers and
it will take care rest of seven segment conversion.
• No external Resistor is required.
• It makes easy to interface with microcontroller.
• 7-Segment Display Driver IC 7447
• 7447 is a 16 pin IC, used to drive common anode type 7-segment LED display.
• This driver IC accepts 4 line of BCD (Binary Coded Decimal) input data, and
drives 7-segments display directly.
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRA=0xff; /* define LED port direction is output */
PORTA= 0xff;
char array[]={0,1,2,3,4,5,6,7,8,9};
/* write BCD value for CA display from 0 to 9 */
while(1)
{
for(int i=0;i<10;i++)
{
PORTA = array[i];/* write data on to the LED port */
_delay_ms(1000); /* wait for 1 second */
}
}
}
#define F_CPU 16000000UL /* with input port push botton
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRA=0xFF; /* DDRAis output */
PORTA= 0x00;
DDRB=0x00; /* DDRB input */
PORTB= 0xFF;
switch(PINB)
{
case 1: PORTA=1; break;
case 2: PORTA=2; break;
case 3: PORTA=3; break;
case 4: PORTA=4; break;
case 5: PORTA=5; break;
case 6: PORTA=6; break;
case 7: PORTA=7; break;
case 8: PORTA=8; break;
case 9: PORTA=9; break;
defualt: PORTA=0;
_delay_ms(1000); }
}