/*
* P2MX683timer0simple 20240322
* description: An LED is blinking using 8bits timer0
* board: Curiosity development with PIC12F683 5V INTOSC 8MHz
* MPU; PIC12F683 DIP8P INTOSC8MHz
* Flash:2KW, RAM:128B,EEPROM 256Byte
* language: C XC8 v2.46 C99
* IDE: MPLABX v6.20, XC8 v2.46
* debug tool: PICkit3/PICkit4
//------------------------------------------------
* PIC12F683 DIP 8Pin on Curiosity development board
5V 1 VDD VSS 8 GND
2 GP5 GP0 7 ICSPDAT
3 GP4 GP1 6 ICSPCLK
RST S2 4 GP3/MCLR GP2 5 LED2 D5
*/
//config:INTOSCIO 8 MHz, Disable watchdog timer, Enable master clear on reset
#pragma config FOSC=INTOSCIO, WDTE=OFF, PWRTE=OFF, MCLRE=ON
#pragma config CP= OFF, CPD=OFF, BOREN=ON, IESO=ON, FCMEN=ON
#include
#define LED2 GP2 //Defines a symbolic constant LED2 to represent GPIO pin GP2
long countT=0; //Counts the number of Timer0 overflows
long previousT=0; //Stores the previous value of countT for interval
long intervalT=0; // interval between LED toggles in terms of Timer0 overflows.
void gpioBegin(); //initialize I/O configuration.
void timer0Begin(void); // initialize 8bi Timer0
//-------------------------------------------------
void main(void) {
gpioBegin();
timer0Begin();
intervalT=500; // adjust blinking period 1interval:1000-> approx. period: 1sec
while(1){ //loop
while(! TMR0IF); //wait until Timer0 overflows 1ms 8MHz/4/256/8:approx1Hz
TMR0IF = 0; // clears Timer0 overflow flag
countT=countT+1; // count overflow
if(countT-previousT>intervalT){ //check interval time
previousT=countT; // set previousT
LED2=!LED2; // Toggles the LED state (on or off)
}
}// end while
}// end main
void gpioBegin(void){
OSCCON = 0x70; // Sets internal oscillator to 8 MHz (OSCCON)
ANSEL = 0b000000; // Sets all pins to digital inputs (ANSEL)
GPIO = 0b000000; // GPIO clear
TRISIO = 0b011011; // GP0,1,3,4:INPUT,GP2,GP5;OUTPUT
CMCON0 = 0x07; // Enables comparator (CMCON0) (potentially for future use)
}
void timer0Begin(void){
OPTION_REG = 0b00000010;//Sets the OPTION_REG register to configure Timer0
//T0CS: Timer0 clock source is the internal instruction cycle clock (FOSC/4).
//PSA: Prescaler is disabled (PS<2:0> = 010).f 1:8
}
/*
result
* data 18/128bytes 14%
* program 147/2048words 7%
*/
/* OPTION_REG: OPTION REGISTER
GPPU INTEDG T0CS T0SE PSA PS2 PS1 PS0
T0CS: Timer0 Clock Source Select bit
1 = Transition on T0CKI pin
0 = Internal instruction cycle clock
bit 4 T0SE: Timer0 Source Edge Select bit
1 = Increment on high-to-low transition on T0CKI pin
0 = Increment on low-to-high transition on T0CKI pin
PS<2:0>: Prescaler Rate Select bits
000 1 : 2
001 1 : 4
010 1 : 8
011 1 : 16
100 1 : 32
101 1 : 64
110 1 : 128
111 1 : 256
*
*/
/*
8bit Timer0 overflow time
* OSCC0N:0x70 INTOSC 8MHz
* prescaler overflow
1;2 0.25mS
1:4 0.5mS
1:8 1.0mS
1:16 2.9mS
1:32 4.1mS
1:64 8.1mS
1:128 16.3mS
1:256 32.7mS
*/
/*
blinking period is determined:
8bit Timer0 Configuration:
The code sets the OPTION_REG register to 0b00000111.
PS<2:0>: Prescaler Rate Select bits
010: a prescaler of 1:8.
* T0CS: Timer0 Clock Source Select bit
* 0: Internal instruction cycle clock (FOSC/4), OSCCON = 0x70; OSC8MHz
8bit counter Overflow Time:, 8MHz/4/8/256=976Hz period: approx,1mS
LED's Blinking Period:
* interval=500 , LED ON:0.5sec, LED OFF:0.5sec
* interval=1000, LED ON:1sec, LED OFF:1sec
* interval=2000, LED ON:2sec, LED OFF:2sec
*/