How do I modify my PIC microcontroller program to deliver 150 hz also with the existing 1hz output

drjohsmith

Joined Dec 13, 2021
1,630
I'm not inclined to learn about it because there's too much pessimism about my ability but eventually I may have to learn about it. If you ask me I think the logic of intelligence was changed by the Antichrist and I was not informed about it that is why it is difficult for me to learn. A new way of understanding was not programmed in my mind.
no one here has pexsimism about anyones ability
were all here to help otheres , giving our time and decades of experiance for free to encourage otheres in the great world of elctronics and engineering

but, you have to do the work,
I have not found anyone that can not program,
 

Thread Starter

Arjune

Joined Jan 6, 2018
354
Its ok, for simulation.

Code:
// PIC16F627A Configuration Bit Settings
// 'C' source line config statements

// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)
#pragma config MCLRE = OFF      // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD)
#pragma config BOREN = ON       // Brown-out Detect Enable bit (BOD enabled)
#pragma config LVP = OFF        // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = ON         // Data EE Memory Code Protection bit (Data memory code-protected)
#pragma config CP = ON          // Flash Program Memory Code Protection bit (0000h to 03FFh code-protected)

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

#include <xc.h>
#include <stdint.h>

#define _XTAL_FREQ          16000000

#define ONE_HZ_PIN          PORTBbits.RB0
#define OHF_HZ_PIN          PORTBbits.RB3

volatile uint16_t s1 = 0;
volatile uint8_t s2 = 0;
volatile uint8_t one_Hz_flag = 0;
volatile uint8_t ohf_Hz_flag = 0;

const uint8_t tmr1_h = 254;
const uint8_t tmr1_l = 112;

/*
*
*/
void mcu_Init(void)
{
    CMCONbits.CM = 0b111;
    PORTA = 0xFF;
    PORTB = 0xFF;

    TRISA = 0x00;
    TRISB = 0x00;
}

/*
* http://eng-serve.com/pic/pic_timer.html
*/
void isr_Init(void)
{
    //Timer1 Registers Prescaler= 1 - TMR1 Preset = 65136 - Freq = 10000.00 Hz - Period = 0.000100 seconds
    T1CONbits.T1CKPS1 = 0b0; // bits 5-4  Prescaler Rate Select bits
    T1CONbits.T1CKPS0 = 0b0; // bit 4
    T1CONbits.T1OSCEN = 0b1; // bit 3 Timer1 Oscillator Enable Control bit 1 = on
    T1CONbits.nT1SYNC = 0b1; // bit 2 Timer1 External Clock Input Synchronization Control bit...1 = Do not synchronize external clock input
    T1CONbits.TMR1CS = 0b0; // bit 1 Timer1 Clock Source Select bit...0 = Internal clock (FOSC/4)
    T1CONbits.TMR1ON = 0b1; // bit 0 enables timer
    TMR1H = tmr1_h; // preset for timer1 MSB register
    TMR1L = tmr1_l; // preset for timer1 LSB register

    // Interrupt Registers
    INTCON = 0; // clear the interrpt control register
    INTCONbits.TMR0IE = 0b0; // bit5 TMR0 Overflow Interrupt Enable bit...0 = Disables the TMR0 interrupt
    PIR1bits.TMR1IF = 0b0; // clear timer1 interupt flag TMR1IF
    PIE1bits.TMR1IE = 0b1; // enable Timer1 interrupts
    INTCONbits.TMR0IF = 0b0; // bit2 clear timer 0 interrupt flag
    INTCONbits.GIE = 0b1; // bit7 global interrupt enable
    INTCONbits.PEIE = 0b1; // bit6 Peripheral Interrupt Enable bit...1 = Enables all unmasked peripheral interrupts
}

/*
*
*/
void main(void)
{
    mcu_Init();
    isr_Init();

    while(1)
    {       
        if(one_Hz_flag == 1)
        {
            ONE_HZ_PIN ^= 0b1;
            one_Hz_flag = 0;
        }
        if(ohf_Hz_flag == 1)
        {
            OHF_HZ_PIN ^= 0b1;
            ohf_Hz_flag = 0;
        }
    }
}

/*
* http://eng-serve.com/pic/pic_timer.html
*/
void interrupt ISR(void)
{   
    // Timer1 Interrupt - Freq = 10000.00 Hz - Period = 0.000100 seconds
    if(PIR1bits.TMR1IF == 0b1) // timer 1 interrupt flag
    {       
        if(s1++ > 4200)
        {
            one_Hz_flag = 1;
            s1 = 0;
        }
       
        if(s2++ > 26)
        {
            NOP();
            NOP();
            NOP();
            ohf_Hz_flag = 1;
            s2 = 0;
        }

        PIR1bits.TMR1IF = 0b0; // interrupt must be cleared by software
        PIE1bits.TMR1IE = 0b1; // reenable the interrupt
        TMR1H = tmr1_h; // preset for timer1 MSB register
        TMR1L = tmr1_l; // preset for timer1 LSB register
    }
}
/*----------------------------------------------------------------------------*/
I get a build error:
CLEAN SUCCESSFUL (total time: 62ms)
make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
make[1]: Entering directory '/home/ronald/MPLABXProjects/RonAmplabx102.X'
make -f nbproject/Makefile-default.mk dist/default/production/RonAmplabx102.X.production.hex
make[2]: Entering directory '/home/ronald/MPLABXProjects/RonAmplabx102.X'
"/opt/microchip/xc8/v3.10/bin/xc8-cc" -mcpu=16F627A -c -mdfp="/opt/microchip/mplabx/v6.05/packs/Microchip/PIC16Fxxx_DFP/1.3.42/xc8" -fno-short-double -fno-short-float -O0 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default -msummary=-psect,-class,+mem,-hex,-file -ginhx32 -Wl,--data-init -mno-keep-startup -mno-osccal -mno-resetbits -mno-save-resetbits -mno-download -mno-stackcall -mdefault-config-bits -std=c99 -gdwarf-3 -mstack=compiled:auto:auto -o build/default/production/RonAAmplabx605.p1 RonAAmplabx605.c
RonAAmplabx605.c:97:6: error: variable has incomplete type 'void'
97 | void interrupt ISR(void)
| ^
RonAAmplabx605.c:97:15: error: expected ';' after top level declarator
97 | void interrupt ISR(void)
| ^
| ;
2 errors generated.
make[2]: *** [nbproject/Makefile-default.mk:104: build/default/production/RonAAmplabx605.p1] Error 1
make[1]: *** [nbproject/Makefile-default.mk:85: .build-conf] Error 2
make: *** [nbproject/Makefile-impl.mk:39: .build-impl] Error 2
make[2]: Leaving directory '/home/ronald/MPLABXProjects/RonAmplabx102.X'
make[1]: Leaving directory '/home/ronald/MPLABXProjects/RonAmplabx102.X'

BUILD FAILED (exit value 2, total time: 196ms)
 

drjohsmith

Joined Dec 13, 2021
1,630
I get a build error:
CLEAN SUCCESSFUL (total time: 62ms)
make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
make[1]: Entering directory '/home/ronald/MPLABXProjects/RonAmplabx102.X'
make -f nbproject/Makefile-default.mk dist/default/production/RonAmplabx102.X.production.hex
make[2]: Entering directory '/home/ronald/MPLABXProjects/RonAmplabx102.X'
"/opt/microchip/xc8/v3.10/bin/xc8-cc" -mcpu=16F627A -c -mdfp="/opt/microchip/mplabx/v6.05/packs/Microchip/PIC16Fxxx_DFP/1.3.42/xc8" -fno-short-double -fno-short-float -O0 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default -msummary=-psect,-class,+mem,-hex,-file -ginhx32 -Wl,--data-init -mno-keep-startup -mno-osccal -mno-resetbits -mno-save-resetbits -mno-download -mno-stackcall -mdefault-config-bits -std=c99 -gdwarf-3 -mstack=compiled:auto:auto -o build/default/production/RonAAmplabx605.p1 RonAAmplabx605.c
RonAAmplabx605.c:97:6: error: variable has incomplete type 'void'
97 | void interrupt ISR(void)
| ^
RonAAmplabx605.c:97:15: error: expected ';' after top level declarator
97 | void interrupt ISR(void)
| ^
| ;
2 errors generated.
make[2]: *** [nbproject/Makefile-default.mk:104: build/default/production/RonAAmplabx605.p1] Error 1
make[1]: *** [nbproject/Makefile-default.mk:85: .build-conf] Error 2
make: *** [nbproject/Makefile-impl.mk:39: .build-impl] Error 2
make[2]: Leaving directory '/home/ronald/MPLABXProjects/RonAmplabx102.X'
make[1]: Leaving directory '/home/ronald/MPLABXProjects/RonAmplabx102.X'

BUILD FAILED (exit value 2, total time: 196ms)
And what have you done to debug this ?
 

mehmetbey

Joined Apr 10, 2025
11
I get a build error:
CLEAN SUCCESSFUL (total time: 62ms)
make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
make[1]: Entering directory '/home/ronald/MPLABXProjects/RonAmplabx102.X'
make -f nbproject/Makefile-default.mk dist/default/production/RonAmplabx102.X.production.hex
make[2]: Entering directory '/home/ronald/MPLABXProjects/RonAmplabx102.X'
"/opt/microchip/xc8/v3.10/bin/xc8-cc" -mcpu=16F627A -c -mdfp="/opt/microchip/mplabx/v6.05/packs/Microchip/PIC16Fxxx_DFP/1.3.42/xc8" -fno-short-double -fno-short-float -O0 -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default -msummary=-psect,-class,+mem,-hex,-file -ginhx32 -Wl,--data-init -mno-keep-startup -mno-osccal -mno-resetbits -mno-save-resetbits -mno-download -mno-stackcall -mdefault-config-bits -std=c99 -gdwarf-3 -mstack=compiled:auto:auto -o build/default/production/RonAAmplabx605.p1 RonAAmplabx605.c
RonAAmplabx605.c:97:6: error: variable has incomplete type 'void'
97 | void interrupt ISR(void)
| ^
RonAAmplabx605.c:97:15: error: expected ';' after top level declarator
97 | void interrupt ISR(void)
| ^
| ;
2 errors generated.
make[2]: *** [nbproject/Makefile-default.mk:104: build/default/production/RonAAmplabx605.p1] Error 1
make[1]: *** [nbproject/Makefile-default.mk:85: .build-conf] Error 2
make: *** [nbproject/Makefile-impl.mk:39: .build-impl] Error 2
make[2]: Leaving directory '/home/ronald/MPLABXProjects/RonAmplabx102.X'
make[1]: Leaving directory '/home/ronald/MPLABXProjects/RonAmplabx102.X'

BUILD FAILED (exit value 2, total time: 196ms)
Share your all code.
Share "RonAAmplabx605.c" file.
 

Futurist

Joined Apr 8, 2025
926
I'm learning somewhat on the forum. When I try to learn code it becomes confusing. I have considered buying a book on programming a PIC microcontroller on amazon Kindle but thought they wouldn't reflect my mind's way of thinking and would get lost due to difficulty.
As a beginner (if I may describe you as such) I think you need to ask yourself what is the easiest processor and tools you can use. You've opted to use PICKIT3, little 16 bit processor, with an unsupported open source set of software tools that produce cryptic error messages, and doing it all on Linux, frankly you are asking for problems.

I suggested to you already, revisit this, why PICKIT3? You'd have a much smoother experience using an STM32 Nucleo board with Visual Studio and the VisualGDB extension, on Windows.

Look at how straightforward it is to get a simple LED blinker running.

https://visualgdb.com/tutorials/arm/stm32/nucleo411re/

See? it's that easy, it just works. The IDE is as good as it gets (and free), the compiler is superb (Clang) and the STM32 boards cheap, common and well documented. Generating a simple square wave signal is almost trivial.

Here's the board:

https://www.amazon.com/NUCLEO-F411RE-Nucleo-64-Development-STM32F411RE-connectivity/dp/B07JYF8RRB/ref=sr_1_1?crid=105OPD6O9EZFY&dib=eyJ2IjoiMSJ9.MvYED0FVr06SbdeVTR7uR4Jx-B7T02F9ucq8DEUpJQDaY0NuwB5L-KC8h3fWQSF1Vwjz9k-7KzMfmAAsdskMiJ8JgjJYKglMBv5YLZ0Zp2SLs89owtPic_0-_TeeTFfVk20iDro7cniIP4R75HcIy50FE_PcPIlX6lBlT4_Eu7dp2Aa1ESQl7-Rd11X80jHljRxtwF5-dunf9aB_l0n7OWSo5GOuVBKfvi8jSAYKCDs.jOF8Imz04_rGbn66335VK3LrNuETYv1YO-tN39ke_7A&dib_tag=se&keywords=stm32+nucleo-f411re&qid=1767279162&sprefix=stm32+nucleo,aps,351&sr=8-1

I'm relatively new to MCU development but I've been developing software for almost 50 years, your situation requires much more experience and skill than you have, there are PIC experts here who can do this, but its not anywhere as straightforward as what I'm recommending.

So take a deep breath and ask yourself why you are choosing a difficult path when life could be easier and more productive, you can then look back on this PIC experience as just that, an experience that taught you some important lessons.

There's a rule in software development - if it can go wrong it will - you're posts here prove it to be true.
 
Last edited:

Thread Starter

Arjune

Joined Jan 6, 2018
354
If there is only a problem with the void interrupt ISR(void) line, change this line as below;
Code:
void __interrupt() ISR(void)
With the line of code change it's working but the timing is too fast==about a minute or more per hour.
 

Thread Starter

Arjune

Joined Jan 6, 2018
354
And what do you think the reason for this is ?
What have you done to debug this ?
You asking me to trust reality but the truth cannot be trusted as a schizophrenic. The trance is in a physical form, that's why my mind cannot process--the mental aspect has turned physical and should not be that way. You're recognizing my physical reactions because it makes sense (such as writing this post) and you're not recognizing my disability. My scope of understanding is very limited because others do not recognize my traits. I don't know how to debug, debug the program. It has always been my will to try hard, but I don't have the requisites. My body may be able to communicate effectively but my mind is not being supportive. I'm functioning as a child in a man's body who's mind has failed to develop at this stage. I would love to understand the program but I can't. Unless I can change the fabric of belief to correlate with me, I remain in a coma. I'm able to have an impression but I am unable to translate that impression into functionality. I am not irresponsible but unknown conditions make me appear to be that way. For my years I have seen psychiatrists and some therapists, they have failed to recognize me for who I am. I guess I'm in shutdown mode.
 
Last edited:

drjohsmith

Joined Dec 13, 2021
1,630
You asking me to trust reality but the truth cannot be trusted as a schizophrenic. The trance is in a physical form, that's why my mind cannot process--the mental aspect has turned physical and should not be that way. You're recognizing my physical reactions because it makes sense (such as writing this post) and you're not recognizing my disability. My scope of understanding is very limited because others do not recognize my traits. I don't know how to debug, debug the program. It has always been my will to try hard, but I don't have the requisites. My body may be able to communicate effectively but my mind is not being supportive. I'm functioning as a child in a man's body who's mind has failed to develop at this stage. I would love to understand the program but I can't. Unless I can change the fabric of believe to correlate with me, I remain in a coma. I'm able to have an impression but I am unable to translate that impression into functionality. I am not irresponsible but unknown conditions make me appear to be that way. For my years I have seen psychiatrists and some therapists, they have failed to recognize me for who I am. I guess I'm in shutdown mode.
I'm sorry to hear this
Unfortunately, not certain the forum is in a position to help you "change the fabric of believe"
I do feel for your situation , I too have had to get fight my demons in the past , keep talking to your medical team .
 
Last edited:

Thread Starter

Arjune

Joined Jan 6, 2018
354
If there is only a problem with the void interrupt ISR(void) line, change this line as below;
Code:
void __interrupt() ISR(void)
With the line of code change it's working but the timing is too fast==about a minute or more per hour.
As a beginner (if I may describe you as such) I think you need to ask yourself what is the easiest processor and tools you can use. You've opted to use PICKIT3, little 16 bit processor, with an unsupported open source set of software tools that produce cryptic error messages, and doing it all on Linux, frankly you are asking for problems.

I suggested to you already, revisit this, why PICKIT3? You'd have a much smoother experience using an STM32 Nucleo board with Visual Studio and the VisualGDB extension, on Windows.

Look at how straightforward it is to get a simple LED blinker running.

https://visualgdb.com/tutorials/arm/stm32/nucleo411re/

See? it's that easy, it just works. The IDE is as good as it gets (and free), the compiler is superb (Clang) and the STM32 boards cheap, common and well documented. Generating a simple square wave signal is almost trivial.

Here's the board:

https://www.amazon.com/NUCLEO-F411RE-Nucleo-64-Development-STM32F411RE-connectivity/dp/B07JYF8RRB/ref=sr_1_1?crid=105OPD6O9EZFY&dib=eyJ2IjoiMSJ9.MvYED0FVr06SbdeVTR7uR4Jx-B7T02F9ucq8DEUpJQDaY0NuwB5L-KC8h3fWQSF1Vwjz9k-7KzMfmAAsdskMiJ8JgjJYKglMBv5YLZ0Zp2SLs89owtPic_0-_TeeTFfVk20iDro7cniIP4R75HcIy50FE_PcPIlX6lBlT4_Eu7dp2Aa1ESQl7-Rd11X80jHljRxtwF5-dunf9aB_l0n7OWSo5GOuVBKfvi8jSAYKCDs.jOF8Imz04_rGbn66335VK3LrNuETYv1YO-tN39ke_7A&dib_tag=se&keywords=stm32+nucleo-f411re&qid=1767279162&sprefix=stm32+nucleo,aps,351&sr=8-1

I'm relatively new to MCU development but I've been developing software for almost 50 years, your situation requires much more experience and skill than you have, there are PIC experts here who can do this, but its not anywhere as straightforward as what I'm recommending.

So take a deep breath and ask yourself why you are choosing a difficult path when life could be easier and more productive, you can then look back on this PIC experience as just that, an experience that taught you some important lessons.

There's a rule in software development - if it can go wrong it will - you're posts here prove it to be true.
It still looks complex and difficult to me. AI is helpful.
 

Thread Starter

Arjune

Joined Jan 6, 2018
354
Share your all code.
Share "RonAAmplabx605.c" file.
AI produced this attached code. There is about a second drift slower overnight.

Code:
#include <xc.h>

// Configuration Bits: 16MHz HS Crystal, Watchdog Off, MCLR On, LVP Off
#pragma config FOSC = HS, WDTE = OFF, PWRTE = ON, MCLRE = ON, BOREN = OFF, LVP = OFF, CPD = OFF, CP = OFF

#define _XTAL_FREQ 16000000 // 16 MHz Crystal
#define CLOCK_OUT PORTBbits.RB0

unsigned int timer_counter = 0;

void __interrupt() isr(void) {
    if (PIR1bits.TMR1IF) { // Check Timer 1 Overflow Flag
        // Preload Timer 1 for 100ms delay:
        // Instruction frequency = 16MHz / 4 = 4MHz
        // Timer 1 with 1:8 Prescaler = 500kHz (2us per tick)
        // 100ms / 2us = 50,000 ticks.
        // Preload value = 65536 - 50000 = 15536 (0x3CB0)
        TMR1H = 0x3C;
        TMR1L = 0xB0;

        timer_counter++;
        if (timer_counter >= 5) { // 10 * 100ms = 1 second
            CLOCK_OUT = ~CLOCK_OUT; // Toggle the pin
            timer_counter = 0;
        }
        PIR1bits.TMR1IF = 0; // Clear interrupt flag
    }
}

void main(void) {
    CMCON = 0x07;      // Disable comparators to use PORTA/B as digital I/O
    TRISBbits.TRISB0 = 0; // Set RB0 as output
    CLOCK_OUT = 0;

    // Timer 1 Configuration
    T1CONbits.T1CKPS = 0b11; // 1:8 Prescaler
    T1CONbits.TMR1CS = 0;    // Internal clock (Fosc/4)
    TMR1H = 0x3C;            // Initial preload
    TMR1L = 0xB0;
    
    // Interrupt Configuration
    PIE1bits.TMR1IE = 1;     // Enable Timer 1 interrupt
    INTCONbits.PEIE = 1;     // Enable peripheral interrupts
    INTCONbits.GIE = 1;      // Enable global interrupts
    T1CONbits.TMR1ON = 1;    // Start Timer 1

    while (1) {
        // Main loop remains empty; logic is handled in ISR
    }
}
 

LesJones

Joined Jan 8, 2017
4,522
I we say overnight is 8 hours then that is 8 * 3600 (Seconds in 1 hour) = 28800 seconds
So if we want to 1 second less then we need to count up to 28799.
So instead of counting to 50000 we need to count to 50000 * 28799/28800 = 49998.26
We can only count in integers so 49998 is the closest.
So change // Preload value = 65536 - 50000 = 15536 (0x3CB0)
to // Preload value = 65536 - 49998 = 15538 (0x3CB2)

You could get closer by clocking the timer faster by using a lower prescale value but then you would have to modify the interrupt code to count a number of overflows of the counter srting from zero folowed by a calculated count.
what are you using as a time standard to compare with ?

Les.
 

drjohsmith

Joined Dec 13, 2021
1,630
I we say overnight is 8 hours then that is 8 * 3600 (Seconds in 1 hour) = 28800 seconds
So if we want to 1 second less then we need to count up to 28799.
So instead of counting to 50000 we need to count to 50000 * 28799/28800 = 49998.26
We can only count in integers so 49998 is the closest.
So change // Preload value = 65536 - 50000 = 15536 (0x3CB0)
to // Preload value = 65536 - 49998 = 15538 (0x3CB2)

You could get closer by clocking the timer faster by using a lower prescale value but then you would have to modify the interrupt code to count a number of overflows of the counter srting from zero folowed by a calculated count.
what are you using as a time standard to compare with ?

Les.
@Arjune
Also reminder , this is running on the boards resonator oscillator, so it will change quiet a bit over temperature, voltage and be different on a different board.
 

drjohsmith

Joined Dec 13, 2021
1,630
@Arjune
what is your aim here ?
you have all but said your not learning,
you are picking answeres from humans or AI

you evidently are not in a position to sell and support this ,
and it would be flagged up as plagerisum if it was presented as your work at a college,

we might be able to help you more if we knew your aims of this work,
 

Futurist

Joined Apr 8, 2025
926
With the line of code change it's working but the timing is too fast==about a minute or more per hour.

It still looks complex and difficult to me. AI is helpful.
You will not progress unless you trust people, my advice could simplify your work. Use a mouse and a powerful - free - IDE, use an MCU family that has a lower barrier to entry than the feeble outdated little 16 bit PIC and find much more helpful online information than you currently can.

Did it cross your mind you might be just stubborn.

Please answer these questions

1. Why did you select that PIC board?
2. How long have you had it?
3. Have you ever written software before this?
4. How long ago did you start using C?
5. Are you a student studying electronics or computing?
 
Last edited:

Thread Starter

Arjune

Joined Jan 6, 2018
354
I we say overnight is 8 hours then that is 8 * 3600 (Seconds in 1 hour) = 28800 seconds
So if we want to 1 second less then we need to count up to 28799.
So instead of counting to 50000 we need to count to 50000 * 28799/28800 = 49998.26
We can only count in integers so 49998 is the closest.
So change // Preload value = 65536 - 50000 = 15536 (0x3CB0)
to // Preload value = 65536 - 49998 = 15538 (0x3CB2)

You could get closer by clocking the timer faster by using a lower prescale value but then you would have to modify the interrupt code to count a number of overflows of the counter srting from zero folowed by a calculated count.
what are you using as a time standard to compare with ?

Les.
I am using a clock I built that works on the timing module I removed from the little clocks with the analog dial that work with the AA battery. It's very accurate and uses a 32, 768hz Crystal. It has the same type of movement like a wall clock, only smaller and cost about three or four dollars. I had to add two diodes and two transistors for a driver circuit along with some resistors to raise the pulse level to about five volts. I want to do something new for the reference clock signal that's why I want to try a microcontroller for the one hertz this time.
 

Thread Starter

Arjune

Joined Jan 6, 2018
354
You will not progress unless you trust people, my advice could simplify your work. Use a mouse and a powerful - free - IDE, use an MCU family that has a lower barrier to entry than the feeble outdated little 16 bit PIC and find much more helpful online information than you currently can.

Did it cross your mind you might be just stubborn.

Please answer these questions

1. Why did you select that PIC board?
2. How long have you had it?
3. Have you ever written software before this?
4. How long ago did you start using C?
5. Are you a student studying electronics or computing?
It does seem I'm stubborn but I believe it's not really Ron, it's some other identity occupying my body--probably the Antichrist. I'm not using a pic board, I'm using a microcontroller and a breadboard with the pickit3 programmer. Many years ago I have written simple software with qbasic like to create random numbers on an IBM Thinkpad 340. I only started using c when I got the microcontroller-I don't know it's intricacies. I'm not a student studying electronics or computers but I've learned electronics in high school, back in 1980 I graduated with a technical diploma in electronics and we were supposed to go to college but I didn't because of my illness or that I was distraught about life. High school was very difficult especially math class. Somehow how to program the microcontroller will sink into my brain. I must say I'm happy with the progress I've made with the pickit3. It works great, why would I do something different, I just have to learn a little code. I'm not trying to do something extraordinary with the microcontroller, so I'm satisfied although it's old.
 

Thread Starter

Arjune

Joined Jan 6, 2018
354
I have a PIC microcontroller that delivers a 1hz output at RB0 and I want to deliver a 150 hertz also to another pin--say RB3. The 150 hertz does not need to be accurate. How do I modify the program and keep the 1hertz code because it seems to be accurate with my 16mhz crystal. I use a PIC 16F627A and program it with a PICKIT3 using MPLAB IDE 6.05 on my Linux machine. The code that is provided for the 1 hertz output works but I don't have knowledge of programming. I got the program with an internet search.

Merry Christmas Everyone!

Code:
#include <xc.h>

// Configuration Bits: 16MHz HS Crystal, Watchdog Off, MCLR On, LVP Off
#pragma config FOSC = HS, WDTE = OFF, PWRTE = ON, MCLRE = ON, BOREN = OFF, LVP = OFF, CPD = OFF, CP = OFF

#define _XTAL_FREQ 16000000 // 16 MHz Crystal
#define CLOCK_OUT PORTBbits.RB0

unsigned int timer_counter = 0;

void __interrupt() isr(void) {
    if (PIR1bits.TMR1IF) { // Check Timer 1 Overflow Flag
        // Preload Timer 1 for 100ms delay:
        // Instruction frequency = 16MHz / 4 = 4MHz
        // Timer 1 with 1:8 Prescaler = 500kHz (2us per tick)
        // 100ms / 2us = 50,000 ticks.
        // Preload value = 65536 - 50000 = 15536 (0x3CB0)
        TMR1H = 0x3C;
        TMR1L = 0xB0;

        timer_counter++;
        if (timer_counter >= 5) { // 10 * 100ms = 1 second
            CLOCK_OUT = ~CLOCK_OUT; // Toggle the pin
            timer_counter = 0;
        }
        PIR1bits.TMR1IF = 0; // Clear interrupt flag
    }
}

void main(void) {
    CMCON = 0x07;      // Disable comparators to use PORTA/B as digital I/O
    TRISBbits.TRISB0 = 0; // Set RB0 as output
    CLOCK_OUT = 0;

    // Timer 1 Configuration
    T1CONbits.T1CKPS = 0b11; // 1:8 Prescaler
    T1CONbits.TMR1CS = 0;    // Internal clock (Fosc/4)
    TMR1H = 0x3C;            // Initial preload
    TMR1L = 0xB0;

    // Interrupt Configuration
    PIE1bits.TMR1IE = 1;     // Enable Timer 1 interrupt
    INTCONbits.PEIE = 1;     // Enable peripheral interrupts
    INTCONbits.GIE = 1;      // Enable global interrupts
    T1CONbits.TMR1ON = 1;    // Start Timer 1

    while (1) {
        // Main loop remains empty; logic is handled in ISR
    }
}
Merry Christmas Everyone!
If I use a 4mhz crystal and a 1:4 prescaler I get 250,000hz at 4uS. 100mS/4uS=25,000 ticks. 65,536-25,000=40,536 or Ox9E58.
I have a PIC microcontroller that delivers a 1hz output at RB0 and I want to deliver a 150 hertz also to another pin--say RB3. The 150 hertz does not need to be accurate. How do I modify the program and keep the 1hertz code because it seems to be accurate with my 16mhz crystal. I use a PIC 16F627A and program it with a PICKIT3 using MPLAB IDE 6.05 on my Linux machine. The code that is provided for the 1 hertz output works but I don't have knowledge of programming. I got the program with an internet search.

Merry Christmas Everyone!

Code:
#include <xc.h>

// Configuration Bits: 16MHz HS Crystal, Watchdog Off, MCLR On, LVP Off
#pragma config FOSC = HS, WDTE = OFF, PWRTE = ON, MCLRE = ON, BOREN = OFF, LVP = OFF, CPD = OFF, CP = OFF

#define _XTAL_FREQ 16000000 // 16 MHz Crystal
#define CLOCK_OUT PORTBbits.RB0

unsigned int timer_counter = 0;

void __interrupt() isr(void) {
    if (PIR1bits.TMR1IF) { // Check Timer 1 Overflow Flag
        // Preload Timer 1 for 100ms delay:
        // Instruction frequency = 16MHz / 4 = 4MHz
        // Timer 1 with 1:8 Prescaler = 500kHz (2us per tick)
        // 100ms / 2us = 50,000 ticks.
        // Preload value = 65536 - 50000 = 15536 (0x3CB0)
        TMR1H = 0x3C;
        TMR1L = 0xB0;

        timer_counter++;
        if (timer_counter >= 5) { // 10 * 100ms = 1 second
            CLOCK_OUT = ~CLOCK_OUT; // Toggle the pin
            timer_counter = 0;
        }
        PIR1bits.TMR1IF = 0; // Clear interrupt flag
    }
}

void main(void) {
    CMCON = 0x07;      // Disable comparators to use PORTA/B as digital I/O
    TRISBbits.TRISB0 = 0; // Set RB0 as output
    CLOCK_OUT = 0;

    // Timer 1 Configuration
    T1CONbits.T1CKPS = 0b11; // 1:8 Prescaler
    T1CONbits.TMR1CS = 0;    // Internal clock (Fosc/4)
    TMR1H = 0x3C;            // Initial preload
    TMR1L = 0xB0;
 
    // Interrupt Configuration
    PIE1bits.TMR1IE = 1;     // Enable Timer 1 interrupt
    INTCONbits.PEIE = 1;     // Enable peripheral interrupts
    INTCONbits.GIE = 1;      // Enable global interrupts
    T1CONbits.TMR1ON = 1;    // Start Timer 1

    while (1) {
        // Main loop remains empty; logic is handled in ISR
    }
}
Merry Christmas Everyone!
With a 4mhz crystal and a 1:4 prescaler I get 250,000hz =4uS. 100mS/4uS =25,000 ticks. 65536-25000=40,536 or 9E58. TMR1H=Ox9E line 18, TMR1L=Ox58 line 19. Would line 22 be the same? How about line 36 using a 1:4 prescaler. Please explain these two lines.
 

LesJones

Joined Jan 8, 2017
4,522
Threre are may different ways to divide down from 4 Mhz to 1 Hz as you always finish up with dividing by 4000000.
But you can't divide 4Mhz by an integer to get 150 hz. (4000000/150 = 26666.666666666666666666666666667)
If you buy a 15 Mkz crystal you can divide by an integer to get to 150 Hz. With a 15 Mhzcrystal the clock frequency will be
15000000/4 = 3750000 hz.
3750000/150 = 25000
You start by generating an exact 150 hz using timer 1 (This is what the present code does .)
In software you then count 150 cycles of 150 hz to get down to an exact 1 hz.
It would help us to help you if you explained why you need 1hz and 150 hz for your clock. (The only thing I can think of is that the 150 hz is for mltiplexing the display.)
How accurate does the clock have to be and what is it being used for ? If it needs to be very accurate then using the cheap crystals that are used for the CPU clock frequency will not be accurate enough.
Posting the schematic of the clock that you need the two fequencies for would be a help.
Threre are methods of generating accurate frequencies using a GPS receiver.
There are also standard frequency transmissions in most countries that can be used to generate accurate frequencies.
Another accurate frequency source for 1Hz and 32768 hz is the DS3231 RTC chip. I bought one of these wrote some code to set it and read it's output. I then put it to one side for a couple of years befor using it in a project and it was only a few seconds out after all that time..
Les.
 
Top