Alexwonglik
Newbie level 5
Hi, I am writing a program to cycle LEDS using push button on STK600 powered by Atmega 2560. Two cases
Case 1: No button pushed - cycle LEDS all the way through
Case 2: Button pushed - cycle LEDS up to the button pressed
I use timer0 for delay in the design and Port B as output while Port A as input to read switch value
I can handle the case 1. However, I plan to use for loop to do case 2 but it is not successful. I want to use the read switch value to be the max number in the for loop. When I tried to implement the design and push the button, no LED is lit up. Here is the main.c file. It will be great to have any advice.
Case 1: No button pushed - cycle LEDS all the way through
Case 2: Button pushed - cycle LEDS up to the button pressed
I use timer0 for delay in the design and Port B as output while Port A as input to read switch value
I can handle the case 1. However, I plan to use for loop to do case 2 but it is not successful. I want to use the read switch value to be the max number in the for loop. When I tried to implement the design and push the button, no LED is lit up. Here is the main.c file. It will be great to have any advice.
Code:
/* wrapper for writing register */
void WriteReg(unsigned int addr, unsigned char val)
{
*((volatile unsigned char *)addr) = val; /*write value to register*/
}
/* wrapper for Reading register */
unsigned char ReadReg(unsigned int addr)
{
unsigned char val;
do
{
val = *((volatile unsigned char *)addr); /*read value from registers*/
}while( (val != *((volatile unsigned char *)addr)) ); /*if the value is the same from same register, don't need to read it again*/
return (val);
}
/*Initialize 8 bit Timer0*/
void timer_init(void)
{
/*Set the clcok in the Timer Control Register B with prescaler you want*/
WriteReg(TCCR0B, TC0_CK_1024);
/*Write the count you want in the Timer/Counter0 */
WriteReg(TCNT0, 256-LED_DELAY);
/*Clear Overflow bit in Timer/Counter 0 Interrupt Flag Register*/
WriteReg(TIFR0, TOV0);
}
void delay(void)
{
/*Initialize the timer*/
timer_init();
/* Poll the TIFR for an overflow in TOV0. That means the counter has expired. Or get interrupted */
while ( !(*((volatile unsigned char *)TIFR0) & TOV0));
}
int main(void)
{
unsigned char LED_Switch,mask,This_LED,LED_index;
unsigned int Button_match;
/*Make port B to be output*/
WriteReg(DDRB, 0xff);
while(1)
{
/*Read LED switches. Value for a pushed switch will be 0, all else will be 1*/
LED_Switch = ReadReg(PINA);
/*Set mask =1*/
mask = 1;
Button_match = 0;
for(LED_index=0; LED_index < 8; LED_index++)
{
/*If no switch is pushed, cyle thru all LEDS*/
if (LED_Switch == 0xff)
{
/*Clear bit for LED_Switch*/
This_LED = (LED_Switch) & mask;
/*writing 0's to this LED to turn on.Invert This_LED*/
WriteReg(PORTB,~This_LED);
/*Use timer delay*/
delay();
/*writing 1's to all LEDs at a time to turn off each LED*/
WriteReg(PORTB,LED_off);
/*Use timer delay*/
delay();
mask = mask << 1;
}
/*For everything else, For loop to check the switch value and toggle the LEDs*/
else if ((~LED_Switch) & Button_match)
{
/*Make This_LED to be 0x01*/
This_LED = (LED_off & mask);
for(LED_index=0; LED_index < Button_match; LED_index++)
{
/*writing 0's to this LED to turn on*/
WriteReg(PORTB,~This_LED);
/*Use timer delay*/
delay();
/*writing 1's to all LEDs at a time to turn off each LED*/
WriteReg(PORTB,LED_off);
/*Use timer delay*/
delay();
/*Shift This_LED by 1 to light up next LED*/
This_LED = This_LED << 1;
}
}
Button_match++;
}
}
return(0);
}