// P6MX313isrtimer0 20240406
// description: An LED is blinking,interval 262ms using interrupt timer0
// board: breadboard PIC16F18313 5V INTOSC32MHz
// MPU; PIC16F18313 Flash:3.5KB, SRAM:256B, EEPROM:256B
// TIMER:3, PWM10bit:2 USART:1,MSSP:1,DSM:1,DA5bit;1
// language: C XC8 v2.46 C99
// IDE: MPLAB X v6.20 , XC8 v2.46,PIC16F1xxx_DFP 1.24.387
// Debug Tool; PICKIT4 low voltage programming mode
// PINOUT: PIC16F18313 8pinDIP
//5V 1 VDD VSS 8 GND
// 2 RA5 RA0 7 ICSPDAT
// 3 RA4 RA1 6 ICSPCLK
//BUT/RST4 MCR RA2 5 LED2
//----------------------------------------
//IO MAP
// RA5 RA4 RA3 RA2 RA1 RA0
// IO IO IO IO IO IO
// OUT OUT IN OUT IN OUT
// - - RST LED 2 - -
//-----------------------------------------
#pragma config FEXTOSC = OFF, RSTOSC = HFINT32 //HFINTOSC with 2x PLL: 32MHz
#pragma config CLKOUTEN = OFF, CSWEN = ON, FCMEN = ON
#pragma config MCLRE = ON, PWRTE = OFF,WDTE = OFF,LPBOREN = OFF,BOREN = ON
#pragma config BORV = LOW, PPS1WAY = ON, STVREN = ON, DEBUG = OFF
#pragma config WRT = OFF, LVP = ON // low voltage programming mode
#pragma config CP= OFF, CPD=OFF
//--------------------------------------------
#include
#define _XTAL_FREQ 32000000 // delay_ms(x)
#define LED2 LATA2 //LED
//------------------------------------
struct Codeinfo{ // for code Information
const char codeId[8]; // code Index
const char name[16]; // code name
const char mcu[8]; // model micro controller unit
const int version; // version number
const unsigned long createdDate; // Date created code
};
void gpioBegin(void); //configure input/output pins.
void timer0Begin(void); //set up Timer0 for interrupt-driven blinking.
//---------------------------------------
void main(void) {
struct Codeinfo ci={"P6MX313","isrtimer0","16F18313",0,20240406};
gpioBegin();
timer0Begin();
PEIE=1; //Enables Peripheral interrupts
GIE=1; //Enables Global interrupts
while(1){
}// end while
}// end main
//----------------------------------------
void __interrupt() isrTimer0(void){
if(TMR0IF){ // Checks the Timer0 overflow
TMR0IF=0; // clears flag
LED2 ^=1; // LED2 toggle
}
}
void gpioBegin(void){
ANSELA = 0b000000 ; // digital
TRISA = 0b111010 ; // RA1,3:input RA0,2,4,5;output
PORTA = 0b000000 ; // Initializes PORTA pins to output low
WPUA = 0b110000 ; // RA4,5:PULLUP
}
void timer0Begin(void){ //overflow every 262ms at 32MHz clock.
T0CON1 = 0b01001101; // Fosc/4, Sync. PreS1101:1/8192
T0CON0 = 0b10000000; // T0 ON, 8bit, Post 1/1
TMR0IF = 0; // TMR0 flag clear
TMR0IE = 1; // Enable TMR0 interrupt
}
/*
data: 25/256bytes 10%
program: 89/2048bytes 4%
*/
/*
TCONT1 overflow time
TCON1 T0CKPS[3:0] 8bit mode FOSC 32MHz
1001: 2048 66mS
1100: 4092 132mS
1101: 8192 262ms
1110: 16384 484mS
1111: 32768 1sec
*/
/*
T0CON0: 8/16bit TIMER0 CONTROL REGISTER 0
T0EN — T0OUT T016BIT T0OUTPS[3:0]
bit 7 T0EN:TMR0 Enable bit
1 = The module is enabled and operating
0 = The module is disabled and in the lowest power mode
bit 6 Unimplemented: Read as ‘0’
bit 5 T0OUT:TMR0 Output (read-only)
TMR0 output bit
bit 4 T016BIT: TMR0 Operating as 16-bit Timer Select bit
1 = TMR0 is a 16-bit timer
0 = TMR0 is an 8-bit timer
bit 3-0 T0OUTPS[3:0]: TMR0 Output Postscaler (divider) Select bits
0000 = 1:1 Postscaler
0001 = 1:2 Postscaler
0010 = 1:3 Postscaler
0011 = 1:4 Postscaler
0100 = 1:5 Postscaler
0101 = 1:6 Postscaler
0110 = 1:7 Postscaler
0111 = 1:8 Postscaler
1000 = 1:9 Postscaler
1001 = 1:10 Postscaler
1010 = 1:11 Postscaler
1011 = 1:12 Postscaler
1100 = 1:13 Postscaler
1101 = 1:14 Postscaler
1110 = 1:15 Postscaler
1111 = 1:16 Postscaler
*/
/*
T0CON1: 8/16bit TIMER0 CONTROL REGISTER 1
T0CS[2:0] T0ASYNC T0CKPS[3:0]
bit 7 bit 0
000 = T0CKIPPS (True)
001 = T0CKIPPS (Inverted)
010 = FOSC/4
011 = HFINTOSC
100 = LFINTOSC
101 = Reserved
110 = SOSC
111 = CLC1
bit 4 T0ASYNC: TMR0 Input Asynchronization Enable bit
1 = The input to the TMR0 counter is not synchronized to system clocks
0 = The input to the TMR0 counter is synchronized to FOSC/4
bit 3-0 T0CKPS[3:0]: Prescaler Rate Select bit
0000 = 1:1
0001 = 1:2
0010 = 1:4
0011 = 1:8
0100 = 1:16
0101 = 1:32
0110 = 1:64
0111 = 1:128
1000 = 1:256
1001 = 1:512
1010 = 1:1024
1011 = 1:2048
1100 = 1:4096
1101 = 1:8192
1110 = 1:16384
1111 = 1:32768
*/