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 support@ccsinfo.com

Need help with pcf8583 program
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Curtisbeef



Joined: 13 Apr 2008
Posts: 12

View user's profile Send private message

PostPosted: Mon Apr 14, 2008 12:42 am     Reply with quote

Hi I seem to be having some trouble with this code i'm getting weird values on my LCD screen and there always the same values.

Here are the values i always get out
Year = 255
Hours = 1
Minutes = 14
Seconds = 15
Day = 110
Month = 0

I'm still very new to the C language so its probably that i'm just making a dumb mistake, so be gentile Smile


Here is my code:
Code:

#include <18F4550.h>
#device ADC=8
#use delay(clock=48000000)
#build(reset=0x800, interrupt=0x808)          // Necessary for Bootloader
#ORG 0x0000,0x07FF {}                     // Necessary for Bootloader
/////////////////Included Files/////////////////////////
#include <GLCD-modified-4550.C>
#include <math.h>
#include <ctype.h>
#include <PCF8583.c>
////////////////////////////////////////////////////////
////////////////Function Prototypes/////////////////////
   void displayInt8(int8 x, int8 y, int8 theValue);
////////////////////////////////////////////////////////
////////////////Global Variables////////////////////////
   char weekday[10];
   date_time_t dt;
////////////////////////////////////////////////////////
void main() {
   SETUP_ADC_PORTS(AN0 | VSS_VDD);
   SETUP_ADC(ADC_CLOCK_DIV_64);
   SETUP_TIMER_0(RTCC_INTERNAL|RTCC_DIV_1);  // Use the int. osc. for Timer 0
   SETUP_TIMER_1(T1_DISABLED);               // Disable Timer 1
   SETUP_TIMER_2(T2_DISABLED, 127, 1);       // Disable Timer 2
   SETUP_TIMER_3(T3_DISABLED);               // Disable Timer 3
   SETUP_CCP1(CCP_OFF);
   SETUP_CCP2(CCP_OFF);
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
   set_adc_channel(0);
   delay_ms(1);
   glcd_init(ON);                            // Must initialize the LCD
 
   PCF8583_init();
// Read the date and time from the PCF8583 and display
// it once per second.
while(1)
  {
   delay_ms(1000);   

   PCF8583_read_datetime(&dt); 

   strcpy(weekday, weekday_names[dt.weekday]);   
   
   displayInt8(4, 5, dt.year);    \\ Send Year to LCD
   displayInt8(4, 15, dt.hours);
   displayInt8(4, 25, dt.minutes);
   displayInt8(4, 35, dt.seconds);
   displayInt8(4, 45, dt.day);
   displayInt8(4, 55, dt.month);
  }
   while (TRUE) {
      delay_ms(100);
   }
}

void displayInt8(int8 x, int8 y, int8 theValue) {
   char toPrint[4];
   sprintf(toPrint, "%u", theValue);                           
   glcd_text35(x, y, toPrint, ON);
}


Thanks in advance Smile
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Apr 14, 2008 3:03 pm     Reply with quote

1. Make sure you have pull-up resistors on the SDA and SCL signals
for the PCF8583.

2. Your program does not set the PCF8583 clock registers to an
inital date and time. So your PCF8583 could just have some
random value. Look at the sample test program in the Code
Library. It shows how to write an initial data and time to the PCF8583.

3. You have this code in your program:
Quote:
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);

But you don't have a #int_rda function. That's a mistake.
I don't see any reason for RDA interrupts anyway. I suggest
that you delete those two lines.

4. You have all this code in your program for the A/D converter.
But you don't ever call the read_adc() function anywhere.
I suggest that you delete all the A/D code, since you are not using it.
All this extra code prevents you from concentrating on the actual
problem, which is how to get the PCF8583 working.

5. I don't see a #fuses statement. You should add one.
Make sure it's correct for 48 MHz operation.
Curtisbeef



Joined: 13 Apr 2008
Posts: 12

View user's profile Send private message

PostPosted: Mon Apr 14, 2008 6:33 pm     Reply with quote

1. I have the pull-up resistors im using this board http://www.futurlec.com/Mini_PCF8583.shtml

2. I set my time using the code in the driver example but i don't really have a way to check that its actually set because i cant get the values read correctly.

3/4. Removed the ADC code and other unessasary code

5. Added Fuses statement.

Im still getting the same values:
Year = 255
Hours = 1
Minutes = 14
Seconds = 15
Day = 110
Month = 0

here is my new modified code including some GLCD stuff


Code:
#include <18F4550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#build(reset=0x800, interrupt=0x808)          // Necessary for Bootloader
#ORG 0x0000,0x07FF {}                     // Necessary for Bootloader
/////////////////Included Files/////////////////////////
#include <GLCD-modified-4550.C>
#include <math.h>
#include <ctype.h>
#include <PCF8583.c>
////////////////////////////////////////////////////////
////////////////Function Prototypes/////////////////////
   void draw_frame();
   void displayInt8(int8 x, int8 y, int8 theValue);
////////////////////////////////////////////////////////
////////////////Global Variables////////////////////////
   char weekday[10];
   date_time_t dt;
////////////////////////////////////////////////////////

void main() {
   delay_ms(1);
   glcd_init(ON);                            // Must initialize the LCD
   draw_frame();
   PCF8583_init();
// Read the date and time from the PCF8583 and display
// it once per second.

while(1)
  {
   delay_ms(1000);   
   PCF8583_read_datetime(&dt);
   strcpy(weekday, weekday_names[dt.weekday]);   
   displayInt8(4, 5, dt.year);
   displayInt8(4, 15, dt.hours);
   displayInt8(4, 25, dt.minutes);
   displayInt8(4, 35, dt.seconds);
   displayInt8(4, 45, dt.day);
   displayInt8(4, 55, dt.month);
  }
   while (TRUE) {
      delay_ms(100);
   }
}

void displayInt8(int8 x, int8 y, int8 theValue) {
   char toPrint[4];
   sprintf(toPrint, "%u", theValue);                             // Limit shown digits to 3
   glcd_text35(x, y, toPrint, ON);
}

void draw_frame() {
   glcd_init(ON);
   glcd_rect(2,0,125,1,YES,ON); // Top Line
   glcd_rect(2,126,125,127,YES,ON); // Bottom Line
   glcd_rect(0,2,1,61,YES,ON); //Right Line
   glcd_rect(126,2,127,61,YES,ON); //Left Line
   glcd_pixel(1,1,ON);  //Top left
   glcd_pixel(1,62,ON); //Bottom Left
   glcd_pixel(126,1,ON); //Top Right
   glcd_pixel(126,62,ON); //Bottom Right
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Apr 14, 2008 7:22 pm     Reply with quote

My comment about the fuses wasn't applicable because you're using a
bootloader. But are you sure that the fuses were programmed with
the bootloader are set correctly for a 48 MHz PLL oscillator ?
Curtisbeef



Joined: 13 Apr 2008
Posts: 12

View user's profile Send private message

PostPosted: Mon Apr 14, 2008 8:01 pm     Reply with quote

Im using basically a unmodified copy of the standard Microchip USB bootloader made for the 4550

Displaying normal text on a LCD works fine so i know thats working...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Apr 14, 2008 10:42 pm     Reply with quote

I'll test it tomorrow in hardware.
Curtisbeef



Joined: 13 Apr 2008
Posts: 12

View user's profile Send private message

PostPosted: Mon Apr 14, 2008 11:01 pm     Reply with quote

Thanks alot Smile

I was wondering do you know of the simplest implementation of USB to virtual RS232? Because deluging stuff with a LCD is a pain in the [spam] Smile i tried using the examples included with CCS but could not get them to work correctly...

i acctually have the PCF8583 hooked up to C6 and C7 for sda and scl i dont know if that may be a problem because arnt these the rs232 transfer pins?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Apr 15, 2008 2:51 pm     Reply with quote

I was able to make it work with an 18F4550. I don't have a GLCD, so I
changed your code to send the output to the serial port and display it on
a terminal window on my PC. Here's the output, below. Notice that the
seconds are incrementing. That means it's working.
Quote:

0 0 1 46 1 1
0 0 1 47 1 1
0 0 1 48 1 1
0 0 1 49 1 1
0 0 1 50 1 1
0 0 1 51 1 1
0 0 1 52 1 1

My board uses the hardware UART pins (C6 and C7) for the serial port,
so I changed the PCF8583 pins in your program to pins D4 and D3, as
you can see below. You can't use the normal i2c pins of C4 and C3 on
this PIC because it uses them for the USB port. I used software i2c
on D4 and D3.

Did you make your own board, or did you buy it ? If you bought the
board, it's probably hardwired to use pins C6 and C7 for the serial port.
In that case, you should not try to use them for i2c. Use two other pins,
such as D4 and D3, as I did. (In fact, most people never use the
hardware UART pins for general purpose i/o. The hardware UART is very useful).

Make sure that you have connected pin A0 (on the PCF8583) to ground.
Also make sure that a 32.768 KHz watch crystal is connected to the
oscillator pins of the PCF8583, and that power and ground are connected
to the Vdd and Vss pins.

Code:

#include <18F4550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#define PCF8583_SDA  PIN_D4
#define PCF8583_SCL  PIN_D3

#include <PCF8583.c>

void displayInt8(int8 x, int8 y, int8 theValue);

char weekday[10];
date_time_t dt;

//=================================
void main()
{

PCF8583_init();

// Read the date and time from the PCF8583 and display
// it once per second.

while(1)
  {
   delay_ms(1000);   

   
   PCF8583_read_datetime(&dt);   

   strcpy(weekday, weekday_names[dt.weekday]);   

   displayInt8(4, 5, dt.year);
   displayInt8(4, 15, dt.hours);
   displayInt8(4, 25, dt.minutes);
   displayInt8(4, 35, dt.seconds);
   displayInt8(4, 45, dt.day);
   displayInt8(4, 55, dt.month);

   printf("\n\r");

  }


}

//========================================
void displayInt8(int8 x, int8 y, int8 theValue)
{
   char toPrint[4];
   sprintf(toPrint, "%u", theValue);   
   printf("%s ", toPrint);
}


This code uses the PCF8583 driver shown in this post in the Code Library:
http://www.ccsinfo.com/forum/viewtopic.php?t=27988
Curtisbeef



Joined: 13 Apr 2008
Posts: 12

View user's profile Send private message

PostPosted: Wed Apr 16, 2008 12:01 am     Reply with quote

hmm its still not working. still getting same values

I changed my the Pins over to E1 and E2 im using D for the LCD

I know my PCF8583 is wired correctly i have this board and everything is pre wired http://www.futurlec.com/Mini_PCF8583.shtml

my pic board is a custom breadboard one fyi

the only thing i noticed that was changed in the last code you just put up was the printf statements i dont have a serial connection so i cant really check it. But now what i have C6 and C7 free im going to go out tomorrow and get a DB9 connector so i can hook it up to the serial port seems like a very necessary part to have for debugging.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Apr 16, 2008 12:48 am     Reply with quote

The Futurlec board appears to have pull-up resistors on it that are
selected by a block of 3 switches on the side of the board, labeled
DIS/ENA. Do you have those switches turned on ? If you don't
have pull-ups, you can't communicate between the PIC and the PCF8583.

If you're going to get a DB-9, you should also get a MAX232A chip
and five 0.1 uF (100 nF) ceramic capacitors.

Or, get a MAX232 (non-A) and get five 1.0 uF capacitors. Make sure
everything is connected correctly, with the correct polarity on the caps.
Curtisbeef



Joined: 13 Apr 2008
Posts: 12

View user's profile Send private message

PostPosted: Wed Apr 16, 2008 1:16 am     Reply with quote

Yup i have them all enabled

also im going to be going a different route than the max232 i have a bunch of DS275 gonna use one of those
Curtisbeef



Joined: 13 Apr 2008
Posts: 12

View user's profile Send private message

PostPosted: Wed Apr 16, 2008 11:57 am     Reply with quote

just got my serial port hooked up and im getting the same old values throught it

Year = 255
Hours = 1
Minutes = 14
Seconds = 15
Day = 110
Month = 0
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Apr 16, 2008 12:12 pm     Reply with quote

I think you have a problem with your hardware, somewhere.

But, what's your compiler version ? It's given at the top of the .LST
file, which is in your project directory.
Curtisbeef



Joined: 13 Apr 2008
Posts: 12

View user's profile Send private message

PostPosted: Wed Apr 16, 2008 12:24 pm     Reply with quote

CCS PCH C Compiler, Version 4.057, 5565

i have tripple checked the hardware not sure whats going on


Last edited by Curtisbeef on Wed Apr 16, 2008 12:33 pm; edited 1 time in total
Curtisbeef



Joined: 13 Apr 2008
Posts: 12

View user's profile Send private message

PostPosted: Wed Apr 16, 2008 12:32 pm     Reply with quote

I just tried your exact code and all im getting is this
255 45 165 165 45 25 \0A\0D
255 45 165 165 45 25 \0A\0D
255 45 165 165 45 25 \0A\0D
255 45 165 165 45 25 \0A\0D
255 45 165 165 45 25 \0A\0D
255 45 165 165 45 25 \0A\0D
255 45 165 165 45 25 \0A\0D
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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