- Code: Select all
#include <xc.h>
//Configurations and Fuses
#pragma config OSC = HS, OSCS = OFF,PWRT = ON,BOR = OFF
#pragma config BORV = 45,WDT = OFF,WDTPS = 1,CCP2MUX = OFF
#pragma config STVR = OFF,LVP = OFF,DEBUG = OFF
#pragma config CP0 = OFF,CP1 = OFF,CP2 = OFF,CP3 = OFF,CPB = OFF,CPD = OFF
#pragma config WRT0 = OFF,WRT1 = OFF,WRT2 = OFF,WRT3 = OFF,WRTB = OFF,WRTC = OFF,WRTD = OFF
#pragma config EBTR0 = OFF,EBTR1 = OFF,EBTR2 = OFF,EBTR3 = OFF,EBTRB = OFF
int Lap = 0;
int timeout = 1000;
void Initialization(void) {
LATA = 0x00;
LATB = 0X00;
LATC = 0X00;
LATD = 0X00;
LATE = 0X00;
TRISA = 0x00;
TRISB = 0x00;
TRISC = 0x00;
TRISD = 0x00;
TRISE = 0x00;
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
PORTD = 0x00;
PORTE = 0x00;
}
//REGISTERS ASSOCIATED WITH TIMER1 AS A TIMER/COUNTER
void Timer1Setup(void) {
T1CONbits.RD16 = 1; //1 = Enables one 16-bit operation
//0 = Enables two 8-bit operations
T1CONbits.T1CKPS1 = 1;
T1CONbits.T1CKPS0 = 1;
//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
T1CONbits.T1OSCEN = 0;
//1 = Timer1 Oscillator is enabled
//0 = Timer1 Oscillator is shut-off
T1CONbits.T1SYNC = 0;
T1CONbits.TMR1CS = 0; //0 = Internal clock (FOSC/4)
T1CONbits.TMR1ON = 1; //Timer1 Disabled = 0 /Enabled = 1
//Interrupt flags
INTCONbits.GIE = 0;
INTCONbits.PEIE = 0;
}
void test2loop (void){
PORTB = 0X04;
}
void test1loop(void) {
PORTB = 0x02;
}
void testloop(void) {
PORTB = 0x01;
}
void main(void) {
Initialization(); //Set all device pins.
Timer1Setup(); //Init Timer1 module
TMR1 = 0xffff; // TMR1L = 0xFF - TMR1H = 0xFF
while (1) {
PORTB = 0x00;
if (PIR1bits.TMR1IF == 0) { // did Timer1 overflow?
testloop();
Lap++; // increment +1
}
if (Lap == timeout) {
test1loop(); //CODE ends
}
}
}
I have given total code in test program.