Write C code for Atmega32 programming for the following scenario:
Divide the last two digits of your ID with 9 and find the remainder. (last two digits of my id is: 58)
If the remainder is even, then use a 7-segment Common Anode Display. If the remainder is odd, then use a 7-segment Common Cathode Display.
Now connect PORTC pin no. 0 to 6 with the display and display the last digit of your ID.
#ifndef F_CPU
#define F_CPU 16000000UL // 16 MHz clock speed
#endif
#include <stdio.h>
#include <avr/io.h>
#include <util/delay.h>
#define PORT_7_SEGMENT PORTB
#define DDR_7_SEGMENT DDRB
void common_anode(int count){
switch (count)
{
case 0:
PORT_7_SEGMENT=0b10001000;
break;
case 1:
PORT_7_SEGMENT=0b10111110;
break;
case 2:
PORT_7_SEGMENT=0b00011001;
break;
case 3:
PORT_7_SEGMENT=0b00011100;
break;
case 4:
PORT_7_SEGMENT=0b00101110;
break;
case 5:
PORT_7_SEGMENT=0b01001100;
break;
case 6:
PORT_7_SEGMENT=0b01001000;
break;
case 7:
PORT_7_SEGMENT=0b10111100;
break;
case 8:
PORT_7_SEGMENT=0b00001000;
break;
case 9:
PORT_7_SEGMENT=0b00001100;
break;
}
}
void common_cathode(int count ){
switch (count)
{
case 0:
PORT_7_SEGMENT=0b01110111;
break;
case 1:
PORT_7_SEGMENT=0b01000001;
break;
case 2:
PORT_7_SEGMENT=0b1100110;
break;
case 3:
PORT_7_SEGMENT=0b11100011;
break;
case 4:
PORT_7_SEGMENT=0b11010001;
break;
case 5:
PORT_7_SEGMENT=0b10110011;
break;
case 6:
PORT_7_SEGMENT=0b10110111;
break;
case 7:
PORT_7_SEGMENT=0b01000011;
break;
case 8:
PORT_7_SEGMENT=0b11110111;
break;
case 9:
PORT_7_SEGMENT=0b11110011;
break;
}
}
int main(void)
{
int d;
printf(" ENTER LAST 2 DIGITS OF YOUR ID ");
scanf("%d", &n);
int remainder =d % 2;
if(remainder==0){
common_anode(remainder);
}else{
common_cathode(remainder);
}
DDRC = 0xFF; //Nakes PORTC as Output
while(1) //infinite loop
{
PORTC =0x5;
_delay_ms(2500);
PORTC= 0x00;
_delay_ms(2500);
PORTC =0x8;
_delay_ms(2500);
PORTC= 0x00;
PORTC =0x5;
PORTC =0x8;
_delay_ms(2500);
}
}
Comments
Leave a comment