caramoan tour package

caramoan tour package

Author Topic: how to code reset in PIC using MPLAB and PICC compiler  (Read 920 times)

Offline ongkal

  • CR2032 Battery
  • **
  • Posts: 8
  • Pogi/Ganda Points: 0
how to code reset in PIC using MPLAB and PICC compiler
« on: May 07, 2010, 08:45:48 PM »
paano po makabalik sa starting point sa main() pag ang program execution ay nasa isang function na nacall nya? bale parang reset button.

Philippine Electronics Forum

how to code reset in PIC using MPLAB and PICC compiler
« on: May 07, 2010, 08:45:48 PM »

Offline Kaizer03

  • Nuclear Reactor
  • ****
  • Posts: 4847
  • Pogi/Ganda Points: 225
  • C#<-->Android<-->Java
    • PhilRobotics
Re: how to code reset in PIC using MPLAB and PICC compiler
« Reply #1 on: May 07, 2010, 09:11:28 PM »
reset_cpu();

but works only in PIC18 Family only=)
Lend a hand for those who are in need!=)

Stop Hijacking!=) More Technical Posts!=)
 ;)

Kaizer Killer EX Pre-Alpha

Philippine Electronics Forum

Re: how to code reset in PIC using MPLAB and PICC compiler
« Reply #1 on: May 07, 2010, 09:11:28 PM »

Offline 'yus

  • Technical People
  • Nuclear Reactor
  • *****
  • Posts: 4251
  • Pogi/Ganda Points: 299
  • Gender: Male
  • hw -> fw -> sw
    • yus' projects
Re: how to code reset in PIC using MPLAB and PICC compiler
« Reply #2 on: May 07, 2010, 09:44:18 PM »
pwede rin na gamitin ang WatchDog timer ng pic para i-reset sya..

or.. i-tie ang reset pin sa isang output pin :D (like RA0, since magkatabi lang sila ni MCLR),
then iki-clear (ilo-low) yung bit ng RA0.. hehe
join  - Philippine Electronics and Robotics Enthusiasts Club - www.philrobotics.com

Philippine Electronics Forum

Re: how to code reset in PIC using MPLAB and PICC compiler
« Reply #2 on: May 07, 2010, 09:44:18 PM »

Offline paranz

  • Moderator
  • Nuclear Reactor
  • *****
  • Posts: 4525
  • Pogi/Ganda Points: 177
  • Gender: Male
  • 1/4W resistor specialist
    • RapidSignal Electronics
Re: how to code reset in PIC using MPLAB and PICC compiler
« Reply #3 on: May 07, 2010, 09:47:56 PM »
pwede ;D
PIC16 Programming Tutorial using MPLAB and Hi-Tech C
www.rapidsignalph.com/tutorials/pic16-tutorials

Arduino & gizDuino Tutorials
www.rapidsignalph.com/tutorials/arduino-tutorials

Philippine Electronics Forum

Re: how to code reset in PIC using MPLAB and PICC compiler
« Reply #3 on: May 07, 2010, 09:47:56 PM »

Offline ongkal

  • CR2032 Battery
  • **
  • Posts: 8
  • Pogi/Ganda Points: 0
Re: how to code reset in PIC using MPLAB and PICC compiler
« Reply #4 on: May 07, 2010, 10:01:10 PM »
thanks po, mga kuya. research po muna ako tungkol sa WDT.

Philippine Electronics Forum

Re: how to code reset in PIC using MPLAB and PICC compiler
« Reply #4 on: May 07, 2010, 10:01:10 PM »

Offline rdpzycho

  • Technical People
  • Solar Power Satellite
  • *****
  • Posts: 10726
  • Pogi/Ganda Points: 632
  • Gender: Male
  • Respect Begets Respect
    • rdpzycho
Re: how to code reset in PIC using MPLAB and PICC compiler
« Reply #5 on: May 07, 2010, 10:51:31 PM »
ano ba ang gagawin nung function? medyo unacceptable 'to for modern computer scientists pero you can use goto.

example:

#define reset goto start

void main (void)
{
     start:
     .
     .
     .
     reset;
}

'yun nga lang hindi mo siya pwedeng gamitin sa ibang function, so ang gagawin mo pagtapos nung function ka maglalagay nung reset.

'yung sa WDT naman, iho-hold mo lang 'yung operation sa tinawag mo na function para hindi ma-reset 'yung WDT timer.
‎"Divide each difficulty into as many parts as is feasible and necessary to resolve it."
- Rene Descartes

"For every difficult problem there is always a simple answer and most of them are wrong."
- Clayton Paul

Offline 0b00000111

  • Technical People
  • Solar Power Satellite
  • *****
  • Posts: 6129
  • Pogi/Ganda Points: 398
  • There is no delight in owning anything unshared.
Re: how to code reset in PIC using MPLAB and PICC compiler
« Reply #6 on: May 08, 2010, 08:59:10 AM »
pwede rin na gamitin ang WatchDog timer ng pic para i-reset sya..

or.. i-tie ang reset pin sa isang output pin :D (like RA0, since magkatabi lang sila ni MCLR),
then iki-clear (ilo-low) yung bit ng RA0.. hehe

yung WDT na ang ginagamit ko ngayon..

pero sa unang unang-unang project ko sa pic, yang 2nd ang ginawa ko hehehe... :D
E-Gizmo Mechatronix Central: www.e-gizmo.com

Tel #: (63)(2) 536-3378
Globe +63915-973-7691
Smart +63921-779-0748

Location Map

YM: julie.egizmo  aka Born2BeWired  ;D

Offline marcelino

  • Moderator
  • Solar Power Satellite
  • *****
  • Posts: 6016
  • Pogi/Ganda Points: 258
  • ...keep moving forward! - Robinson's
Re: how to code reset in PIC using MPLAB and PICC compiler
« Reply #7 on: May 08, 2010, 12:42:24 PM »
papano natin to ginagamit in particular?

sa pagkakaintindi ko, kung gagamitin ko ang WDT (pakomment naman kung tama :D):
1. magkacount ako ng paulit ang WDT (based sa prescalers at ibang settings...).  kapag nag "overflow" ang WDT counter, marereset ang CPU...
2. ngunit pwede naman iclear ang WDT somewhere sa code kung nanaisin...


so kunyari, eto ang code:
Code: [Select]
void pogi(void)
{
printf("gwapo mo!");
delay();
counter2++;
}

void marcelino (void)
{
printf ("marcelino");
delay();
counter1++;
}


void main()
{
while(1)// forever loop
{
marcelino();
pogi();
clearWDT(); //clear ako dito para di na magreset gamit ang WDT
}
}

at kunyari laging nahahang sa delay() function, eventually mag ooverflow ang WDT causing CPU to reset.... tama din ba?
papano kung yung delay function ko ay sobrang haba as in mas mahaba
papano naman kung di pa nga tapos ang main loop ko, nagrereset na?

tanong ko din, anong mangyayari sa mga variables, for instance sa psuedo code sa taas yung counter1 and counter2 variables?

ano-ano ang mga precautions (or special attention) na dapat inote sa paggamit ng WDT?
"Don't take life seriously. After all, no one has ever come out of it alive. -Bugs Bunny"

Offline rdpzycho

  • Technical People
  • Solar Power Satellite
  • *****
  • Posts: 10726
  • Pogi/Ganda Points: 632
  • Gender: Male
  • Respect Begets Respect
    • rdpzycho
Re: how to code reset in PIC using MPLAB and PICC compiler
« Reply #8 on: May 08, 2010, 01:17:48 PM »
dapat meron ng timing sis. normally, linalagay ko 'yun sa Timer na nagha-handle ng flow ng program. pagpasok sa timer, reset kaagad ng WDT. tapos madalas linalagay ko sa mahaba 'yung WDT.

ngayon kung gustong mag-reset:

void reset (void)
{
     while(1)
     ;
}

'di na mare-reset 'yung WDT. ;D

kapag nag-reset sa WDT, wala na lahat. meron atang mga MCU na merong handler 'yung WDT, para ma-check kung ano nangyari bakit nag-hang. then pwede muna i-save 'yung variables na kailangan, bago reset.

‎"Divide each difficulty into as many parts as is feasible and necessary to resolve it."
- Rene Descartes

"For every difficult problem there is always a simple answer and most of them are wrong."
- Clayton Paul

Offline paranz

  • Moderator
  • Nuclear Reactor
  • *****
  • Posts: 4525
  • Pogi/Ganda Points: 177
  • Gender: Male
  • 1/4W resistor specialist
    • RapidSignal Electronics
Re: how to code reset in PIC using MPLAB and PICC compiler
« Reply #9 on: May 08, 2010, 08:55:52 PM »
ano-ano ang mga precautions (or special attention) na dapat inote sa paggamit ng WDT?

Basic precaution would include not using long software delay in the code. If using software delay function is unavoidable, it should be very short like a few hundred microseconds maximum. Never include other blocking functions like those that manage buttons, keypads, etc..

dapat meron ng timing sis. normally, linalagay ko 'yun sa Timer na nagha-handle ng flow ng program.

I agree, dapat deterministic ang total timing ng main system loop if reliable use of WDT is desired.

This is basically achieve by using state machines implementation for each tasks inside the main loop, and a hardware timer to regulate the number of passes to the main loop. For example, we can use TMR0 to overflow 1000x per second and also make the main loop to execute 1000x per second by blocking the end of the main loop while TMR0 has not overflowed yet.

Here is a basic code.

Code: [Select]

#include <pic.h>

__CONFIG(WDTEN & HS);


void interrupt isr(void)
{
GIE = 0;
if(T0IE && T0IF)
{
TMR0 = TMR0_PRESET_VALUE; //TMR0 overflow period = 1 ms
tmr0_rollover_flag = TRUE;
T0IF = 0;
}
GIE = 1;
}


void main()
{
InitSystem(); //include initializeing TMR0 as interrupt source

while(1)
{
//do what your system should do
//all tasks should execute within 1 ms
doTask1(); //ex. serial com-related task
doTask2(); //ex. adc-related task
.. //other tasks
..
..
doTaskn();

//just in case your system went nuts, recover!
if(hard_sys_error_flag == TRUE)
doErrorRecoverTask();

Systimer(); //manage system timing
}

}

void SystemTimer(void)
{
//manage other timing related tasks here
..
..
..

        CLRWDT();                                              //clear the WDT

while (tmr0_rollover_flag != TRUE); //wait for TMR0 to overflow, 1 ms
tmr0_rollover_flag = FALSE;
return;
}


void doErrorRecoverTask(void)
{
//do some initial recovery tasks here like, resetting variables and state machines
..
..
//The sys_error_recovery_flag should be set somewhere here
..

//lastly, if error recovery failed, reset PIC!!
if(sys_error_recovery_flag == FALSE)
while(1); //blocking code, WDT not cleared, will cause PIC to reset after 7 ms

}


All the tasks in the loop should execute within 1 ms. An error recovery task is also included to cure any system errors. This is a must if you are building a commercial product. Note that resetting the PIC using WDT is a last recourse and is placed last in the error recovery function. At the end of the main loop, the system timing is managed by the SystemTimer().

Again, in this example the main loop is passed 1000x per second, which also means that the WDT is cleared every 1 ms. The WDT overflow period is 7 ms if not using prescaler, so a reliable functional firmware should clear the WDT every less than 7 ms.

If the system has a major error (i.e. the program is still running but crazy), you can make the mcu reset itself by making the WDT overflows. If somehow the program freezes (i.e. a power glitch hangs up the mcu), the WDT will still overflow and reset the MCU.
PIC16 Programming Tutorial using MPLAB and Hi-Tech C
www.rapidsignalph.com/tutorials/pic16-tutorials

Arduino & gizDuino Tutorials
www.rapidsignalph.com/tutorials/arduino-tutorials

Philippine Electronics Forum

Re: how to code reset in PIC using MPLAB and PICC compiler
« Reply #9 on: May 08, 2010, 08:55:52 PM »

 

Privacy Policy

Contact Us: elabph@yahoo.com