News:

Let's find out together what makes a PIC Tick!

Main Menu

MCLR LOW

Started by Yves, Apr 30, 2023, 07:35 AM

Previous topic - Next topic

Yves

Is bringing MCLR low or floating will bring the PIC to a lowest current consumption. I was thinking to use MCLR as a deep sleep even reset of the chip. I'm not sure yet how to do it but if by bringing MCLR to a low sate I could use it as a single button ON/OFF condition. When turning ON by momentary applying the power to the PIC a pin will go high holding MCLR high. When the same button is pressed it will turn the pin low which in turn will bring the MCLR to a low stage. Basically the Pic will commit suicide LOL.

Yves
Yves

trastikata

Holding MCLR low will keep the PIC in reset state which is not low power.

Lowest current consumption is sleep (or where supported deep sleep) and lowest possible frequency for the internal oscillator.

Changing the clock frequency in Positron is done through the SFRs however Positron will use the initial crystal declare for any timing calculations. So if you have watchdog or counters enabled after changing the frequency, you will need to prorate them manually.

Yves

Hi Trastikata,

thank you for your very clear explanation. I will have to figure out something else. Back to the drawing board.

Yves
Yves

trastikata

Quote from: Yves on Apr 30, 2023, 10:06 AMthank you for your very clear explanation. I will have to figure out something else. Back to the drawing board.

Hello Yves,

you might not need to change anything in the design, depending on what PIC is being used.

The MCLR pin can be used as Input and by pressing the button you change it back to Reset function prior to enter into Sleep mode. Now by pressing the MCLR you can reset the device and bring it to normal mode and turn the pin back to input ... some EEPROM/HEF data can be used for keeping important MCU state information.

tumbleweed

QuoteThe MCLR pin can be used as Input and by pressing the button you change it back to Reset function
How do you do that?

On the devices I've used the MCLR function is controlled by a CONFIG setting, so it's not runtime programmable.

RGV250

Hi,
If you want to use sleep, Les did a good example here.
https://protoncompiler.com/index.php/topic,1340.msg10231.html#msg10231

Bob

trastikata

#6
Quote from: tumbleweed on Apr 30, 2023, 10:43 AMOn the devices I've used the MCLR function is controlled by a CONFIG setting, so it's not runtime programmable.

In 18F devices the configuration bits are self-programmable at run-time and can be re-written in bytes without the need of erasure.

Quote from: PIC18F2553 DatasheetProgramming the Configuration registers is done in a manner similar to programming the Flash memory. The WR bit in the EECON1 register starts a self-timed write to the Configuration register. In normal operation mode, a TBLWT instruction, with the TBLPTR pointing to the Configuration register, sets up the address and the data for the Configuration register write. Setting the WR bit starts a long write to the Configuration register. The Configuration registers are written a byte at a time. To write or erase a configuration cell, a TBLWT instruction can write a '1' or a '0' into the cell.

Quote from: Microchip AN851Configuration Bits
PIC18F devices allow access to the device configuration bits (addresses starting at 300000h) during normal operation. In the bootloader, the Control register address must point to memory region 300000h to 3FFFFFh to provide Configuration Memory access. Data is read one byte at a time and, unlike Program Memory, is written one byte at a time. Since configuration bits are automatically erased before being written, the erase control bit will have no affect on Configuration Memory.

It is also important to note some configuration bits are single direction bits in Normal mode; they can only be changed to one state, and cannot be changed back. The code protection bits in Configuration registers 5L and 5H are a good example. If any type of code protection is enabled for a block, it cannot be disabled without a device programmer. Essentially, the bootloader cannot reverse code protection. The Device ID (addresses 3FFFFEh and 3FFFFFh) is also considered Program Memory. While they can be accessed, however, they are read only and cannot be altered.

Here's an example for PIC18F2553 - clearing the MCLR bit in Config3H and thus turning it into input (PORTE.3)

    TBLPTRU = $30
    TBLPTRH = $00
    TBLPTRL = $05           'Adress Config3H
    TABLAT = %00000011      'Load TABLAT with value for CONFIG3H
    Asm
        Tblwt*              'Table Write
    EndAsm
    EECON1.7 = 1            'Flash memory Select
    EECON1.6 = 1            'Configuration memory Select
    EECON1.2 = 1            'Allows write cycles to Flash program/data EEPROM
    INTCON.7 = 0            'Global Interrupt Disable
    EECON2 = $55
    EECON2 = $AA
    EECON1.1 = 1            'Set Write Control bit
    Asm
        Nop
        Nop
        Nop
        Nop
        Nop
        Nop
        Nop
        Nop
    EndAsm
    INTCON.7 = 1            'Global Interrupt Enable

And this is a program example I've just tested on a PIC18F2553 test board I have to make sure all works as I remember. The code was only for demonstration purposes, thus it is not so well organized and the Print subs are omitted.

-The PIC is programmed with MCLRE = On and will reset if the Reset button is pressed in the first few seconds until CONFIG3H has been changed.
- The CONFIG3H.MCLR bit is cleared in software and the bCounter variable will increment (and displayed) every time the Reset button is pressed
- When the bCounter variable reaches 6, the CONFIG3H.MCLR bit is set and the next time the Reset button is pushed, the device will reset.


Device = 18F2553
Config_Start
  PLLDIV = 2    ;Divide by 5 (8 MHz oscillator input)
  CPUDIV = OSC1_PLL2    ;[Primary Oscillator Src: /1][96 MHz PLL Src: /2]
  USBDIV = 2    ;USB clock source comes from the 96 MHz PLL divided by 2
  FOSC = HSPLL_HS    ;HS oscillator, PLL enabled (HSPLL)
  FCMEN = OFF    ;Fail-Safe Clock Monitor disabled
  IESO = OFF    ;Oscillator Switchover mode disabled
  PWRT = OFF    ;PWRT disabled
  BOR = On    ;Brown-out Reset enabled in hardware only (SBOREN is disabled)
  BORV = 3    ;Minimum setting
  VREGEN = On    ;USB voltage regulator enabled
  WDT = OFF    ;WDT disabled (control is placed on the SWDTEN bit)
  WDTPS = 128    ;1:128
  CCP2MX = On    ;CCP2 input/output is multiplexed with RC1
  PBADEN = On    ;PORTB<4:0> pins are configured as analog input channels on Reset
  LPT1OSC = OFF    ;Timer1 configured for higher power operation
  MCLRE = On    ;MCLR pin enabled; RE3 input pin disabled
  STVREN = On    ;Stack full/underflow will cause Reset
  LVP = OFF    ;Single-Supply ICSP disabled
  XINST = OFF    ;Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
  Debug = OFF    ;Background debugger disabled, RB6 and RB7 configured as general purpose I/O pins
  Cp0 = OFF    ;Block 0 (000800-001FFFh) is not code-protected
  CP1 = OFF    ;Block 1 (002000-003FFFh) is not code-protected
  CP2 = OFF    ;Block 2 (004000-005FFFh) is not code-protected
  CP3 = OFF    ;Block 3 (006000-007FFFh) is not code-protected
  CPB = OFF    ;Boot block (000000-0007FFh) is not code-protected
  CPD = OFF    ;Data EEPROM is not code-protected
  WRT0 = OFF    ;Block 0 (000800-001FFFh) is not write-protected
  WRT1 = OFF    ;Block 1 (002000-003FFFh) is not write-protected
  WRT2 = OFF    ;Block 2 (004000-005FFFh) is not write-protected
  WRT3 = OFF    ;Block 3 (006000-007FFFh) is not write-protected
  WRTC = OFF    ;Configuration registers (300000-3000FFh) are not write-protected
  WRTB = OFF    ;Boot block (000000-0007FFh) is not write-protected
  WRTD = OFF    ;Data EEPROM is not write-protected
  EBTR0 = OFF    ;Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks
  EBTR1 = OFF    ;Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks
  EBTR2 = OFF    ;Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks
  EBTR3 = OFF    ;Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks
  EBTRB = OFF    ;Boot block (000000-0007FFh) is not protected from table reads executed in other blocks
Config_End

Declare Xtal = 48

Dim bCounter As Byte

On_Interrupt GoTo Isr
GoTo START

START:
    DelayMS 10000
  
'Clear Config3H.MCLR     
    TBLPTRU = $30
    TBLPTRH = $00
    TBLPTRL = $05           'Adress Config3H
    TABLAT = %00000011      'Load TABLAT with value for CONFIG3H
    Asm
        Tblwt*              'Table Write
    EndAsm
    EECON1.7 = 1            'Flash memory Select
    EECON1.6 = 1            'Configuration memory Select
    EECON1.2 = 1            'Allows write cycles to Flash program/data EEPROM
    INTCON.7 = 0            'Global Interrupt Disable
    EECON2 = $55
    EECON2 = $AA
    EECON1.1 = 1            'Set Write Control bit
    Asm
        Nop
        Nop
        Nop
        Nop
        Nop
        Nop
        Nop
        Nop
    EndAsm
    INTCON.7 = 1            'Global Interrupt Enable
   
 
While 1 = 1
    If PORTE.3 = 0 Then  
        Inc bCounter
        PrintLine("                ", 2)
        PrintInteger(bCounter, 3, 2, 0)
        DelayMS 2000
        If bCounter = 6 Then
            GoSub SetReset
            PrintLine("WILL RESET", 1)
        EndIf
    EndIf    
Wend

SetReset:
'Set Config3H.MCLR   
    TBLPTRU = $30
    TBLPTRH = $00
    TBLPTRL = $05           'Adress Config3H
    TABLAT = %10000011      'Load TABLAT with value for CONFIG3H
    Asm
        Tblwt*              'Table Write
    EndAsm
    EECON1.7 = 1            'Flash memory Select
    EECON1.6 = 1            'Configuration memory Select
    EECON1.2 = 1            'Allows write cycles to Flash program/data EEPROM
    INTCON.7 = 0            'Global Interrupt Disable
    EECON2 = $55
    EECON2 = $AA
    EECON1.1 = 1            'Set Write Control bit
    Asm
        Nop
        Nop
        Nop
        Nop
        Nop
        Nop
        Nop
        Nop
    EndAsm
    INTCON.7 = 1            'Global Interrupt Enable
Return


tumbleweed

QuoteAnd this is a program example I've just tested on a PIC18F2553 test board I have to make sure all works as I remember.
I was aware that they can be written on the 18F, but I didn't know that the change took effect immediately... I thought that they were only read at startup/reset.

That's good to know. Thanks.

John Drew

Just for others who may read this thread, I've changed USART fuse settings in a running 18F program. Very handy if you need it.
John