• AVR Freaks

AnsweredHot!MCLR as digital input is resetting the device

Page: 12 > Showing page 1 of 2
Author
HighTechPower
Starting Member
  • Total Posts : 46
  • Reward points : 0
  • Joined: 2021/01/27 01:35:43
  • Status: offline
2021/03/06 14:04:02 (permalink)
0

MCLR as digital input is resetting the device

Hi. I'm using PIC12F683 in a circuit. I'm using it's pin 4, MCLR as digital input by adjusting the configuration bits accordingly. I have connected a SPDT switch to pin 4 whose level can be either 3.3 V or 0 V, however whenever there is a change in input voltage level at pin 4 (low to high or high to low) by changing switch position, it resets pin 7 (perhaps the whole device). Any idea why this is happening and how to avoid that.
 
Please attached here find the part of schematic.
post edited by HighTechPower - 2021/03/06 14:29:34

Attached Image(s)

#1
GilJ
Super Member
  • Total Posts : 119
  • Reward points : 0
  • Joined: 2011/04/05 02:36:47
  • Location: 0
  • Status: offline
Re: MCLR as digital input is resetting the device 2021/03/06 15:14:25 (permalink)
0
Hi,
If you have disabled MCLR it shouldn't reset the microcontroller. Can you post the code?
#2
upand_at_them
Super Member
  • Total Posts : 788
  • Reward points : 0
  • Joined: 2005/05/16 07:02:38
  • Location: Pennsylvania
  • Status: offline
Re: MCLR as digital input is resetting the device 2021/03/06 18:19:25 (permalink)
0
100 ohms is likely too small a value.  3.3V / 100 ohms = 33mA, which may exceed the rating for the pin.  And you may not need to pull it high, as the datasheet says that MCLR (as GPIO) is tied to Vcc...so a pulldown through your switch should be all that's needed.  200 ohms for the LEDs may be borderline, depending on your LED forward voltage...modern LED's don't need much current to work well.
#3
1and0
Access is Denied
  • Total Posts : 12269
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: MCLR as digital input is resetting the device 2021/03/06 22:48:11 (permalink)
+1 (1)
HighTechPower
Hi. I'm using PIC12F683 in a circuit. I'm using it's pin 4, MCLR as digital input by adjusting the configuration bits accordingly. I have connected a SPDT switch to pin 4 whose level can be either 3.3 V or 0 V, however whenever there is a change in input voltage level at pin 4 (low to high or high to low) by changing switch position, it resets pin 7 (perhaps the whole device). Any idea why this is happening and how to avoid that.

Post your code. I suspect you did not configure GP3/MCLR as a digital input. Also, add a 4.7K to 10K pull-up resistor on GP3, so the pin would not be floating when the switch is switching.
#4
ric
Super Member
  • Total Posts : 30244
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: MCLR as digital input is resetting the device 2021/03/06 23:16:40 (permalink)
0
HOW and WHERE exactly are you setting the configuration bits?
What language are you programming in, and what software are you using to drive the programmer/debugger (MPLAX? MPLAB8?  Microchip IPE?
 

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!
#5
HighTechPower
Starting Member
  • Total Posts : 46
  • Reward points : 0
  • Joined: 2021/01/27 01:35:43
  • Status: offline
Re: MCLR as digital input is resetting the device 2021/03/06 23:40:12 (permalink)
0
ric
HOW and WHERE exactly are you setting the configuration bits?
What language are you programming in, and what software are you using to drive the programmer/debugger (MPLAX? MPLAB8?  Microchip IPE?



I'm not setting configuration bits in code instead using menu bar to set the configuration bits. I'm programming in C and using MPLAB 8.92.
#6
HighTechPower
Starting Member
  • Total Posts : 46
  • Reward points : 0
  • Joined: 2021/01/27 01:35:43
  • Status: offline
Re: MCLR as digital input is resetting the device 2021/03/07 00:56:31 (permalink)
0
GilJ
Hi,
If you have disabled MCLR it shouldn't reset the microcontroller. Can you post the code?




Please attached here find the full schematic and code as well.
 
#include <xc.h>

#define _XTAL_FREQ 4000000

void init_micro (void);
void init_timer (void);
void init_adc (void);
void init_pwm (void);
void read_adc(void);
void pwm_dr (unsigned int dr);
void status (void);
int abval (int);

static int dr = 0, counter_a = 0, counter_b = 0, counter_c = 0, counter_d = 0, adc = 0, flag_c = 0, buzzer_flag = 0;
//static signed long sample_fresh = 0, sample_previous = 0, filtered_previous = 0, filtered_fresh = 0, temp_sample = 0, temp_filtered = 0;

#define SBIT_LED_CHARGING GP4
#define SBIT_LED_CHARGED GP5
#define SBIT_BUZZER GP2
#define TRUE 1
#define FALSE 1
//#define PB_SWITCH GP0
#define PB_SWITCH GP3
#define POWER_GOOD GP0

void main (void)
{
 init_micro();
 init_adc();
 init_timer();
// init_pwm();
 do
 {
 }
 while(TRUE);
}

void init_micro (void)
{
 OSCCON = 0b01100111;
 TRISIO = 0b00001010;
 CMCON0 = CMCON0 | 0b00000100;
 ANSEL = 0b01010010;
 GPIO = 0b00000000;
 POWER_GOOD = TRUE;
 __delay_ms(100);
}

void interrupt team_isr()
{
 if(TMR1IF)
 {
  counter_b++;
  if(adc >= 820)
  counter_c++;
  if(counter_b >= 300)
  {
   if(counter_c >= 299)
   flag_c = 1;
   else if(counter_c < 299)
   flag_c = 0;
   counter_b = 0;
   counter_c = 0;
  }
  counter_a++;
  if(!PB_SWITCH)
  {
   SBIT_BUZZER = 0;
   if(adc >= 820)
   {
    SBIT_BUZZER = 0;
    if(flag_c)
    {
     SBIT_LED_CHARGED = 1;
     SBIT_LED_CHARGING = 0;
     if(counter_a >= 10)
     {
      counter_a = 0;
     }
    }
    else if(!flag_c)
    {
     SBIT_LED_CHARGED = 0;
     if(counter_a >= 10)
     {
      SBIT_LED_CHARGING ^= 1;
      counter_a = 0;
     }
    }
   }
   else if(adc < 820)
   {
    if(counter_a >= 10)
    {
     SBIT_LED_CHARGING ^= 1;
     counter_a = 0;
    }
    SBIT_BUZZER = 0;
    SBIT_LED_CHARGED = 0;
   }
  }
  else if(PB_SWITCH)
  {
   if(adc >= 820)
   {
    if(flag_c)
    {
     if(counter_a >= 10)
     {
      SBIT_BUZZER ^= 1;
      counter_a = 0;
     }
     SBIT_LED_CHARGED = 1;
     SBIT_LED_CHARGING = 0;
    }
    else if(!flag_c)
    {
     SBIT_LED_CHARGED = 0;
     SBIT_BUZZER = 0;
     if(counter_a >= 10)
     {
      SBIT_LED_CHARGING ^= 1;
      counter_a = 0;
     }
    }
   }
   else if(adc < 820)
   {
    if(counter_a >= 10)
    {
     SBIT_LED_CHARGING ^= 1;
     counter_a = 0;
    }
    SBIT_BUZZER = 0;
    SBIT_LED_CHARGED = 0;
   }
  }
  TMR1IF = 0;
  TMR1H = 0xCF;
  TMR1L = 0x2C;
  TMR1IE = 1;
  PEIE = 1;
  GIE = 1;
  TMR1ON = 1;
 }
 
 if(ADIF)
 {
  read_adc();
  ADIF = 0;
  ADON = 1;
  ADIE = 1;
  PEIE = 1;
  GIE = 1;
 // __delay_us(4);
  GO_nDONE = 1;
 }
}

void init_timer (void)
{
 T1CON = 0b00110100;
 TMR1H = 0xCF;
 TMR1L = 0x2C;
 TMR1IE = 1;
 PEIE = 1;
 GIE = 1;
 TMR1ON = 1;
 TMR1IF = 0;
}

void init_pwm (void)
{
 PR2 = 255;
// if(filtered_fresh < 4)
// if(adc < 4)
// pwm_dr((adc + 94));
// pwm_dr((filtered_fresh + 4));
// pwm_dr((adc + 4));
// else
// pwm_dr(adc);
// pwm_dr(filtered_fresh);
// pwm_dr(adc);
 pwm_dr(4);
 TRISIO = 0b00001011;
 CCP1CON = 0b00001100;
 T2CON = 0b00000101;
}

void init_adc (void)
{
 ADCON0 = 0b10000100;
 ADON = 1;
 ADIF = 0;
 ADIE = 1;
 PEIE = 1;
 GIE = 1;
// __delay_us(4);
 GO_nDONE = 1;
}

void pwm_dr (unsigned int dr)
{
 CCPR1L = (dr >> 2);
 CCP1CON = ((CCP1CON & 0x3F) + ((dr << 4) & 0x30));
}

void read_adc (void)
{
 adc = (((unsigned)ADRESH << 8) | ADRESL);
/* filtered_previous = filtered_fresh;
 temp_filtered = 255 * filtered_previous;
 temp_filtered = temp_filtered >> 8;
 sample_previous = sample_fresh;
 sample_fresh = adc;
 temp_sample = 255 * abval((sample_fresh - sample_previous));
 temp_sample = temp_sample >> 8;
 filtered_fresh = temp_filtered + temp_sample; */
// run_pwm();
// status();
}

void status (void)
{
 if(adc < 820)
 SBIT_LED_CHARGING = 0;
 else
 SBIT_LED_CHARGED = 1;
}

int abval (int val)
{
 return (val<0 ? (-val) : val);
}
 






Please attached here find the settings preview.
post edited by HighTechPower - 2021/03/07 01:10:49

Attached Image(s)

#7
HighTechPower
Starting Member
  • Total Posts : 46
  • Reward points : 0
  • Joined: 2021/01/27 01:35:43
  • Status: offline
Re: MCLR as digital input is resetting the device 2021/03/07 01:17:05 (permalink)
0
Please also reply to the following thread which belongs to same schematic and code.
 
CMCON0 and ANSEL registers | Microchip
#8
blue_led
New Users
  • Total Posts : 146
  • Reward points : 0
  • Status: online
Re: MCLR as digital input is resetting the device 2021/03/07 01:20:09 (permalink) ☼ Best Answerby HighTechPower 2021/03/08 04:54:02
0
Are you shure that buzzer switch S1 don't shorten the power supply.
I suggest to use only one section shorten go ground a pullup resistor to MCLR input.
You can use it as reset sw too :-)
post edited by blue_led - 2021/03/07 01:24:23
#9
ric
Super Member
  • Total Posts : 30244
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: MCLR as digital input is resetting the device 2021/03/07 02:01:28 (permalink)
0
Never, ever set the GIE bit yourself inside an interrupt service.
 
If you're reloading the timer count in the ISR, do it the start of the service, not the end, or you add lots of jitter to the count, depending upon what path it takes through the ISR.
 

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!
#10
1and0
Access is Denied
  • Total Posts : 12269
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: MCLR as digital input is resetting the device 2021/03/07 02:49:03 (permalink)
+1 (1)
HighTechPower
I'm not setting configuration bits in code instead using menu bar to set the configuration bits. I'm programming in C and using MPLAB 8.92.

You are NOT setting any configuration bits at all.  When the "Configuration Bits set in code" checkbox is checked (as shown in your image from Post #7) configuration bit values are taken from the code, which you DO NOT have in YOUR code!!!
 
#11
Murton Pike Systems
Super Member
  • Total Posts : 247
  • Reward points : 0
  • Joined: 2020/09/10 02:13:01
  • Location: 0
  • Status: offline
Re: MCLR as digital input is resetting the device 2021/03/07 06:15:10 (permalink)
0
Near top of your code should be config bits:
 
Click on "Windows" "target memory views" "configuartion bit" 
Once set accordingly use "generate source code to output"
 

 
Here is config bits from one of my programs.
// CONFIG
#pragma config FOSC0 = INT // FOSC: Oscillator Selection bit (Internal oscillator mode. I/O function on RA5/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (Watchdog Timer disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (Power-up Timer disabled)
#pragma config MCLRE = OFF // MCLR/VPP Pin Function Select bit (MCLR pin is alternate function)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config BOREN = EN // Brown-out Reset Enable bits (BOR enabled)
#pragma config WRT = OFF // Flash Program Memory Self Write Enable bit (Flash self-write protection off)
#pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function disabled. CLKOUT pin acts as I/O)
 

#12
Jan Audio
Super Member
  • Total Posts : 193
  • Reward points : 0
  • Joined: 2018/09/24 08:12:24
  • Location: 0
  • Status: offline
Re: MCLR as digital input is resetting the device 2021/03/07 06:28:48 (permalink)
0
You need High voltage programming set.
#13
1and0
Access is Denied
  • Total Posts : 12269
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: MCLR as digital input is resetting the device 2021/03/07 07:06:36 (permalink)
+1 (1)
nigelwright7558
Near top of your code should be config bits:
 
Click on "Windows" "target memory views" "configuartion bit" 
Once set accordingly use "generate source code to output"

Here is config bits from one of my programs.
// CONFIG
#pragma config FOSC0 = INT // FOSC: Oscillator Selection bit (Internal oscillator mode. I/O function on RA5/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (Watchdog Timer disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (Power-up Timer disabled)
#pragma config MCLRE = OFF // MCLR/VPP Pin Function Select bit (MCLR pin is alternate function)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config BOREN = EN // Brown-out Reset Enable bits (BOR enabled)
#pragma config WRT = OFF // Flash Program Memory Self Write Enable bit (Flash self-write protection off)
#pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function disabled. CLKOUT pin acts as I/O)


... which is NOT available on the MPLAB v8.92. ;)
 
#14
1and0
Access is Denied
  • Total Posts : 12269
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: MCLR as digital input is resetting the device 2021/03/07 07:09:39 (permalink)
0
@OP: Either uncheck the "Configuration Bits set in code" checkbox in MPLAB or preferably place the following at the beginning of your .c source file, and make sure it is located before the #include line:
// PIC12F683 Configuration Bit Settings

// 'C' source line config statements

// CONFIG
#pragma config FOSC = INTOSCIO  // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF      // Brown Out Detect (BOR disabled)
#pragma config IESO = OFF       // Internal External Switchover bit (Internal External Switchover mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>

 
 
#15
HighTechPower
Starting Member
  • Total Posts : 46
  • Reward points : 0
  • Joined: 2021/01/27 01:35:43
  • Status: offline
Re: MCLR as digital input is resetting the device 2021/03/07 11:52:49 (permalink)
0
1and0
@OP: Either uncheck the "Configuration Bits set in code" checkbox in MPLAB or preferably place the following at the beginning of your .c source file, and make sure it is located before the #include line:
// PIC12F683 Configuration Bit Settings

// 'C' source line config statements

// CONFIG
#pragma config FOSC = INTOSCIO  // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF      // Brown Out Detect (BOR disabled)
#pragma config IESO = OFF       // Internal External Switchover bit (Internal External Switchover mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
 

 
 




I inserted these lines at the start of code but no change and I still get pin 7 reset for a short while when I change switch position.
#16
HighTechPower
Starting Member
  • Total Posts : 46
  • Reward points : 0
  • Joined: 2021/01/27 01:35:43
  • Status: offline
Re: MCLR as digital input is resetting the device 2021/03/07 11:55:32 (permalink)
0
ric
Never, ever set the GIE bit yourself inside an interrupt service.
 
If you're reloading the timer count in the ISR, do it the start of the service, not the end, or you add lots of jitter to the count, depending upon what path it takes through the ISR.
 




You mean I don't add any code inside ISR before re-enabling timer routine?
#17
ric
Super Member
  • Total Posts : 30244
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: MCLR as digital input is resetting the device 2021/03/07 13:51:40 (permalink) ☄ Helpfulby HighTechPower 2021/03/08 00:03:10
0
I mean exactly what I said. The instructions that load the new count value into the timer should come before all the tests. The only "reenabling" required is clearing the xxIF flag, which can be done anywhere before you exit the ISR.
Note, this is a general correct PIC programming issue, not the source of your reported problem.
 
Edit: To be pedantic
  TMR1IF = 0;    // can be anywhere after you have tested that it is set
  TMR1H = 0xCF;    // should be straight after you have tested TMR1IF
  TMR1L = 0x2C;    // should be straight after you have tested TMR1IF
  TMR1IE = 1;    // not required
  PEIE = 1;    // not required
  GIE = 1;    // MUST NOT DO INSIDE AN ISR!
  TMR1ON = 1;    // not required


 
post edited by ric - 2021/03/07 14:39:11

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!
#18
ric
Super Member
  • Total Posts : 30244
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: MCLR as digital input is resetting the device 2021/03/07 14:31:25 (permalink)
0
@OP. If you are using the old MPLAB v8.92, which version of XC8 are you using?
 

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!
#19
Tinkerer7777
Junior Member
  • Total Posts : 117
  • Reward points : 0
  • Joined: 2015/04/04 01:46:02
  • Location: 0
  • Status: offline
Re: MCLR as digital input is resetting the device 2021/03/07 15:53:19 (permalink)
+1 (1)
Also check to see you are sourcing enough power to the "system" if the buzzer pulls too much current when you turn ON, the BOD on RCON register flag will set and it will cause it to reset the whole system. This is a classic case of not sourcing enough current. Hopefully you have a good power source. In any case, you can use persistent storage for RCON (use shadow variable that is persistent storage) so when you get back into turning the system ON, you will have the debugging information available on the next power up and it will have persistent storage. 
#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2021 APG vNext Commercial Version 4.5