• AVR Freaks

Hot!Frequency of MC is wrong, but not by a (2,4,8) factor

Author
PyThagoras2
PHYSICS ENG, UGRD
  • Total Posts : 22
  • Reward points : 0
  • Joined: 2018/07/16 05:16:16
  • Location: Quebec, Canada
  • Status: offline
2019/07/17 04:44:20 (permalink)
0

Frequency of MC is wrong, but not by a (2,4,8) factor

It's been a while that my PIC18F47K42 seems off.
I'm making a countdown on a LCD and it displays up to the millisecond.
So, using the datasheet I set:
 

 
void OSCILLATOR_Initialize(void)
{
// NOSC HFINTOSC; NDIV 1;
OSCCON1 = 0x60;
OSCCON3 = 0x00;
OSCEN = 0x00;
// HFFRQ 8_MHz;
OSCFRQbits.FRQ = 0b0011;
// TUN 0;
OSCTUNE = 0x00;
}
 
void COUNTDOWN_Initialize()
{
/*SET THE TIMER4 REGISTER AND INETRRUPT PARAMETERS*/
PIE7bits.TMR4IE = 1; //ENABLES TIMER1 INTERRUPT
PIR7bits.TMR4IF = 0; // Sets interrupt flag to 0.
T4CONbits.ON = 0; //Stops timer
T4CLKbits.CS = 0b0001; //Fosc/4 (2MHz)
T4CONbits.CKPS = 0b111;//1:16 prescaler
T4PRbits.PR4 = 0x7D; //comparator to 125
T4TMRbits.TMR4 = 0x00;
}

void countdownStart()
{
    T4CONbits.ON = 1;
}
 
void interrupt ISR(void)
{
    if (PIR7bits.TMR4IF == 1)
    {
        T4PRbits.PR4 = 0x7D;
        PIR7bits.TMR4IF = 0;
        countdown_drop_timer();
    }
}
 

 
I figured that with a 8Mhz clock, having the signal hooked on F/4 would give me 2MHz. I need one could to be 0.001s, so 1000Hz. 2MHz/1000Hz = 2000.
So, The timer2 prescaler * counter comparator value should give 2000. [2000000/prescaler)*(1/comparator) = 1000]
So 2000 = Prescaler*comparator.
 
Fortunately, The prescaler goes up to 128.
So I set the prescaler to 16 and set the comparator up to 125. 125*16 = 2000.
 
But the counter is dropping at like 5x faster than it should. one minutes takes exactly 11 seconds to run. Any ideas?
post edited by PyThagoras2 - 2019/07/17 06:45:57
#1

14 Replies Related Threads

    mbrowning
    Just a Member
    • Total Posts : 1455
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: offline
    Re: Frequency of MC is wrong, but not by a (2,4,8) factor 2019/07/17 04:49:01 (permalink)
    +1 (1)
    Show the config settings. They affect this too

    Oh well - there's always next year
    #2
    pcbbc
    Super Member
    • Total Posts : 1188
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: Frequency of MC is wrong, but not by a (2,4,8) factor 2019/07/17 06:14:19 (permalink)
    +1 (1)
    PyThagoras2But the counter is dropping at like 10x faster than it should. one minutes takes exactly 11 seconds to run. Any ideas?



    That's about 5 times faster.  60 seconds / 5 = 12 seconds.
    #3
    ric
    Super Member
    • Total Posts : 23185
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Frequency of MC is wrong, but not by a (2,4,8) factor 2019/07/17 06:16:16 (permalink)
    0
    PyThagoras2
    But the counter is dropping at like 10x faster than it should. one minutes takes exactly 11 seconds to run. Any ideas?

    Are you sure your code to convert counts into minutes is correct? You didn't show any of that code.
    (I assume it is inside countdown_drop_timer() )
    Your ISR should NOT be rewriting T4PR. Just write it once and leave it.
    Also, you don't need to write to the bitfield T4PRbits.PR4, just write to T4PR as it uses all 8 bits of that register.

    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
    PyThagoras2
    PHYSICS ENG, UGRD
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2018/07/16 05:16:16
    • Location: Quebec, Canada
    • Status: offline
    Re: Frequency of MC is wrong, but not by a (2,4,8) factor 2019/07/17 06:58:36 (permalink)
    0
    Yes, 5x faster, sorry for that incoherence.
     
    I'll post the configuration tonight. 
     
    As for the countdown_drop_timer(), I can post it tonight, but it's a rather large function and I didn't want to bother people, but I figured that the timer interrupt was independent of that function. It consists of a bunch of `ifs` which decreases a value in an array depending on what the values are.
    int nowTime[6] = 0,1,0,0,0,0

    So it looks at each index and changes the numbers according to a timer. The mechanics work because it was working well with a previous configuration where I used Timer0 for the count. At each interrupt I would:
    1- stop the timer
    2- reset the TMR0H and TMR0L bytes to the desired value
    3- drop_down_timer() [pretty long function]
    4- reset the flag
    5- turn the timer on
     
    A friend told me it was bad practice, since a timer's purpose is exactly to be running without being stopped to do something. So my timer was working well, but had some minor delays. One minute was like 65 seconds, which made sense to have the timer running slower than what it was setted up to do, because I stopped it for a moment. That's why I changed to this code, hoping the delay would just vanish: nope.
     
    As for the T4PR you are right. I wrote that based on advice from a collegue.
    post edited by PyThagoras2 - 2019/07/17 11:44:29
    #5
    PyThagoras2
    PHYSICS ENG, UGRD
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2018/07/16 05:16:16
    • Location: Quebec, Canada
    • Status: offline
    Re: Frequency of MC is wrong, but not by a (2,4,8) factor 2019/07/17 14:02:44 (permalink)
    0
    This is my whole configuration file:

    #include <xc.h>
    #include "mcc.h"
    /*--------------------------------------------------------------------------|
    | SETUP OF MICROCONTROLLER |
    |--------------------------------------------------------------------------*/
    void SYSTEM_Initialize(void)
    {
    CONFIGURATION_Initialize();
    INTERRUPT_Initialize();
    PMD_Initialize();
    PIN_MANAGER_Initialize();
    OSCILLATOR_Initialize();
    ADC_MANAGER_Initialize();
    }
    void INTERRUPT_Initialize (void)
    {
    #define INTERRUPT_GlobalInterruptEnable() (INTCON0bits.GIE = 1)
    #define INTERRUPT_GlobalInterruptDisable() (INTCON0bits.GIE = 0)
    #define INTERRUPT_PriorityEnable() (INTCON0bits.IPEN = 1)
    #define INTERRUPT_PriorityDisable() (INTCON0bits.IPEN = 0)
    #define INTERRUPT_GlobalLowPriorityEnable() (INTCON0bits.GIEL = 1)
    #define INTERRUPT_GlobalLowPriorityDisable() (INTCON0bits.GIEL = 0)
    #define INTERRUPT_GlobalHighPriorityEnable() (INTCON0bits.GIEH = 1)
    #define INTERRUPT_GlobalHighPriorityDisable() (INTCON0bits.GIEH = 0)

    INTERRUPT_GlobalInterruptEnable();
    INTERRUPT_GlobalLowPriorityEnable();
    INTERRUPT_GlobalHighPriorityEnable();
    INTERRUPT_PriorityEnable();
    }
    void OSCILLATOR_Initialize(void)
    {
    // NOSC HFINTOSC; NDIV 1;
    OSCCON1 = 0x60;
    // CSWHOLD may proceed; SOSCPWR Low power;
    OSCCON3 = 0x00;
    // MFOEN disabled; LFOEN disabled; ADOEN disabled; SOSCEN disabled; EXTOEN disabled; HFOEN disabled;
    OSCEN = 0x00;
    // HFFRQ 8_MHz;
    OSCFRQbits.FRQ = 0b0011;
    // TUN 0;
    OSCTUNE = 0x00;
    }
    void PMD_Initialize(void)
    {
    // CLKRMD CLKR enabled; SYSCMD SYSCLK enabled; SCANMD SCANNER enabled; FVRMD FVR enabled; IOCMD IOC enabled; CRCMD CRC enabled; HLVDMD HLVD enabled; NVMMD NVM enabled;
    PMD0 = 0x00;
    // NCO1MD DDS(NCO1) enabled; TMR0MD TMR0 enabled; TMR1MD TMR1 enabled; TMR4MD TMR4 enabled; TMR5MD TMR5 enabled; TMR2MD TMR2 enabled; TMR3MD TMR3 enabled; TMR6MD TMR6 enabled;
    PMD1 = 0x00;
    // ZCDMD ZCD enabled; DACMD DAC enabled; CMP1MD CMP1 enabled; ADCMD ADC enabled; CMP2MD CMP2 enabled;
    PMD2 = 0x00;
    // CCP2MD CCP2 enabled; CCP1MD CCP1 enabled; CCP4MD CCP4 enabled; CCP3MD CCP3 enabled; PWM6MD PWM6 enabled; PWM5MD PWM5 enabled; PWM8MD PWM8 enabled; PWM7MD PWM7 enabled;
    PMD3 = 0x00;
    // CWG3MD CWG3 enabled; CWG2MD CWG2 enabled; CWG1MD CWG1 enabled;
    PMD4 = 0x00;
    // U2MD UART2 enabled; U1MD UART1 enabled; SPI1MD SPI1 enabled; I2C2MD I2C2 enabled; I2C1MD I2C1 enabled;
    PMD5 = 0x00;
    // DSMMD DSM1 enabled; CLC3MD CLC3 enabled; CLC4MD CLC4 enabled; SMT1MD SMT1 enabled; CLC1MD CLC1 enabled; CLC2MD CLC2 enabled;
    PMD6 = 0x00;
    // DMA1MD DMA1 enabled; DMA2MD DMA2 enabled;
    PMD7 = 0x00;
    }
    void PIN_MANAGER_Initialize(void)
    {
    //LATx REGISTER (Set1 : Cause pin to be HIGH)
    LATE = 0x00;
    LATD = 0x00;
    LATA = 0x00;
    LATB = 0x00;
    LATC = 0x00;
    //TRISx REGISTER (Set1 : Cause pin to be input)


    //ANSELx REGISTER (Set1 : Cause reads to be analog.)
    ANSELD = 0x00;
    //WPUx REGISTER (Set1 : Weak pull-ups make input pin High until grounded)
    WPUD = 0x00;
    WPUA = 0x00;
    //ODCx REGISTER (Set1 : make output a drain rather than source)
    ODCONA = 0x00;
    ODCOND = 0x00;
    //INTERRUPT ON CHANGE
    PIE0bits.IOCIE = 1;

    }
    void ADC_MANAGER_Initialize(void)
    {
    //ALL PORTB IS GONNA BE DEDICATED TO ADC INPUTS
    }
    void CONFIGURATION_Initialize(void)
    {
    // CONFIG1L
    #pragma config FEXTOSC = OFF // External Oscillator Selection->Oscillator not enabled
    #pragma config RSTOSC = HFINTOSC_1MHZ // Reset Oscillator Selection->HFINTOSC with HFFRQ = 4 MHz and CDIV = 4:1
    // CONFIG1H
    #pragma config CLKOUTEN = OFF // Clock out Enable bit->CLKOUT function is disabled
    #pragma config PR1WAY = ON // PRLOCKED One-Way Set Enable bit->PRLOCK bit can be cleared and set only once
    #pragma config CSWEN = ON // Clock Switch Enable bit->Writing to NOSC and NDIV is allowed
    #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit->Fail-Safe Clock Monitor enabled
    // CONFIG2L
    #pragma config MCLRE = EXTMCLR // MCLR Enable bit->If LVP = 0, MCLR pin is MCLR; If LVP = 1, RE3 pin function is MCLR
    #pragma config PWRTS = PWRT_OFF // Power-up timer selection bits->PWRT is disabled
    #pragma config MVECEN = OFF // Multi-vector enable bit->Interrupt contoller does not use vector table to prioritze interrupts
    #pragma config IVT1WAY = ON // IVTLOCK bit One-way set enable bit->IVTLOCK bit can be cleared and set only once
    #pragma config LPBOREN = OFF // Low Power BOR Enable bit->ULPBOR disabled
    #pragma config BOREN = SBORDIS // Brown-out Reset Enable bits->Brown-out Reset enabled , SBOREN bit is ignored
    // CONFIG2H
    #pragma config BORV = VBOR_2P45 // Brown-out Reset Voltage Selection bits->Brown-out Reset Voltage (VBOR) set to 2.45V
    #pragma config ZCD = OFF // ZCD Disable bit->ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON
    #pragma config PPS1WAY = ON // PPSLOCK bit One-Way Set Enable bit->PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle
    #pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit->Stack full/underflow will cause Reset
    #pragma config DEBUG = OFF // Debugger Enable bit->Background debugger disabled
    #pragma config XINST = OFF // Extended Instruction Set Enable bit->Extended Instruction Set and Indexed Addressing Mode disabled
    // CONFIG3L
    #pragma config WDTCPS = WDTCPS_31 // WDT Period selection bits->Divider ratio 1:65536; software control of WDTPS
    #pragma config WDTE = OFF // WDT operating mode->WDT Disabled; SWDTEN is ignored
    // CONFIG3H
    #pragma config WDTCWS = WDTCWS_7 // WDT Window Select bits->window always open (100%); software control; keyed access not required
    #pragma config WDTCCS = SC // WDT input clock selector->Software Control
    // CONFIG4L
    #pragma config BBSIZE = BBSIZE_512 // Boot Block Size selection bits->Boot Block size is 512 words
    #pragma config BBEN = OFF // Boot Block enable bit->Boot block disabled
    #pragma config SAFEN = OFF // Storage Area Flash enable bit->SAF disabled
    #pragma config WRTAPP = OFF // Application Block write protection bit->Application Block not write protected
    // CONFIG4H
    #pragma config WRTB = OFF // Configuration Register Write Protection bit->Configuration registers (300000-30000Bh) not write-protected
    #pragma config WRTC = OFF // Boot Block Write Protection bit->Boot Block (000000-0007FFh) not write-protected
    #pragma config WRTD = OFF // Data EEPROM Write Protection bit->Data EEPROM not write-protected
    #pragma config WRTSAF = OFF // SAF Write protection bit->SAF not Write Protected
    #pragma config LVP = ON // Low Voltage Programming Enable bit->Low voltage programming enabled. MCLR/VPP pin function is MCLR. MCLRE configuration bit is ignored
    // CONFIG5L
    #pragma config CP = OFF // PFM and Data EEPROM Code Protection bit->PFM and Data EEPROM code protection disabled
    }
     

    #6
    Antipodean
    Super Member
    • Total Posts : 1722
    • Reward points : 0
    • Joined: 2008/12/09 10:19:08
    • Location: Didcot, United Kingdom
    • Status: offline
    Re: Frequency of MC is wrong, but not by a (2,4,8) factor 2019/07/17 14:11:37 (permalink)
    +1 (1)
    I'm not surprised it doesn't work properly. 
     
    You don't put configuration word properties inside a procedure ...
     
     

    Do not use my alias in your message body when replying, your message will disappear ...

    Alan
    #7
    PyThagoras2
    PHYSICS ENG, UGRD
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2018/07/16 05:16:16
    • Location: Quebec, Canada
    • Status: offline
    Re: Frequency of MC is wrong, but not by a (2,4,8) factor 2019/07/17 14:18:45 (permalink)
    0
    I wasn't aware you needed to program those.
    I thought they had default values. Are there other parameters that are crucial to the MCU initialisation ?
    I forgot to mention that I rarely touch MCU. It is mainly for educational purposes.
    post edited by PyThagoras2 - 2019/07/17 14:20:38
    #8
    ric
    Super Member
    • Total Posts : 23185
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Frequency of MC is wrong, but not by a (2,4,8) factor 2019/07/17 15:00:40 (permalink)
    +2 (2)
    As anti-podean already pointed out, these are NOT executable statements:
    void CONFIGURATION_Initialize(void)
    {
    // CONFIG1L
    #pragma config FEXTOSC = OFF // External Oscillator Selection->Oscillator not enabled
    #pragma config RSTOSC = HFINTOSC_1MHZ // Reset Oscillator Selection->HFINTOSC with HFFRQ = 4 MHz and CDIV = 4:1
    // CONFIG1H
    #pragma config CLKOUTEN = OFF // Clock out Enable bit->CLKOUT function is disabled
    #pragma config PR1WAY = ON // PRLOCKED One-Way Set Enable bit->PRLOCK bit can be cleared and set only once
    #pragma config CSWEN = ON // Clock Switch Enable bit->Writing to NOSC and NDIV is allowed
    #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit->Fail-Safe Clock Monitor enabled
    // CONFIG2L
    #pragma config MCLRE = EXTMCLR // MCLR Enable bit->If LVP = 0, MCLR pin is MCLR; If LVP = 1, RE3 pin function is MCLR
    #pragma config PWRTS = PWRT_OFF // Power-up timer selection bits->PWRT is disabled
    #pragma config MVECEN = OFF // Multi-vector enable bit->Interrupt contoller does not use vector table to prioritze interrupts
    #pragma config IVT1WAY = ON // IVTLOCK bit One-way set enable bit->IVTLOCK bit can be cleared and set only once
    #pragma config LPBOREN = OFF // Low Power BOR Enable bit->ULPBOR disabled
    #pragma config BOREN = SBORDIS // Brown-out Reset Enable bits->Brown-out Reset enabled , SBOREN bit is ignored
    // CONFIG2H
    #pragma config BORV = VBOR_2P45 // Brown-out Reset Voltage Selection bits->Brown-out Reset Voltage (VBOR) set to 2.45V
    #pragma config ZCD = OFF // ZCD Disable bit->ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON
    #pragma config PPS1WAY = ON // PPSLOCK bit One-Way Set Enable bit->PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle
    #pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit->Stack full/underflow will cause Reset
    #pragma config DEBUG = OFF // Debugger Enable bit->Background debugger disabled
    #pragma config XINST = OFF // Extended Instruction Set Enable bit->Extended Instruction Set and Indexed Addressing Mode disabled
    // CONFIG3L
    #pragma config WDTCPS = WDTCPS_31 // WDT Period selection bits->Divider ratio 1:65536; software control of WDTPS
    #pragma config WDTE = OFF // WDT operating mode->WDT Disabled; SWDTEN is ignored
    // CONFIG3H
    #pragma config WDTCWS = WDTCWS_7 // WDT Window Select bits->window always open (100%); software control; keyed access not required
    #pragma config WDTCCS = SC // WDT input clock selector->Software Control
    // CONFIG4L
    #pragma config BBSIZE = BBSIZE_512 // Boot Block Size selection bits->Boot Block size is 512 words
    #pragma config BBEN = OFF // Boot Block enable bit->Boot block disabled
    #pragma config SAFEN = OFF // Storage Area Flash enable bit->SAF disabled
    #pragma config WRTAPP = OFF // Application Block write protection bit->Application Block not write protected
    // CONFIG4H
    #pragma config WRTB = OFF // Configuration Register Write Protection bit->Configuration registers (300000-30000Bh) not write-protected
    #pragma config WRTC = OFF // Boot Block Write Protection bit->Boot Block (000000-0007FFh) not write-protected
    #pragma config WRTD = OFF // Data EEPROM Write Protection bit->Data EEPROM not write-protected
    #pragma config WRTSAF = OFF // SAF Write protection bit->SAF not Write Protected
    #pragma config LVP = ON // Low Voltage Programming Enable bit->Low voltage programming enabled. MCLR/VPP pin function is MCLR. MCLRE configuration bit is ignored
    // CONFIG5L
    #pragma config CP = OFF // PFM and Data EEPROM Code Protection bit->PFM and Data EEPROM code protection disabled
    }

    They should appear OUTSIDE any function declaration, at the top of ONE of your C files.
    They MIGHT work where you have them, but doing it that way is pointless.
    They set how your PIC is configured BEFORE any code starts running, so it is nonsensical to put them inside a function.
     

    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!
    #9
    Aussie Susan
    Super Member
    • Total Posts : 3607
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: Frequency of MC is wrong, but not by a (2,4,8) factor 2019/07/17 18:15:55 (permalink)
    0
    And don't try to set the DEBUG configuration item to anything - the IDE will take care of that for you.
    Susan
    #10
    PyThagoras2
    PHYSICS ENG, UGRD
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2018/07/16 05:16:16
    • Location: Quebec, Canada
    • Status: offline
    Re: Frequency of MC is wrong, but not by a (2,4,8) factor 2019/07/17 19:29:52 (permalink)
    0
    Adding the config file and putting the pragma outside of the function, it slowed down everything, but now the countdown still takes takes 11 seconds for a 60 seconds. Here's all the code of the relevant code related to this timer:
     
    device_config.h

     
     
     
     
     
     
     
    #ifndef DEVICE_CONFIG_H
    #define DEVICE_CONFIG_H
     
     
     
     
     
     
     
    #define _XTAL_FREQ 8000000
     
     
     
     
     
     
     
    #endif
     
     
     
     
     
     
     

     
    device_config.c

     
     
     
     
     
     
     
    #pragma config FEXTOSC = OFF // External Oscillator Selection->Oscillator not enabled
    #pragma config RSTOSC = HFINTOSC_1MHZ // Reset Oscillator Selection->HFINTOSC with HFFRQ = 4 MHz and CDIV = 4:1
     
     
     
     
     
     
     
    // CONFIG1H
    #pragma config CLKOUTEN = OFF // Clock out Enable bit->CLKOUT function is disabled
    #pragma config PR1WAY = ON // PRLOCKED One-Way Set Enable bit->PRLOCK bit can be cleared and set only once
    #pragma config CSWEN = ON // Clock Switch Enable bit->The NOSC and NDIV bits cannot be changed by user software
    #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit->Fail-Safe Clock Monitor enabled
     
     
     
     
     
     
     
    // CONFIG2L
    #pragma config MCLRE = EXTMCLR // MCLR Enable bit->If LVP = 0, MCLR pin is MCLR; If LVP = 1, RE3 pin function is MCLR
    #pragma config PWRTS = PWRT_OFF // Power-up timer selection bits->PWRT is disabled
    #pragma config MVECEN = OFF // Multi-vector enable bit->Interrupt contoller does not use vector table to prioritze interrupts
    #pragma config IVT1WAY = ON // IVTLOCK bit One-way set enable bit->IVTLOCK bit can be cleared and set only once
    #pragma config LPBOREN = OFF // Low Power BOR Enable bit->ULPBOR disabled
    #pragma config BOREN = SBORDIS // Brown-out Reset Enable bits->Brown-out Reset enabled , SBOREN bit is ignored
     
     
     
     
     
     
     
    // CONFIG2H
    #pragma config BORV = VBOR_2P45 // Brown-out Reset Voltage Selection bits->Brown-out Reset Voltage (VBOR) set to 2.45V
    #pragma config ZCD = OFF // ZCD Disable bit->ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON
    #pragma config PPS1WAY = ON // PPSLOCK bit One-Way Set Enable bit->PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle
    #pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit->Stack full/underflow will cause Reset
    #pragma config DEBUG = OFF // Debugger Enable bit->Background debugger disabled
    #pragma config XINST = OFF // Extended Instruction Set Enable bit->Extended Instruction Set and Indexed Addressing Mode disabled
     
     
     
     
     
     
     
    // CONFIG3L
    #pragma config WDTCPS = WDTCPS_31 // WDT Period selection bits->Divider ratio 1:65536; software control of WDTPS
    #pragma config WDTE = OFF // WDT operating mode->WDT Disabled; SWDTEN is ignored
     
     
     
     
     
     
     
    // CONFIG3H
    #pragma config WDTCWS = WDTCWS_7 // WDT Window Select bits->window always open (100%); software control; keyed access not required
    #pragma config WDTCCS = SC // WDT input clock selector->Software Control
     
     
     
     
     
     
     
    // CONFIG4L
    #pragma config BBSIZE = BBSIZE_512 // Boot Block Size selection bits->Boot Block size is 512 words
    #pragma config BBEN = OFF // Boot Block enable bit->Boot block disabled
    #pragma config SAFEN = OFF // Storage Area Flash enable bit->SAF disabled
    #pragma config WRTAPP = OFF // Application Block write protection bit->Application Block not write protected
     
     
     
     
     
     
     
    // CONFIG4H
    #pragma config WRTB = OFF // Configuration Register Write Protection bit->Configuration registers (300000-30000Bh) not write-protected
    #pragma config WRTC = OFF // Boot Block Write Protection bit->Boot Block (000000-0007FFh) not write-protected
    #pragma config WRTD = OFF // Data EEPROM Write Protection bit->Data EEPROM not write-protected
    #pragma config WRTSAF = OFF // SAF Write protection bit->SAF not Write Protected
    #pragma config LVP = ON // Low Voltage Programming Enable bit->Low voltage programming enabled. MCLR/VPP pin function is MCLR. MCLRE configuration bit is ignored
     
     
     
     
     
     
     
    // CONFIG5L
    #pragma config CP = OFF // PFM and Data EEPROM Code Protection bit->PFM and Data EEPROM code protection disabled
     
     
     
     
     
     
     

     
    mcc.h

     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    #define MCC_H
    #include <xc.h>
    #include "device_config.h"
    #include <stdint.h>
    #include <stdbool.h>
    /*GENERAL DEFINES*/
    #define _XTAL_FREQ 8000000
     
     
     
     
     
     
     
    /*GLOBAL USER VARIABLES*/
    char password[8] = {'1','1','1','1','1','1','1'};//has to have a 0 at end for strcomp
    char userPassword[8] = {0,0,0,0,0,0,0};
    char userPswdEntry[8] = {0,0,0,0,0,0,0};
     
     
     
     
     
     
     
    /*GLOBAL MAIN INTERFACE VARIABLES*/
    char mode = 0;
    char countdownMode = 0;
    char alarmMode = 1;
     
     
     
     
     
     
     
    int keyIterrator = 0;
    int isPasswordTrue = 0;
     
     
     
     
     
     
     
    int timerChange = 0;
    int passwordChange = 0;
     
     
     
     
     
     
     
    int status = 0;
    int stanby = 0;
    int armed = 1;
    int disarmed = 2;
    int done = 3;
     
     
     
     
     
     
     
    /*OPTIONS FLAGS*/
    int keyBeeps = 0; int keyBeepsMem = 0;
    int countdownBeeps = 1; int countdownBeepsMem = 1;
     
     
     
     
     
     
     
    int cursorBlinkMem = 0;
     
     
     
     
     
     
     
    /*KEYWORDS*/
    int ON = 1;
    int OFF = 0;
    int TRUE = 1;
    int FALSE = 0;
    int NONE = 0;
     
     
     
     
     
     
     


    void SYSTEM_Initialize(void);
     
     
     
     
     
     
     
    void INTERRUPT_Initialize(void);
     
     
     
     
     
     
     
    void TIMER0_Initialize(void);
     
     
     
     
     
     
     
    void OSCILLATOR_Initialize(void);
     
     
     
     
     
     
     
    void PMD_Initialize(void);
     
     
     
     
     
     
     
    void PIN_MANAGER_Initialize (void);
     
     
     
     
     
     
     

     
     
    mcc.c

     
     
     
     
     
     
     
    #include <xc.h>
    #include "mcc.h"
    /*--------------------------------------------------------------------------|
    | SETUP OF MICROCONTROLLER |
    |--------------------------------------------------------------------------*/
     
     
     
     
     
     
     
    void SYSTEM_Initialize(void)
    {
    INTERRUPT_Initialize();
    PMD_Initialize();
    PIN_MANAGER_Initialize();
    OSCILLATOR_Initialize();
    ADC_MANAGER_Initialize();
    }
     
     
     
     
     
     
     
    void INTERRUPT_Initialize (void)
    {
    #define INTERRUPT_GlobalInterruptEnable() (INTCON0bits.GIE = 1)
    #define INTERRUPT_GlobalInterruptDisable() (INTCON0bits.GIE = 0)
    #define INTERRUPT_PriorityEnable() (INTCON0bits.IPEN = 1)
    #define INTERRUPT_PriorityDisable() (INTCON0bits.IPEN = 0)
    #define INTERRUPT_GlobalLowPriorityEnable() (INTCON0bits.GIEL = 1)
    #define INTERRUPT_GlobalLowPriorityDisable() (INTCON0bits.GIEL = 0)
    #define INTERRUPT_GlobalHighPriorityEnable() (INTCON0bits.GIEH = 1)
    #define INTERRUPT_GlobalHighPriorityDisable() (INTCON0bits.GIEH = 0)

    INTERRUPT_GlobalInterruptEnable();
    INTERRUPT_GlobalLowPriorityEnable();
    INTERRUPT_GlobalHighPriorityEnable();
    INTERRUPT_PriorityEnable();
    }
     
     
     
     
     
     
     

    void OSCILLATOR_Initialize(void)
    {
    // NOSC HFINTOSC; NDIV 1;
    OSCCON1 = 0x60;
    // CSWHOLD may proceed; SOSCPWR Low power;
    OSCCON3 = 0x00;
    // MFOEN disabled; LFOEN disabled; ADOEN disabled; SOSCEN disabled; EXTOEN disabled; HFOEN disabled;
    OSCEN = 0x00;
    // HFFRQ 8_MHz;
    OSCFRQbits.FRQ = 0b0011;
    // TUN 0;
    OSCTUNE = 0x00;
    }
     
     
     
     
     
     
     
    void PMD_Initialize(void)
    {
    // CLKRMD CLKR enabled; SYSCMD SYSCLK enabled; SCANMD SCANNER enabled; FVRMD FVR enabled; IOCMD IOC enabled; CRCMD CRC enabled; HLVDMD HLVD enabled; NVMMD NVM enabled;
    PMD0 = 0x00;
    // NCO1MD DDS(NCO1) enabled; TMR0MD TMR0 enabled; TMR1MD TMR1 enabled; TMR4MD TMR4 enabled; TMR5MD TMR5 enabled; TMR2MD TMR2 enabled; TMR3MD TMR3 enabled; TMR6MD TMR6 enabled;
    PMD1 = 0x00;
    // ZCDMD ZCD enabled; DACMD DAC enabled; CMP1MD CMP1 enabled; ADCMD ADC enabled; CMP2MD CMP2 enabled;
    PMD2 = 0x00;
    // CCP2MD CCP2 enabled; CCP1MD CCP1 enabled; CCP4MD CCP4 enabled; CCP3MD CCP3 enabled; PWM6MD PWM6 enabled; PWM5MD PWM5 enabled; PWM8MD PWM8 enabled; PWM7MD PWM7 enabled;
    PMD3 = 0x00;
    // CWG3MD CWG3 enabled; CWG2MD CWG2 enabled; CWG1MD CWG1 enabled;
    PMD4 = 0x00;
    // U2MD UART2 enabled; U1MD UART1 enabled; SPI1MD SPI1 enabled; I2C2MD I2C2 enabled; I2C1MD I2C1 enabled;
    PMD5 = 0x00;
    // DSMMD DSM1 enabled; CLC3MD CLC3 enabled; CLC4MD CLC4 enabled; SMT1MD SMT1 enabled; CLC1MD CLC1 enabled; CLC2MD CLC2 enabled;
    PMD6 = 0x00;
    // DMA1MD DMA1 enabled; DMA2MD DMA2 enabled;
    PMD7 = 0x00;
    }
     
     
     
     
     
     
     
    void PIN_MANAGER_Initialize(void)
    {
    //LATx REGISTER (Set1 : Cause pin to be HIGH)
    LATE = 0x00;
    LATD = 0x00;
    LATA = 0x00;
    LATB = 0x00;
    LATC = 0x00;
     
     
     
     
     
     
     
    //TRISx REGISTER (Set1 : Cause pin to be input)


    //ANSELx REGISTER (Set1 : Cause reads to be analog.)
    ANSELD = 0x00;
     
     
     
     
     
     
     
    //WPUx REGISTER (Set1 : Weak pull-ups make input pin High until grounded)
    WPUD = 0x00;
    WPUA = 0x00;
     
     
     
     
     
     
     
    //ODCx REGISTER (Set1 : make output a drain rather than source)
    ODCONA = 0x00;
    ODCOND = 0x00;
     
     
     
     
     
     
     
    //INTERRUPT ON CHANGE
    PIE0bits.IOCIE = 1;

    }
     
     
     
     
     
     
     

     
    beeper.h

     
     
     
     
     
     
     
    #define BEEPER_H


    int oldCount = 1;
    int count = 0;
    int variable =0 ;
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    void interrupt ISR(void);
     
     
     
     
     
     
     

     
    beeper.c

     
     
     
     
     
     
     
    #include "mcc.h"
    #include "beeper.h"
     
     
     
     
     
     
     
    #include <xc.h>
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    void interrupt ISR(void)
    {
    if (PIR7bits.TMR4IF == 1)
    {
    //T4PRbits.PR4 = 0x7D;
    PIR7bits.TMR4IF = 0;
    countdown_drop_timer();
    count++;

    variable = count/10;

    if (variable >= oldCount)
    {
    countdown_drop_timer();
    oldCount += 1;
    }


    if (status == done)
    {
    T4CONbits.ON = 0;
    LCD_Set_Cursor(1,1);
    LCD_Write_String("DONE ");
    }
    }
     
     
     
     
     
     
     

       
    and just for the sake of it, here's the drop_timer function

     
     
     
     
     
     
     
    void countdown_drop_timer()
    {
    if (numCountdownNowTime[0]==0 && numCountdownNowTime[1]==0 && numCountdownNowTime[2]==4 && numCountdownNowTime[3]==5)
    {countdownBeeps = 0;
    T0CON0bits.EN = 1;
    }

    if(numCountdownNowTime[5]!=0){numCountdownNowTime[5]--;}
    else
    {
    if (numCountdownNowTime[4]!=0)
    {

    numCountdownNowTime[5]=9;
    numCountdownNowTime[4]--;
    }
    else
    {

    if (numCountdownNowTime[3]!=0)
    {

    if (countdownBeeps == ON){normalBeep();}
    numCountdownNowTime[3]--;
    numCountdownNowTime[5]=9;
    numCountdownNowTime[4]=9;
    }
    else
    {
    if (countdownBeeps == ON){normalBeep();}
    if (numCountdownNowTime[2]!=0)
    {

    numCountdownNowTime[2]--;
    numCountdownNowTime[5]=9;
    numCountdownNowTime[4]=9;
    numCountdownNowTime[3]=9;
    }
    else
    {

    if (numCountdownNowTime[1]!=0)
    {

    numCountdownNowTime[1]--;
    numCountdownNowTime[5]=9;
    numCountdownNowTime[4]=9;
    numCountdownNowTime[3]=9;
    numCountdownNowTime[2]=5;
    }
    else
    {

    if (numCountdownNowTime[0]!=0)
    {
    numCountdownNowTime[0]--;
    numCountdownNowTime[5]=9;
    numCountdownNowTime[4]=9;
    numCountdownNowTime[3]=9;
    numCountdownNowTime[2]=5;
    numCountdownNowTime[1]=9;
    }
    else {countdownStop(); status = done; endBeep();}
     
     
     
     
     
     
     
    }
    }
    }
    }
    }

    countdown_now_num2str();
    LCD_printNowCountdown();
    }
     
     
     
     
     
     
     

     
    I only copied the parts of the code that I thought was relevant to the timer. If there are missing part or missing includes in the header it is possible. the reason why i'm counting multiples of tens is that i'm only displaying centiseconds, but with the prescaler and comparator, it was easier finding a match for a milisecond count. I tested and debugged the function, it works as it is supposed to.
    post edited by PyThagoras2 - 2019/07/18 03:09:42
    #11
    pcbbc
    Super Member
    • Total Posts : 1188
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: Frequency of MC is wrong, but not by a (2,4,8) factor 2019/07/17 20:13:04 (permalink)
    +2 (2)
    int8_t i = 5;
    while (i >= 0) {
    if(numCountdownNowTime[i] != 0) {
    numCountdownNowTime[i]--;
    break;
    }
    numCountdownNowTime[i] = i == 2 ? 5 : 9;
    i--;
    }
    #12
    PyThagoras2
    PHYSICS ENG, UGRD
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2018/07/16 05:16:16
    • Location: Quebec, Canada
    • Status: offline
    Re: Frequency of MC is wrong, but not by a (2,4,8) factor 2019/07/18 03:08:28 (permalink)
    +1 (1)
    This might be more efficient, but it is not the problem.
     
    OMG
     
    didn't even realize I called countdown_drop() twice. One each second, and each tenth of a second, which gives exactly the time.
    I removed it and now it is working. So, Before I didn't have that, I just think I tried to modify the function without realizing it.
     
    So the problem was really the configuration bits. Thank you all. very appreciated.
     
    post edited by PyThagoras2 - 2019/07/18 06:38:48
    #13
    pcbbc
    Super Member
    • Total Posts : 1188
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: Frequency of MC is wrong, but not by a (2,4,8) factor 2019/07/18 06:45:20 (permalink)
    +1 (1)
    PyThagoras2This might be more efficient, but it is not it, I tried

    The loop unrolled version will almost always be faster (if that's what you mean by efficient).  But I didn't claim it was faster.
     
    Main advantage is less C code and better test code coverage.  It's also very easily extendible to more digits.  Your version requires you to write yet more code (and more to the point test it for each individual digit roll-over if you want 100% code coverage).
     
    Regardless you should really sort out your indenting, and while you are at it learn how to use "else if" instead of having you code indents stack up like the Towers of Hanoi...
        if(numCountdownNowTime[5]!=0)
        {
            numCountdownNowTime[5]--;
        }
        else if (numCountdownNowTime[4]!=0)
        {
            numCountdownNowTime[4]--;
            numCountdownNowTime[5]=9;
        }
        else if (numCountdownNowTime[3]!=0)
        {
            numCountdownNowTime[3]--;
            numCountdownNowTime[4]=9;
            numCountdownNowTime[5]=9;
        }
        else if (numCountdownNowTime[2]!=0)
        {
            numCountdownNowTime[2]--;
            numCountdownNowTime[3]=9;
            numCountdownNowTime[4]=9;
            numCountdownNowTime[5]=9;
        }
        else if (numCountdownNowTime[1]!=0)
        {
            numCountdownNowTime[1]--;
            numCountdownNowTime[2]=5;
            numCountdownNowTime[3]=9;
            numCountdownNowTime[4]=9;
            numCountdownNowTime[5]=9;
        }
        else if (numCountdownNowTime[0]!=0)
        {
            numCountdownNowTime[0]--;
            numCountdownNowTime[1]=9;
            numCountdownNowTime[2]=5;
            numCountdownNowTime[3]=9;
            numCountdownNowTime[4]=9;
            numCountdownNowTime[5]=9;
        }
        else
        {
            countdownStop();
            status = done;
            endBeep();
        }

    #14
    PyThagoras2
    PHYSICS ENG, UGRD
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2018/07/16 05:16:16
    • Location: Quebec, Canada
    • Status: offline
    Re: Frequency of MC is wrong, but not by a (2,4,8) factor 2019/07/18 09:47:52 (permalink)
    0
    You are right. Your version is much more compact, testable and reusable than mine.
    I never truly coded in C nor C++. It's good you're giving me those lessons so I can see my mistakes.
     
    I really just wanted to make the unrolled version because a colleague told me it was faster. Since the countdown_drop() function is inside the interrupt, I didn't want to risk the function being so long that I would miss an interrupt.
    #15
    Jump to:
    © 2019 APG vNext Commercial Version 4.5