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

Thread Starter

Arjune

Joined Jan 6, 2018
354
When you chose the PIC16F15313 did you check that you could program it using MPLAB and the PICKIT3 ?
If you can't program it there is no point in anyone writing code to run on the PIC16F15313.
Also the normal 16 Mhz crystal will probably only be accurate to about 20 PPM and you said that you want 0.2 PPM .
Les.
Mplabx ide lists that pic. It is programmable by the pickit3. I want to use the 32khz crystal as timer 1 or the secondary oscillator if I am correct.
 

trebla

Joined Jun 29, 2019
599
my pic16f628a had device ID faults
What you do mean- the ID fault? During programming you can get warning about that the chip ID does not match to expected value. This can happen if the program is compiled for another chip, for example the program is compiled for the PIC16F628 and you are trying write it to the PIC16F628A chip. In this case you can ignore this warning and write anyway.
 

LesJones

Joined Jan 8, 2017
4,522
The PIC16F15313 does NOT have a dedicated timer 1 oscillator so you can only use the 32768 crystal to drive the system clock. (See page 101 of the PIC16F15313 datasheet.) You can drive timer1 from the system clock but then it a long time before timer 1 can be reloaded and started after the timer 1 interrupt. You could use a cmos locic IC as a 32768 hz oscllator and use that as an external clock for timer 1 . Another solution is to use a PIC16F628 (A) with the 32768 hz crystal on timer 1 oscillator and a 4 Mhz (16 Mhz) crystal as the main oscillator. . (No doubt you will find a reason to rejct this suggestion like so many others.)

Les.
 

drjohsmith

Joined Dec 13, 2021
1,630
// PIC16F15313 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 got this program from post #31. Can someone modify it to use with my PIC16F15313 chips with a 32khz crystal as the secondary oscillator.
Why are you putting NOP in the interrupt routine ?
 

drjohsmith

Joined Dec 13, 2021
1,630
I haven't tried anything because I don't know what to do. I changed pic because is was cheap and my pic16f628a had device ID faults. I anticipate further errors when attempting to program them.
Do you know why your original PICs had ID faults and how to stop that happening in the new device you have selected ?
 

Thread Starter

Arjune

Joined Jan 6, 2018
354
What you do mean- the ID fault? During programming you can get warning about that the chip ID does not match to expected value. This can happen if the program is compiled for another chip, for example the program is compiled for the PIC16F628 and you are trying write it to the PIC16F628A chip. In this case you can ignore this warning and write anyway.
I listed the chip change in the properties of the IDE and IPE
 

Thread Starter

Arjune

Joined Jan 6, 2018
354
It feels like I am going through a court martial for my lack of knowledge. Like I am expected to have a certain amount of ability in programming.
 

drjohsmith

Joined Dec 13, 2021
1,630
I don't know what I am doing. My conceptualization is limited. I don't know what is NOP.
If you don't know what the NOP is, why did you include it in the ISR ?

Isr routines need to be kept as short as possible , I think your NOP is a small wait , which sounds strange ..
Suggest you read up on ISR and the NOP .
 

drjohsmith

Joined Dec 13, 2021
1,630
It feels like I am going through a court martial for my lack of knowledge. Like I am expected to have a certain amount of ability in programming.
Certainly a more structured learning to program would be advantages, have you considered a book ? It would guide you through , and let you experiment and go back .
I could not imagine a harder way to learn than the route you seem to be following. I'm impressed you have gotten this far .
You will get there , keep at it ,
 

Thread Starter

Arjune

Joined Jan 6, 2018
354
If you don't know what the NOP is, why did you include it in the ISR ?

Isr routines need to be kept as short as possible , I think your NOP is a small wait , which sounds strange ..
Suggest you read up on ISR and the NOP .
Like I have to pay a price for learning to be significant. What is that price? I don't have telepathy, I think you all do. I am at a disadvantage because I am not being informed of your capabilities. Why don't you all just provide the information to help me--why am I disqualified on this forum? This is not a ballroom dance. I am not going to play as a friend to this game--a game with no rules expressed to me! I have a heart: a heart that seems to have no significance to the intellectual ones. It seems I've joined a fraternity here and I'm very dismayed. It seems that America is a fraternity along with their media. Electronics is not meant to be run by a fraternity but to have a free spirit.
 
Last edited:

BobTPH

Joined Jun 5, 2013
11,616
You have been given good, honest information in this thread. If you don’t understand it, ask for clarification. If you chose to ignore it, that is not our problem. But stop trying to blame us for your failures.
 

Thread Starter

Arjune

Joined Jan 6, 2018
354
I think I understand the reason for my learning failures now. People have telepathy and the don't want to admit this because the order of words would change and the power of time would thereby change--intelligence don't want the power of time to change because this would alter the physical world.
 

Ya’akov

Joined Jan 27, 2019
10,278
I think I understand the reason for my learning failures now. People have telepathy and the don't want to admit this because the order of words would change and the power of time would thereby change--intelligence don't want the power of time to change because this would alter the physical world.
I promise you, Arjune—no member here has telepathic abilities. In all seriousness. So your causal chain fails at the first step. It's worth looking elsewhere, places much more mundane, for the solution to the problem you are having.
 
Top