malli12
Newbie level 2
Hi al,
Im working on a bot. It works in 2 modes : Manual and Auto.
In manual mode, instructions are given using the mobile. These instructions are stored in an array along with time duration for that instruction.
In auto mode, the bot starts retrieving the directions n timers from the array n traces the same path. When i press number 9 on the mobile, auto mode takes over. Here;s the program.
#include<avr/io.h>
#include<stdio.h>
uint8_t lim;
uint8_t action[100];
uint16_t time[100];
int autotrip(void)
{
uint8_t j=1;
uint8_t k,l;
while (1)
{
TCCR1B=0x00;
if(j==lim) {j=1;}
k=action[j];
OCR1A=time[j];
j=j+1;;
l=k & 0x0F;
switch (l)
{
case 0x02: //if I/P is 0x02
{
TCCR1B=0x05;
TCNT1=0x00;
PORTB=0x80;
PORTD=0x20;
loop_until_bit_is_set(TIFR,4);
break;
}
case 0x08: //if I/P is 0x08
{
TCCR1B=0x05;
TCNT1=0x00;
PORTB=0x00;
PORTD=0xC0;
loop_until_bit_is_set(TIFR,4);
break;
}
case 0x04: //if i/p is 0x04
{
TCCR1B=0x05;
TCNT1=0x00;
PORTB=0x00;
PORTD=0x20;
loop_until_bit_is_set(TIFR,4);
break;
}
case 0x06: // if i/p is 0x06
{
TCCR1B=0x05;
TCNT1=0x00;
PORTB=0x80;
PORTD=0x00;
loop_until_bit_is_set(TIFR,4);
break;
}
case 0x05: //if i/p is 0x05
{
TCCR1B=0x05;
TCNT1=0x00;
PORTB=0x80;
PORTD=0xFF;
loop_until_bit_is_set(TIFR,4);
break;
}
case 0x01: //if i/p is 0x01
{
TCCR1B=0x05;
TCNT1=0x00;
PORTB=0x10;
loop_until_bit_is_set(TIFR,4);
break;
}
case 0x03: //if i/p is 0x03
{
TCCR1B=0x05;
TCNT1=0x00;
PORTB=0x20;
loop_until_bit_is_clear(TIFR,4);
break;
}
} //end of switch
} //end of while
return 1;
} //end of program
int main(void)
{
uint8_t key,h;
uint8_t i=1;
//input
PORTB=0x00;
DDRB=0xF0;
DDRD=0xFF;
while (1)
{
key =PINB;
h=key & 0x0F;
action=h;
switch (h)
{
case 0x09:
{
lim=i;
time[i-1]=TCNT1;
TCCR1B=0x00;
PORTB=0x80;
PORTD=0xFF;
autotrip();
}
break;
case 0x02: //if I/P is 0x02
{
TCCR1B=0x00;
time[i-1]=TCNT1;
i++;
PORTB=0x80;
PORTD=0x20; // Forward
TCCR1B=0x05;
TCNT1=0x00;
break;
}
case 0x08: //if I/P is 0x08
{
TCCR1B=0x00;
time[i-1]=TCNT1;
i++;
PORTB=0x00;
PORTD=0xC0; //reverse
TCCR1B=0x05;
TCNT1=0x00;
break;
}
case 0x04: //if i/p is 0x04
{
TCCR1B=0x00;
time[i-1]=TCNT1;
i++;
PORTB=0x00;
PORTD=0x20; // Left turn
TCCR1B=0x05;
TCNT1=0x00;
break;
}
case 0x06: // if i/p is 0x06
{
TCCR1B=0x00;
time[i-1]=TCNT1;
i++;
PORTB=0x80;
PORTD=0x00; // Right turn
TCCR1B=0x05;
TCNT1=0x00;
break;
}
case 0x05: //if i/p is 0x05
{
TCCR1B=0x00;
time[i-1]=TCNT1;
i++;
PORTB=0x80;
PORTD=0xFF; // motor stop
TCCR1B=0x05;
TCNT1=0x00;
break;
}
}
//end of switch
} // end of if
//end of while
return 1;
} // end of program
----------------------------------
when 9 is pressed the control goes to autotrip().
Im using atmega16.
The problem im facing is:
1. during simulation there is no change in value of TCNT1.
2. The array implementation works .i.e. the sequence gets repeated as given in manual in mode.
Since it involves timers, i thought testing it on hardware would be a better option. i downloaded it onto the chip n tried with the hardware. the manual mode works fine, no prob.
3. But wen i press 9, the bot still keeps moving the direction of the key pressed before 9.
4. Timers is not getting implemented.
i worked with timers separately on hardware, it worked fine.
int main(void)
{
DDRA=0X0F;
PORTA=0X0F;
TCCR1A=0X00;
TCCR1B=0X05; //PRE SCALER 1024
TCNT1=0X00;
loop_until_bit_is_clear(PINA,7); //USING PA.7 AS A SWITCH
OCR1A=TCNT1;
YIMSK(1<<OCIE1A);
PORTA=0X00;
sei();
while(1);
}
ISR (TIMER1_COMPA_vect)
{
PORTA=~PORTA;
TIFR=0X10; // CLEARING OCF1A FLAG
TCNT1=0X00;
}
guys i need your help solving the timers prob. i think im missing some logic which is not flashing in my head.
---------- Post added at 11:31 ---------- Previous post was at 11:29 ----------
the prog luks big, but can just go through two case statements n you'll get the logic. its repeated logic for all other cases
---------- Post added at 11:33 ---------- Previous post was at 11:31 ----------
the prog luks big, but can just go through two case statements n you'll get the logic. its repeated logic for all other cases
Im working on a bot. It works in 2 modes : Manual and Auto.
In manual mode, instructions are given using the mobile. These instructions are stored in an array along with time duration for that instruction.
In auto mode, the bot starts retrieving the directions n timers from the array n traces the same path. When i press number 9 on the mobile, auto mode takes over. Here;s the program.
#include<avr/io.h>
#include<stdio.h>
uint8_t lim;
uint8_t action[100];
uint16_t time[100];
int autotrip(void)
{
uint8_t j=1;
uint8_t k,l;
while (1)
{
TCCR1B=0x00;
if(j==lim) {j=1;}
k=action[j];
OCR1A=time[j];
j=j+1;;
l=k & 0x0F;
switch (l)
{
case 0x02: //if I/P is 0x02
{
TCCR1B=0x05;
TCNT1=0x00;
PORTB=0x80;
PORTD=0x20;
loop_until_bit_is_set(TIFR,4);
break;
}
case 0x08: //if I/P is 0x08
{
TCCR1B=0x05;
TCNT1=0x00;
PORTB=0x00;
PORTD=0xC0;
loop_until_bit_is_set(TIFR,4);
break;
}
case 0x04: //if i/p is 0x04
{
TCCR1B=0x05;
TCNT1=0x00;
PORTB=0x00;
PORTD=0x20;
loop_until_bit_is_set(TIFR,4);
break;
}
case 0x06: // if i/p is 0x06
{
TCCR1B=0x05;
TCNT1=0x00;
PORTB=0x80;
PORTD=0x00;
loop_until_bit_is_set(TIFR,4);
break;
}
case 0x05: //if i/p is 0x05
{
TCCR1B=0x05;
TCNT1=0x00;
PORTB=0x80;
PORTD=0xFF;
loop_until_bit_is_set(TIFR,4);
break;
}
case 0x01: //if i/p is 0x01
{
TCCR1B=0x05;
TCNT1=0x00;
PORTB=0x10;
loop_until_bit_is_set(TIFR,4);
break;
}
case 0x03: //if i/p is 0x03
{
TCCR1B=0x05;
TCNT1=0x00;
PORTB=0x20;
loop_until_bit_is_clear(TIFR,4);
break;
}
} //end of switch
} //end of while
return 1;
} //end of program
int main(void)
{
uint8_t key,h;
uint8_t i=1;
//input
PORTB=0x00;
DDRB=0xF0;
DDRD=0xFF;
while (1)
{
key =PINB;
h=key & 0x0F;
action=h;
switch (h)
{
case 0x09:
{
lim=i;
time[i-1]=TCNT1;
TCCR1B=0x00;
PORTB=0x80;
PORTD=0xFF;
autotrip();
}
break;
case 0x02: //if I/P is 0x02
{
TCCR1B=0x00;
time[i-1]=TCNT1;
i++;
PORTB=0x80;
PORTD=0x20; // Forward
TCCR1B=0x05;
TCNT1=0x00;
break;
}
case 0x08: //if I/P is 0x08
{
TCCR1B=0x00;
time[i-1]=TCNT1;
i++;
PORTB=0x00;
PORTD=0xC0; //reverse
TCCR1B=0x05;
TCNT1=0x00;
break;
}
case 0x04: //if i/p is 0x04
{
TCCR1B=0x00;
time[i-1]=TCNT1;
i++;
PORTB=0x00;
PORTD=0x20; // Left turn
TCCR1B=0x05;
TCNT1=0x00;
break;
}
case 0x06: // if i/p is 0x06
{
TCCR1B=0x00;
time[i-1]=TCNT1;
i++;
PORTB=0x80;
PORTD=0x00; // Right turn
TCCR1B=0x05;
TCNT1=0x00;
break;
}
case 0x05: //if i/p is 0x05
{
TCCR1B=0x00;
time[i-1]=TCNT1;
i++;
PORTB=0x80;
PORTD=0xFF; // motor stop
TCCR1B=0x05;
TCNT1=0x00;
break;
}
}
//end of switch
} // end of if
//end of while
return 1;
} // end of program
----------------------------------
when 9 is pressed the control goes to autotrip().
Im using atmega16.
The problem im facing is:
1. during simulation there is no change in value of TCNT1.
2. The array implementation works .i.e. the sequence gets repeated as given in manual in mode.
Since it involves timers, i thought testing it on hardware would be a better option. i downloaded it onto the chip n tried with the hardware. the manual mode works fine, no prob.
3. But wen i press 9, the bot still keeps moving the direction of the key pressed before 9.
4. Timers is not getting implemented.
i worked with timers separately on hardware, it worked fine.
int main(void)
{
DDRA=0X0F;
PORTA=0X0F;
TCCR1A=0X00;
TCCR1B=0X05; //PRE SCALER 1024
TCNT1=0X00;
loop_until_bit_is_clear(PINA,7); //USING PA.7 AS A SWITCH
OCR1A=TCNT1;
YIMSK(1<<OCIE1A);
PORTA=0X00;
sei();
while(1);
}
ISR (TIMER1_COMPA_vect)
{
PORTA=~PORTA;
TIFR=0X10; // CLEARING OCF1A FLAG
TCNT1=0X00;
}
guys i need your help solving the timers prob. i think im missing some logic which is not flashing in my head.
---------- Post added at 11:31 ---------- Previous post was at 11:29 ----------
the prog luks big, but can just go through two case statements n you'll get the logic. its repeated logic for all other cases
---------- Post added at 11:33 ---------- Previous post was at 11:31 ----------
the prog luks big, but can just go through two case statements n you'll get the logic. its repeated logic for all other cases