Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

regarind sleep instruction IN PIC18f452

Status
Not open for further replies.

Munib

Advanced Member level 4
Joined
Jun 11, 2004
Messages
114
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Activity points
1,129
pic sleep instruction

well i am workiong on PIC 18f452, using C18 compiler and using MPLab 6.62
PLz have a look at this code :

when i run this code in the simulator and watch on the timer 1 contents, the timer 1L dont increase from 0, and as aresult program never comes out of sleep,
i am not using any subroutines for handling of interrupts so, GIE=0;

and my operating frequncy is 8MHZ, and timer 1 oscalltor works at 32 KHz
Code:
#include <p18f452.h>  /* for the special function register declarations */
#include <delays.h>
#pragma config OSC = HS
#pragma config OSCS = ON
#pragma config WDT = OFF
#pragma config LVP = OFF


int a;
void main ()
{
	TRISB=0;
	IPR1bits.TMR1IP=1;    //setting priority for interrupt
	INTCON=0x00;           //GIE=0;
	a=1;	
while (1)
{
	


	T1CON=0b10111110; //configuring timer 1 without switching it on
	OSCCON=0x01;        //switching the clock
	PIE1bits.TMR1IE=1;         //setting the interrupt
	IPR1bits.TMR1IP=1;        
                T1CONbits.TMR1ON=1;   //  makjing timer 1 on
	PORTB=a;
	Sleep();
	T1CONbits.TMR1ON=0;
	Delay10KTCYx(1);
	OSCCON=0x00;    //swtichign teh clock back
	a=a+1;

}

}



when i ran this code on MPLAb simulator, it showed PD pin going low, but timer 1 never started , also the BOR and POR bits are also cleared ,
and i cant figure out wats going on
 

what is 32-khz sleep-clock

Hi,

Im sorry to say that you got it all wrong. This is what it should look like:
Code:
#include <p18f452.h>  /* for the special function register declarations */ 
#include <delays.h> 
#pragma config OSC = HS 
#pragma config OSCS = ON 
#pragma config WDT = OFF 
#pragma config LVP = OFF 


int a; 
void main () 
{ 
   TRISB=0; 
   T1CON=0b10111110; //configuring timer 1 without switching it on 
   PIE1bits.TMR1IF=0;         //clearing the interrupt 
   PIE1bits.TMR1IE=1;         //setting the interrupt 
   a=1;    
while (1) 
{ 
   PORTB=a; 
   T1CONbits.TMR1ON=1;   //  makeing timer 1 on 
   Sleep(); // Not sure this is correct Try SLEEP();
   T1CONbits.TMR1ON=0; 
   PIE1bits.TMR1IF=0;         //clearing the interrupt 
   Delay10KTCYx(1); 
   a=a+1; 

} //End while

}//End main

Im not using C18 compiler, so i hope this will work for you. Also coment (//)the SLEEP() function and add this insted of the sleep()
Code:
while (PIE1bits.TMR1IF == 0 )
 continue;
And put a break point after this. The code sould loop in the while like it was in sleep and it will exit when the TMR1IF expiers.
Good luck.
 

    Munib

    Points: 2
    Helpful Answer Positive Rating
t1con pic 18f452

while in my code, i did the clock switching, which u did not do clock switching from exteranal crystal to 32 Khz,
and then u said that my code is wrong, so does it mean that my code should not include that clock swirtching statement?

ie
OSCCON=0x01; //swithjving to 32kHz
and
OSCCON=0x00; //switching back to main clock
 

timer example pic18f452

Hi Munib,

Yes. The clock switching is wrong and also You didnt clear up the flag after waking up from sleep. Look again at the diffrences between the code and where i put each line.
Good luck.
 

timer external clock in pic 18f452

:S

i am a bit confused,
on page 23 of data sheet its written that before switching the clock u need to make switching feature enable and also enable timer 1,

after that u need to set bit 1 of OSCCON register to switch the clock, its there in the data sheet ,

So is there any diffrence in wat u r saying and wats tere in the dats sheet,

Also, if i assume taht you are right and we donot need to set OSCCON, tehn what should i do to bring my clock back from 32KHz to my original clock of 8Mhz (for example), you did not mention taht in your code
 

pic24 timer using 32 khz clock

Hi,

Ok, let me explain now that i know what problem you have. On page 23 it says this:
2.6.1 SYSTEM CLOCK SWITCH BIT
The system clock source switching is performed under
software control. The system clock switch bit, SCS
(OSCCON<0>) controls the clock switching. When the
SCS bit is ’0’, the system clock source comes from the
main oscillator that is selected by the FOSC configuration
bits in Configuration Register1H. When the SCS bit
is set, the system clock source will come from the
Timer1 oscillator. The SCS bit is cleared on all forms of
RESET.
What it means that you can switch the source of your clock to the 32KHz clock. This is nothing to do with the SLEEP!
This is used for other features, or in other words to slow down the system. For example, you want to save power but you dont want to put the PIC to SLEEP, so what you do you slow the clock down to 32KHz. Each instruction now takes 32KHz / 4 = 8KHz. The PIC will work much slower and use much less energy. If the PIC will trigger, then you need to switch the PIC back to the FAST clock and now it will work faster and use much more energy.

So, Thats why i didnt use it in the code. I start the TMR1 from the external 32KHz clock. The PIC will go to sleep ( the OSC 4MHz will stop dead where the sleep code is, its like if you pulled out the OSC source from the PIC ). TMR1 will continue to work. Then when the timer will happen, the OSC will be connected again, and the code iwll keep on runing where it stoped.

If you found my answer useful, click on the button that says Helped me.

Good luck.
 

sleep instruction for c18

well, ARE U sure?

because it is not working that way, my timer 1 does not increment , so, it never comes out of


while (PIRbits.TMR1IF==0);



and i cant figure out why?
:(

and i have another query,
if ur above post was right and in u post u said that, as soon as my timer 1 overflows, MCU comes out of sleep and my main oscialltor of 4Mhz is on,

but when our MCu start using our 32Khz clock, ? is it during the sleep only that it uses 32 Khz clock,?

if yes then it also means taht out timer 1 On statement should be exactly above the sleep instruction


am i rite?
 

slow down clock for pic18

Hi,

because it is not working that way, my timer 1 does not increment , so, it never comes out of
while (PIRbits.TMR1IF==0);

Use the debug mode to read the TMR1L and TMR1H. You can run a bit and then stop and then see if the TMR1X changed the the watch window. If it didnt then go back to FIGURE 11-1: TIMER1 BLOCK DIAGRAM in the pdf and see how you configured the settings. Also, i saw that for some reson i didnt write in the code the loaded sequance of the timer. i.e. TMR1L = ? and TMR1H = ?. Also, it has to be loaded again after the TMR1IF expaired.

if ur above post was right and in u post u said that, as soon as my timer 1 overflows, MCU comes out of sleep and my main oscialltor of 4Mhz is on,
but when our MCu start using our 32Khz clock, ? is it during the sleep only that it uses 32 Khz clock,?
The answer is in 11.2: Timer1 Oscillator
... The oscillator is a low power oscillator rated up to 200 kHz. It will
continue to run during SLEEP. It is primarily intended for a 32 kHz crystal.
So, te 32KHz is always on for TMR1. The MCU is NOT using this timer to run the code.
Look at FIGURE 11-1: TIMER1 BLOCK DIAGRAM and you will see how the TMR1 works, and what you do when you switch it on or off.
Good luck.
 

    Munib

    Points: 2
    Helpful Answer Positive Rating
Hi
i was visiting this site and same problem was with me.still T1RUN is not high and my TMR1 is not running. i cant understand what is the problem.
Saira Saleem
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top