* P2MX683isrtimer1onesec 20240407
* description:An LED is blinking each cycle 1sec ,using 16bits timer1 FOSC/4
* 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 high voltage programming
* measuring tools: kyutech Arduino Scope on processing 4 windows11
* measured pulse period
//------------------------------------------------
* PIC12F683 DIP 8Pin on Curiosity development board
5V 1 VDD VSS 8 GND
2 GP5 GP0 7 ICSPDAT
low active BUT4 3 GP4 GP1 6 ICSPCLK
RST S2 4 GP3/MCLR GP2/CCP1 5 LED for blinking
* jumper BUT4 GP4/PIN3 <> RC4/PIN6 S1
*/
//INTOSCIO 8MHz 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
#include
#define LED2 GP2 // LED2
#define _XTAL_FREQ 8000000 // int osc 8MH
#define TXPIN GP1 //Defines the pin used for serial transmission
#define RXPIN GP4 //Defines the pin used for serial reception
#define BAUDRATE 19200
#define BAUD (uint8_t)(((_XTAL_FREQ / BAUDRATE) / 16) - 1)
#define DTIME 10 // delay time: 1/4bitat 9600baud, need adjust
struct Codeinfo{ // for code Information
const char codeId[8]; // code Index
const char function[8];
const char mcu[8]; // model micro controller unit
const int version; // version number
const unsigned long createdDate; // Date created code
};
struct Intervaltime{
unsigned long millis;
unsigned long previous;
unsigned long interval;
};
struct Codeinfo ci={"P2MX683","timer1s","P12F683",0,20240407};
struct Intervaltime it={0,0,1000};
void gpioBegin(void); //initialize I/O configuration.
void timer1Begin(void);//Setup timer1 for 1ms interval
void serialBegin(void);
void serialWrite(char data);
void main(void) {
gpioBegin(); // Initializes General Purpose Input/Output (GPIO) pins.
timer1Begin(); // initialize timer1
serialBegin();
PEIE = 1; // enable interrupt
GIE = 1; // enable global interrupt
while(1){
if(it.millis-it.previous>it.interval){
it.previous=it.millis;
LED2 ^=1;
printf("%d\n",it.millis/1000);
}
}// end while
}// end main
//---------------------------------------------------------
void __interrupt() t1Isr(void){
if(TMR1IF){ // check Timer1 interrupt each 131mS
TMR1H=0xFE; //65035:0xFE0B
TMR1L=0x12; // TMR1:0xFE12
TMR1IF = 0; //
it.millis++;
}
}
void gpioBegin(void){
OSCCON = 0x70; // Sets internal oscillator to 8 MHz (OSCCON)
ANSEL = 0b00000000; // Sets all pins to digital inputs
GPIO = 0b000000; // GPIO clear
TRISIO = 0b011001; // GP0,3,4:INPUT,GP1,2,5;OUTPUT
CMCON0 = 0x07; // Enables comparator (CMCON0) (potentially for future use)
WPU = 0b010000; // GP4 pullup
nGPPU=0; // enable pullup
}
void timer1Begin(void){
T1CON = 0b00100001; // clock:FOSC/4=8MHz/4=2MHz
// prescaler: 1/4 , timer1 clock=0.5MHz, 2uS //
// 16bittimer1 on, overflow: 2uS x 500(65535-500) = 1mS
TMR1H=0xFE; //65035:0xFE0B
TMR1L=0x0B; // TMR1:0xFE12
TMR1IF = 0;
TMR1IE = 1;
}
void serialBegin(void) {
T2CON = 0x01; // Enables Timer2 (T2CON)
TMR2IF = 0; // Clears Timer2 interrupt flag (TMR2IF)
TXPIN = 1; // Setups TXPIN high (initial state)
}
void serialWrite(char data) {
uint8_t bit_cnt = 0; //Initializes bit counter (bit_cnt) and bit position
uint8_t bit_pos = 0x01; //Initial bit position (bit_pos)
PR2 = BAUD; //Sets baud rate register (PR2)
TXPIN = 0; //Outputs start bit (TXPIN low)
TMR2 = 0; //Clears Timer2 (TMR2)
T2CON = 0x05;//Timer2 for 1/4 prescaler and 1:1 postscaler
while(bit_cnt < 10) { //Waits for transmission completion
while(!TMR2IF); // Wait Detect TMR2 Flag
TMR2IF = 0;
bit_cnt++;
if(bit_cnt > 0 && bit_cnt < 9) {
TXPIN = (data & bit_pos) ? 1 : 0; // Output Data Bit
bit_pos <<= 1;
} else if(bit_cnt == 9) { // End Data
TXPIN = 1; // Output STOP Bit
} else if(bit_cnt == 10) { // End STOP Bit
T2CON = 0x01; // Stop TMR2
}
}
}
void putch(char ch) {
serialWrite(ch);
}
/* result
19:53:08.180 -> 167
19:53:09.204 -> 168
19:53:10.187 -> 169
19:53:11.216 -> 170
19:53:12.237 -> 170
19:53:13.220 -> 172
19:53:14.250 -> 172
19:53:15.234 -> 174
19:53:16.262 -> 175
19:53:17.239 -> 176
*/
/*
Timer1 Interrupt
The Timer1 register pair (TMR1H:TMR1L) increments
to FFFFh and rolls over to 0000h. When Timer1 rolls
over, the Timer1 interrupt flag bit of the PIR1 register is
set. To enable the interrupt on rollover, you must set
these bits:
? Timer1 interrupt enable bit of the PIE1 register TMR1IE = 1;
? PEIE bit of the INTCON register PEIE=1
? GIE bit of the INTCON register GIE=1
The interrupt is cleared by clearing the TMR1IF bit
in the Interrupt Service Routine. TMR1IF = 0;
*/
/*
T1CON: TIMER1 CONTROL REGISTER
T1GINV(1) TMR1GE(2) T1CKPS1 T1CKPS0 T1OSCEN T1SYNC TMR1CS TMR1ON
bit 7 T1GINV: Timer1 Gate Invert bit(1)
1 = Timer1 gate is active-high (Timer1 counts when gate is high)
0 = Timer1 gate is active-low (Timer1 counts when gate is low)
bit 6 TMR1GE: Timer1 Gate Enable bit(2)
If TMR1ON = 0:
This bit is ignored
If TMR1ON = 1:
1 = Timer1 is on if Timer1 gate is not active
0 = Timer1 is on
bit 5-4 T1CKPS<1:0>: Timer1 Input Clock Prescale Select bits
11 = 1:8 Prescale Value
10 = 1:4 Prescale Value
01 = 1:2 Prescale Value
00 = 1:1 Prescale Value
bit 3 T1OSCEN: LP Oscillator Enable Control bit
If INTOSC without CLKOUT oscillator is active:
1 = LP oscillator is enabled for Timer1 clock
0 = LP oscillator is off
Else:
This bit is ignored. LP oscillator is disabled.
bit 2 T1SYNC: Timer1 External Clock Input Synchronization Control bit
TMR1CS = 1:
1 = Do not synchronize external clock input
0 = Synchronize external clock input
TMR1CS = 0:
This bit is ignored. Timer1 uses the internal clock
bit 1 TMR1CS: Timer1 Clock Source Select bit
1 = External clock from T1CKI pin (on the rising edge)
0 = Internal clock (FOSC/4)
bit 0 TMR1ON: Timer1 On bit
1 = Enables Timer1
0 = Stops Timer1
*/