This circuit is about atmega16L:
PORTA is connected to switches.
PORTB is connected to two 7segments.
PORTC is connected to on led on pin 2 for testing.
PORTD is connected to two switch transistors to choice which 7segment to work.
The code is:
Code:
#include <util/delay.h>
#include <avr/io.h>
#define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT))
#define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT))
#define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT))
void makeit (int my)
{
switch (my)
{
case 0 :
PORTB = ~(0b01111110);
break;
case 1:
PORTB = ~0b00010010;
SETBIT(PORTC,2);
break;
case 2:
PORTB = ~0b10111101;
break;
case 3:
PORTB = ~0b10110111;
break;
case 4:
PORTB = ~0b11010011;
break;
case 5:
PORTB = ~(0b11100111);
break;
case 6:
PORTB = ~0b11101111;
break;
case 7:
PORTB = ~0b00110010;
break;
case 8:
PORTB = ~0b11111111;
SETBIT(PORTC,2);
break;
case 9:
PORTB = ~0b11110111;
break;
}
}
int main(void)
{
DDRA=0x00;
DDRB=0xFF;
DDRC=0xFF;
DDRD=0xFF;
int num=00;
int ten;
int one;
PORTB=0xFF;
PINA=0b11111111;
PORTA=0xFF;
do
{
if(PINA==0b11111110)num=01;
else if(PINA==0b11111101)num=12;
else if(PINA==0b11111011)num=23;
else if(PINA==0b11110111)num=34;
else if(PINA==0b11101111)num=45;
else if(PINA==0b11011111)num=56;
else if(PINA==0b10111111)num=67;
else if(PINA==0b01111111)num=78;
else PINA=0b11111111;
SETBIT(PORTC,2);
_delay_ms(1000);
CLEARBIT(PORTC,2);
_delay_ms(1000);
} while (PINA =0b11111111);
do
{
SETBIT(PORTC,2);
asm("NOP");
} while (PINA!=0xFF);
while(1)
{
if(PINA==0b11111110) num++;
//else if(PORTA==0b01111111)num=89;
else if(PINA==0b11111101)num--;
ten=num/10;
one=num%10;
SETBIT(PORTD,4);
makeit(ten);
_delay_ms(10);
CLEARBIT(PORTD,4);
SETBIT(PORTD,1);
makeit(one);
_delay_ms(10);
CLEARBIT(PORTD,1);
}
}
It should work like this :
1- Nothing is displayed on the 7segments , and the led begins to light on and off.
2- When I press a bottom on any of portA it should get out of the first while loop and keeps the led turned on and a number is saved in the integer num and the led turns off so it is on the second do while loop.
3- When I stop pressing the bottom the number appears on the 7segment and I can increase or decrease it when I press the bottom on PINA,0 or PINA,1.
The problem is:
1- The operation execution is still stuck in the first loop even when I press any bottom.
2- if I skipped the first do while loop , the increment and decrement doesn’t appear working good. Is it because it is increasing too fast ? what is the solution?
3- also I’m having some warning:
• #warning "F_CPU not defined for <util/delay.h>"
• #warning "Compiler optimizations disabled; functions from <util/delay.h> won't work as designed"
• large integer implicitly truncated to unsigned type
How can I solve all of these problems?
Thanks in advanced.