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

Programming Help

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



Joined: 25 Jan 2007
Posts: 3

View user's profile Send private message Visit poster's website

Programming Help
PostPosted: Thu Jan 25, 2007 2:22 pm     Reply with quote

I have some code that is not working correctly. I think it is because I am not using the pointers and or references correctly, in the main program.

The code is to interface a PIC16F88 with a DS1337. The problem I am running into is when running the code, in circuit, I am getting random numbers for the date and time, like it is trying to print out the addresses of the values, and not the actual values.

Any help would be appreciated.

Here is the code in question:

//-----------CODE STARTS BELOW THIS LINE------------

#ifndef DS1337_SDA
#define DS1337_SDA PIN_B5
#define DS1337_SCL PIN_B4
#endif

#use i2c(master, sda=DS1337_SDA, scl=DS1337_SCL)

// i2c addresses
#define DS1337_I2C_WRITE_ADDR 0xD0
#define DS1337_I2C_READ_ADDR 0xD1

// DS1337 register addresses
#define DS1337_SECONDS_REG 0x00
#define DS1337_MINUTES_REG 0x01
#define DS1337_HOURS_REG 0x02
#define DS1337_DAY_OF_WEEK_REG 0x03
#define DS1337_DATE_REG 0x04
#define DS1337_MONTH_REG 0x05
#define DS1337_YEAR_REG 0x06
//DS1337 Unique Registers
#define DS1337_ALM1_SECONDS_REG 0x07
#define DS1337_ALM1_MINUTES_REG 0x08
#define DS1337_ALM1_HOURS_REG 0x09
#define DS1337_ALM1_DAYDATE_REG 0x0A
#define DS1337_ALM2_MINUTES_REG 0x0B
#define DS1337_ALM2_HOURS_REG 0x0C
#define DS1337_ALM2_DAYDATE_REG 0x0D
#define DS1337_CONTROL_REG 0x0E
#define DS1337_STATUS_REG 0x0F

//Enable Oscillator, Disable SQWV, Disable Ints
#define DS1337_CTRL_REG_INIT_VAL 0x04

//Clear OSF (Oscillator Stop Flag), A2F (Alarm 2 Flag), and A1F (Alarm 1 Flag)
#define DS1337_CLEAR_STATUS_VAL 0x00


char const weekday_names[7][4] = { {"Sun"},
{"Mon"},
{"Tue"},
{"Wed"},
{"Thu"},
{"Fri"},
{"Sat"} };

// This structure defines the user's date and time data.
// The values are stored as unsigned integers. The user
// should declare a structure of this type in the application
// program. Then the address of the structure should be
// passed to the DS1337 read/write functions in this
// driver, whenever you want to talk to the chip.
typedef struct {
int8 seconds; // 0 to 59
int8 minutes; // 0 to 59
int8 hours; // 0 to 23 (24-hour time)
int8 day; // 0 = Sunday, 1 = Monday, etc.
int8 date; // 1 to 31
int8 month; // 1 to 12
int8 year; // 00 to 99
} date_time_t;



//----------------------------------------------
// Read the Date and Time from the hardware registers
// in the DS1337.
void DS1337_read_datetime(date_time_t *dt) {

int8 bcd_sec;
int8 bcd_min;
int8 bcd_hrs;
int8 bcd_day;
int8 bcd_date;
int8 bcd_mon;
int8 bcd_year;

// Disable interrupts so the i2c process is not disrupted.
disable_interrupts(GLOBAL);

// Read the date/time registers from the DS1337.
i2c_start();
i2c_write(DS1337_I2C_WRITE_ADDR);
i2c_write(DS1337_SECONDS_REG); // Start at seconds register
i2c_start();
i2c_write(DS1337_I2C_READ_ADDR);

bcd_sec = i2c_read();
bcd_min = i2c_read();
bcd_hrs = i2c_read();
bcd_day = i2c_read();
bcd_date = i2c_read();
bcd_mon = i2c_read();
bcd_year = i2c_read(0);
i2c_stop();

enable_interrupts(GLOBAL);

// Convert the date/time values from BCD to
// unsigned 8-bit integers. Unpack bits in
// DS1337 registers where required.
dt->seconds = bcd2bin(bcd_sec & 0x7F);
dt->minutes = bcd2bin(bcd_min & 0x7F);
dt->hours = bcd2bin(bcd_hrs & 0x3F);
dt->day = bcd2bin(bcd_day & 0x07);
dt->date = bcd2bin(bcd_date & 0x3F);
dt->month = bcd2bin(bcd_mon & 0x1F);
dt->year = bcd2bin(bcd_year);
}

void main() {
char *str;
char chr;
char *ptr;
int8 write;
int8 *date_time;

int result;
date_time_t *dt;

DS1337_init();

do {

do {
printf("\r\n<S>et time, <G>et time: ");
chr = getc();
chr = toupper(chr);
putc(chr);
} while ((chr != 'S') && (chr != 'G'));

if (chr == 'G') {
printf("\r\nCurrent Date and Time: ");

DS1337_read_datetime(&dt);
printf("%02u-%02u-%02u %02u:%02u:%02u Weekday %u\r\n",
dt->year,dt->month,dt->date,dt->hours,dt->minutes,
dt->seconds,dt->day);
}
} while (TRUE);
}

And this its the output I am getting:

<S>et time, <G>et time: G
Current Date and Time: 00-00-01 00:100:00 Weekday 0

<S>et time, <G>et time: G
Current Date and Time: `0-00-00 00:00:100 Weekday 1

<S>et time, <G>et time: G
Current Date and Time: n1-254-00 00:01:00 Weekday 0
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jan 25, 2007 4:14 pm     Reply with quote

This is jecottrell's driver. He hasn't been on the forum for about a month.

I have a few comments:

Quote:

#define DS1337_SDA PIN_B5
#define DS1337_SCL PIN_B4

These aren't the best pins to pick for your i2c bus.

The hardware i2c pins on the 16F88 are:
SDA -- Pin B1
SCL -- Pin B4

I wouldn't use pin B5 for SDA, because B5 is the hardware UART's Tx
pin. The hardware UART is a very nice thing to use.

I'm sort of hoping that Mr. Cottrell will show up to support his driver.
If he doesn't in a day or so, then I'll look at it.
eveready1010



Joined: 25 Jan 2007
Posts: 3

View user's profile Send private message Visit poster's website

PostPosted: Thu Jan 25, 2007 4:55 pm     Reply with quote

Thanks for the reply.

Actually I have code that works using the DS1337 with the PIC16F88 using pins B4 and B5 due to pins B0 thru B3 being used for temp sensor inputs, but in the final design I will see if I can reassign the pins so that the I2C uses the correct pins.

I found the functions to read and write date and time to the 1337 online and thought that function calls would be a better way to code this project (rather than just putting all of the code in the main function), and I understand just about the whole block of code, but I think I am having trouble with the pointers (This is the only part I had a hard time with in college Smile , and I think my lack of understanding not of the concept, but of how and when to use pointers, and the different ways of coding pointers) is the problem.

Anyway, I tried changing the pins to their correct assignments, and this is what I get:

//Begin Output************************************

<S>et time, <G>et time: G
Current Date and Time: 28-38-251 254:219:71 Weekday 254

<S>et time, <G>et time: G
Current Date and Time: 40-64-40 251:254:254 Weekday 40

<S>et time, <G>et time: G
Current Date and Time: 01-02-40 01:42:251 Weekday 2

<S>et time, <G>et time: G
Current Date and Time: 01-01-02 01:01:43 Weekday 2

<S>et time, <G>et time: G
Current Date and Time: 01-01-01 01:01:01 Weekday 1

<S>et time, <G>et time: G
Current Date and Time: 00-01-01 02:01:01 Weekday 1

<S>et time, <G>et time: G
Current Date and Time: 34-00-01 01:01:01 Weekday 1

<S>et time, <G>et time: G
Current Date and Time: 01-34-00 01:01:01 Weekday 1

<S>et time, <G>et time: G
Current Date and Time: 00-01-34 01:01:01 Weekday 0

<S>et time, <G>et time: G
Current Date and Time: 00-00-01 00:01:01 Weekday 34

<S>et time, <G>et time: G
Current Date and Time: 00-00-01 00:01:01 Weekday 34

<S>et time, <G>et time: G
Current Date and Time: 00-00-00 34:00:01 Weekday 1

//End output***********************************************

Also, I forgot to copy and paste the code from the main file. Here it is and it should be placed at the top of the code I initially sent:

//Begin Code*********************************************

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

#include <input.c>
#include <stdlib.h>

//Previous Code Inserted Here******************************
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Thu Jan 25, 2007 9:24 pm     Reply with quote

A pointer is a variable which holds the address of another variable. Your problem is that you declared dt as a pointer. You shouldn't do that. It should just be
Code:

date_time_t dt;

You still need to pass the address to the function using &dt. I see a lot of other pointers in you main which probably are being used incorrectly as well. I don't see any code using them though. Remember to use the '.' operator on a variable and not the '->' operator.
eveready1010



Joined: 25 Jan 2007
Posts: 3

View user's profile Send private message Visit poster's website

PostPosted: Fri Jan 26, 2007 5:36 pm     Reply with quote

That did it! Thanks for the help. (As I said earlier, I had a hard time with - in this case when to use - pointers.)
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