Home     Contact     Forum     Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science     RSS     Terms of services     Privacy policy  
   
 Home     Forum     Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science   

Author Topic: Sleep Instruction  (Read 18751 times)

0 Members and 1 Guest are viewing this topic.

_pike

  • Administrator
  • Full Member
  • *****
  • Posts: 182
Sleep Instruction
« on: March 26, 2011, 20:53:20 PM »
Ok another issue that i face with.....
If you put the chip into sleep mode,then what you will  have to do to wake it up????Also when it wakes up, then it will continue from the sleep instruction and after?The program counter resets?

Example
.....
.......
BSF PORTB,1
CALL 500MSDELAY
BCF PORTB,2
SLEEP
........




Thank you!!!!!


kam

  • Administrator
  • Hero Member
  • *****
  • Posts: 1849
Re: Sleep Instruction
« Reply #1 on: March 26, 2011, 21:44:44 PM »
there are several "alarms" to wake it up from sleep. when it wakes up, it continues immediately after the sleep instruction. The PC does not reset.

Regarding the sleep, i paste from the 16F88 datasheet

Quote
The user can wake-up from Sleep through external Reset, Watchdog Timer wake-up or through an interrupt.

But other chips may have different wake up methods. Also note that during sleep, other modules are operating and others not! You must be very careful before you put it into bed. For example, timer0 does NOT operate during sleep, but timer 1 and 2 do operate.

Let me ask you something regarding my e-book. Sometimes i get the feeling that i chose the wrong PIC for the book. 16F88 has a heck load of modules and functions, that the rookies do not feel very comfortable. Do you think that i should start the book with a much simpler PIC, with down to minimum modules? I am talking about the PWM module, the serial SPI and I2C, the comparator, the USART.... These are features that i think that beginners feel helpless when they read the manual.

My point is this: through my book, i will try to make the users to feel comfortable to read the datasheet. Learning to read the datasheets for all sort of components is the alpha and omega. But the 16F88 has like 200 pages datasheet. Most of the info in these pages are not addressed to a beginner. But the problem is that the datasheet is a datasheet and not a tutorial, so there is no obvious line between the "beginner's staff" and the "advanced staff". they are all mixed.

So here is my question: Do you think that i should choose a simpler PIC for beginners? With more straight forward datasheet?

_pike

  • Administrator
  • Full Member
  • *****
  • Posts: 182
Re: Sleep Instruction
« Reply #2 on: March 27, 2011, 00:54:01 AM »
Before 6 months i didn't know what a register is! I learnt lot of things thru EXAMPLES (were was GOOD explanation among them)and from a couple e-books that i downloaded!!!I also didn't know anything about assembly.The only programming language that i was tought to my school was basic so it was more difficult to understand a new language and how to use "smart" the commands.
Right now i am not able to read PROPERLY any pic's datasheet!!!!!I just need a little guideance of how to....I keep keep learning and believe me I TRULY WANT TO LEARN!!!!

Do you think that i should start the book with a much simpler PIC, with down to minimum modules? I am talking about the PWM module, the serial SPI and I2C, the comparator, the USART.... These are features that i think that beginners feel helpless when they read the manual.
.
And to do what ???? Turning a led on off??? (i am joking)I mean that you can do simply code with the particular pic chip, ....For those who want to go further and their knowledge increses, they will have a good base to continue.....If you start with 12f for example..what they can do?I mean that this series have few I/O ports which is not good for the beginner.
If you want to interface an lcd ,or segments displays, can you do this with 12F easy????NO!!You have to use shift register chips and the things can be more complicated!!! (And believe many rookies want to start with the particular project.)
I thing that you have chosen THE RIGHT PIC where anyone can improve his skills while you move on....Keep walking i am a fun of yours!!!!I am waiting impatience the next part of your book!!!!

Now about the sleep function,if i send a pulse to MCLR the pic will wake up? And what duration should have?


« Last Edit: March 27, 2011, 00:58:57 AM by _pike »

kam

  • Administrator
  • Hero Member
  • *****
  • Posts: 1849
Re: Sleep Instruction
« Reply #3 on: March 27, 2011, 09:49:13 AM »
I am not talking about the 12 series. for example PIC16F676, has 14 pins, 12 I/O ports, 8 A/D channels and 2 timer modules, and because there are no other peripheral modules, it has a small datasheet (about 100 pages) easy to read. For example, compare the 16F88 datasheet and the 16F676, and read the interrupt chapter from both. You will notice that life is easier in the 676 because of the absence of the peripheral modules. There is only one register to cope with. As a matter of fact, there are only 2 register banks to deal.
Regarding the MCLR interrupt, the width of the pulse is limited to the electrical characteristics. Ideally, this can by a tiny single shot, but due to rise and fall times it is widened. But do not worry, send a single quick pulse and it will work, the PIC will wake up. But the MCLR will also cause a reset! Keep that in mind.

_pike

  • Administrator
  • Full Member
  • *****
  • Posts: 182
Re: Sleep Instruction
« Reply #4 on: March 27, 2011, 13:37:40 PM »
Do not start over....
1)PMW.. can be useful for ambient lights, servos ,time controllers.....etc
2)The serial SPI and I2C Who will not want to interface with SD cards and eeproms.....?
3)The USART.... Communication for pic-pc-(bluetooth(i think) For controlling relays,data logger and so on.....
.
As you see you can do MANY GOOD PROJECTS with these modules....I admit that i can't use them yet ,but i am trying to fill my GAPS to move on...We (rookies) need a little of your patience and a good detailed description among instruction.The one who will want to learn , will learn no matter what....
.
These are features that i think that beginners feel helpless when they read the manual. 
.
Yes but with these features you can do intresting projects that will help you understand better.....(My opinion)
About the sleep function now.....
I thought that when you send a pulse to MCLR during sleep operation the pic will wake up without a complete reset....So what can i do to wake it up from the outside world????? Also lets forget for now a wake up from a built-in timer overflow..

kam

  • Administrator
  • Hero Member
  • *****
  • Posts: 1849
Re: Sleep Instruction
« Reply #5 on: March 28, 2011, 15:14:30 PM »
I will think again about the change of PIC. Maybe i change it, maybe not. No matter what, i need to rewrite the examples to be little bit more easy to read. anyway.

the best method to wake up from the outside world is the RB0/INT interrupt. absolutely customizable (can be either HIGH to LOW or LOW to HIGH transition). Here is half of an example:

Code: [Select]
org 0x00
          goto Start

org 0x04
          BSF PORTA,0    ;Turn on LED to indicate that the interrupt is working
          RETFIE


Start
          Intilialize all ports as inputs except PORTA 0 to be output and connect an LED through 220 ohm resistor
          BCF PORTA,0     ;turn off led
          Initialize also the RB0 INTerrupt...

MainLoop     
          goto MainLoop ;DO nothing, just wait for the interrupt to occur


You need to do the initialization job, which is fairly simple. It could be simpler if it was not the 16F88 :) but now you need to initialize also the peripheral interrupts...

_pike

  • Administrator
  • Full Member
  • *****
  • Posts: 182
Re: Sleep Instruction
« Reply #6 on: March 28, 2011, 17:48:05 PM »
Ok!!!!I am going to complile it tonight...I will post it to tell me if i am correct THANK YOU!!!

Hope to have minimum errors.....
Code: [Select]

list       F=inhx8m, P=16F88, R=hex, N=0

#include  ; PIC definitions

 __config _config1,_INTRC_OSC_NOCLKOUT & _WDT_OFF & _LVP_OFF & _PWRTE_OFF & _MCLR_OFF & _BODEN_OFF & _LVP_OFF & _CPD_OFF & _WRT_PROTECT_OFF & _CCP1_RB0 & _CP_OFF

 __config _config2,_IESO_OFF & _FCMEN_OFF

 Errorlevel -302      ; switches off Message [302]: Register in operand not in bank 0.

cblock
DELAYGPR1
DELAYGPR2
endc

        org     0x00
        goto    Init


; Interrupt Service Routine -----------------------------------------------

        org     0x04               ; ISR beginning
        BSF PORTA,0    ;Turn on LED to indicate that the interrupt is working
        CALL DELAY_250MS
BCF PORTA,0
CALL DELAY_250MS
BSF PORTA,0   
        CALL DELAY_250MS
BCF PORTA,0
CALL DELAY_250MS
RETFIE


; -------------------------------------------------------------------------
; Microcontroller initialization
Init           


BCF     STATUS,RP0          ; Bank 0
        BCF     STATUS,RP1          ; Bank 0
       
CLRF    PORTA               ; Initialize PORTA by clearing output data latches
        CLRF    PORTB               ; Initialize PORTB by clearing output data latches
       
        BSF     STATUS,RP0          ; Bank 1
        BCF     STATUS,RP1          ; Bank 1

MOVLW   0x07                ; Turn comparators off and enable pins (PORTA) for I/O functions
        MOVWF   CMCON               ;

        MOVLW   B'11111110'         ; Value used to initialize data direction
        MOVWF   TRISA               ;
                                    ;
                                    ;
        MOVLW   B'01000000'         ; Value used to initialize data direction
        MOVWF   TRISB               ;

BSF INTCON,4

        BCF     STATUS,RP0          ; Bank 0
        BCF     STATUS,RP1          ; Bank 0


; Main Program ------------------------------------------------------------

Start
          CALL DELAY_250MS
          BCF PORTA,0     ;turn off led
          SLEEP
  NOP


MainLoop     
          goto MainLoop ;DO nothing, just wait for the interrupt to occur
           
         
DELAY_250MS                     
        DECFSZ   DELAYGPR1, f       
        GOTO     $+2                 
        DECFSZ   DELAYGPR2, f       
        GOTO     DELAY_250MS       
        GOTO     $+1                 
        NOP                         
        RETURN 

     end             ; end of program

Take a look of the schematic also..... ;)

P.s When i wrote the code the instructions were in the same column when i post it voila...!How can i fix this problem?
« Last Edit: March 29, 2011, 00:50:18 AM by _pike »

kam

  • Administrator
  • Hero Member
  • *****
  • Posts: 1849
Re: Sleep Instruction
« Reply #7 on: March 31, 2011, 23:28:13 PM »
ahem... did you test this code? i do not think it will work properly.  where do you set the interrupts? You need to enable intcon, gie and also enable intcon,rb0, and also clear the interrupt flag of rbo/int interrupt every time before retfie to re-enable the interrupt. I did not compile the code and i do not really know if you have more errors.

Also, here are some tips: First, do not use hex numbers to set registers. look:
MOVLW   0x07                ; Turn comparators off and enable pins (PORTA) for I/O functions
MOVWF   CMCON 

and this:
MOVLW   b'00000111'                ; Turn comparators off and enable pins (PORTA) for I/O functions
MOVWF   CMCON 

This is easier to understand what each bit is.
-------------------------------------------------------------

Also, it is very very important to keep all ports as inputs if they are not used, during prototyping time. That is because during prototyping, you may make a mistake and connect VCC or GND to an output PIN. If you connect VCC to a LOW output, then the pin (and maybe the PIC) will be destroyed. When you move from prototyping to design, then all unused pins should be declared as outputs, because input pins must not left floating.

-------------------------------------------------------------

finally, learn how to use macros to change banks. look this:
BSF     STATUS,RP0 
BCF     STATUS,RP1          ; Bank 1

and this:

BANK1

which one is better? You need of course to declare a macro with name BANK1 somewhere in the beginning. I usually add those macros in the header file. Here is a macro to switch to bank 1:

Bank1   macro
        bcf     status,rp1
        bsf     status,rp0 
        endm

You can now switch to Bank1 simply by writing "bank1"... Nice eh?

-------------------------------------------------------------
Same declare basic flags like zero, carry and dcarry :
#define carry   status,c ; STATUS register
#define dcarry  status,dc
#define zero    status,z

-------------------------------------------------------------
As a matter of fact, this is what i have add at the end of file PIC16F88.inc:

Code: [Select]
; Macros
Bank0   macro
        bcf     status,rp1
        bcf     status,rp0        ; BANK0 (Data Memory)
        ;bcf     status,irp
        endm

Bank1   macro
        bcf     status,rp1
        bsf     status,rp0        ; BANK1 (Data Memory)
        ;bcf     status,irp
        endm

Bank2   macro
        bsf     status,rp1
        bcf     status,rp0        ; BANK1 (Data Memory)
        ;bsf     status,irp
        endm

Bank3   macro
        bsf     status,rp1
        bsf     status,rp0        ; BANK1 (Data Memory)
        ;bsf     status,irp
        endm

; Definitions of Bits
#define carry   status,c ; STATUS register
#define dcarry  status,dc
#define zero    status,z


The schematic is ok ;)

_pike

  • Administrator
  • Full Member
  • *****
  • Posts: 182
Re: Sleep Instruction
« Reply #8 on: April 01, 2011, 01:55:55 AM »
Some of your tips are FULLY DETAILED in your book and are really helpful!!!!!!!!!
Since i met your template and your examples, i wasn't familiar of using header files...and i must admit i am not feeling comfortable yet.... (For having macros,etc on them) But i am going to place them in the start of any program that i make, until i'll learn how to use correctly the header files.....
.
You said.....
Also, it is very very important to keep all ports as inputs if they are not used, during prototyping time. That is because during prototyping, you may make a mistake and connect VCC or GND to an output PIN. If you connect VCC to a LOW output, then the pin (and maybe the PIC) will be destroyed. When you move from prototyping to design, then all unused pins should be declared as outputs, because input pins must not left floating.
.
During prototyping you probably use breadboard for experiments.But what happens if you approach your hand near the pic pins?Does your hand affects pic?If so,is there any solution to be taken?
I will correct these errors you told me and i will post again!. :)
Please don't loose your composure because this is the first time that i am dealing with these functions :-[
Thanks again for helping me..... ;)


P.s   If you write this one.......
BSF    INTCON,7
BSF    INTCON,4


instead of.....
clrw
movlw b'10010000'
movwf intcon


Isn't the same?Don't you enable the bits you told me to...?


« Last Edit: April 01, 2011, 02:12:09 AM by _pike »

kam

  • Administrator
  • Hero Member
  • *****
  • Posts: 1849
Re: Sleep Instruction
« Reply #9 on: April 02, 2011, 09:30:10 AM »
PICs hate ESD, and i've learned it the hard way. They are supposed to be ESD protected, but how much ESD? This is relative. I mean, An Electro-Static Discharge from your body to the PIC, may not actually destroy it immediately, but it can do such damage, that it will shorten its lifespan radically. Avoid by any means touching the PIC leads. When i prototype, i hook myself to the grounding pole of a socket with an anti-static wristband. This wristband you can get from ebay for 0.99, and it is a bracelet with a metal plate connected to a wire and a crocodile clip. This way, the electrostatic charge you may gain is transferred to mother earth. Also, for not getting confused, the ESD may cause problem on any pin of the PIC, no matter if it is input, output or power.

Regarding the INTCON, yes they are the same. And now here is the nice part with the names. You write:

BSF    INTCON,7
BSF    INTCON,4

And i write:

BSF    INTCON,GIE
BSF    INTCON,INT0IE

In time, you will learn that GIE mean Global Interrupt Enable, and INT0IE means INT 0 (from INT RB0 pin) Interrupt Enable. You do not need to remember the actual bit number for each, just remember the name. And that is really helpful when you start working with long assembly sheets with hundreds of register calls, and you need to debug it!