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

RTCC alarm problem with PIC18F67k22

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



Joined: 27 Apr 2011
Posts: 19

View user's profile Send private message

RTCC alarm problem with PIC18F67k22
PostPosted: Mon Apr 02, 2012 4:47 am     Reply with quote

Hello

I'm using PIC18F67K22 with internal OSC 4MHz and CCS compiler ver
4.130. I have the RTCC clock selection used with (SOSC0 & SOSC1)
via crystal 32.768kHz.

I have a simple test program where I set the alarm for 10secs
and then wait to see if my LED will flash after 10secs. The process
then repeats itself as per my program.

However, I can not achieve this since my LED is not flashing
at will with the RTCC alarm. On its own, the LED does flash and i can
even set the flashing rate to any rate that i desire.

Please can someone help and suggest where i'm wrong in my code
below and how to achieve RTCC alarm.

Thanks
Haseeb

Code:

#include <18F67K22.h>

#fuses INTRC_IO, NOWDT, NOBROWNOUT, MCLR
       
// Set Speed to 4Mhz
#use delay(clock=4000000)

#include <stdio.h> 


int alarm_flag = 0;


#int_RTC
void RTC_isr(void)
{

   alarm_flag = 1; //raise flag once 10secs lapse
   
}


void main(void)
{

   delay_ms(500); //startup delay


   enable_interrupts(INT_RTC); //enable RTC interrupts


   setup_rtc(RTC_ENABLE); //enable RTC

   
   while(true) //loop forever
   {
   
      setup_rtc_alarm(RTC_ALARM_ENABLE, RTC_ALARM_10_SECONDS, 0x08); //initiallize 10 sec alarm

      while(alarm_flag == 0); //stay here until 10sec alarm
         
      alarm_flag = 0; //reset flag

      //flash to indicate 10sec alarm
      output_high(pin_B4);
       delay_ms(200);   
       output_low(pin_B4);         
       delay_ms(200);   
       output_high(pin_B4);
       delay_ms(200);   
       output_low(pin_B4);         
       delay_ms(200);      

   }   


}
jeremiah



Joined: 20 Jul 2010
Posts: 1345

View user's profile Send private message

PostPosted: Mon Apr 02, 2012 5:00 am     Reply with quote

Try changing
Code:

enable_interrupts(INT_RTC); //enable RTC interrupts


Code:

enable_interrupts(INT_RTC); //enable RTC interrupts
enable_interrupts(GLOBAL); //enable interrupts


If it gives a compiler error, look in the .h file for which "global" define is correct.
haseeb



Joined: 27 Apr 2011
Posts: 19

View user's profile Send private message

PostPosted: Mon Apr 02, 2012 5:11 am     Reply with quote

Hi

I have just done that and enabled GLOBAL interrupts as well.
Compiler gives NO error. But still my LED will not flash via
alarm....

please help...

Haseeb
haseeb



Joined: 27 Apr 2011
Posts: 19

View user's profile Send private message

PostPosted: Mon Apr 02, 2012 5:57 am     Reply with quote

hello

anybody got any ideas regarding my alarm problem

haseeb
dyeatman



Joined: 06 Sep 2003
Posts: 1933
Location: Norman, OK

View user's profile Send private message

PostPosted: Mon Apr 02, 2012 6:25 am     Reply with quote

Haseeb, this is a user forum, not CCS, so everyone may not be looking at
the forum right now. It may take a while before someone with the specific
knowledge you need gets a chance to check in. You have to be patient. If
you let your project get down to the deadline that is your problem...not one
that we caused.
_________________
Google and Forum Search are some of your best tools!!!!
Ttelmah



Joined: 11 Mar 2010
Posts: 19498

View user's profile Send private message

PostPosted: Mon Apr 02, 2012 8:41 am     Reply with quote

I don't see you enabling the secondary oscillator. This is needed for the RTC to run. SOSC_LOW, and RTCOSC_T1 in the fuses, and setup_timer_1(T1_ENABLE_SOSC); in the code

I think it is probably defaulting to T1, but this is not enabled, so it doesn't run.
The T1 setting controls whether the secondary oscillator runs or not.

Best Wishes
haseeb



Joined: 27 Apr 2011
Posts: 19

View user's profile Send private message

PostPosted: Tue Apr 03, 2012 9:05 am     Reply with quote

Hi

I have just tried your suggestion and still the RTC is not working.
No interrupt occur and my LED does not flash.

please help!

haseeb
jeremiah



Joined: 20 Jul 2010
Posts: 1345

View user's profile Send private message

PostPosted: Tue Apr 03, 2012 10:23 am     Reply with quote

Post your updated code (or just update the code in your first post and let us know), so we can see that changes got in there as it was suggested.
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Tue Apr 03, 2012 12:38 pm     Reply with quote

Code:
 while(alarm_flag == 0); //stay here until 10sec alarm


Syntax...

take the Semi colon out, and add brackets.

Code:
 while(alarm_flag == 0){} //stay here until 10sec alarm



G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
Ttelmah



Joined: 11 Mar 2010
Posts: 19498

View user's profile Send private message

PostPosted: Tue Apr 03, 2012 2:44 pm     Reply with quote

Gabriel wrote:
Code:
 while(alarm_flag == 0); //stay here until 10sec alarm


Syntax...

take the Semi colon out, and add brackets.

Code:
 while(alarm_flag == 0){} //stay here until 10sec alarm



G.


This is not needed.

In C, an 'if', executes the single code block after the command.
A line terminated with ';' is a code block, as are multiple lines contained in '{}' braces.

The original is syntactically perfectly OK.

Best Wishes
haseeb



Joined: 27 Apr 2011
Posts: 19

View user's profile Send private message

PostPosted: Wed Apr 04, 2012 7:04 am     Reply with quote

ok thanks guys for your suggestions....

i will post the new updated code....now

haseeb
haseeb



Joined: 27 Apr 2011
Posts: 19

View user's profile Send private message

PostPosted: Thu Apr 19, 2012 3:16 am     Reply with quote

Hello people

I have tried all that you have suggested and i have even tried out
the RTCC example program as below and it compiles and the output
is as below....HOWEVER....the clock does not seem to tick at all as the
seconds do not increment.

I have disabled the alarm and now its only the clock that i want to
get up and running.

Please can someone help....

Haseeb



Code:
 
OUTPUT on HyperTerminal as below....


Press ENTER after 1 digit answers.
Year 20: 12
Month: 04
Day: 19
Weekday 1-7: 05
Hour: 10
Min: 09

04/19/2012 10:09:00




Code:

//Internal RTCC....

#include <18F67K22.h>


#FUSES NOWDT
#FUSES INTRC_IO
#FUSES NOPROTECT
#FUSES RTCOSC_T1
#FUSES SOSC_LOW
#FUSES MCLR
       
 
// Set Speed to 4Mhz
#use delay(clock=4000000)


// RS232 to PC
#use rs232(baud=9600, UART1, STREAM=User)



#include <stdio.h> 



int8 get_number()
{
  char first,second;

  do {
    first=fgetc(User);
  } while ((first<'0') || (first>'9'));
  fputc(first,User);
  first-='0';

  do {
    second=fgetc(User);
  } while (((second<'0') || (second>'9')) && (second!='\r'));
  fputc(second,User);

  if(second=='\r')
    return(first);
  else
    return((first*10)+(second-'0'));
}

void set_clock(rtc_time_t &date_time)
{
   fprintf(User, "\r\nPress ENTER after 1 digit answers.");
   fprintf(User, "\r\nYear 20: ");
   date_time.tm_year=get_number();
   fprintf(User, "\r\nMonth: ");
   date_time.tm_mon=get_number();
   fprintf(User, "\r\nDay: ");
   date_time.tm_mday=get_number();
   fprintf(User, "\r\nWeekday 1-7: ");
   date_time.tm_wday=get_number();
   fprintf(User, "\r\nHour: ");
   date_time.tm_hour=get_number();
   fprintf(User, "\r\nMin: ");
   date_time.tm_min=get_number();
   date_time.tm_sec=0;

   printf("\r\n\n");
}


void main(void)
{

   //Initiallize All I/O Pins....      

   //PORTA
   output_low(pin_A2);
   output_low(pin_A3);
   output_low(pin_A4);
   output_low(pin_A5);
   output_low(pin_A6);
   output_low(pin_A7);
   
   //PORTB
   output_low(pin_B1);
   output_low(pin_B2);
   output_low(pin_B3);
   output_low(pin_B5);

   //PORTC
   output_low(pin_C2);
   output_high(pin_C5);

   //PORTD
   output_d(0);

   //PORTE
   output_e(0);

   //PORTF
    output_low(pin_F1);
   output_low(pin_F2);
   output_low(pin_F3);
   output_low(pin_F4);
   output_low(pin_F5);
   output_low(pin_F6);
   output_low(pin_F7);

   //PORTG
    output_low(pin_G0);
   output_low(pin_G3);
   output_low(pin_G4);




   disable_interrupts(GLOBAL);



   delay_ms(100); //startup delay



   setup_timer_1(T1_ENABLE_SOSC);



   rtc_time_t write_clock, read_clock;
   
   setup_rtc(RTC_ENABLE,0);         //enables internal RTCC    

   set_clock(write_clock);

   rtc_write(&write_clock);         //writes new clock setting to RTCC
   
   while(true)
   {
      rtc_read(&read_clock);        //reads clock value from RTCC

      fprintf(User, "\r%02u/%02u/20%02u %02u:%02u:%02u",
      read_clock.tm_mon,
      read_clock.tm_mday,
      read_clock.tm_year,
      read_clock.tm_hour,
      read_clock.tm_min,
      read_clock.tm_sec);

      delay_ms(250);
   }




}
haseeb



Joined: 27 Apr 2011
Posts: 19

View user's profile Send private message

PostPosted: Fri Apr 20, 2012 6:25 am     Reply with quote

Hello People...

I have finally been able to get both the clock ticking and the alarm interrupts working as well. A change of crystal solved the problem.

However I can only get alarm interrupts for half second and one second only.

Please can you let me know why I cannot get higher delays with my alarm interrupts. Ideally I want this to be for 1 hour alarm setting.
But I tried to progress to that stage and found I could only do up to 1second complete.

Below is how I set the alarm configurations as per the PIC header fileā€¦



Code:
 
//setup_rtc_alarm(RTC_ALARM_ENABLE, RTC_ALARM_HALFSECOND, 0); //WORKS

//setup_rtc_alarm(RTC_ALARM_ENABLE, RTC_ALARM_SECOND, 0);     //WORKS

//setup_rtc_alarm(RTC_ALARM_ENABLE, RTC_ALARM_10_SECONDS, 0);   //DOES NOT WORK!!

   setup_rtc_alarm(RTC_ALARM_ENABLE, RTC_ALARM_10_MINUTES, 0);   //DOES NOT WORK!!




This clearly reflects a software issue.

Please can you look at my program below and kindly suggest.

Thanks
haseeb

Code:

#include <18F67K22.h>

#FUSES INTRC_IO, NOWDT, SOSC_LOW, NOBROWNOUT, MCLR

 
#device ADC = 12 
 
// Set Speed to 4Mhz
#use delay(clock=4000000)
 
// RS232 to Modem
#use rs232(baud=9600, UART2, STREAM=Modem)

// RS232 to PC
#use rs232(baud=9600, UART1, STREAM=User)


#define Boost_Converter      pin_B5   
#define LED               pin_B4   
#define Emerg_RST_Modem     pin_C2   
#define V285_Pin            pin_C3   
#define V180_Pin            pin_C4   
#define Modem_ON            pin_C5


#include <stdio.h> 


char clock_sleeping = 1; //flag to tell us that we have woken up from ...
                   // ... sleeping when the alarm INT (0 = wakeup / 1 = sleep)    


   
#int_RTC
void RTC_isr(void)
{
   //we have woken up from sleep
   clock_sleeping = 0;   
}



//RTCC function
void set_clock(rtc_time_t &date_time)    
{

   //reseting the clock...
   
   date_time.tm_year=12;  //2012
 
   date_time.tm_mon=4;    //April

   date_time.tm_mday=20;  //20th
   
   date_time.tm_wday=6;     //Friday
   
   date_time.tm_hour=13;  //13hrs
   
   date_time.tm_min=8;     //8min

   date_time.tm_sec=0;    //0secs

}


void main(void)
{

   //Initiallize All I/O Pins....      

   //PORTA
   output_low(pin_A2);
   output_low(pin_A3);
   output_low(pin_A4);
   output_low(pin_A5);
   output_low(pin_A6);
   output_low(pin_A7);
   
   //PORTB
   output_low(pin_B1);
   output_low(pin_B2);
   output_low(pin_B3);
   output_high(pin_B5);

   //PORTC
   output_low(pin_C2);   
   output_high(pin_C5);

   //PORTD
   output_d(0);

   //PORTE
   output_e(0);

   //PORTF
    output_low(pin_F1);
   output_low(pin_F2);
   output_low(pin_F3);
   output_low(pin_F4);
   output_low(pin_F5);
   output_low(pin_F6);
   output_low(pin_F7);

   //PORTG
    output_low(pin_G0);
   output_low(pin_G3);
   output_low(pin_G4);


   enable_interrupts(INT_RTC); //enable RTC interrupts
   enable_interrupts(GLOBAL); //enable interrupts




   delay_ms(100); //startup delay




   //Clock Initiation Commands...
   rtc_time_t write_clock, read_clock;   
   setup_rtc(RTC_ENABLE,0);         //enables internal RTCC    
   set_clock(write_clock);
   rtc_write(&write_clock);         //writes new clock setting to RTCC
   
         
         
   
   //Print initial clock settings....
   rtc_read(&read_clock);        //reads clock value from RTCC   
   fprintf(User, "\n\r\n\r %02u:%02u:%02u", read_clock.tm_hour, read_clock.tm_min, read_clock.tm_sec);   
   

 
 

   while(TRUE)   
   {

      
      //Before going to SLEEP .... set the Alarm!!!

      //setup_rtc_alarm(RTC_ALARM_ENABLE, RTC_ALARM_HALFSECOND, 0); //WORKS
      //setup_rtc_alarm(RTC_ALARM_ENABLE, RTC_ALARM_SECOND, 0);     //WORKS
      //setup_rtc_alarm(RTC_ALARM_ENABLE, RTC_ALARM_10_SECONDS, 0);   //DOES NOT WORK!!
      setup_rtc_alarm(RTC_ALARM_ENABLE, RTC_ALARM_10_MINUTES, 0);   //DOES NOT WORK!!

 
      sleep(); //now goto sleep...


      delay_ms(100); //When waking up, delay needed to bring the PIC up to working state



      if(clock_sleeping == 0) //did we wake up from clock INT?      
      {         

         rtc_read(&read_clock);        //reads clock value from RTCC
   
           fprintf(User, "\n\r\n\r %02u:%02u:%02u", read_clock.tm_hour, read_clock.tm_min, read_clock.tm_sec);   
         
         clock_sleeping = 1; //set sleep flag to indicate that we are about to goto sleep again...

         delay_ms(100); //before we go back and set alarm...put delay as PIC likes it!

      }
   


   }





}
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