foxbrain
Full Member level 2
hi:
i'm programming a simple program on atmega 16L:
PORTB is connected to 7 segment and PORTA to bottoms......
the code is :
#include <util/delay.h>
#include <avr/io.h>
int main()
{
#define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT))
#define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT))
#define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT))
DDRC=0xFF;
DDRA=0x00;
DDRB=0xFF;
PORTB=0xFF;
PORTA=0xFF;
uint8_t num;
for(;
{
CLEARBIT(PORTC,2);
PORTB=0XFF;
num=0xFF;
num &=PINA;
switch (num)
{
case 0b11011111:
PORTB &=~0b10111100;
asm("NOP");
_delay_ms(100);
case 0b11101111:
PORTB &=~0b10110110;
asm("NOP");
_delay_ms(100);
case 0b11110111:
PORTB &=~0b11010010;
asm("NOP");
_delay_ms(100);
case 0b11111011:
PORTB &=~(0b11100110);
asm("NOP");
_delay_ms(100);
case 0b11111101:
PORTB &=~0b11101110;
asm("NOP");
_delay_ms(100);
case 0b11111110:
PORTB &=~0b00110010;
asm("NOP");
_delay_ms(100);
case 0b00111111:
PORTB &=~0b11111110;
asm("NOP");
_delay_ms(100);
case 0b01011111:
PORTB &=~0b11110110;
asm("NOP");
_delay_ms(100);
case 0b11111111:
PORTB &=0b11111111;
asm("NOP");
_delay_ms(100);
}
if (num==0b01111111)
{PORTB &=~(0b01111110);
SETBIT(PORTC,2);
asm("NOP");
_delay_ms(100);}
else if(num==0b10111111)
{PORTB &=~0b00010010;
asm("NOP");
_delay_ms(100);}
else {PORTB &=0b11111111;
asm("NOP");
_delay_ms(100);}
}
return 0;
}
when a make the case of num == 0b01111111 or 0b10111111 or 0b11111111 (in if & else command) everything seems going good.
but when i make the any other case (in switch & case command) i'm not sure what it makes on the output of the 7 segment because it's to short but when i want to press any other bottom the atmega seems it stopped working or finished working (it doesn't respond for anything).....
so what's wrong with that??????
is there different between if & else and switch & case ???
thanks in advance......
i'm programming a simple program on atmega 16L:
PORTB is connected to 7 segment and PORTA to bottoms......
the code is :
#include <util/delay.h>
#include <avr/io.h>
int main()
{
#define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT))
#define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT))
#define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT))
DDRC=0xFF;
DDRA=0x00;
DDRB=0xFF;
PORTB=0xFF;
PORTA=0xFF;
uint8_t num;
for(;
{
CLEARBIT(PORTC,2);
PORTB=0XFF;
num=0xFF;
num &=PINA;
switch (num)
{
case 0b11011111:
PORTB &=~0b10111100;
asm("NOP");
_delay_ms(100);
case 0b11101111:
PORTB &=~0b10110110;
asm("NOP");
_delay_ms(100);
case 0b11110111:
PORTB &=~0b11010010;
asm("NOP");
_delay_ms(100);
case 0b11111011:
PORTB &=~(0b11100110);
asm("NOP");
_delay_ms(100);
case 0b11111101:
PORTB &=~0b11101110;
asm("NOP");
_delay_ms(100);
case 0b11111110:
PORTB &=~0b00110010;
asm("NOP");
_delay_ms(100);
case 0b00111111:
PORTB &=~0b11111110;
asm("NOP");
_delay_ms(100);
case 0b01011111:
PORTB &=~0b11110110;
asm("NOP");
_delay_ms(100);
case 0b11111111:
PORTB &=0b11111111;
asm("NOP");
_delay_ms(100);
}
if (num==0b01111111)
{PORTB &=~(0b01111110);
SETBIT(PORTC,2);
asm("NOP");
_delay_ms(100);}
else if(num==0b10111111)
{PORTB &=~0b00010010;
asm("NOP");
_delay_ms(100);}
else {PORTB &=0b11111111;
asm("NOP");
_delay_ms(100);}
}
return 0;
}
when a make the case of num == 0b01111111 or 0b10111111 or 0b11111111 (in if & else command) everything seems going good.
but when i make the any other case (in switch & case command) i'm not sure what it makes on the output of the 7 segment because it's to short but when i want to press any other bottom the atmega seems it stopped working or finished working (it doesn't respond for anything).....
so what's wrong with that??????
is there different between if & else and switch & case ???
thanks in advance......