pic18f4550 with ds1307 code problem

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
hIMANSh
Posts: 158
Joined: 01 Jan 2011 17:48

pic18f4550 with ds1307 code problem

#1 Post by hIMANSh » 11 Jun 2014 16:37

Hi guys,

I am using pic18f4550 and DS1307 rtc to display date time on LCD, below is the sorce code that work fine on protues but when burn in actual hardware code get stuck what is the any guess about the reason??

Code: Select all

unsigned char sec, min1, hr, week_day, day, mn, year;
char *txt, tnum[4];

// LCD module connections
sbit LCD_RS at RC1_bit;
sbit LCD_EN at RC2_bit;
sbit LCD_D4 at RD0_bit;
sbit LCD_D5 at RD1_bit;
sbit LCD_D6 at RD2_bit;
sbit LCD_D7 at RD3_bit;
sbit LCD_RS_Direction at TRISC1_bit;
sbit LCD_EN_Direction at TRISC2_bit;
sbit LCD_D4_Direction at TRISD0_bit;
sbit LCD_D5_Direction at TRISD1_bit;
sbit LCD_D6_Direction at TRISD2_bit;
sbit LCD_D7_Direction at TRISD3_bit;
// End LCD module connections

void Zero_Fill(char *value) {    // fill text repesentation
  if (value[1] == 0) {           //      with leading zero
    value[1] = value[0];
    value[0] = 47;
    value[2] = 0;
  }
}//~

//--------------------- Reads time and date information from RTC (DS1307)
void Read_Time(char *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year) {
  I2C1_Start();
  I2C1_Wr(0xD0);
  I2C1_Wr(0);
  I2C1_Repeated_Start();
  I2C1_Wr(0xD1);
  *sec =I2C1_Rd(1);
  *min =I2C1_Rd(1);
  *hr =I2C1_Rd(1);
  *week_day =I2C1_Rd(1);
  *day =I2C1_Rd(1);
  *mn =I2C1_Rd(1);
  *year =I2C1_Rd(0);
  I2C1_Stop();
}//~

//-------------------- Formats date and time
void Transform_Time(char  *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year) {
  *sec  =  ((*sec & 0x70) >> 4)*10 + (*sec & 0x0F);
  *min  =  ((*min & 0xF0) >> 4)*10 + (*min & 0x0F);
  *hr   =  ((*hr & 0x30) >> 4)*10 + (*hr & 0x0F);
  *week_day =(*week_day & 0x07);
  *day  =  ((*day & 0xF0) >> 4)*10 + (*day & 0x0F);
  *mn   =  ((*mn & 0x10) >> 4)*10 + (*mn & 0x0F);
  *year =  ((*year & 0xF0)>>4)*10+(*year & 0x0F);
}//~

//-------------------- Output values to LCD
void Display_Time(char sec, char min, char hr, char week_day, char day, char mn, char year) {
   switch(week_day){
     case 1: txt="Sun"; break;
     case 2: txt="Mon"; break;
     case 3: txt="Tue"; break;
     case 4: txt="Wed"; break;
     case 5: txt="Thu"; break;
     case 6: txt="Fri"; break;
     case 7: txt="Sat"; break;
   }
   LCD_Out(1,1,txt);
   Lcd_Chr(1, 6, (day / 10)   + 48);    // Print tens digit of day variable
   Lcd_Chr(1, 7, (day % 10)   + 48);    // Print oness digit of day variable
   Lcd_Chr(1, 9, (mn / 10) + 48);
   Lcd_Chr(1,10, (mn % 10) + 48);
   Lcd_Chr(1,15,  year  + 48);          // Print year vaiable + 8 (start from year 2008)

   Lcd_Chr(2, 6, (hr / 10)   + 48);
   Lcd_Chr(2, 7, (hr % 10)   + 48);
   Lcd_Chr(2, 9, (min / 10) + 48);
   Lcd_Chr(2,10, (min % 10) + 48);
   Lcd_Chr(2,12, (sec / 10) + 48);
   Lcd_Chr(2,13, (sec % 10) + 48);

}//~

//------------------ Performs project-wide init
void Init_Main() {
  ADCON1=0x0F;
  TRISB=0x00;
  PORTB=0x00;
  
  I2C1_Init(100000);         // initialize I2C communication
  Lcd_Init();                // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);       // Clear LCD display
  Lcd_Cmd(_LCD_CURSOR_OFF);  // Turn cursor off

  I2C1_Init(100000);                        // initialize I2C
  LCD_Chr(1,8,'.');
  LCD_Chr(1,11,'.');
  txt = "Time:";
  LCD_Out(2,1,txt);
  LCD_Chr(2,8,':');
  LCD_Chr(2,11,':');
  txt = "201";
  LCD_Out(1,12,txt);
  LCD_Cmd(_LCD_CURSOR_OFF);
}//~

void DS107_Write(){
  I2C1_Start();              // issue I2C start signal
  I2C1_Wr(0xD0);             // send byte via I2C  (device address + W)
  I2C1_Wr(0x00);             // second
  I2C1_Wr(0x00);             // minute
  I2C1_Wr(0x00);             // hour
  I2C1_Wr(0x00);             // Weekday
  I2C1_Wr(0x00);             // day
  I2C1_Wr(0x00);             // month
  I2C1_Wr(0x00);             // year
  I2C1_Wr(0x00);
  I2C1_Stop();               // issue I2C stop signal
  Delay_100ms();
}

//----------------- Main procedure
void main() {
  Init_Main();                                               // perform initialization
  DS107_Write();

  while (1) {
    Read_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year);      // read time from RTC(DS1307)
    Transform_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year); // format date and time
    Display_Time(sec, min1, hr, week_day, day, mn, year);    // prepare and display on LCD
    Delay_ms(1000);                                          // wait 1s
  }
}//

jumper
Posts: 466
Joined: 07 Mar 2007 14:36

Re: pic18f4550 with ds1307 code problem

#2 Post by jumper » 11 Jun 2014 16:50

configuration bits... or configurations bits

that is the most common problem i think

hIMANSh
Posts: 158
Joined: 01 Jan 2011 17:48

Re: pic18f4550 with ds1307 code problem

#3 Post by hIMANSh » 11 Jun 2014 17:13

I think I already make portb as digital IO using below code. else what to configure?

//------------------ Performs project-wide init
void Init_Main() {
ADCON1=0x0F;
TRISB=0x00;
PORTB=0x00;

hIMANSh
Posts: 158
Joined: 01 Jan 2011 17:48

Re: pic18f4550 with ds1307 code problem

#4 Post by hIMANSh » 11 Jun 2014 17:22

I think I already make portb as digital IO using below code. else what to configure?

//------------------ Performs project-wide init
void Init_Main() {
ADCON1=0x0F;
TRISB=0x00;
PORTB=0x00;

p.erasmus
Posts: 3391
Joined: 05 Mar 2009 10:28

Re: pic18f4550 with ds1307 code problem

#5 Post by p.erasmus » 11 Jun 2014 17:37

hIMANSh wrote:I think I already make portb as digital IO using below code. else what to configure?
No help for ARM users ask you buddies in the ARM forum to help you
P.Erasmus
Saratov,Russia
--------------------------------------------------------------

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re: pic18f4550 with ds1307 code problem

#6 Post by Dany » 11 Jun 2014 17:51

hIMANSh wrote:I think I already make portb as digital IO using below code. else what to configure?

//------------------ Performs project-wide init
void Init_Main() {
ADCON1=0x0F;
TRISB=0x00;
PORTB=0x00;
See, in the IDE, the "Edit Project" button (the one with the pencil). That will allow you to set the PIC's configuration (which is much more then defining analog or digital functioning of a port). Is this your first PIC project?
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

hIMANSh
Posts: 158
Joined: 01 Jan 2011 17:48

Re: pic18f4550 with ds1307 code problem

#7 Post by hIMANSh » 11 Jun 2014 18:20

Hi Dany,

I am not new bee it quite while using mikroc, I attached the zip file of project
Attachments
Actual hardware sounds code get stuck!!
Actual hardware sounds code get stuck!!
IMG_0454.JPG (29.68 KiB) Viewed 3695 times
Simulation work ok
Simulation work ok
Capture.PNG (13.98 KiB) Viewed 3695 times
DS1307.zip
(99.79 KiB) Downloaded 146 times

hexreader
Posts: 1785
Joined: 27 Jun 2010 12:07
Location: England

Re: pic18f4550 with ds1307 code problem

#8 Post by hexreader » 11 Jun 2014 18:53

The project that you posted in the zip file works fine on my hardware.

Guess your hardware is faulty, or the code does not match the hardware that you are using.

Shouldn't R2 and R3 be a much lower value than 10K Ohms Maybe 1K Ohms ?

Doesn't PIC18F4550 require a capacitor on Vusb?

Have you connected both sets of power pins and provided a decoupling capacitor across each pair? - your schematic does not show any power connections and shows no decoupling capacitors.

But I may be wrong....
Start every day with a smile...... (get it over with) :)

hIMANSh
Posts: 158
Joined: 01 Jan 2011 17:48

Re: pic18f4550 with ds1307 code problem

#9 Post by hIMANSh » 13 Jun 2014 12:55

Thanks hexreader for confirming working of software. I will try this code on other hardware and will update soon

ducu
Posts: 30
Joined: 04 Jul 2010 02:32
Location: Galati,RO

Re: pic18f4550 with ds1307 code problem

#10 Post by ducu » 15 Jun 2014 23:06

If it helps, look how I designed this watch DS1307 with Set Functions

hIMANSh
Posts: 158
Joined: 01 Jan 2011 17:48

Re: pic18f4550 with ds1307 code problem

#11 Post by hIMANSh » 16 Jun 2014 06:10

HI,

Guys thanks for your response. I switch to new hardware and every ting work as required

esarearthur
Posts: 62
Joined: 16 Apr 2011 14:13

Re: pic18f4550 with ds1307 code problem

#12 Post by esarearthur » 21 Jun 2014 15:39

Please pull up the I2C lines with 4.7K resistor, they are a major cause of this.
Never be afraid to try.

PICKit3 + MikroC Pro for PIC
DAQ with Ethernet -- Current project

Code: Select all

TRISA = 0x00

Post Reply

Return to “mikroC PRO for PIC General”