Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Power down data save to EEprom - PIC

Status
Not open for further replies.
Do you have a schematic you can post.....?

Did you debounce the switch for the save code ? or disable the interrupt after first edge detected ?
Also if you use a global variable in interrupt did you declare it as volatile ?
Regards, Dana.
No switch involved, just a voltage divider off the unregulated rail to detect a power off condition - this is fed in to PORTB,7 pin set up for interrupt on falling edge trigger.

Divider is 4k7 & 2k2 from power rail with the tap off to the PIC.

The power off condition is as in somebody shut it down, not a random power fail circumstance.
 
Yes to all re the code (see below) and no to Brown out, it is turned off.
So what prevents the chip from running as VDD falls below 1.8V (the min VDD spec).
Once it does that all bets are off until the chip decides not to run any longer.

Divider is 4k7 & 2k2 from power rail with the tap off to the PIC
That means that the external power might have to fall to as low as 2.35V in order to get a valid Vil input low voltage (approx 0.8V)
It will typically trigger before this, but that's the spec.

That's why I recommended running the resistive divider into one of the comparators instead of treating it as a digital input.
 
If power fail ramp rate very slow it will produce, due to noise on Vcc and on power divider
from supply, a possible condition where input looks like a bouncing switch.

Example sim of CMOS inverter, 5V ramp, and 100 mV RMS noise at input of gate. As
you can see output of gate, as ramp passes thru its threshold, oscillates significantly
due to small amount of noise and G thru gate.

1640564533734.png



A Schmidt at input of processor, or use its A/D for V detection, or debounce it in code.


Regards,. Dana.
 
So what prevents the chip from running as VDD falls below 1.8V (the min VDD spec).
Good point - will change that.

Re the divider and how far voltage has to drop - see below.
If power fail ramp rate very slow it will produce, due to noise on Vcc and on power divider
from supply, a possible condition where input looks like a bouncing switch.
Yep, got the scope out and seeing something similar in 'real life' about 50ish % of the time.

The divider voltage also intermittently arrives at the trip point at about the same time as the noise.

Going to follow up on Tumbleweeds comment re using the internal comparator once I finish reading the datasheet and figuring out what settings I need to get it going - not a peripheral I've used (or needed to use) before.

Must admit, I'm actually finding this learning to use new bits of the PIC quite enjoyable.
 
Many of the pins on that chip have a schmitt trigger input. Using one of those may solve your problem. Alternatively, use an analogue pin to read the voltage (only takes <50uS to read one pin).

Mike.
 
What is range of freqs out of the DDS needed ?

There are ARM processors out there with DDS on chip....example of a single chip solution that
used 2 DDS, all onchip -

1621444400166-png.131480


Another approach with same chip, quad wavedac generator -

quad-dds-and-wavedac-jpg.159414



Regards, Dana.
 
Last edited:
Many of the pins on that chip have a schmitt trigger input
Unfortunately, the 16F1827 doesn't let you select the input type, and when used as a regular input pin or IOC they're TTL, not schmitt trigger.
There is a single pin (INT) that's schmitt trigger, but as I've said using a digital input to detect an analog voltage is a bad idea.

Using the ADC would work but that adds a lot of overhead and has timing issues as you have to constantly monitor the pin voltage.
If you're busy doing something else when power falls, well...

Not to beat a dead horse, but if you use the comparator you overcome these issues.
You get fast response, programmable polarity, hysteresis, and a programmable trip point using the DAC or FVR as the other input.
 
Unfortunately, the 16F1827 doesn't let you select the input type, and when used as a regular input pin or IOC they're TTL, not schmitt trigger.
There is a single pin (INT) that's schmitt trigger, but as I've said using a digital input to detect an analog voltage is a bad idea.

Using the ADC would work but that adds a lot of overhead and has timing issues as you have to constantly monitor the pin voltage.
If you're busy doing something else when power falls, well...

Not to beat a dead horse, but if you use the comparator you overcome these issues.
You get fast response, programmable polarity, hysteresis, and a programmable trip point using the DAC or FVR as the other input.

Easier to just use a more modern device, which gives you more of everything, including selectable Schmitt inputs.

For some strange reason MicroChip seem to have dropped the 18 pin size, and moved to 20 pin on many devices - the 16F18446 would be a good choice, or if less pins are suitable the 16F18426 comes in a 14 pin package.
 
The onchip comparator has hysteresis, if V divider junction noise < Vhys of
comparator you should be all right. If necessary use bypass cap on Vdivider
junction going to chip to lower noise into comparator.

Caps to use, keep ESR performance in mind when you select Cap technology.

iu



Regards, Dana.
 
What is range of freqs out of the DDS needed ?
From about 500kHz to 30Mhz depending on the builders needs (usually in a small band eg 5-5.5MHz).
Easier to just use a more modern device, which gives you more of everything, including selectable Schmitt inputs.
Well, it would be if I didn't have to use that MPlab X rubbish - I'm sticking to what I can compile for using the last version of MPlab (8.92).

I don't do stuff for the commercial market anymore, just the hobbyist - mostly amateur radio - so it doesn't need to be the latest whiz-bang do everything including make coffee doo-hickey.
Not to beat a dead horse, but if you use the comparator you overcome these issues.
You get fast response, programmable polarity, hysteresis, and a programmable trip point using the DAC or FVR as the other input.
Working on that today - I was listening, I had to shuffle some functions around to different pins to get to the comparator pins.

For the moment, I will bring both inputs to the comparator out to pins, with dividers on both (as you would with a stand alone analog comparator chip) and then when I have that working get the FVR/DAC interfaced.

Will probably be back asking dumb questions about how to get the comparator working later, this 66 year-old brain gets too easily confused these days :)
 
Well, it would be if I didn't have to use that MPlab X rubbish - I'm sticking to what I can compile for using the last version of MPlab (8.92).

MPLABX is perfectly fine, once you get used to it! - and you wouldn't want to go back to the old MPLAB - the later devices have so many extras that it's a no-brainer upgrading :D

I redesigned one of our products at work, moving from the 16F1827/47 to the 16F18446 which enabled me to dispense with the extra Schmitt trigger chip used on the inputs, so lowered the manufacturing price, and made it faster to build. The extra memory space also allowed increased facilities - and rather interestingly my boss told me the other week that he's just sold one to New Zealand :D
 
About to get the FVR working.
The FVR will give you a choice of three ref points... 1V, 2V, or 4V (assuming you're using a 5V LDO).
The 5-bit DAC allows a bit more flexibility since that gives you 32 different settings.

Now that you're using the comparator you can set the resistors to tell you when power begins to fall
instead of trying to set the ratio to generate a logic low. Using the DAC as the ref you should just have to scale
the input resistors so that the max voltage is always < VDD. The DAC would let you adjust the trip point.

You could even make it programmable to account for different input supply voltages.
 
Having trouble getting it to work with the FVR at the moment, more than likely something I've missed.

Resistor network is set to feed a tad over 4.4V to the comparator -ve input and setting the FVR for 4.096V on the comparator +v internally.

I've got a few things on the todo list for today, so will get back to it tomorrow.
 
Here's the code, I stripped everything else out to play with the comparator.

C-like:
; *******************************************************************************
;                    comparator-16F1827.asm
;********************************************************************************
;                                                                             
; Target Controller        PIC16F1827
;                __________                                         
;        COMP-PLUS--RA2 |1       18| RA1---SPARE
;        COMP-NEG---RA3 |2       17| RA0---DDS_LOAD
;        COMP-OUT---RA4 |3       16| RA7---DDS_DATA
;        SPARE------RA5 |4       15| RA6---DDS_CLK
;        Ground-----Vss |5       14| VDD---+5 V
;        ENCODER----RB0 |6       13| RB7---SPARE
;        ENCODER----RB1 |7       12| RB6---LED STEP 10kHz
;        STEP SW----RB2 |8       11| RB5---LED STEP 1kHz
;        CAL SW-----RB3 |9       10| RB4---LED STEP 10Hz
;                ----------                                         
;                                                                             
; *******************************************************************************
; * Device type and options                            *
; *******************************************************************************
;
        processor 16F1827
        radix     dec
    errorlevel -207    ; Skip found label after column 1
    errorlevel -302    ; Skip out of bank nuisance messages
    errorlevel -303    ; Skip program word too large. Truncated to core size
;
; *******************************************************************************
; * Configuration fuse information for 16F1827:                    *
; *******************************************************************************
;
        include   <P16F1827.INC>

 __CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
 __CONFIG _CONFIG2, _WRT_OFF & _PLLEN_OFF & _STVREN_OFF & _BORV_LO & _LVP_OFF
;
; *******************************************************************************
; * Assign names to IO pins.                            *
; *******************************************************************************

;
;    16F1827 Oscillator setup
    ; OSCCON - Oscillator control reg.
    ; ---------------------------------
    ; SPLLEN b7 enable PLL x4
    ; 1 = enabled 0 = disabled
    ; IRCF | b6-3 frequency selection
    ; 1111 = 16MHz HF
    ; 1110 = 8 or 32MHz HF
    ; 1101 = 4MHz HF
    ; 1100 = 2MHz HF
    ; 1011 = 1MHz HF
    ; 1010 = 500kHz HF
    ; 1001 = 250kHz HF
    ; 1000 = 125kHz HF
    ; 0111 = 500kHz MF (default)
    ; 0110 = 250kHz MF
    ; 0101 = 125kHz MF
    ; 0100 = 62.5kHz MF
    ; 0011 = 31.25kHz HF
    ; 0010 = 31.25kHz MF
    ; 000x = 31.25kHz LF
    ; Reserved B2 reserved, 0
    ; SCS      B1 0-: 1x = int. OSC.
    ; 01 = Timer1 oscillator
    ; 00 = determined by FOSC <2:0> in Configuration
    ; POR default 00111-00 500 kHz (POR = Power On Reset)

OSCCONVAL    EQU    b'01101000'    ; 4MhZ CLOCK
;
; *******************************************************************************
; *           Allocate variables in general purpose register space        *
; *******************************************************************************
;
    CBLOCK    0x20        ; Start Data Block



    ENDC            ; End of Data Block

    CBLOCK    0x70        ; Use bank common locations - saves bank switching

    ENDC            ; End of Data Block
;
; *******************************************************************************
; *    Macro's                                    *
; *******************************************************************************
;
LED_on    macro
    bsf    PORTA,1
    endm

LED_off macro
    bcf    PORTA,1
    endm
;
; *******************************************************************************
; * Purpose:  This is the start of the program.                    *
; *******************************************************************************
;
;
    ORG     0x0000               
    goto    start        ; Jump to main program

    ORG     0x0004        ; interrupt routine data save

    bcf    INTCON,GIE    ; Clear Global Interrupt Enable bit

    BANKSEL    PIR2
    bcf    PIR2,C2IF    ; Clear comparator flag
    movlb    0

    clrf    PORTA
    clrf    PORTB

    LED_on

    goto    $

    retfie             ; Enable general interrupts and return
;
;--------------------------------------------------------------------------------

start
    clrf    INTCON        ; clear INTCON
    clrf    PORTA
    clrf    PORTB

; Set PIC oscillator frequency
    banksel    OSCCON        ; Select OSCCON
    movlw    OSCCONVAL    ; Oscillator frequency
    movwf    OSCCON        ; Loads the wanted value

; Configures all I / O as digital
    Banksel    ANSELA
    movlw    b'00001000'    ; RA3 analog all others digital
    movwf    ANSELA
    clrf    ANSELB        ; PORTB all digital

; Disable all wakeup pull-ups
    Banksel    WPUA
    clrf    WPUA
    clrf    WPUB

    banksel    OPTION_REG
    movlw    b'10000111'    ; Pull-ups disabled, TMR0 clock source internal
                ; clock, prescaler to TMR0, set TMR0 prescaler
    movwf    OPTION_REG    ; 1:256
;
    movlw    b'00111100'    ; PORTA (RA2:5 inputs RA0:1 & 6:7 outputs)     
    movwf    TRISA        ;
    movlw    b'10001111'    ; PORTB 0:3 & 7 inputs 4:6 outputs
    movwf    TRISB        ; NOTE: Pull-up via 10k resistor all unused pins
;
    BANKSEL CM1CON0
    clrf    CM1CON0
    clrf    CM1CON1
    movlw    b'10000110'
    movwf    CM2CON0        ; Comparator enabled
                ; Comparator Output bit polarity
                ; Comparator Output internal
                ; Comparator output is not inverted
                ; Unimplemented
                ; Comparator operates in normal power, higher speed mode
                ; Comparator hysteresis enabled
                ; Comparator output to Timer1 and I/O pin is asynchronous
    movlw    b'10100011'
    movwf    CM2CON1        ; Positive interrupt on
                ; Negative interrupt off
                ; C2VP connects to FVR
                ; C2VP connects to FVR
                ; Unimplemented
                ; Unimplemented
                ; C2VN connects to C12IN3- pin - RA3
                ; C2VN connects to C12IN3- pin - RA3

    movlw    b'10001100'    ; FVR on, Comparator ref 4.096V
    movwf    FVRCON

    BANKSEL    PIE2
    bsf    PIE2,C2IE
    bsf    INTCON,PEIE
    movlb    0

main
    bsf    INTCON,GIE
    nop
    goto    $-1

;
        END
;--------------------------------------------------------------------------------
 
A quick look at your code suggests you're turning on the LED only when power is shut off. How do you know what is happening?

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top