|
|
View previous topic :: View next topic |
Author |
Message |
mhjccsinfo
Joined: 17 Oct 2011 Posts: 23 Location: Iran-tehran
|
coming out of loop |
Posted: Thu Nov 29, 2012 3:28 am |
|
|
Hello guys
Some strange problem occurred for me. The program automatically comes out of loops. For example from for(;;); and from while(1); and from:
Code: |
while(1)
{
delay_ms(10);
}
|
MCU is 16f877 and 18f425.
All timers and interrupts are disabled.
Sometimes (from LCD reacts ) it seems that MCU jump from a program part to another part!
Is it noise problem? I have no special noise source in my place.
Is it ICSP problem? of course I have had this problem without that.
Waiting for your help - thanks |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9217 Location: Greensville,Ontario
|
|
Posted: Thu Nov 29, 2012 5:57 am |
|
|
Without seeing the entire program, my guess is you might have the WDT enabled ?
Could be static electricity ?? Bad wiring, noisey power supply, EMI,?
Show us all the code,compiler version.
is the board homemade,commercial,PCB,whiteboard,???
We nee more information to better help you.
hth
jay |
|
|
mhjccsinfo
Joined: 17 Oct 2011 Posts: 23 Location: Iran-tehran
|
|
Posted: Sat Dec 01, 2012 1:37 am |
|
|
my program has 600 lines without it's libraries (with them it's above 1500 lines) so i can't put them here
but it's my main
Code: | void main(void)
{
char select,select2,select3=0;//for choosing buttons ( answer! )
unsigned long s_v;//for saving step_motor_value to the eeprom and also reading from there
char str[40];//general variable for putting every writted text in it
struct coordinate bp_back,bp_1,bp_2,bp_3,bp_4,bp_5,bp_6,bp_7,bp_8;//all of buttons
struct coordinate window;//for frames
saya_prim();
while(read_eeprom(door_status)!=door_open)
{
door_check();
saya_announce();
begining://-------------------------------------------------------------
saya_mainmenu(&str,&window,&select,&bp_1,&bp_2);
auto_prof://--------------------------------------------------------------
switch(select)
{
case professional:saya_professional(&str,&window,&select,&bp_back,&bp_1);break;
case automatic :saya_automatic(&str,&window,&select,&bp_back,&bp_1,&bp_2,&bp_3);break;
default :show_initialization();break;
}
auto_matic://--------------------------------------------------------------
valve_off(valv_diam);
valve_off(valv_vacu);
valve_off(valv_powd);
switch(select)
{
case back :select=automatic;goto begining;break;
//case auto_diamond:saya_diamond();break;
//case auto_vacuum : saya_vacuum();break;
case auto_powder : saya_powder(&str,&window,&select,&bp_back,&bp_1,&bp_2,&bp_3,&bp_4);break;
default :show_initialization();sprintf(str,"default",);show_text(1,1,str);break;
}
problem_choosing://-------------------------------------------------------
switch(select)
{
case pwdr_back :select=automatic;goto auto_prof;break;
case pwdr_reju :saya_body(&str,&select2,&window,&bp_back,&bp_1,&bp_2,&bp_3,&bp_4,&bp_5,&bp_6,&bp_7,&bp_8);if(select2==body_back){select=auto_powder;goto auto_matic;}saya_reju(&str,&window,&select2,&bp_back,&bp_1,&bp_2);break;
/* case pwdr_acne :saya_body();saya_acne();break;
case pwdr_hype :saya_body();saya_hype();break;
case pwdr_oxyg :saya_body();saya_oxyg();break;*/
default :show_initialization();sprintf(str,"default",);show_text(1,1,str);break;
}
//select=auto_powder;goto auto_matic;
saya_work(&str,&window,&bp_back,&bp_1,&bp_2,&select3);
//str="in the main";
//saya_problem(str);
if(select3==work_back)
{
select=pwdr_reju;
goto problem_choosing;
}
while(true);
delay_ms(10);
}
} |
and also
Code: |
void saya_prim (void)
{
//getting primary values for the pic micro setups
disable_interrupts(INT_TIMER1);
disable_interrupts(global);
port_b_pullups(false);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_internal|T1_DIV_BY_1);
setup_timer_2(T2_DISABLED,0,1);
setup_wdt(WDT_OFF);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_low_volt_detect(FALSE);
setup_oscillator(False);
show_initialization();
//show_pic();
//while(1);
//delay_ms(2500);
show_initialization();
//checking the furst time
if(read_eeprom(f_t_var)!=first_time)
{
write_eeprom(f_t_var,first_time);
//-------------getting primary values to eeprom parameters----------
write_eeprom(usage_counter,first_time);
write_eeprom(door_status,first_time);
write_long_eeprom(step_value,step_value_prim);
write_long_eeprom(step2_value,step2_value_prim);
}
//giving value to the usage counter
else
{
write_eeprom(usage_counter,read_eeprom(usage_counter)+1);
}
//seeing if the door has gone to the open state even , or not.
if(read_eeprom(door_status)==door_open)
{
char str[40];
show_initialization();
str="Internal Problem,please call producer";
show_text(1,1,str);
}
}
|
Version is 4.105 pcw
I don't know what is EMI.
Board is breadboard.
thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19481
|
|
Posted: Sat Dec 01, 2012 2:40 am |
|
|
Post your fuses.
Realise that if:
'read_eeprom(door_status)' does equal 'door_open'
The code will either hang forever (if the watchdog is not enabled), or restart...
Do some basic searching on EMI. Electro magnetic interference. Seriously unless you have been _very_ careful on your layout with breadboard, and are decoupling carefully, this could well be your problem.
Best Wishes |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sat Dec 01, 2012 3:26 am |
|
|
The code snippet in original post hasn't to do with the real application. In so far the problem description
is meaningless. In the real application, jumping out of the loop is an intended behaviour. It would need
more detailed explanation why you think that the processor (or compiler) is behaving different than
commanded by your actual code.
For the time beeing, I prefer to assume that your code is behaving as written. This might be different
from your intentions. |
|
|
mhjccsinfo
Joined: 17 Oct 2011 Posts: 23 Location: Iran-tehran
|
|
Posted: Sat Dec 01, 2012 5:17 am |
|
|
1. Thanks for your reply.
2. About EEPROM: my project has many things for considering as problem(like eeprom , timer , E-valve , Step motor , 2 adaptor (switching 12 volts and transformer based 9 volt+7805),ISCP .... BUT I have disabled them in my program and separate it from my circuit and then I test it again .(except that 9 volts adapter power supply).
3. Fuses are these:
Code: |
#DEVICE ADC=10
#include <t6963.h>
#include <t6963.c>
#include <string.h>//<cstring>
//----------------------------Fuses---------------------------------------------
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protected
|
4. I have not checked completely but i think it is related to the t6963 lcd library that i have got from here: http://www.ccsinfo.com/forum/viewtopic.php?p=162055
If you want I can post this library because i think it's a little different from what i download.
5. Why I think this is working different:
I have written some lines for showing a picture on the LCD and then i type while(1); but the LCD after showing picture, shows something different and again shows picture.
Also when I add touch and step motor to my project it halts or reset or goes some other place of program (before or after ) and I find it from works of peripheral devices like step, touch, buzzer, Evalve.
6. Thanks again |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sat Dec 01, 2012 5:29 am |
|
|
Besides all the comments given above: Code: | char str[40];
show_initialization();
str="Internal Problem,please call producer";
show_text(1,1,str); | This is not the correct way to handle strings.
Either you declare str as a pointer (without size) in combination with the '#device PASS_STRINGS=IN_RAM' option: Code: | #device PASS_STRINGS=IN_RAM
.
.
.
char *str;
show_initialization();
str="Internal Problem,please call producer";
show_text(1,1,str); | or you use strcpy to copy the string contents from ROM to RAM: Code: | char str[40];
show_initialization();
strcpy(str, "Internal Problem,please call producer");
show_text(1,1,str); |
Similar problems do I expect everywhere in your code where I see the construct '&str'. Note that str by itself already is a pointer. Now you are passing a pointer-to-a-pointer. Not the most sensible way of writing code and a common beginner's error. Thinking about it, this could be a large risk for overwriting random RAM and could explain your weird program behaviour.
A trivial bug: Code: | while(true);
delay_ms(10);
| this is equal to: Code: | while(true);
delay_ms(10);
| I think you intended: Code: | while(true)
delay_ms(10);
| Note the missing ';'
But then, there is no need to call delay_ms() when you want to loop forever. It is good practice in a multitasking OS so the process can go to sleep but here you have no OS so just a plain will do the trick.
Or when you want to save power: Code: | while(TRUE)
sleep();
|
A general remark about layout: Code: | case pwdr_reju :saya_body(&str,&select2,&window,&bp_back,&bp_1,&bp_2,&bp_3,&bp_4,&bp_5,&bp_6,&bp_7,&bp_8);if(select2==body_back){select=auto_powder;goto auto_matic;}saya_reju(&str,&window,&select2,&bp_back,&bp_1,&bp_2);break; | This is difficult to read. Why write such dense code? Other people who will have to maintain your code in the future will prefer the easier to read layout like: Code: | case pwdr_reju:
saya_body(&str,&select2,&window,&bp_back,&bp_1,&bp_2,&bp_3,&bp_4,&bp_5,&bp_6,&bp_7,&bp_8);
if(select2==body_back)
{
select=auto_powder;
goto auto_matic;
}
saya_reju(&str,&window,&select2,&bp_back,&bp_1,&bp_2);
break; |
A last note: most C programmers (and school teachers) don't like the use of the goto command. It is considered bad practice as it often leads to code jumping in all directions and making it difficult to maintain, also called 'spaghetti code'. |
|
|
mhjccsinfo
Joined: 17 Oct 2011 Posts: 23 Location: Iran-tehran
|
|
Posted: Sat Dec 01, 2012 7:59 am |
|
|
Thanks for your tips and comments
1- I will have in mind that pointing to strings are incorrect. It should goes to functions like this?
Code: |
main{
char str[40]="ssssssssssssss";
function(str);
}
void function(char str){
char alpha[40],beta[40];
alpha=str;
str=beta;
}
|
Of course this is not related to the main problem, is it? I have the problem when i had no text on the program (only showing a pic).
2- While(1); delay_ms(10); was a mistake I have check the program with true type.
THANKS _________________ thanks and hope of success for you
Mohammad_Hosein |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19481
|
|
Posted: Sat Dec 01, 2012 8:43 am |
|
|
What you show would waste a lot of space, and cause innumerable problems. Key is to understand that a _string_, is not a fundamental 'type' in C. It is an array of characters, terminated with a null.
Now:
alpha=str;
Will _not_ copy the array 'str'. It'll put into the variable 'alpha' the contents of the variable 'str'. If 'str' is the address of another array, then it'll copy the address, and the forty characters of storage being pointed to by alpha, will no longer be used, since it'll now point to the array str. C does directly not have the ability to copy strings as a variable. This is what strcpy does.
However there is another problem. in your function declaration, you declare 'str' as a character. It isn't. It is the _address_ of an array of characters. the function declaration needs to be:
void function(char * str)
which says that 'str' is an address, or:
void function(char str[])
which means exactly the same.
Now, invalid declarations like this, will result in variables being corrupted, since 'alpha' would end up pointing to the wrong location in memory, and _will_ cause the code to fail in dozens of unpredictable ways.
Best Wishes |
|
|
mhjccsinfo
Joined: 17 Oct 2011 Posts: 23 Location: Iran-tehran
|
|
Posted: Sun Dec 02, 2012 10:08 am |
|
|
I have changed program .is it OK now?
Code: | void main(void)
{
char body_place=0;//for choosing situations after pressing buttons
unsigned long s_v;//for saving step_motor_value to the eeprom and also reading from there
char str[40];//general variable for putting every writted text in it
struct coordinate bp_back,bp_1,bp_2,bp_3,bp_4,bp_5,bp_6,bp_7,bp_8;//all of buttons
struct coordinate window;//for frames
saza_prim();
while(1)
{
door_check();
if(page==anno)
saza_announce();
if(page==mainm)
saza_mainmenu(str,&window,&bp_1,&bp_2);
if(page==pref)
saza_professional(str,&window,&bp_back,&bp_1);
if(page==autom)
saza_automatic(str,&window,&bp_back,&bp_1,&bp_2,&bp_3);
if(page==powd)
saza_powder(str,&window,&bp_back,&bp_1,&bp_2,&bp_3,&bp_4);
if(page==reju)
{
saza_body(str,&body_place,&window,&bp_back,&bp_1,&bp_2,&bp_3,&bp_4,&bp_5,&bp_6,&bp_7,&bp_8);
saza_reju(str,&window,&body_place,&bp_back,&bp_1,&bp_2);
}
if(page==work)
saza_work(str,&window,&bp_back,&bp_1,&bp_2);
}
strcpy(str,"out of while(1);");
saza_problem(str);
} |
and this is one of functions:
Code: | void saza_mainmenu (char *str,struct coordinate *window,struct coordinate *bp_auto,struct coordinate *bp_prof)
{
int i;//counter
page=0;
show_initialization();
strcpy(str,"Main Menu");
show_window(window,str);
strcpy(str,"<Please select operation Mode>");
show_text(4,0,str);
strcpy(str," Automatic Mode ");
show_button(8,5,str,bp_auto);
strcpy(str,"professional Mode");
show_button(11,5,str,bp_prof);
while(!page)
page=t_whitch(bp_auto , bp_prof);
if(page==1)
page=autom;
else
page=pref;
} |
"goto" removed and strings corrected. but here is 2 question:
1. why by that wrong style of using strings , everything was OK?!?!?!
2. why restarts , jumps and halts are still active????
waiting for your kind help _________________ thanks and hope of success for you
Mohammad_Hosein |
|
|
|
|
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
|