|
|
View previous topic :: View next topic |
Author |
Message |
khollam.kamlesh33
Joined: 18 Jun 2010 Posts: 3
|
Problem regarding resetting of PIC 18f4520 |
Posted: Fri Jun 18, 2010 6:44 am |
|
|
Hi...
I am using a PIC18F4520 in my ROBOT. My ROBOT is a maze solver robot. When it reaches to its destination it stops, but within a second the PIC get reseted and robot again start traversing. I'm not getting the actual problem. Please reply as quickly as possible. |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Fri Jun 18, 2010 8:18 am |
|
|
With absolutely nothing to go on, the only thing I can suggest is a code problem. But, without seeing the code it is a wild guess!
Are you putting it to sleep? If so, make sure you disable all interrupts and then execute the sleep command. _________________ Google and Forum Search are some of your best tools!!!! |
|
|
khollam.kamlesh33
Joined: 18 Jun 2010 Posts: 3
|
|
Posted: Mon Jun 21, 2010 8:19 am |
|
|
I'm not putting it in sleep mode.
I will upload the code on 22nd june. |
|
|
khollam.kamlesh33
Joined: 18 Jun 2010 Posts: 3
|
|
Posted: Wed Jun 30, 2010 3:35 am |
|
|
#include <18F4520.h>
#include <Motor.h>
#include <RFC.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#ifndef MUSIC_NOTES
#define MUSIC_NOTES
// NOTE FREQUENCY
// Octave0 Octave1 Octave2 Octave3
const long C_NOTE[4] ={ 262, 523, 1047, 2093};
const long Db_NOTE[4] ={ 277, 554, 1109, 2217};
const long D_NOTE[4] ={ 294, 587, 1175, 2349};
const long Eb_NOTE[4] ={ 311, 622, 1245, 2489};
const long E_NOTE[4] ={ 330, 659, 1329, 2637};
const long F_NOTE[4] ={ 349, 698, 1397, 2794};
const long Gb_NOTE[4] ={ 370, 740, 1480, 2960};
const long G_NOTE[4] ={ 392, 784, 1568, 3136};
const long Ab_NOTE[4] ={ 415, 831, 1661, 3322};
const long A_NOTE[4] ={ 440, 880, 1760, 3520};
const long Bb_NOTE[4] ={ 466, 923, 1865, 3729};
const long B_NOTE[4] ={ 494, 988, 1976, 3951};
#endif
#define TONE_PIN PIN_B0
#define period 255
#define SIZE 25
const struct note
{
long tone;
long length;
} happy_bday[SIZE] = {
C_note[0],350, C_note[0],100, D_note[0],500, C_note[0],500, F_note[0],500, E_note[0],900,
C_note[0],350, C_note[0],100, D_note[0],500, C_note[0],500, G_note[0],500, F_note[0],900,
C_note[0],350, C_note[0],100, C_note[1],500, A_note[0],500, F_note[0],500, E_note[0],500, D_note[0],900,
Bb_note[0],350, Bb_note[0],100, A_note[0],500, F_note[0],500, G_note[0],500, F_note[0],1200};
void init_path();
void turn_left();
void turn_right();
void do_delay(int,int,int,int);
void generate_tone(long,long);
void main()
{
char ch;
int8 i=0;
//int8 k;
int1 dead_end=0;
int1 mode=0;
int1 corner,left_path,right_path,straight_path,left_adj,right_adj,straight_ahead;
left_path=0;
right_path=0;
straight_path=0;
left_adj=0;
right_adj=0;
straight_ahead=0;
corner=0;
set_tris_a(0X00);
set_tris_c(0x01);
set_tris_b(0X00);
set_tris_d(0XFF);
set_tris_e(0);
// output_low(PIN_B0);
config(period);
output_b(0);
if(input_state(PIN_C0)==0)
{
output_e(7);
delay_ms(1000);
output_e(0);
delay_ms(500);
init_path();
while(1)
{
corner=input_state(PIN_D1);
left_path=input_state(PIN_D2);
left_adj=input_state(PIN_D3);
right_adj=input_state(PIN_D4);
right_path=input_state(PIN_D5);
straight_path=input_state(PIN_D6);
straight_ahead=input_state(PIN_D7);
if(left_path && right_path && straight_path && left_adj && right_adj && straight_ahead && corner)
{
halt();
output_e(0);
delay_ms(1000);
i=0;
while(1)
{
ch=read_eeprom(i);
i++;
if (ch=='*')
{
break;
send('Z');
}
else
send(ch);
switch(ch)
{
case 'L':
output_high(PIN_E0);
output_low(PIN_E1);
output_low(PIN_E2);
break;
case 'R':
output_high(PIN_E2);
output_low(PIN_E1);
output_low(PIN_E0);
break;
case 'S':
output_high(PIN_E1);
output_low(PIN_E0);
output_low(PIN_E2);
break;
}
delay_ms(1000);
output_low(PIN_E0);
output_low(PIN_E1);
output_low(PIN_E2);
}
/*for(k=0; k<SIZE; ++k)
{
generate_tone(happy_bday[k].tone,happy_bday[k].length);
delay_ms(75);
} */
break;
}
if(left_path || right_path)
{
if (dead_end==0)
{
if(left_path)
{
output_high(PIN_E0);
output_low(PIN_E1);
output_low(PIN_E2);
turn_left();
write_eeprom(i,'L');
i++;
}
else if(straight_path)
{
output_high(PIN_E1);
output_low(PIN_E0);
output_low(PIN_E2);
straight();
delay_ms(100);
write_eeprom(i,'S');
i++;
}
else if(right_path)
{
output_high(PIN_E2);
output_low(PIN_E1);
output_low(PIN_E0);
turn_right();
write_eeprom(i,'R');
i++;
}
}
else
{
dead_end=0;
if(left_path)
{
output_high(PIN_E0);
output_low(PIN_E1);
output_low(PIN_E2);
turn_left();
if(read_eeprom(i-1)=='L')
{
i--;
write_eeprom(i,'S');
i++;
}
else if(read_eeprom(i-1)=='S')
{
i--;
write_eeprom(i,'R');
i++;
}
else if(read_eeprom(i-1)=='R')
{
i--;
write_eeprom(i,'*');
dead_end=1;
}
}
else if(straight_path)
{
output_high(PIN_E1);
output_low(PIN_E0);
output_low(PIN_E2);
straight();
delay_ms(100);
if(read_eeprom(i-1)=='L')
{
i--;
write_eeprom(i,'R');
i++;
}
else if(read_eeprom(i-1)=='S')
{
i--;
write_eeprom(i,'*');
dead_end=1;
}
}
else
{
output_high(PIN_E2);
output_low(PIN_E1);
output_low(PIN_E0);
turn_right();
i--;
write_eeprom(i,'*');
dead_end=1;
}
}
}
else
{
if((left_path || straight_path || right_path || left_adj || right_adj)==0)
{
output_low(PIN_E2);
output_low(PIN_E0);
output_low(PIN_E1);
while(!(input_state(PIN_D6)) || !(input_state(PIN_D7)))
{
//output_high(PIN_B0);
reverse();
delay_ms(50);
// output_low(PIN_B0);
//halt();
//delay_ms(50);
}
dead_end=1;
}
else if(left_adj)
left();
else if(right_adj)
right();
else
{
output_high(PIN_E1);
output_low(PIN_E0);
output_low(PIN_E2);
straight();
}
}
delay_ms(100);
// halt();
}
}
else
{
i=0;
while(1)
{
corner=input_state(PIN_D1);
left_path=input_state(PIN_D2);
left_adj=input_state(PIN_D3);
right_adj=input_state(PIN_D4);
right_path=input_state(PIN_D5);
straight_path=input_state(PIN_D6);
straight_ahead=input_state(PIN_D7);
if(left_path && right_path && straight_path && left_adj && right_adj && straight_ahead && corner)
{
halt();
while(1)
{
output_e(7);
delay_ms(100);
output_e(0);
delay_ms(100);
}
}
if(left_path || right_path)
{
ch=read_eeprom(i);
if (ch=='*')
break;
else
{
switch(ch)
{
case 'L':
output_high(PIN_E0);
output_low(PIN_E1);
output_low(PIN_E2);
turn_left();
break;
case 'R':
output_high(PIN_E2);
output_low(PIN_E1);
output_low(PIN_E0);
turn_right();
break;
case 'S':
output_high(PIN_E1);
output_low(PIN_E0);
output_low(PIN_E2);
straight();
delay_ms(100);
break;
}
i++;
}
}
else
{
if(left_adj)
left();
else if(right_adj)
right();
else
{
output_high(PIN_E1);
output_low(PIN_E0);
output_low(PIN_E2);
straight();
}
}
delay_ms(50);
}
}
while(1)
{
output_e(0);
}
}
void init_path()
{
int8 i=0;
while(i<20)
{
write_eeprom(i,'*');
i++;
}
}
void turn_left()
{
// output_high(PIN_B0);
while(input_state(PIN_D6))
{
left();
delay_ms(100);
}
while(!input_state(PIN_D6))
{
left();
delay_ms(100);
}
}
void turn_right()
{
//output_high(PIN_B0);
while(input_state(PIN_D6))
{
right();
delay_ms(100);
}
while(!input_state(PIN_D6))
{
right();
delay_ms(100);
}
// output_low(PIN_B0);
}
void do_delay(int ms_delay, int num_ms, int us_delay, int num_us)
{
int i;
for(i=0;i<num_ms;i++)
delay_ms(250);
delay_ms(ms_delay);
for(i=0;i<num_us;i++)
delay_us(250);
delay_us(us_delay);
}
void generate_tone(long frequency, long duration)
{
int32 total_delay_time; // in microseconds
long total_ms_delay_time, total_us_delay_time;
int num_us_delays, num_ms_delays, ms_delay_time, us_delay_time;
long num_periods;
total_delay_time = (1000000/frequency)/2-10; // calculate total delay time (10 for error)
total_ms_delay_time = total_delay_time/1000; // total delay time of ms
num_ms_delays = total_ms_delay_time/250; // number of 250ms delays needed
ms_delay_time = total_ms_delay_time%250; // left over ms delay time needed
total_us_delay_time = total_delay_time%1000; // total delay time of us (ms already acounted for)
num_us_delays = total_us_delay_time/250; // number of 250us delays needed
us_delay_time = total_us_delay_time%250; // left over us delay time needed
num_periods = ((int32)duration*1000)/(1000000/frequency);
while((num_periods--) != 0)
{
do_delay(ms_delay_time, num_ms_delays, us_delay_time, num_us_delays);
output_high(TONE_PIN);
do_delay(ms_delay_time, num_ms_delays, us_delay_time, num_us_delays);
output_low(TONE_PIN);
}
return;
} |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Wed Jun 30, 2010 7:02 am |
|
|
Could you edit that but put [ code ] [ /code ] tags at the beginning and end. It is a pain to try and read it without indentation. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Jun 30, 2010 7:03 am |
|
|
Sorry, but I get a headache reading your code. Not a good thing when you want a good response to your question.
First of all, use the 'code' buttons when posting code so the layout of your code is preserved. Don't post new code but go back to your previous posting and use the 'edit' function to modify it.
Second problem is that you have created a huge main function of 353 lines. Good coding practice is to keep functions small, maximum of about 40-50 lines. This is because people like me have limited memory capacity, we can only see the impact of a small piece of code. If the function becomes larger we don't see the details of how all parts interact with each other.
The trick to solve this is by breaking your large function in smaller functions with names expressing the functionality of the function.
Breaking up the function also removes another bad coding practice you used. The use of while(1) loops and then breaking out of the loop on a certain condition will work, but is difficult to follow. Easier is to call a function and repeat this for as long as the function returns TRUE. Example: Code: | while (not_hitting_wall()); |
Third problem is that your program is lacking comments. It costs me too much time to understand what each part is doing.
Not a problem, but just complicating things is your tone generator. Why are you using a custom delay function? The lowest tone frequency you have is 262Hz, a period of 3.8ms. The CCS function delay_us() can be called with an int16, a sufficient long 65ms.
The tone generator can then be reduced to: Code: | void generate_tone(long frequency, long duration)
{
int16 total_us_delay_time; // in microseconds
long num_periods;
total_us_delay_time = (1000000/frequency)/2-10; // calculate total delay time (10 for error)
num_periods = ((int32)duration*1000)/(1000000/frequency);
while((num_periods--) != 0)
{
delay_us(total_us_delay_time);
output_high(TONE_PIN);
delay_us(total_us_delay_time);
output_low(TONE_PIN);
}
} |
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|