void InitMain(void) {
DACCON0.b7 = 0; // DAC disabled
SRCON0.b7 = 0; // SR latch disabled
CM1CON0.b7 = 0; // Disable comparator 1
CM2CON0.b7 = 0; // Disable comparator 2
TRISA = 0b00011100; // Set RA2,3,4 as inputs, others are output 0 = output 1 = input
// RA3 is VREF+, RA4 is ADC, RA2 tied to ground VREF-, RA5 transistor pull
TRISB = 0b10010011; // Set all RB as output except RB0, RB1, RB4, RB7. RB0 button RB1 provision for button
ANSELA = 0b00010000; // Set all as digital except RA4 which is ADC
ANSELB = 0b00000000; // Set all B ports as digital
INTCON = 0b11011000; // Interrupt on change bit enabled
OPTION_REG = 0b10000000;
T1CON = 0b00111101; //Timer1 Registers Prescaler= 8 - TMR1 Preset = 3035 - Freq = 2.00 Hz - Period = 0.500008 seconds
TMR1H = 11; // preset for timer1 MSB register
TMR1L = 219; // preset for timer1 LSB register
PIE1 = 0b00000001; // Enable Timer1 interrupt
IOCBP = 0b00000000;
IOCBN = 0b00000011; // Negative edge detection on pin RB0 & RB1
ADCON0 = 0b00010001; // AN4 is ADC, ADC is enabled
ADCON1 = 0b10010010; // FOSC/8
FlagForHalfSecond = 0;
}
void Interrupt(){
if (TMR1IF_bit){ // Timer Interrupt happened
TMR1IF_bit = 0;
if (TimerRounds > 120 ) { // 60 seconds to sleep
TimerRounds = 0;
FlagForSleep = 1;
} else TimerRounds ++;
FlagForHalfSecond = 1;
}
if (INTF_bit){ // Pin Interrupt happened
INTF_bit = 0;
if (IOCBF0_bit)
{
FlagForBtnPress = 1;
IOCBF = 0b00000000;
}
delay_ms(5);
}
}
#include "float2ascii.h"
#include "LCD_control.h"
#define POWERSUPPLY lata.f5
unsigned int TimerRounds = 0;
char FlagForSleep = 0;
char FlagForBtnPress = 0;
bit FlagForHalfSecond;
double VoltageReading = 0.0;
char voltage[30];
void InitMain(void) {
DACCON0.b7 = 0; // DAC disabled
SRCON0.b7 = 0; // SR latch disabled
CM1CON0.b7 = 0; // Disable comparator 1
CM2CON0.b7 = 0; // Disable comparator 2
TRISA = 0b00011100; // Set RA2,3,4 as inputs, others are output 0 = output 1 = input
// RA3 is VREF+, RA4 is ADC, RA2 tied to ground VREF-, RA5 transistor pull
TRISB = 0b10010011; // Set all RB as output except RB0, RB1, RB4, RB7. RB0 button RB1 provision for button
ANSELA = 0b00010000; // Set all as digital except RA4 which is ADC
ANSELB = 0b00000000; // Set all B ports as digital
OPTION_REG = 0b10000000;
T1CON = 0b00111101; //Timer1 Registers Prescaler= 8 - TMR1 Preset = 3035 - Freq = 2.00 Hz - Period = 0.500008 seconds
TMR1H = 11; // preset for timer1 MSB register
TMR1L = 219; // preset for timer1 LSB register
PIE1 = 0b00000001; // Enable Timer1 interrupt
IOCBP = 0b00000000;
IOCBN = 0b00000011; // Negative edge detection on pin RB0 & RB1
ADCON0 = 0b00010001; // AN4 is ADC, ADC is enabled
ADCON1 = 0b10010010; // FOSC/8
FlagForHalfSecond = 0;
}
void Interrupt(){
if (TMR1IF_bit){ // Timer Interrupt happened
TMR1IF_bit = 0;
if (TimerRounds > 120 ) { // 60 seconds to sleep
TimerRounds = 0;
FlagForSleep = 1;
} else TimerRounds ++;
FlagForHalfSecond = 1;
}
if (INTF_bit){ // Pin Interrupt happened
INTF_bit = 0;
if (IOCBF0_bit)
{
FlagForBtnPress = 1;
IOCBF = 0b00000000;
}
}
}
// Read the pressure
double ReadVoltage (void) {
double analogvalue = 0.0;
ADCON0.f1 = 1; // Enable conversion
while (ADCON0.f1); // Wait for conversion to end
analogvalue = (ADRESH << 8 ) + ADRESL; // Store results
analogvalue = (analogvalue/1024.00)*5; //calculate actual voltage across the sensor
return analogvalue;
}
void Turnoffdisplay(){
POWERSUPPLY = 1; // Turn off display and stepup supply (Transistor)
lata.f0 = 0; // Turn off all display outputs
lata.f1 = 0; // before sleep
latb.f2 = 0;
latb.f3 = 0;
latb.f5 = 0;
latb.f6 = 0;
delay_ms(10);
}
void main() {
InitMain();
POWERSUPPLY = 0; // ground PNP transistor base to turn it on
INTCON = 0b11011000; // Interrupt on change bit enabled
delay_ms(10);
Lcd_Init(); // Initialize Lcd
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(2,1,"Test "); // This is test text that does it ever get this far.
delay_ms(3000);
while(1)
{
if (FlagForHalfSecond) {
FlagForHalfSecond = 0;
VoltageReading = ReadVoltage();
Float2Ascii(VoltageReading, Voltage,4);
Lcd_Out(1,1,voltage);
LCD_Chr_Cp("V ");
}
if (FlagForSleep == 1)
{
Turnoffdisplay(); // Timer triggered sleep
FlagForSleep = 0;
asm sleep;
}
}
}
..is no error description. Tell us what you expect. Tell us what happens...and what does not happen as expected.Still no luck
Usually not. But in your case: maybe.Good point, I should disable timer interrupts before going to sleep. Only interrupt that should wake from sleep is button RB0 press.
This is a good solution.TMR1-Registers = 65536 - preset-value + TMR1-registers
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?