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

DS1302 driver do not work with dspic30f3011

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



Joined: 25 Jan 2012
Posts: 13

View user's profile Send private message

DS1302 driver do not work with dspic30f3011
PostPosted: Sun Aug 04, 2013 9:39 pm     Reply with quote

this is my code



main.c----------------------------------------------------------------------------
#include <main.h>
#include <string.h>
#include <stdlib.h>

//#include <input.c>


#include "board.h"
#include "DS1302.C"
#include <flex_lcd.c>
void chime();
#include "clock.c"

#PRIORITY EXT1,RDA2,RDA
#use rs232(UART2,baud=4800,parity=N,bits=8,stream=GPS,errors)
#use rs232(UART1A,baud=9600,parity=N,bits=8,stream=GSM,errors)

char send=0;

//gps variable definitions
char GPSData[160];
unsigned int8 GPSDataPtr=0;
char c;
int8 GPSDataReady = FALSE;


typedef struct _DateTimeInfo
{
int8 Day;
int8 Month;
int8 Year;
int8 Hour;
int8 Minute;
int8 Second;
} DateTimeInfo;


typedef struct _GPRMCInfo
{
char Valid;
DateTimeInfo DT;
float Latitude;
char N_S;
float Longitude;
char E_W;
float Speed;
} GPRMCInfo;

//function definitions
void send_message();



GPRMCInfo RMCInfo;
#include "gps_decode.c"
#include "interrupts.c"

char state=0;

void main()
{
setup_spi( FALSE );
setup_timer1(TMR_INTERNAL|TMR_DIV_BY_256);

EXT_INT_EDGE(H_TO_L);
enable_interrupts(INT_EXT1);
interrupt_active(INT_EXT1);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_RDA);
enable_interrupts(INT_RDA2);
enable_interrupts(INTR_GLOBAL);

lcd_init();
delay_ms(100);

// TODO: USER CODE!!
rtc_init();
delay_ms(100);
while(1){

if (GPSDataReady){GPRMC_decode(GPSData);GPSDataReady = FALSE;}
if(RMCInfo.Valid=='V'){
output_high(LED_R);//red led lights if gps data is not valid
}
else output_low(LED_R);//red led turn off if gps data is valid
if(send){send_message();send=0;}

if(state==0){
display_date_time();
}
if(state==1){
set_time();
}

if(B0_PRESSED){
delay_ms(25);
state++;
if(state>1)state=0;
chime();
}

delay_ms(50);
}//while ends

}//main ends

void send_message(){
output_high(LED_G);
fprintf(GSM,"AT+CMGF=1\r");
delay_ms(500);
fprintf(GSM,"AT+CMGS=0718184226\r");
delay_ms(500);
fprintf(GSM,"Warning! Accident Location\r");
fprintf(GSM,"Latitude: %f %c\r", RMCInfo.Latitude, RMCInfo.N_S);
fprintf(GSM,"Longitude: %f %c\r", RMCInfo.Longitude, RMCInfo.E_W);
delay_ms(500);
char d=26;
fprintf(GSM,"%c",d);
delay_ms(2000);
output_low(LED_G);
}

void chime(){
output_high(BEEP);
delay_ms(25);
output_low(BEEP);
}
main.h-------------------------------------------------------------------------
#include <30F3011.h>
#device ICD=TRUE
#device NESTED_INTERRUPTS=TRUE


/*#FUSES NOWDT //No Watch Dog Timer
#FUSES XT //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES CKSFSM //Clock Switching is enabled, fail Safe clock monitor is enabled
#FUSES NOPUT //No Power Up Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES DEBUG //Debug mode for use with ICD
#FUSES MCLR //Master Clear pin enabled
*/
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
//#FUSES PR //Primary Oscillator
#FUSES NOCKSFSM //Clock Switching is disabled, fail Safe clock monitor is disabled
#FUSES PUT64 //Power On Reset Timer value 64ms
#FUSES NOBROWNOUT //No brownout reset
#FUSES MCLR //Master Clear pin enabled
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOWRT //Program memory not write protected
#FUSES DEBUG //No Debug mode for ICD
//#FUSES NOCOE //Device will reset into operational mode
#FUSES ICSP1 //ICD uses PGC1/PGD1 pins
#FUSES RESERVED //Used to set the reserved FUSE bits
#FUSES NOCKSFSM //Clock Switching is disabled, fail Safe clock monitor is disabled

#use delay(clock=8000000)
#use fixed_IO(E_outputs = PIN_E5,PIN_E8)

clock.h--------------------------------------------------------------------------
int8 hr,min,sec;
volatile int8 set_hr,set_min;
int8 day,mth,year,dow,hour;
volatile int8 set_day,set_mth,set_year,set_dow;

void set_time();
void display_date_time();

clock.c-------------------------------------------------------------------
#include "clock.h"
void display_date_time(){
rtc_get_time(hr,min,sec);
rtc_get_date(day,mth,year,dow);
printf(lcd_putc,"\fTIME %02d:%02d:%02d"hr,min,sec);
printf(lcd_putc,"\nDATE %02d:%02d:%02d"day,mth,year);
}

void set_time(){
rtc_get_time(hr,min,sec);
printf(lcd_putc,"\fSET TIME %02d:%02d"hr,min);
delay_ms(100);
set_hr=hr;
set_min = min;
int8 k=1;
if(B1_PRESSED){
delay_ms(20);
chime();
set_hr=set_hr+k;
if(set_hr>23){
set_hr=0;
}
sec=0;
hour=set_hr;
rtc_set_datetime(day,mth,year,dow,hour,min);
}

if(B2_PRESSED){
delay_ms(20);
chime();
set_min++;
if(set_min==60)set_min=0;
min=set_min;
sec=0;
rtc_set_datetime(day,mth,year,dow,hour,min);
}

if(B3_PRESSED){
delay_ms(20);
chime();
set_min = 0;
min = set_min;
set_hr = 0;
hour = set_hr;
rtc_set_datetime(day,mth,year,dow,hour,min);
}


}

/*void set_date_year(){
rtc_get_date(day,mth,year,dow);
printf(lcd_putc,"\fSET YEAR %02d" year);
delay_ms(100);

set_year=year;

if(B1_PRESSED){
set_year++;
if(set_year>99)set_year=0;
year=set_year;
rtc_get_time(hr,min,sec) ;
rtc_set_datetime(day,mth,year,dow,hour,min);
}

if(B2_PRESSED){
set_year--;
if(set_year<0)set_year=99;
year=set_year;
rtc_set_datetime(day,mth,year,dow,hour,min);
}

if(input_state(PIN_A5)==0){
set_year = 0;
year=set_year;
rtc_get_time(hr,min,sec) ;
rtc_set_datetime(day,mth,year,dow,hour,min);
}
}

void set_date_mth(){
rtc_get_date(day,mth,year,dow);
printf(lcd_putc,"\fSET MONTH %02d" mth);
delay_ms(100);


set_mth=mth;

if(input_state(PIN_A3)==0){
set_mth++;
if(set_mth>12)set_mth=1;
mth=set_mth;
rtc_get_time(hr,min,sec) ;
rtc_set_datetime(day,mth,year,dow,hour,min);
}

if(input_state(PIN_A4)==0){
set_mth--;
if(set_mth<1)set_mth=12;
mth=set_mth;
rtc_get_time(hr,min,sec) ;
rtc_set_datetime(day,mth,year,dow,hour,min);
}

if(input_state(PIN_A5)==0){

backlit=500;
output_high(PIN_D1);

set_mth = 1;
mth=set_mth;
rtc_get_time(hr,min,sec) ;
hour = hr;
rtc_set_datetime(day,mth,year,dow,hour,min);
backlit =0;
}


loop++;
if(loop==2000){state=0;system_state=0;setup=0;}

if(input_state(PIN_E0)==0){

backlit=500;
output_high(PIN_D1);

delay_ms(20);
setup=setup-10;
}


}*/


-------------------------the problem--------------------------------
the lcd displays 26 after 9
format hh:mm:ss
01:06:09
01:06:26 <---error starts here
01:06:27
01:06:32---so on
this is the circuit
Ttelmah



Joined: 11 Mar 2010
Posts: 19447

View user's profile Send private message

PostPosted: Mon Aug 05, 2013 1:45 am     Reply with quote

Seriously:
1) Learn the basics of debugging. Just a ten line (or less) program calling the clock functions only.
2) Compiler version?.

With the first, since operation will now be dependant on just the clock, one of us may be able to test it as well, and if the problem disappears, then you have a clue as to what is happening.
Second is vital.
temtronic



Joined: 01 Jul 2010
Posts: 9202
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Aug 05, 2013 5:28 am     Reply with quote

Mr. T is right, again!!
Get rid of 99% of your code, I scanned it 10 times and couldn't FIND any printf(..) that displays the time only as you posted.So either I missed it,its early here or that code is not in the program.
Regulars here know the 1302 driver is OK.providing the hardware is correct,access tot he RTC should be OK.That leaves the 'missing' or 'hard to find' printf of the RTC data to LCD.


also
it might be just how it's displayed here, but if(){..) do not have indentations, which makes it harder to read/decode
and it's helpful to add comment line above every function defintion to say what it does and separate it from the rest of the code.
oh.. one last thing, it's laso helpful to give I/O pins names for what they do

hth
jay
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Mon Aug 05, 2013 7:26 am     Reply with quote

Hi,

This is going to be a 'data type' issue. The DS1302 stores time/date info in BCD format. So when the DS1302 registers are written, the data must be in
BCD format, and when the DS1302 registers are read, you must keep in mind that the data is in BCD format. You should have BCDtoBIN() and
BINtoBCD() routines to go back and forth between the two formats. My hunch is that you aren't setting the time correctly when you 'set' the DS1302 to a
predefined time. But, based on the code you've posted, it's hard to tell exactly. The real clue to the problem is that your data is fine for values from
'0' to '9', and then goes off the rails for larger values......

As has already been said, strip your code to the bare minimum so that you are only testing the problem of interest!

John
Ttelmah



Joined: 11 Mar 2010
Posts: 19447

View user's profile Send private message

PostPosted: Mon Aug 05, 2013 8:07 am     Reply with quote

I'd particularly be 'suspicious', of rm_bdc. This is the function that converts the bcd return to internal binary. If you think, the 'fault' value, is the point where this first receives a value in the upper nibble. It works correctly in 4.141, and in 5.010, but there have been mathematical oddities with older PCD compilers, and I'd suspect the version is something old like 4.100ish, where several problems of this sort existed, or even older.....

Best Wishes
lasal



Joined: 25 Jan 2012
Posts: 13

View user's profile Send private message

problem was solved the ds1302 driver was modified
PostPosted: Sat Aug 10, 2013 10:17 pm     Reply with quote

Code:

unsigned int8 get_bcd(unsigned int8 data)
{
   unsigned int8 nibh;
   unsigned int8 nibl;

   nibh=data/10;
   nibl=data-(nibh*10);

   return((nibh<<4)|nibl);
}


Code:

unsigned int8 rm_bcd(unsigned int8 data)
{                                                         
   unsigned int8 i;               

   i=data;                     
   data=(i>>4)*10;
   data=data+(i & 0x0F);       

   return data;
}
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