Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

RTCC alarm problem with PIC18F67k22

Status
Not open for further replies.

haseeb123

New Member
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);		

	}	


}
 
I have also added in the missing line of code....


enable_interrupts(INT_GLOBAL);


but still my program does not work....

plz can someone help.

Haseeb
 
Hi Haseeb,

Need more information:

Is your RTCC actually running?

Does your RTCCFGbits.HALFSEC bit toggle twice every second?

Does your program ever enter the RTCC interrupt?
 
Thanks for your reply.

Please tell me how to add that piece of code line in CCS syntax. I'm using CCS....not C18.
Otherwise i do not know how to monitor the RTCCFGbits.HALFSEC bit in CCS.

thanks
haseeb
 
Sorry Haseeb I have no idea. I have never used CCS, only microchip C.

Hopefully somebody else can chime in?
 
Just a couple more suggestions...

You probably have to enable the SOSC in your fuse settings.

You should make your alarm flag volatile:

volatile int alarm_flag = 0;
 
Last edited:
guys thanks you very much for your suggestions.
however still no luck and im struggling to get the RTCC working....

this is how im enabling the fuses in CCS...

Code:
#FUSES NOWDT 
#FUSES INTRC_IO 
#FUSES NOPROTECT 
#FUSES RTCOSC_T1
 
Does it ever enter the RTCC interrupt?
Is the RTCC is actually running?
Perhaps read the seconds register to see if it's incrementing?
 
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);
   }




}
 
What value capacitors are you using with your 32.768KHz crystal?
 
If you have tried their working example and still no go... It is more likely to be hardware than software.

Use the internal RC oscillator for the RTCC and see if that works instead. If it does, it is definitely a hardware problem.

Make sure the traces between the crystal and legs of the chip are short.
Try a different crystal.
Try a different chip.
 
Hello and thanks for your suggestion.

Please can you advise on how to enable the internall RC oscillator for the RTCC in CCS. I'm a bit confused as im already using the
internal OSC for the main program.

haseeb
 
According to the datasheet there is an internal RC oscillator for the RTCC too.

I have no idea how to enable it in CCS though.
 
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!

		}
	


	}





}
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top