CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Sleep Mode PIC16F877

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
znses



Joined: 15 Apr 2011
Posts: 9

View user's profile Send private message

Sleep Mode PIC16F877
PostPosted: Tue May 10, 2011 8:44 am     Reply with quote

How can I configure a sleep mode for PIC16F877 and how to quit from sleep mode in order to enable the PIC to read from the sensors.


Can anyone help me in formulating a code for doing that.
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Tue May 10, 2011 10:02 am     Reply with quote

hi,

You need to have an interrupt to wake up from the sleep mode.

You may do so by using pin B0 (specific interrupt pin), or port_B interrupts on change, or (im not sure) timer interrupts.

You need to set up the interrupt servce routine (ISR), enable interrupts.

You need something to provide the external interrupt signal,
a RTC, a control signal, a comparator, a 555 timer, what ever.

G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 10, 2011 11:44 am     Reply with quote

CCS has an example file for the 16F877 to wake up from sleep by using
the #int_ext interrupt:
Quote:

c:\program files\picc\examples\ex_wakup.c
znses



Joined: 15 Apr 2011
Posts: 9

View user's profile Send private message

PostPosted: Tue May 10, 2011 12:47 pm     Reply with quote

I got this error message A #DEVICE required before this line

when I want to use the example code

If I want to use a bush button of RB4 in ICD3 how can I manage that ?
Code:

#if defined(__PCM__)
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)  // Jumpers: 8 to 11, 7 to 12 to 12
#endif

// global flag to send processor into sleep mode
short sleep_mode;

// external interrupt when button pushed and released
#INT_EXT
void ext_isr() {
static short button_pressed=FALSE;

   if(!button_pressed)        // if button action and was not pressed
   {
      button_pressed=TRUE;    // the button is now down
      sleep_mode=TRUE;        // activate sleep
      printf("The processor is now sleeping.\r\n");
      ext_int_edge(L_TO_H);   // change so interrupts on release
   }
   else                       // if button action and was pressed
   {
      button_pressed=FALSE;   // the button is now up
      sleep_mode=FALSE;       // reset sleep flag
      ext_int_edge(H_TO_L);   // change so interrupts on press
   }
   if(!input(PIN_B0))         // keep button action sychronized wth button flag
      button_pressed=TRUE;
   delay_ms(100);             // debounce button
}

// main program that increments counter every second unless sleeping
void main()   {
   long counter;

   sleep_mode=FALSE;          // init sleep flag

   ext_int_edge(H_TO_L);      // init interrupt triggering for button press
   enable_interrupts(INT_EXT);// turn on interrupts
   enable_interrupts(GLOBAL);

   printf("\n\n");

   counter=0;                 // reset the counter
   while(TRUE)
   {
      if(sleep_mode)          // if sleep flag set
         sleep();             // make processor sleep
      printf("The count value is:  %5ld     \r\n",counter);
      counter++;              // display count value and increment
      delay_ms(1000);         // every second
   }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 10, 2011 1:03 pm     Reply with quote

Quote:
I got this error message A #DEVICE required before this line

It means you don't have the PCM compiler installed, or you don't have
the 16F877 selected in MPLAB, or in the CCS IDE. You've got to learn
how to use the compiler before you do a project.


Quote:
when I want to use the example code

If I want to use a push button of RB4 in ICD3 how can I manage that ?

The ICD3 programmer doesn't affect anything about using RB4 as an
interrupt. It's completely an unrelated topic. To use RB4, you would
have to use the #int_rb interrupt. But in the 16F877, this interrupt works
for pins B4-B7 all at the same time. You can't enable it for just one pin.
At your level of knowledge, this is too difficult. I suggest you use pin B0
with #int_ext. The #int_ext interrupt only uses Pin B0, and is much
easier for you to do.
znses



Joined: 15 Apr 2011
Posts: 9

View user's profile Send private message

PostPosted: Tue May 10, 2011 1:22 pm     Reply with quote

Regarding the error : Error 128 "C:\Users\kustaff\Desktop\bunage\T.c" Line 4(1,70): A #DEVICE required before this line


I ensured that all your step for detecting this error are confirmed but I still get this error despite I have been programming other codes and it worked !

Regards
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 10, 2011 1:28 pm     Reply with quote

It compiles OK for me. I copy-and-pasted it into an existing project for
the 16F877 in MPLAB (I commented out the other code for that project)
and it compiled without errors.

If I change the PIC in MPLAB menu for "Configure / Select Device" to
something else like 18F452, then I get the same error as you.

So, I still say that your configuration is probably incorrect.

What is your compiler version ?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group