Clock training board fir PIC 12F275
/* short button push gives one output pulse
* longer button push will trigger automatic pulses with 1 sec intervals
* push again and it stops the automatic pulses
*/
// PIC12F675 Configuration Bit Settings
#pragma config FOSC = INTRCIO // Oscillator Selection bits (INTOSC oscillator: I/O function on GP4/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-Up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // GP3/MCLR pin function select (GP3/MCLR pin function is digital I/O, MCLR internally tied to VDD)
#pragma config BOREN = OFF // Brown-out Detect Enable bit (BOD disabled)
#pragma config CP = OFF // Code Protection bit (Program Memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#include <pic12f675.h>
#define _XTAL_FREQ 4000000
unsigned int counter = 0;
unsigned int timer = 0;
char clock_on = 0;
void __interrupt() ISR(void) {
if (INTCONbits.T0IF == 1) {
counter++; // 0-65535
if (clock_on) {
if (counter > 1000) {
GP0 = ~GP0; // ~ means not and toggles GP0
counter = 0;
}
}
INTCONbits.T0IF = 0; // Clear Timer0 overflow
TMR0 = 0;
}
}
void main(void) {
ANSEL = 0b00000000; // Set ports as digital I/O, no analog input
CMCON = 0b00000111; //comparators CM2:CM0 off
VRCON = 0x00; // Shut off the Voltage Reference
ADCON0 = 0x00; // Shut off the A/D Converter
WPU = 0x00;
TRISIO = 0b00011000; // GP3, GP4 = input
GPIO = 0x00;
OPTION_REG = 0b00000000; // bit 7 and 6 don't matter, bit 5 CLKO, bit 4 ?, bit 3 timer0 , bit 2,1,0 = 1:2 pre-scaler => 1125Hz
INTCON = 0b11100000; // Enables all unmasked, peripheral and Timer0 interrupt
PIE1 = 0x00; // Peripheral Interrupt Enable 1 all zero
T1CON = 0x00; //clear the unused timer
TMR0 = 0;
while (1) {
if (GP4 == 1) {
counter = 0;
while (counter < 20); // de-bouncing, not using __delay_ms()
while (GP4 == 1) { // while pressed timer increments
GP0= 1;
clock_on = 0;
timer++;
}
} else { // not pressed
if (timer > 20000) { // about 3 seconds
clock_on = 1; // t0 loop
}
if (clock_on == 0) GP0 = 0; // not interfere with t0 loop
timer = 0;
}
} // while
return;
}