#include #pragma config FOSC=INTOSC, WDTE=ON, PWRTE=ON, MCLRE=OFF, CP=OFF, BOREN=OFF, CLKOUTEN=OFF #pragma config WRT=OFF, PLLEN=OFF, STVREN=OFF, BORV=HI, LPBOREN=OFF, LVP=OFF // Definitions #define _XTAL_FREQ 32000 // this is used by the __delay_ms(xx) and __delay_us(xx) functions #define P_BASE LATA1 #define P_COMP PORTAbits.RA0 //#define P_COMP PORTAbits.RA2 //TMP DEB //********************************************************************************** //***************** main routine *********************************************** //********************************************************************************** void main ( ) { unsigned char LevelCounter=0; unsigned char SleepCounter=0; unsigned char SkipSleep=0; // set up oscillator control register OSCCON = 0; //31kHz WDTCON = 0b00001100; //WDT period 64msec //OPTION_REG = 0b10000111; //TMR0 internal FOSC/4/256 //OPTION_REG = 0b10000001; //TMR0 internal FOSC/4/4 - 0.5msec OPTION_REG = 0b10000000; //TMR0 internal FOSC/4/2 - 0.25msec // PORT A Assignments TRISA = 0b00001101; //make RA1,RA4,RA5 outputs //WPUA = 0b00000000; ANSELA = 0b00000000; // set all analog pins to digital I/O LATA = 0x0; APFCONbits.CWGASEL=1; APFCONbits.CWGBSEL=1; //APFCONbits.CCP1SEL=0;APFCON // The CCP1SEL bit selects which pin the PWM output is on. // The default value for this bit is 0 which sets the // PWM output on RA2. If you want the PWM output on // RA5 instead then set this bit to a 1. PWM2DCL = 0x04; PWM2DCH = 0; PWM2PRH = 0; PWM2PRL = 0x07; PWM2PHH = 0; PWM2PHL = 0; PWM2OFH = 0; PWM2OFL = 0; //PWM2CON = 0xC0; PWMEN= 0b00000010; //PWM2EN_A // Note: PWM uses TMR2 so we need to configure it //PIR1bits.TMR2IF=0; // clear TMR2 interrupt flag //T2CONbits.T2CKPS=0x00; // select TMR2 prescalar as divide by 1 as per our example above ////T2CONbits.TMR2ON=1; // turn TMR2 on //T2CONbits.TMR2ON=0; // turn TMR2 off //INTCONbits.TMR0IF = 0; //CWG1CON0 = 0b11100000; //ON,Aon,Bon CWG1CON1 = 0b00000011; //PWM2 CWG1CON2 = 0b00000000; //No SHDN CWG1DBR = 0b00000000; CWG1DBF = 0b00000000; LevelCounter=0; while (1) { if(!P_COMP) { P_BASE = 1; P_BASE = 0; } SleepCounter++; //for interval = 64ms if((!SleepCounter)&&LevelCounter) { LevelCounter--; //for interval = 8s SleepCounter = 0x80; //for interval = 8s } if(INTCONbits.INTF) { //LATA5 = 1; //TMP DEB INTCONbits.INTF = 0; if(LevelCounter>=5) //for interval = 16sec { //P_BASE = 1; //DEB TMR0 = 10; PWM2CON = 0xC0; //PWM2DCL = 0x04; CWG1CON0 = 0b11100000; while(!(TMR0&0x80)); //delay 29.5msec = 32msec-2.5msec CLRWDT(); //P_BASE = 0; //DEB //P_BASE = 1; //DEB while(TMR0&0x80); //delay 32msec CWG1CON0 = 0b00000000; CLRWDT(); //P_BASE = 0; //DEB SkipSleep=1; } if(LevelCounter<7) LevelCounter ++; //for interval = 16sec } if(SkipSleep) SkipSleep=0; else SLEEP(); } }