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

PCF8563.c driver problems, problems with struct

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







PCF8563.c driver problems, problems with struct
PostPosted: Sun Jun 07, 2009 5:19 pm     Reply with quote

Hi all,

I'm a self learning newbie to this game and at the moment stuck on what is probably a simple problem.

I have set up a PIC18F2550 with a LCD and am trying to communicate to a Temp senor and RTC over I2C.

I have no problem with the temp sensor which is working fine, however when I try to read from the RTC using the inbuilt pcf8563 driver I can't find the right code to achieve passing the arguments with. It's using a struct but I can't find any examples no matter how much I search on how to use it.

Please help!!!

In the headder file of pcf8563.c It states to use rtc_get_datetime(TIME_STRUCT *ts) to read the time.

So I use:
My_sec = rtc_get_datetime(TIME_STRUCT.sec)
but I keep getting "Error 54 expecting a variable" when I con to compile.
I have tried all sorts of other options but get nowhere.

I have included my code below:
Code:

#include "C:\PICproj\Timecheck.h"
#include "lcd.c"
#include "pcf8563.c"

#use I2C(master, SCL=PIN_B1, SDA=PIN_B0, FORCE_HW)//USE SLOW=40000 IF HAVING PROBLEMS

#define LCD_ENABLE_PIN  PIN_C1                                 
#define LCD_RS_PIN      PIN_C2                                 
#define LCD_RW_PIN      PIN_C0                                 
#define LCD_DATA0       PIN_B4                                 
#define LCD_DATA1       PIN_B5                                 
#define LCD_DATA2       PIN_B6                                 
#define LCD_DATA3       PIN_B7

void main()
{

   int data;
   int ack;
   
//   RTC variables
   unsigned int8 My_year;  //years since 2000
   unsigned int8 My_month; //1..12
   unsigned int8 My_day;   //1..31
   unsigned int8 My_dow;   //0..6
   unsigned int8 My_hour;  //0..23
   unsigned int8 My_min;   //0..59
   unsigned int8 My_sec;   //0..59
//end of RTC clock varibles   
   
   int16 adc_value;
   float volts;
   unsigned int8 time;
   set_tris_b(0x03);
   set_tris_c(0x00);
   lcd_init();
   delay_ms(500);
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   setup_adc_ports(AN0);
   setup_adc(ADC_CLOCK_DIV_8);
   set_adc_channel(0);
   delay_us(20);

//*****initilse the RTC********
   i2c_start();               //talk to the RTC
   i2c_write(0xA2);           //write out the RTC ID
   i2c_write(0x00);           //write out the control byte
   i2c_write(0x00);           //write out two 00's
   i2c_write( 0x00);
   i2c_stop();                //write stop



while(1)
  {

//******Pulse LED heartbeat
      output_high(PIN_A1);    //turn heartbeat LED on
      delay_ms(100);          //wait 100 ms
      output_low(PIN_A1);     //turn heartbeat LED off

//******Read the Temp sensor
      i2c_start();             //talk to the temp sensor
      ack = i2c_write(0x90);   //write out the senor ID
      ack = i2c_write(0x00);   
      i2c_stop();
      /* send start again */
      i2c_start();
      ack = i2c_write(0x91);
      data = i2c_read(0);
      i2c_stop();
     
//******Read the time from the RTC
   My_sec = rtc_get_datetime(TIME_STRUCT.sec);

//******Write to LCD
   printf(lcd_putc, "\fTime: %u2 \n", My_sec);  //LCD first line
   printf(lcd_putc, "Temp: %u deg", data);      //LCD second line

   delay_ms(500);                               // system delay

  }


If anyone could point me in the right direction it would be most appreciated.

Regards.....
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jun 07, 2009 6:31 pm     Reply with quote

Look at this pcf8583 driver to get some ideas. It also uses a typedef'ed
date-time structure.
http://www.ccsinfo.com/forum/viewtopic.php?t=27988
Steove
Guest







PostPosted: Mon Jun 08, 2009 6:19 am     Reply with quote

Thanks PCM Programmer, once again your knowledge on CCS C has saved many hours.

I looked through the suggested code and realized I was way off track, started from scratch and now have the LCD display showing the time ticking over nicely with only one small issue......

The seconds are counting from 1 - 11, then jumps to 50 - 65 then jumps back to 60 - 75 and finally 70 - 85 but wait, the best of all is that the minutes update when the seconds jump from 11 to 50!!!

I have timed the cycle and it is exactly 60 sec still.

The minutes and hours appear to be updating OK.

Has anyone seen this before??

Once again thanks for the help!!
Charlie U



Joined: 09 Sep 2003
Posts: 183
Location: Somewhere under water in the Great Lakes

View user's profile Send private message

PostPosted: Mon Jun 08, 2009 7:24 am     Reply with quote

Take a quick look at the data that is returned. If I am not mistaken, the data is in BCD (Binary Coded Decimal) format. You need to handle the high and low nibbles separately.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 08, 2009 10:06 am     Reply with quote

Post your current test program.
Steove
Guest







PostPosted: Mon Jun 08, 2009 2:38 pm     Reply with quote

I agree Charlie, looks to me as its something to do with the way the BCD is handled, but I cant find it (its all handled within the pcf device driver.

Here's my current code, its much cleaner now thanks to PCM programmer....

Code:


#include "C:\PICproj\Temp.h"


#define LCD_ENABLE_PIN  PIN_C1                                 
#define LCD_RS_PIN      PIN_C2                                 
#define LCD_RW_PIN      PIN_C0                                 
#define LCD_DATA0       PIN_B4                                 
#define LCD_DATA1       PIN_B5                                 
#define LCD_DATA2       PIN_B6                                 
#define LCD_DATA3       PIN_B7
#include <lcd.c>
#include <pcf8563.c>

void main()
{
TIME_STRUCT ts;

   lcd_init();
   rtc_init();

   lcd_putc("\fReady...\n");
   
   while (TRUE) {
   rtc_get_datetime(&ts);
   delay_ms(500);
   printf(lcd_putc, "\fTime: %u:%u:%u \n", ts.hour, ts.min, ts.sec); //LCD first line
   printf(lcd_putc, "Temp: %u deg", 20);            //LCD second line

   }

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab

   // TODO: USER CODE!!

}

PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 08, 2009 2:56 pm     Reply with quote

Where is the line of code to initialize the RTC chip to a date and time ?
Steove
Guest







PostPosted: Mon Jun 08, 2009 10:56 pm     Reply with quote

In this test code I haven't gone to the trouble of setting the time yet, this was the next step after being able to read from the RTC accurately.

Will this have an effect on the registers counting normally???


What does Confucius say.
Even though the journey takes many months.... it must start with one single step...... :-)

Personally, I think I will be tripping up and falling more than the steps themselves.

Thanks for your help.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 08, 2009 11:21 pm     Reply with quote

You should initialize the registers to a known value and then determine
if it's working properly.
Steove
Guest







PostPosted: Tue Jun 09, 2009 7:17 am     Reply with quote

All Fixed!!!

It turned out that there was a bug in the PCF8563 driver, during reading the seconds it effectively performed a double "BCD to Decimal" conversion.

I don't know if this is fixed in the later versions of CCS C, this may need to be checked.

The incorrect code was as below:

Code:


void rtc_get_datetime(TIME_STRUCT *pTs)
{
   TIME_STRUCT ts;
   int8 val;

   i2c_start(STREAM_PCF8563);
   i2c_write(STREAM_PCF8563, PCF8563_I2C_ADDR);
   i2c_write(STREAM_PCF8563, PCF8563_SECONDS);
   i2c_start(STREAM_PCF8563, 2);
   i2c_write(STREAM_PCF8563, PCF8563_I2C_ADDR | 1);
   val = _rm_bcd(i2c_read(STREAM_PCF8563, 1));
   //ts.valid = !bit_test(val, 7);
   ts.sec = _rm_bcd(val & 0x7F);
   ts.min = _rm_bcd(i2c_read(STREAM_PCF8563, 1) & 0x7F);
   ts.hour = _rm_bcd(i2c_read(STREAM_PCF8563, 1) & 0x3F);
   ts.day = _rm_bcd(i2c_read(STREAM_PCF8563, 1) & 0x3F);
   ts.dow = _rm_bcd(i2c_read(STREAM_PCF8563, 1) & 0x07);
   ts.month = _rm_bcd(i2c_read(STREAM_PCF8563, 1) & 0x1F);
   ts.year = _rm_bcd(i2c_read(STREAM_PCF8563, 0));
   i2c_stop(STREAM_PCF8563);
   
   memcpy(pTs, &ts, sizeof(ts));
}



I replaced the offending code with:

ts.sec = _rm_bcd(i2c_read(STREAM_PCF8563, 1) & 0x7F);

Thanks for all the help.

Onwards to the rest of the project..........
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jun 09, 2009 12:23 pm     Reply with quote

The bug is still there in vs. 4.093. You should email CCS tech support
about it. Tell me if you want to do it. If not, I'll do it.
Steove
Guest







PostPosted: Tue Jun 09, 2009 3:36 pm     Reply with quote

Thanks PCM.

I send a bug report in now, I have an older version, so if you don't mind I'll mention that you have checked 4.093 and it's still there.

Cheers.
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