CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

18f4520 random restart
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
eng/ IBRAHIM



Joined: 14 Aug 2007
Posts: 31

View user's profile Send private message

18f4520 random restart
PostPosted: Thu Apr 26, 2018 8:06 pm     Reply with quote

Use pic16f887 to control of ac motor. The code is work fine
but when use pic 18f4520, find two problems:
1- the program is randomly restart (not rest)
2- the value of delay is different at practically

fuse
Code:

#device PIC18F4520
 #device adc=8
 
 #FUSES NOWDT                    //No Watch Dog Timer
 #FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
 #FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
 #FUSES NOPROTECT                //Code not protected from reading
 #FUSES NOBROWNOUT               //No brownout reset
 #FUSES BORV25                   //Brownout reset at 2.5V
 #FUSES NOPUT                    //No Power Up Timer
 #FUSES NOCPD                    //No EE protection
 #FUSES NOSTVREN                 //Stack full/underflow will not cause reset
 #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
 #FUSES NOWRTD                   //Data EEPROM not write protected
 #FUSES NOIESO                   //Internal External Switch Over mode disabled
 #FUSES NOFCMEN                  //Fail-safe clock monitor disabled
 #FUSES NOPBADEN                 //PORTB pins are configured as digital I/O on RESET
 #FUSES NOWRTC                   //configuration not registers write protected
 #FUSES NOWRTB                   //Boot block not write protected
 #FUSES NOEBTR                   //Memory not protected from table reads
 #FUSES NOEBTRB                  //Boot block not protected from table reads
 #FUSES NOCPB                    //No Boot Block code protection
 #FUSES NOLPT1OSC                //Timer1 configured for higher power operation
 #FUSES NOMCLR                   //Master Clear pin used for I/O
 #FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
 
 #use delay(clock=4000000)

program
Code:

int32 var,VARW,VARh,phaseangle,phaseold;
int16 isr_ccp_delta,current_ccp,frequency,rest,speed,j;
signed int16 spdelta,spdeltaold;

#int_EXT
void  EXT_isr(void)
{
static int16 old_ccp = 0;
setup_timer_3(T3_INTERNAL|T3_DIV_BY_8);
current_ccp=get_timer3();
isr_ccp_delta=current_ccp - old_ccp;
old_ccp = current_ccp;
if(varspin==0)
{
if(frequency>200)
{
VARW=10000;
}
}
if(phaseangle<58000)
{
phaseangle=58500;
phaseold=58500;
}
}
#int_EXT1
void  EXT1_isr(void)
{
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
set_timer1(phaseangle);
output_low(mot);
enable_interrupts(INT_TIMER1);
if (j==0)
{
j=1;
ext_int_edge(1, H_TO_L);
}
else if(j==1)
{
j=0;
ext_int_edge( 1, L_TO_H);
}
if(phaseangle>64000)
{
VARW=10000;
phaseangle=58000;
phaseold=58000;
}
}
#int_TIMER1
void  TIMER1(void)
{
output_high(mot);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1|RTCC_8_BIT);
set_timer0(0);
enable_interrupts(INT_TIMER0);
}
#int_TIMER0
void  TIMER0(void)
{
output_low(mot) ;
}

//////////////////////////////////////////////////////////////////////////
void run(speed,rest)
{
output_high(playled);
isr_ccp_delta=125000;
phaseold=59000;
phaseangle=59000;
output_high(left);
delay_ms(100);
for (VARW=0;VARW<=2100;++VARW)
{
frequency = (125000 / isr_ccp_delta);
spdelta=speed-frequency;
phaseangle=phaseold+0.8*spdelta-0.4*spdeltaold;
spdeltaold=spdelta;
phaseold=phaseangle;
if(phaseangle>61500)
{
VARW=10000;
}
}
//////////////////////////
for (VARW=0;VARW<=rest;++VARW)
{
isr_ccp_delta=125000;
phaseangle=59000;
phaseold=59000;
output_low(Triac);
delay_ms(100);
output_low(right);
output_low(left);
delay_ms(900);
}
////////////////////////////
output_high(right);
delay_ms(100);
for (VARW=0;VARW<=2100;++VARW)
{
frequency = (125000 / isr_ccp_delta);
spdelta=speed-frequency;
phaseangle=phaseold+0.8*spdelta-0.4*spdeltaold;
spdeltaold=spdelta;
phaseold=phaseangle;
if(phaseangle>61500)
{
VARW=10000;
}
}
///////////////////////////
for (VARW=0;VARW<=rest;++VARW)
{
isr_ccp_delta=125000;
phaseold=59000;
phaseangle=59000;
output_low(Triac);
delay_ms(100);
output_low(right);
output_low(left);
delay_ms(900);
}
///////////////////////////
void main()
{

enable_interrupts(INT_EXT);
enable_interrupts(INT_EXT1);
enable_interrupts(GLOBAL);

delay_ms(500);

 while(1)
{
if(input(start)){output_high(playled);
goto L;
}
}

L:
delay_ms(1500);
while(1)
      {
run(55,7);
      }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 13536

View user's profile Send private message

PostPosted: Thu Apr 26, 2018 11:22 pm     Reply with quote

Assuming the code is the same.
Sounds like a classic EMF problem.

The 4520, has a slightly smaller die geometry that the 887. Makes it slightly more sensitive to RFI and supply noise. The early PIC's were very rugged in this regard. Later chips not so good.
PCM programmer



Joined: 06 Sep 2003
Posts: 20385

View user's profile Send private message

Re: 18f4520 random restart
PostPosted: Thu Apr 26, 2018 11:32 pm     Reply with quote

eng/ IBRAHIM wrote:

Use pic16f887 to control of ac motor. The code is work fine but
when use pic 18f4520

You suggest that you took the exact same code that worked with the
16F887 and you dropped it into an 18F4520 and it fails.
But it's not the same code that you used on the 16F887. Your code uses
External interrupt 1 and Timer3. Those peripherals are not available in
the 16F887. Therefore, you have substantially changed the code.
It is possible that you introduced errors when you added the changes.

eng/ IBRAHIM wrote:

output_low(Triac);
output_high(mot);
output_low(right);
output_low(left);

Disconnect all these devices from the circuit. Then run the PIC.
Does it still do random restarts ? If not, you probably have some
of these problems:
1. Insufficient power supply
2. Insufficient bulk capacitance
3. Insufficient bypass capacitors
4. Poor board layout (thin power supply traces, stubs, lack of ground plane)
5. Triac noise
6. Missing diodes across coils
Ttelmah



Joined: 11 Mar 2010
Posts: 13536

View user's profile Send private message

PostPosted: Fri Apr 27, 2018 12:11 am     Reply with quote

I must admit, when I saw 'TRIAC', I felt RFI/EMF was immediately the most likely problem.....
Just how horrible these parts really can be is often missed.

Another question to add:

Is the load exactly the same as was being used before?.
temtronic



Joined: 01 Jul 2010
Posts: 6251
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Apr 27, 2018 5:45 am     Reply with quote

I see the clock is 40MHz, the 887 tops out at 20MHz. You need really good PCB layout,traces, filters, etc. to run reliably at 40MHz. Also anything attached to PIC pins needs to be '40MHz' compatible meaning,again, wiring, filters, etc. Is the PCB super clean, all solder joints shiny, everything well soldered? I got bit by a pot wiper NOT being soldered once, It took 3 MONTHS to figure that out as it 'mostly worked'.
In effect, you've 'overclocked' your program and PIC environment.
Now maybe, this isn't the core to your problem but 'noise' comes from a LOT of sources and can be difficult to track down.

just somethings to consider.
Jay
eng/ IBRAHIM



Joined: 14 Aug 2007
Posts: 31

View user's profile Send private message

PostPosted: Fri Apr 27, 2018 9:41 am     Reply with quote

When the problem occurred after changing the pic on the same PCB and on the same motor
I took into account the differences between them and I changed Timer 2 to Timer 3. Change #int_rb to #int_EXT1 . But remained the same problem.

When disable #int_TIMER0 ( or disable #int_TIMER01 ), the motor does not work but the problem disappears.

I've met the noise problem before and solved it.
But it was causing RESET not RESTART ( The program starts from the beginning, with the playled still working without pressing the start).
PCM programmer



Joined: 06 Sep 2003
Posts: 20385

View user's profile Send private message

PostPosted: Fri Apr 27, 2018 11:05 pm     Reply with quote

The 3 variables shown in bold are not initialized at the start of main():
Quote:

int16 isr_ccp_delta, current_ccp, frequency, rest, speed, j;


The variable 'rest' is used in two for() loops, but is never set to a value
in your program !
Quote:

void run(speed,rest)

for (VARW=0; VARW <= rest; ++VARW)

for (VARW=0; VARW <= rest; ++VARW)



The 'speed' variable is used in two calculations but it's never set to any value !
Quote:

void run(speed,rest)

spdelta=speed-frequency;

spdelta=speed-frequency;



Your program doesn't compile. It gives these errors
Quote:

>>> Warning 204 "PCH_Test.c" Line 45(1,1): Condition always FALSE varspin
*** Error 12 "PCH_Test.c" Line 45(4,11): Undefined identifier varspin
*** Error 12 "PCH_Test.c" Line 67(12,15): Undefined identifier mot
*** Error 12 "PCH_Test.c" Line 92(13,16): Undefined identifier mot
*** Error 12 "PCH_Test.c" Line 101(12,15): Undefined identifier mot
*** Error 12 "PCH_Test.c" Line 107(13,20): Undefined identifier playled
*** Error 12 "PCH_Test.c" Line 111(13,17): Undefined identifier left
*** Error 12 "PCH_Test.c" Line 134(12,17): Undefined identifier Triac
*** Error 12 "PCH_Test.c" Line 136(12,17): Undefined identifier right
*** Error 12 "PCH_Test.c" Line 137(12,16): Undefined identifier left
*** Error 12 "PCH_Test.c" Line 141(13,18): Undefined identifier right
*** Error 12 "PCH_Test.c" Line 164(12,17): Undefined identifier Triac
*** Error 12 "PCH_Test.c" Line 166(12,17): Undefined identifier right
*** Error 12 "PCH_Test.c" Line 167(12,16): Undefined identifier left
*** Error 173 "PCH_Test.c" Line 173(6,10): Functions may not be nested
*** Error 12 "PCH_Test.c" Line 184(10,15): Undefined identifier start
*** Error 12 "PCH_Test.c" Line 184(30,37): Undefined identifier playled
*** Error 79 "PCH_Test.c" Line 195(1,2): Expect }
17 Errors, 1 Warnings.
eng/ IBRAHIM



Joined: 14 Aug 2007
Posts: 31

View user's profile Send private message

PostPosted: Sat Apr 28, 2018 7:04 am     Reply with quote

Code:

#define start           PIN_E1
#define playled         PIN_C6
#define standbyled      PIN_D5
#define endled          PIN_C7
#define right           PIN_A3
#define left            PIN_C2
#define COD3            PIN_B3
#define COD4            PIN_B4
#define COD5            PIN_A7
#define COD6            PIN_B6
#define TAC             PIN_B0
#define mot             PIN_C0
#define ZCD             PIN_B5

int32 VARW,phaseangle,phaseold;
int16 isr_ccp_delta,current_ccp,frequency,rest,speed,j;
signed int16 spdelta,spdeltaold;

#int_EXT
void  EXT_isr(void)
{
static int16 old_ccp = 0;
setup_timer_3(T3_INTERNAL|T3_DIV_BY_8);
current_ccp=get_timer3();
isr_ccp_delta=current_ccp - old_ccp;
old_ccp = current_ccp;

if(frequency>200)
{
VARW=10000;
}

if(phaseangle<58000)
{
phaseangle=58500;
phaseold=58500;
}
}
#int_EXT1
void  EXT1_isr(void)
{
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
set_timer1(phaseangle);
output_low(mot);
enable_interrupts(INT_TIMER1);
if (j==0)
{
j=1;
ext_int_edge(1, H_TO_L);
}
else if(j==1)
{
j=0;
ext_int_edge( 1, L_TO_H);
}
if(phaseangle>64000)
{
VARW=10000;
phaseangle=58000;
phaseold=58000;
}
}

#int_TIMER1
void  TIMER1(void)
{
output_high(mot);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1|RTCC_8_BIT);
set_timer0(0);
enable_interrupts(INT_TIMER0);
}
#int_TIMER0
void  TIMER0(void)
{
output_low(mot) ;
}

//////////////////////////////////////////////////////////////////////////
void run(speed,rest)
{
output_high(playled);
isr_ccp_delta=125000;
phaseold=59000;
phaseangle=59000;
output_high(left);
delay_ms(100);
for (VARW=0;VARW<=2100;++VARW)
{
frequency = (125000 / isr_ccp_delta);
spdelta=speed-frequency;
phaseangle=phaseold+0.8*spdelta-0.4*spdeltaold;
spdeltaold=spdelta;
phaseold=phaseangle;
if(phaseangle>61500)
{
VARW=10000;
}
}
//////////////////////////
for (VARW=0;VARW<=rest;++VARW)
{
isr_ccp_delta=125000;
phaseangle=59000;
phaseold=59000;
output_low(mot);
delay_ms(100);
output_low(right);
output_low(left);
delay_ms(900);
}
////////////////////////////
output_high(right);
delay_ms(100);
for (VARW=0;VARW<=2100;++VARW)
{
frequency = (125000 / isr_ccp_delta);
spdelta=speed-frequency;
phaseangle=phaseold+0.8*spdelta-0.4*spdeltaold;
spdeltaold=spdelta;
phaseold=phaseangle;
if(phaseangle>61500)
{
VARW=10000;
}
}
///////////////////////////
for (VARW=0;VARW<=rest;++VARW)
{
isr_ccp_delta=125000;
phaseold=59000;
phaseangle=59000;
output_low(mot);
delay_ms(100);
output_low(right);
output_low(left);
delay_ms(900);
}
}
///////////////////////////
void main()
{
enable_interrupts(INT_EXT);
enable_interrupts(INT_EXT1);
enable_interrupts(GLOBAL);

delay_ms(500);
output_high(standbyled);
 while(1)
{
if(input(start)){output_high(playled);output_low(standbyled);
goto L;
}
}

L:
delay_ms(1500);
while(1)
      {
run(55,5);
      }
      }

program compile no error.
program run fine. Motor run good but The program starts from start randomly. Delay time of rest must be 5sec. In actually rest time is 10sec.
temtronic



Joined: 01 Jul 2010
Posts: 6251
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Apr 28, 2018 7:18 am     Reply with quote

hmm...
first delay is this..
delay_ms(500);

you say 'rest' delay is really 10 seconds, so that's 20X longer

That could be the clock isn't really running properly.

I suggest you code/compile/run a '1Hz LED' program to confirm/deny the PIC is actually running at the correct speed. Report back what happens.

Jay
eng/ IBRAHIM



Joined: 14 Aug 2007
Posts: 31

View user's profile Send private message

PostPosted: Sat Apr 28, 2018 7:53 am     Reply with quote

temtronic wrote:
hmm...
first delay is this..
delay_ms(500);

you say 'rest' delay is really 10 seconds, so that's 20X longer

That could be the clock isn't really running properly.

I suggest you code/compile/run a '1Hz LED' program to confirm/deny the PIC is actually running at the correct speed. Report back what happens.

Jay


Clock is really running properly.
When disable #int_TIMER0 ( or disable #int_TIMER01 ), delay_ms(500); give 0.5sec and rest delay is really 5 seconds, and no RESTART.
PCM programmer



Joined: 06 Sep 2003
Posts: 20385

View user's profile Send private message

PostPosted: Sat Apr 28, 2018 8:33 am     Reply with quote

1. Post your CCS compiler version.

2. Your latest posted code still is missing the intialization of 'rest' and 'speed'.

Since you want this loop to run at 5 seconds, and it has a total delay
time of 100 ms + 900 ms = 1 second,
Quote:
for (VARW=0;VARW<=rest;++VARW)
{
isr_ccp_delta=125000;
phaseold=59000;
phaseangle=59000;
output_low(mot);
delay_ms(100);
output_low(right);
output_low(left);
delay_ms(900);
}

I suggest that you initialize 'rest' to 5, like this:
Quote:
void main()
{
rest = 5;

enable_interrupts(INT_EXT);
enable_interrupts(INT_EXT1);
enable_interrupts(GLOBAL);


But you are still doing calculations based on the 'speed' value, and your
code never initializes 'speed'. It probably is 0x0000.
Quote:

spdelta=speed-frequency;
phaseangle=phaseold+0.8*spdelta-0.4*spdeltaold;

How can you possibly write your progam without setting 'speed' or 'rest'
to an initial value in main() ?
eng/ IBRAHIM



Joined: 14 Aug 2007
Posts: 31

View user's profile Send private message

PostPosted: Sat Apr 28, 2018 9:04 am     Reply with quote

CCS compiler version. 4.014


void run(speed,rest)
setting 'speed' ;'rest'
are initial value in main() . run(55,5)
Ttelmah



Joined: 11 Mar 2010
Posts: 13536

View user's profile Send private message

PostPosted: Sun Apr 29, 2018 12:23 pm     Reply with quote

There are type declaration problems in the code.

First 'run', doesn't declare the types for the variables it is being passed. This is relying on the 'implicit int' rule, but is potentially dangerous.
Then you have:
int16 isr_ccp_delta;

but load this with:
isr_ccp_delta=125000;

an int16 can hold 65535 maximum. Ugh....

There are several other similar issues.

frequency = (125000 / isr_ccp_delta);

will have a wrong value because of isr_ccp_delta etc..
temtronic



Joined: 01 Jul 2010
Posts: 6251
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Apr 29, 2018 2:30 pm     Reply with quote

I was thinking about HOW could this code have worked on the other PIC as there are lots of small code 'details' any of which can cause failure.
So I suspect it's not hardware related, like EMI or PCB, 'just' code.
eng/ IBRAHIM



Joined: 14 Aug 2007
Posts: 31

View user's profile Send private message

PostPosted: Sun Apr 29, 2018 7:47 pm     Reply with quote

thanks all

i deleted the
Code:
enable_interrupts(INT_TIMER0);
enable_interrupts(INT_TIMER1);

from interrupts and but that in RUN loop
Code:

void run(speed,rest)
{
enable_interrupts(INT_TIMER0);
enable_interrupts(INT_TIMER1);
output_high(playled);
isr_ccp_delta=125000;
phaseold=59000;
phaseangle=59000;
delay_ms(200);
output_high(left);
delay_ms(100);
for (VARW=0;VARW<=2100;++VARW)
{
frequency = (125000 / isr_ccp_delta);
spdelta=speed-frequency;
phaseangle=phaseold+0.8*spdelta-0.4*spdeltaold;
spdeltaold=spdelta;
phaseold=phaseangle;
if(phaseangle>61500)
{
VARW=10000;
}
}
//////////////////////////
disable_interrupts(INT_TIMER1);
clear_interrupt(INT_TIMER1);
disable_interrupts(INT_TIMER0);
clear_interrupt(INT_TIMER0);
for (VARW=0;VARW<=rest;++VARW)
{
isr_ccp_delta=125000;
phaseangle=59000;
phaseold=59000;
output_low(mot);
delay_ms(100);
output_low(right);
output_low(left);
delay_ms(900);
}

After that the delay time became real and actually. But the RESTART problem still occurs.
but occur after a longer period than before.

Already the same program was used on another pic and works fairly well despite the errors in the code. But these problems did not appear.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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