dadel
Joined: 24 Nov 2024 Posts: 1 Location: Ecuador
|
TIMER1 during SLEEP with an external oscillator |
Posted: Sun Nov 24, 2024 9:26 pm |
|
|
Hello everyone,
I'm working on a project using a PIC16F877A, where I read data from a sensor every minute and display it on an LCD. Afterward, the PIC enters sleep mode. To wake it up, I'm using an external interrupt from RB0 and an interrupt from TMR1. According to the datasheet, in Asynchronous Counter Mode, TMR1 can still count while the PIC is in sleep mode.
For this, I followed the recommended configuration, using a 32kHz crystal with 33pF capacitors. However, the oscillator isn't functioning at all. Any insights into what might be causing the issue or alternatives to this aplication would be greatly appreciated. Thank you in advance!
Code: |
#include <16f877a.h>
#device ADC=10
#fuses XT,NOWDT,NOPROTECT,PUT,NOLVP,BROWNOUT
#use delay(clock=4M)
#include <lcd.c>
int ADCV=0,ADCA=0,cont=0;
short sleep_mode;
#INT_TIMER1
void timer1_isr() {
cont++;
if (cont >= 19) {
sleep_mode=FALSE;
cont = 0;
}
}
#INT_EXT
void RBO (){
static short button_pressed=FALSE;
if(!button_pressed)
{
button_pressed=TRUE;
sleep_mode=FALSE;
ext_int_edge(L_TO_H);
}
else
{
button_pressed=FALSE;
sleep_mode=TRUE;
ext_int_edge(H_TO_L);
}
if(!input(PIN_B0))
button_pressed=TRUE;
delay_ms(100);
}
void main()
{
sleep_mode=FALSE;
ext_int_edge(H_TO_L);
setup_timer_1(T1_EXTERNAL | T1_DIV_BY_1);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(AN0_AN1_AN2_AN3_AN4);
set_adc_channel(1);
set_tris_b(0xFF);
while(true)
{
if(sleep_mode) {
sleep();
}
delay_us(10);
ADCA=read_adc();
ADCV=(ADCA*0.488);
lcd_init();
delay_ms(10);
lcd_gotoxy(1,1);
printf(lcd_putc, "Temp: %u", ADCV);
delay_ms(2000);
sleep_mode=TRUE;
}
}
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19529
|
|
Posted: Mon Nov 25, 2024 2:26 am |
|
|
T1OSCEN.
T1_EXTERNAL, says to clock timer 1 from an external clock coming in. Not
to enable the oscillator. Hence your problem. You have to turn the clock output
'on' to make the oscillator run. So:
setup_timer_1(T1_EXTERNAL | T1_DIV_BY_1 |T1_CLK_OUT);
This turns on the oscillator enable bit T1OSCEN.
Your heading for the post says 'external oscillator', but your description says
you want to use the internal T1 oscillator (with external timing components
of course). |
|