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!
Merry Christmas Everyone!
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
}
}
