Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help needed in solving the Timers problem

Status
Not open for further replies.

malli12

Newbie level 2
Joined
Apr 1, 2011
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,328
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
 

so if I understtod your problem correctly, your bot works absolutely fine in manual mode but as soonas you switch over to auto mode, it keeps going according to the last given input?

if this is the case, then I suspect that because you have not put in any kind of delay machanism in your code, the 100 array spaces are filled up very quickly and are overwritten very fast.

To avoid this, I suggest that you enter a particular case and start the timer as soon as you enter it. Start a while loop to wait for a change of state and as soon as state has changed, stop the timer and store its value. Move to the next switch case and repeat the same, except for case 9.
 

@raco_rage.
the 100 array space wont get filled so quickly. i using 1Mhz clock n pre scaler value of 1024, so i get a maximum time of around 60 seconds. n moreover the array index gets incremented for each key pressed. it will store 100 directions/instructions, same with timers array.i checked in the watch window also, it shows the stored sequence.
i can surely say that its entering autotrip() because, after pressing 9, if i press any other key, the bot wont respond.
I making some mistake with the timers.
say if the keys pressed are :2,4,6,8. when it switches to autotrip(), all these sequences get executed. but since there is no values stored in timer, all the sequences get executed rapidly leaving us to think that the bot is only executing the last key pressed before 9.
hope you are getting my point..
 

60 seconds is the value in which your 16-bit timer register would overflow. But your controller is still working at around 1kHz clock cycle, which is fast enough when interfacing with manual inputs. Also, you have to consider bouncing contacts.
Secondly, I dont see anywhere in the code where you start the timer.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top