1
\$\begingroup\$

I have a problem with sleep mode on PIC16F628a.
Configuration:
microcontroller running on 4Mhz internal oscillator.
32kHz external oscillator on T1OSI/T1OSO pins.

Program below works fine (having 1s pulses on RA1) with "SLEEP()" commented out.

I'm also positive, that TIMER1 uses external oscillator, because pulses disappear if I disconnect external crystal.

But when I try to use SLEEP() mode, TIMER1 interrupt does not wake PIC up, I'm getting nothing on RA1. I think that I do miss something obvious here.

Code below:

#define _XTAL_FREQ 4000000
// CONFIG
#pragma config FOSC = INTOSCIO  // Oscillator Selection bits (INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF      // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD)
#pragma config BOREN = OFF      // Brown-out Detect Enable bit (BOD disabled)
#pragma config LVP = OFF        // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

#include <xc.h>

void interrupt isr(void){
    if(TMR1IF){
        TMR1ON = 0;
        RA1 =~ RA1;
        TMR1H = 0x80;
        TMR1L = 0x00;
        TMR1IF = 0; 
        TMR1ON = 1;
    }
}

void main(void) {
    CMCON = 0x07; // comparators off
    TRISA = 0x00;
    RA0=1;
    RA1=0;
    RA2 = 0;
    __delay_ms(5000);
    RA0 = 0;
    __delay_ms(500);
    T1CKPS0 = 0;
    T1CKPS1 = 0;
    TMR1CS = 1;
    TMR1H = 0x80;
    TMR1L = 0x00;
    T1OSCEN = 1;
    T1CONbits.nT1SYNC = 1; 
    TMR1IE = 1;
    GIE = 1;          //Enable Global Interrupt
    PEIE = 1; 
    TMR1ON = 1;
    while(1){
        SLEEP();
    }
}

Thank you for your help.

\$\endgroup\$
1
\$\begingroup\$

Okay, I've finally managed to solve the problem.
It is not code related - it's more of my devboard thing (ql-200).
As it seems, when you put PIC into sleep mode, oscillator uses much less power, and thus is shunted out by devboard programmer (which is connected to T1OSI/T1OSO pins).
Thus oscillator fails to generate in sleep mode.
Everything works fine when pulled out of the devboard.
For anyone to bump into this thread - above code works fine, and could be used for test purposes.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

your sleep function is embedded in an infinite loop (you did not input any test conditions in your while loop)

while(1){sleep()}

you can use the TIMER1 interrupt flag as the loop test condition; for example

while(TMIF==0){sleep()}

this will leave the MCU in sleep mode until the flag changes and becomes 1.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thank you for your reply, but it still does not help. I'm not sure it should, though. TIMER1 interrupt calls for ISR routine first, and only then returns to main loop, so on TIMER1 interrupt, I still should get pulse on RA1, then after ISR is finished, next SLEEP should occur. Is it not the way it supposed to be? \$\endgroup\$ – Seitan Oct 31 '17 at 16:22
0
\$\begingroup\$

You are running timer 1 in counter mode. When in counter mode, you can have it run synchronized or asynchronous. In synchronized mode, the timer stops counting and no interrupt is generated when you go to sleep. You therefore have to set T1SYNC so that you run in async mode (active low). Refer to datasheet section 7.1 onwards for more info.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thank you for your suggestion. But I've tried using T1CONbits.nT1SYNC = 1; - and it did nothing, so I've accidentally excluded it from the source code. \$\endgroup\$ – Seitan Oct 31 '17 at 16:27
  • \$\begingroup\$ @Seitan Try configuring TxSOSC (T1SOSC) \$\endgroup\$ – Adam Z Oct 31 '17 at 16:46
  • \$\begingroup\$ I'm afraid PIC16F628 does not have this bit. \$\endgroup\$ – Seitan Oct 31 '17 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.