• AVR Freaks

AnsweredHot!PIC16F1825 restarting?

Page: 123 > Showing page 1 of 3
Author
kart
Starting Member
  • Total Posts : 78
  • Reward points : 0
  • Joined: 2016/12/01 07:55:28
  • Location: 0
  • Status: offline
2020/02/25 03:38:03 (permalink)
0

PIC16F1825 restarting?

Dear all,
I have a strange problem programming my PIC16F1825. When testing without hardware (simulator) everything works fine. But as soon as I programm the device and implement it in my test breadboard it looks like either the microcontroller restarts again and again (or the code works unexpected):
 
What is different to all other programs I have done so far:
1) Voltage is 3 Volt
2) I drive up to 4 x 5mm LEDs, red with 300 OHM resistors with the PORTA and PORTC (one single LED per PIN)
 
Problem decription:
 
When powering up, the LED on RC4 lights up periodically (0,5 second interval) while it should only light up one single time and never again (called only once outside the working loop). So
 
 
// CONFIG1
#pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = OFF        // Internal/External Switchover (Internal/External Switchover mode is enabled)
#pragma config FCMEN = OFF       // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)

// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = OFF       // PLL Enable (4x PLL enabled)
#pragma config STVREN = OFF      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = OFF        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = ON         // Low-Voltage Programming Enable (Low-voltage programming enabled)


#define _XTAL_FREQ 800000

#include <xc.h>

#define Taster PORTAbits.RA2   // Würfeltaster an RA2


int Wurf,Wurf6  = 0;


void main(void)
{
   
    TRISAbits.TRISA1=0;
    TRISAbits.TRISA2=1;     //input Würfeltaster
    TRISAbits.TRISA4=0;
    TRISAbits.TRISA0=0;
    TRISCbits.TRISC0=0;
    TRISCbits.TRISC1=0;
    TRISCbits.TRISC2=0;
    TRISCbits.TRISC3=0;
    TRISCbits.TRISC4=0;
    TRISCbits.TRISC5=0;
    
    ANSELA=0x0000;          //alle A ports digital
    ANSELC=0x0000;          //alle C ports digital
    
    LATCbits.LATC4 = 1;         // Blinke zum Start mit der mittleren LED
    __delay_ms(1000);
    LATCbits.LATC4 = 0;
    
    while(1){
        //while(!Taster){}        // Warte auf den Tastendruck
        Wurf=0;
        Wurf6=0;
                                // Laufe bis das Ergebnis 1-6 ergibt
        while((Wurf6>6)||(Wurf6<1)) {
        Wurf=rand();            //Generiere Zufallszahl
        Wurf6=Wurf&0b00000111;  // UNDe die letzten 3 Stellen, Wurf6 = 0 bis 7
        }
        //while(Taster){}         // Warte bis der Taster losgelassen wurde
        
        LATAbits.LATA0=0;       // Lösche das letzte Ergebnis
        LATAbits.LATA1=0;
        LATAbits.LATA4=0;
        LATCbits.LATC0=0;
        LATCbits.LATC1=0;
        LATCbits.LATC2=0;
        LATCbits.LATC3=0;
        
        __delay_ms(100);
        
                                //Übersetze die Zahl in "Augen"
        switch (Wurf6) {
            case 1: LATCbits.LATC3 = 1; break;
            case 2: LATCbits.LATC2 = 1; LATAbits.LATA1=1; break;
            case 3: LATCbits.LATC2 = 1; LATAbits.LATA1=1; LATCbits.LATC3=1; break;
            case 4: LATAbits.LATA1=1; LATAbits.LATA4=1; LATCbits.LATC0=1; LATCbits.LATC2=1; break;
            case 5: LATAbits.LATA1=1; LATAbits.LATA4=1; LATCbits.LATC0=1; LATCbits.LATC2=1; LATCbits.LATC3=1; break;
            case 6: LATAbits.LATA0=1; LATAbits.LATA1=1; LATAbits.LATA4=1; LATCbits.LATC0=1; LATCbits.LATC2=1; LATCbits.LATC1=1; break;
                
        }
        //if (Wurf6==1) LATAbits.LATA0=1;
        //if (Wurf6==2) LATAbits.LATA1=1;
        //if (Wurf6==3) LATAbits.LATA4=1;
        //if (Wurf6==4) LATCbits.LATC0=1;
        //if (Wurf6==5) LATCbits.LATC1=1;
        //if (Wurf6==6) LATCbits.LATC2=1;
        //if (Wurf6==7) LATCbits.LATC3=1;
        //if (Wurf6==8) LATCbits.LATC4=1;
        //if (Wurf6==9) LATCbits.LATC5=1;
    
        __delay_ms(1000);
 
    }
    
}


#1
ric
Super Member
  • Total Posts : 26159
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC16F1825 restarting? 2020/02/25 03:40:25 (permalink)
+2 (2)
Is your MCLR pin pulled high?
You are trying to disable it with MCLRE = OFF, but this is overidden by LVP = ON, which forces MCLR to be enabled.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#2
kart
Starting Member
  • Total Posts : 78
  • Reward points : 0
  • Joined: 2016/12/01 07:55:28
  • Location: 0
  • Status: offline
Re: PIC16F1825 restarting? 2020/02/25 03:54:04 (permalink)
0
Hi,
 
I changed MCLRE to ON ... and tried both ... MCLRE open and MCLRE pulled high ... in both cases the problem seems still to be there
#3
ric
Super Member
  • Total Posts : 26159
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC16F1825 restarting? 2020/02/25 04:09:09 (permalink)
+1 (1)
Do you really need LVP on?
How are you programming the PIC?
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#4
kart
Starting Member
  • Total Posts : 78
  • Reward points : 0
  • Joined: 2016/12/01 07:55:28
  • Location: 0
  • Status: offline
Re: PIC16F1825 restarting? 2020/02/25 04:11:18 (permalink)
0
Hi,
I am using the PICkit 3 ... your suggestion is to change to LVP OFF and MCLRE OFF?
 
regards
 
#5
ric
Super Member
  • Total Posts : 26159
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC16F1825 restarting? 2020/02/25 04:20:20 (permalink)
0
Yes

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#6
kart
Starting Member
  • Total Posts : 78
  • Reward points : 0
  • Joined: 2016/12/01 07:55:28
  • Location: 0
  • Status: offline
Re: PIC16F1825 restarting? 2020/02/25 05:37:07 (permalink)
0
MCLRE=OFF
LVP=OFF
 
still blinking
#7
pcbbc
Super Member
  • Total Posts : 1649
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: online
Re: PIC16F1825 restarting? 2020/02/25 07:13:23 (permalink)
+2 (2)
Perhaps your processor may be re-setting?
How do you have the 3v supplied?
Do you have correct supply decoupling caps close to the uP supply pins?
 
As a test, try a predictable sequence on the LEDs, instead of random.  For example, count up in binary...
//Wurf=0;
//Wurf6=0;
//while((Wurf6>6)||(Wurf6<1)) {
//    Wurf=rand();            //Generiere Zufallszahl
//    Wurf6=Wurf&0b00000111;  // UNDe die letzten 3 Stellen, Wurf6 = 0 bis 7
//}
 
Wurf++;
Wurf6 = Wurf & 0b00000111;

 
Then you will know if the uP is resetting mid sequence of not.
 
And please learn how to place [​code]//code tags[​/code] around your code before posting on forums...
//code tags

#8
pcbbc
Super Member
  • Total Posts : 1649
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: online
Re: PIC16F1825 restarting? 2020/02/25 07:20:22 (permalink)
+2 (2)
Oh, and not that it's causing your problem, but...
while((Wurf6>6)||(Wurf6<1)) {
    Wurf=rand();            //Generiere Zufallszahl
    Wurf6=Wurf&0b00000111;  // UNDe die letzten 3 Stellen, Wurf6 = 0 bis 7
}

 
Can probably be replaced with...
Wurf6 = (rand() % 6) + 1;

 
I suppose the masking is more efficient than the modulus operator, but you need to offset that against possibly multiple calls to rand while it returns you 0b000 or 0b111 in the lower order 3 bits.
 
#9
kart
Starting Member
  • Total Posts : 78
  • Reward points : 0
  • Joined: 2016/12/01 07:55:28
  • Location: 0
  • Status: offline
Re: PIC16F1825 restarting? 2020/02/26 00:56:22 (permalink)
0
I think I can rule resetting out.
 
When having the aplication running I see the LATC4 blining while the rest of the LED pattern changes on pressing the switch but if I don't touch the switch the pattern stays unchanged while LATC4 blinks.
 
When the programm would restart then the actual/last LED pattern would disappear as the values would not survive a reset/restart. So the keeping of the last LED pattern should indicate that the programm isn't restarted
#10
kart
Starting Member
  • Total Posts : 78
  • Reward points : 0
  • Joined: 2016/12/01 07:55:28
  • Location: 0
  • Status: offline
Re: PIC16F1825 restarting? 2020/02/26 01:25:49 (permalink)
0
Things get stranger,
 
I expanded my setup to:
 
LATCbits.LATC5 = 1;        
__delay_ms(100);
LATCbits.LATC5 = 0;
    
LATAbits.LATA0=0;     
LATAbits.LATA1=0;
LATAbits.LATA4=0;
LATCbits.LATC0=0;
LATCbits.LATC1=0;
LATCbits.LATC2=0;
LATCbits.LATC3=0;
 
Now with every blinking of LATC5 the pattern gets really reset (all LEDs go out).
The code is never touched a 2nd time when run with the simulator.
Is there a hardware triggered scenario that would restart the program WITHOUT deleting/resetting all registers? Why does the pattern survive if I do not delete it manually as I do now?
 
regards
 
#11
pcbbc
Super Member
  • Total Posts : 1649
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: online
Re: PIC16F1825 restarting? 2020/02/26 02:16:38 (permalink)
0
You also have BOR turned off. So all bets are off if Vdd falls below acceptable limits.
Most likely the uP will not reset (that’s the job of BOR after all) and just do something crazy.
Like jump to a random location in program memory, forget port or memory settings....
 
Turn on BOR, see if things get more predictable (if not more stable).
#12
kart
Starting Member
  • Total Posts : 78
  • Reward points : 0
  • Joined: 2016/12/01 07:55:28
  • Location: 0
  • Status: offline
Re: PIC16F1825 restarting? 2020/02/26 02:50:25 (permalink)
0
OK,
- BOREN = ON
- replaced the microcontroller
- stopped using the breadboard. Now the PIC is just in a plane soccet with power supplied and connected an oszilloscope to RC5 (no LED connected at all so there can not be any problem regarding consumtion).
 
Checking the scope I get a high spike on RC5 approx all 2 seconds.
2nd channel of the cope is on VDD and there is a flat line @3 Volt
#13
kart
Starting Member
  • Total Posts : 78
  • Reward points : 0
  • Joined: 2016/12/01 07:55:28
  • Location: 0
  • Status: offline
Re: PIC16F1825 restarting? 2020/02/26 02:55:03 (permalink)
0
Actual code after all changes:
 
#pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = ON      // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = OFF        // Internal/External Switchover (Internal/External Switchover mode is enabled)
#pragma config FCMEN = OFF       // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)

// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = OFF       // PLL Enable (4x PLL enabled)
#pragma config STVREN = OFF      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = OFF        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = OFF         // Low-Voltage Programming Enable (Low-voltage programming enabled)
------------------------------------------------------
#define _XTAL_FREQ 500000

#include <xc.h>

#define Taster PORTAbits.RA2  


int Wurf,Wurf6  = 0;
int counter = 0;             


void main(void)
{
   
    TRISAbits.TRISA1=0;
    TRISAbits.TRISA2=1;    
    TRISAbits.TRISA0=0;
    TRISCbits.TRISC0=0;
    TRISCbits.TRISC1=0;
    TRISCbits.TRISC2=0;
    TRISCbits.TRISC3=0;
    TRISCbits.TRISC4=0;
    TRISCbits.TRISC5=0;
    
    ANSELA=0x0000;         
    ANSELC=0x0000;         
    
    LATCbits.LATC5 = 1;       
    __delay_ms(100);
    LATCbits.LATC5 = 0;
    
    LATAbits.LATA0=0;     
    LATAbits.LATA1=0;
    LATAbits.LATA4=0;
    LATCbits.LATC0=0;
    LATCbits.LATC1=0;
    LATCbits.LATC2=0;
    LATCbits.LATC3=0;
    
    while(1){
        while(!Taster){ counter++;}      
        Wurf=0;
        Wurf6=0;
                             
        while((Wurf6>6)||(Wurf6<1)) {
      
        Wurf=rand()+counter;         
        Wurf6=Wurf&0b00000111; 
        }
        while(Taster){}        
        
        LATAbits.LATA0=0;      
        LATAbits.LATA1=0;
        LATAbits.LATA4=0;
        LATCbits.LATC0=0;
        LATCbits.LATC1=0;
        LATCbits.LATC2=0;
        LATCbits.LATC3=0;
        
        __delay_ms(100);
        
                               
        switch (Wurf6) {
            case 1: LATCbits.LATC3 = 1; break;
            case 2: LATCbits.LATC2 = 1; LATAbits.LATA1=1; break;
            case 3: LATCbits.LATC2 = 1; LATAbits.LATA1=1; LATCbits.LATC3=1; break;
            case 4: LATAbits.LATA1=1; LATAbits.LATA4=1; LATCbits.LATC0=1; LATCbits.LATC2=1; break;
            case 5: LATAbits.LATA1=1; LATAbits.LATA4=1; LATCbits.LATC0=1; LATCbits.LATC2=1; LATCbits.LATC3=1; break;
            case 6: LATAbits.LATA0=1; LATAbits.LATA1=1; LATAbits.LATA4=1; LATCbits.LATC0=1; LATCbits.LATC2=1; LATCbits.LATC1=1; break;
                
        }
       
    
        __delay_ms(1000);
     
    }
    
}
 
#14
kart
Starting Member
  • Total Posts : 78
  • Reward points : 0
  • Joined: 2016/12/01 07:55:28
  • Location: 0
  • Status: offline
Re: PIC16F1825 restarting? 2020/02/26 03:23:27 (permalink)
0
Hi,
 
I just saw that something happened to my PC installation. I get an unresolved include for my
 
#include <xc.h>
 
Library File C:\Program Files (x86)\Microchip\xc8\v2.05\pic\include\xc.h
but there is an unresolved #include <alloca.h>
in included C:\Program Files (x86)\Microchip\xc8\v2.05\pic\include\c99\stdlib.h
 
but not only for this latest code but for all old projects that I want to rebuild too. Any suggestions how to fix this problems?
#15
pcbbc
Super Member
  • Total Posts : 1649
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: online
Re: PIC16F1825 restarting? 2020/02/26 03:28:41 (permalink)
+1 (1)
kartChecking the scope I get a high spike on RC5 approx all 2 seconds.

When you do what?
What else happens?  To the other LEDs for example?
 
Do you perhaps mean EVERY 2 seconds?
If not then I do not understand what "all 2 seconds" means.  Sorry.
 
Check for the cause of the reset:
7.10 Determining the Cause of a Reset
 
Also, please place [​code]//code tags[​/code] around your code when posting here:
//code tags

Thanks.
#16
kart
Starting Member
  • Total Posts : 78
  • Reward points : 0
  • Joined: 2016/12/01 07:55:28
  • Location: 0
  • Status: offline
Re: PIC16F1825 restarting? 2020/02/26 03:54:41 (permalink)
0
When you do what?   :Just powering up ... (no external hardware connected)
- What else happens? : nothing ... I didn't connect ANY LED to be sure there are no problems with power consumtion,..
- yes EVERY 2 seconds
- 7.10 as all resets are disabled (I disabled BOREN again) and there is no exit command I my code a reset should never occur?!
#17
pcbbc
Super Member
  • Total Posts : 1649
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: online
Re: PIC16F1825 restarting? 2020/02/26 04:37:28 (permalink)
0
kart- 7.10 as all resets are disabled (I disabled BOREN again) and there is no exit command I my code a reset should never occur?!

And yet it apparently does....
Turn BOREN back ON.
And stop assuming you think you "know" it is not resetting, and actually check the cause of the reset as per 7.10.
You will see there are possibly many causes, not just BOR and your code exiting while.
 
As your code works in the simulator, we are almost certainly looked at some environmental cause that the simulator can not cover here.
 
Also you might try removing as much code as possible until you get stable behaviour.  In your case I might remove everything in the while, so you just have a single flash on reset.  Now do you still get unpredictable behaviour?  If not, add things back in until you do...
#18
kart
Starting Member
  • Total Posts : 78
  • Reward points : 0
  • Joined: 2016/12/01 07:55:28
  • Location: 0
  • Status: offline
Re: PIC16F1825 restarting? 2020/02/26 05:18:27 (permalink)
0
I tested the code with an emty while(1) ... and the problem is still existing.
I try to import my code to my laptop where I use an older version of the IDE and older version of the compiler as I already once encountered a problem where code works on the old environment but shows strange errors on the new environment
#19
kart
Starting Member
  • Total Posts : 78
  • Reward points : 0
  • Joined: 2016/12/01 07:55:28
  • Location: 0
  • Status: offline
Re: PIC16F1825 restarting? 2020/02/26 06:05:28 (permalink)
0
And it really comes down to a version issue.
 
My desktop PC where I encounter the problem:
 
MPLAB IDE V5.15
C8v 2.05
 
My laptop
 
MPLAB IDE v3.4
C8v1.45
 
The problem exists on my PC only. When I created a new project on my Laptop and imported the source data (ANSI) into the new created project on the LAPTOP I just first got an compiler error that BORV is unknown?! But after removing this line the project could be built.
 
And the code works without problems in my test harware environment (with LEDs,...).
 
Are there any stable new versions from the IDE and the C8 compiler? It happened already several times to me that when programming on new versions I get "misterious" timing errors,... but as soon as I port back the code to this very old versions everything works fine?!
 
post edited by kart - 2020/02/26 06:16:59
#20
Page: 123 > Showing page 1 of 3
Jump to:
© 2020 APG vNext Commercial Version 4.5