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

void ds1307_display()
Goto page Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
temtronic



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

View user's profile Send private message

PostPosted: Wed Nov 07, 2018 12:49 pm     Reply with quote

OK... NOW I see a BIG problem !
The function rtc_write(...) is SPECIFIC for the INTERNAL Real Time Clock Counter NOT to be used for an EXTERNAL RealTime Clock 'chip' like the DS1307. The DS1307 requires a separate 'driver' to be #included at the start of your program.
You don't say which PIC you're using, but the DS1307.c 'driver' will work for all PICs.
Only a few PICs actually have a builtin( internal) RTCC peripheral.

Jay
PROMOD



Joined: 01 Feb 2018
Posts: 42

View user's profile Send private message

PostPosted: Thu Nov 08, 2018 11:30 am     Reply with quote

In the following program there is no "DS1307.c" included in the beginning then how are ds1307_display(), ds1307_write() functions working?

Also there is no "ds1307_read()" function used in the following program then how does the time & date data, stored in ds1307 resister address from 00h to 07h , read by microprocessor and display in the LCD?

Code:
#define LCD_RS_PIN PIN_D0
#define LCD_RW_PIN PIN_D1
#define LCD_ENABLE_PIN PIN_D2
#define LCD_DATA4 PIN_D3
#define LCD_DATA5 PIN_D4
#define LCD_DATA6 PIN_D5
#define LCD_DATA7 PIN_D6
//End LCD module connections

#include <18F4431.h>
#fuses NOMCLR, INTRC_IO, NOWDT,NOPROTECT,NOLVP
#use delay(clock = 8000000)
#include <lcd.c>
#use fast_io(B)
#use I2C(master, I2C1, FAST = 100000)

char time[] = "TIME:  :  :  ";
char calendar[] = "DATE:  /  /20  ";
unsigned int8 second, second10, minute, minute10,
              hour, hour10, date, date10, month, month10,
              year, year10, day;
void ds1307_display(){
  second10  =  (second & 0x70) >> 4;
  second = second & 0x0F;
  minute10  =  (minute & 0x70) >> 4;
  minute = minute & 0x0F;
  hour10  =  (hour & 0x30) >> 4;
  hour = hour & 0x0F;
  date10  =  (date & 0x30) >> 4;
  date = date & 0x0F;
  month10  =  (month & 0x10) >> 4;
  month = month & 0x0F;
  year10  =  (year & 0xF0) >> 4;
  year = year & 0x0F;
  time[12]  = second  + 48;
  time[11]  = second10  + 48;
  time[9]  = minute  + 48;
  time[8]  = minute10  + 48;
  time[6]  = hour  + 48;
  time[5]  = hour10  + 48;
  calendar[14]  = year  + 48;
  calendar[13]  = year10  + 48;
  calendar[9]  = month + 48;
  calendar[8]  = month10 + 48;
  calendar[6]  = date + 48;
  calendar[5]  = date10 + 48;
  lcd_gotoxy(1, 1);                     // Go to column 1 row 1
  printf(lcd_putc, time);               // Display time
  lcd_gotoxy(1, 2);                     // Go to column 1 row 2
  printf(lcd_putc, calendar);           // Display calendar
}
void ds1307_write(unsigned int8 address, data_){
  i2c_start();                          // Start I2C
  i2c_write(0xD0);                      // DS1307 address
  i2c_write(address);                   // Send register address
  i2c_write(data_);                     // Write data to the selected register
  i2c_stop();                           // Stop I2C
}
void main(){
  setup_oscillator(OSC_8MHZ);                // Set internal oscillator to 8MHz
  setup_adc_ports(NO_ANALOGS);                // Configure AN pins as digital
  port_b_pullups(TRUE);
  output_b(0);
  set_tris_b(0x0F);
  lcd_init();                           // Initialize LCD module
  lcd_putc('\f');                       // LCD clear
  while(TRUE){
   if(input(PIN_B2) == 0){
    // Convert BCD to decimal
    minute = minute + minute10 * 10;
    hour = hour + hour10 * 10;
    date = date + date10 * 10;
    month = month + month10 * 10;
    year = year + year10 * 10;
    // End conversion
    lcd_putc('\f');                     // LCD clear
    lcd_gotoxy(5, 1);                   // Go to column 5 row 1
    lcd_putc("Minutes:");
    delay_ms(200);
    while(TRUE)
    {
     if(input(PIN_B3) == 0)
      minute++;
     if(minute > 59)
      minute = 0;
     lcd_gotoxy(8, 2);                  // Go to column 8 row 2
     printf(lcd_putc,"%02u", minute);
     if(input(PIN_B2) == 0)
      break;
     delay_ms(200);
    }
    lcd_putc('\f');                     // LCD clear
    lcd_gotoxy(6, 1);                   // Go to column 6 row 1
    lcd_putc("Hour:");
    delay_ms(200);
    while(TRUE){
     if(input(PIN_B3) == 0)
      hour++;
     if(hour > 23)
      hour = 0;
     lcd_gotoxy(8, 2);                  // Go to column 8 row 2
     printf(lcd_putc,"%02u", hour);
     if(input(PIN_B2) == 0)
      break;
     delay_ms(200);
    }
    lcd_putc('\f');                     // LCD clear
    lcd_gotoxy(6, 1);                   // Go to column 6 row 1
    lcd_putc("Date:");
    delay_ms(200);
    while(TRUE){
     if(input(PIN_B3) == 0)
      date++;
     if(date > 31)
      date = 1;
     lcd_gotoxy(8, 2);                  // Go to column 8 row 2
     printf(lcd_putc,"%02u", date);
     if(input(PIN_B2) == 0)
      break;
     delay_ms(200);
    }
    lcd_putc('\f');                     // LCD clear
    lcd_gotoxy(6, 1);                   // Go to column 6 row 1
    lcd_putc("Month:");
    delay_ms(200);
    while(TRUE){
     if(input(PIN_B3) == 0)
      month++;
     if(month > 12)
      month = 1;
     lcd_gotoxy(8, 2);                  // Go to column 8 row 2
     printf(lcd_putc,"%02u", month);
     if(input(PIN_B2) == 0)
      break;
     delay_ms(200);
    }
    lcd_putc('\f');                     // LCD clear
    lcd_gotoxy(6, 1);                   // Go to column 6 row 1
    lcd_putc("Year:");
    lcd_gotoxy(7, 2);                   // Go to column 7 row 1
    lcd_putc("20");
    delay_ms(200);
    while(TRUE){
     if(input(PIN_B3) == 0)
      year++;
     if(year > 99)
      year = 0;
     lcd_gotoxy(9, 2);                  // Go to column 9 row 2
     printf(lcd_putc,"%02u", year);
     if(input(PIN_B2) == 0){
      // Convert decimal to BCD
      minute = ((minute/10) << 4) + (minute % 10);
      hour = ((hour/10) << 4) + (hour % 10);
      date = ((date/10) << 4) + (date % 10);
      month = ((month/10) << 4) + (month % 10);
      year = ((year/10) << 4) + (year % 10);
      // End conversion
      ds1307_write(1, minute);
      ds1307_write(2, hour);
      ds1307_write(4, date);
      ds1307_write(5, month);
      ds1307_write(6, year);
      ds1307_write(0, 0);               //Reset seconds and start oscillator
      delay_ms(200);
      break;
     }
     delay_ms(200);
    }
   }
   i2c_start();                         // Start I2C
   i2c_write(0xD0);                     // DS1307 address
   i2c_write(0);                        // Send register address
   i2c_start();                         // Restart I2C
   i2c_write(0xD1);                     // Initialize data read
   second =i2c_read(1);                 // Read seconds from register 0
   minute =i2c_read(1);                 // Read minuts from register 1
   hour = i2c_read(1);                  // Read hour from register 2
   day = i2c_read(1);                   // Read day from register 3
   date = i2c_read(1);                  // Read date from register 4
   month = i2c_read(1);                 // Read month from register 5
   year = i2c_read(0);                  // Read year from register 6
   i2c_stop();                          // Stop I2C
   ds1307_display();                    // Diaplay results   delay_ms(50);
  }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Thu Nov 08, 2018 1:53 pm     Reply with quote

In this code, ds1307_display() & ds1307_write() are standalone functions to do similar operations to the standard driver.
Again this code has it's own read.
PROMOD



Joined: 01 Feb 2018
Posts: 42

View user's profile Send private message

PostPosted: Thu Nov 08, 2018 8:34 pm     Reply with quote

Code:
void ds1307_write(unsigned int8 address, data_){
  i2c_start();                          // Start I2C
  i2c_write(0xD0);                      // DS1307 address
  i2c_write(address);                   // Send register address
  i2c_write(data_);                     // Write data to the selected register
  i2c_stop();                           // Stop I2C
}


In the above code there is no "i2c_write();" function initialized/defined/declared! Then how in the following portion "i2c_read();" works?

Code:
 i2c_start();                         // Start I2C
   i2c_write(0xD0);                     // DS1307 address
   i2c_write(0);                        // Send register address
   i2c_start();                         // Restart I2C
   i2c_write(0xD1);                     // Initialize data read
   second =i2c_read(1);                 // Read seconds from register 0
   minute =i2c_read(1);                 // Read minuts from register 1
   hour = i2c_read(1);                  // Read hour from register 2
   day = i2c_read(1);                   // Read day from register 3
   date = i2c_read(1);                  // Read date from register 4
   month = i2c_read(1);                 // Read month from register 5
   year = i2c_read(0);                  // Read year from register 6
   i2c_stop();                          // Stop I2C
   ds1307_display();                    // Diaplay results   delay_ms(50);
  } 
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Fri Nov 09, 2018 12:17 am     Reply with quote

Read the compiler manual....
These are standard supplied functions from the compiler.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Fri Nov 09, 2018 4:11 am     Reply with quote

PROMOD wrote:
Then how in the following portion "i2c_read();" works?

Code:
 i2c_start();                         // Start I2C
   i2c_write(0xD0);                     // DS1307 address
   i2c_write(0);                        // Send register address
   i2c_start();                         // Restart I2C
   i2c_write(0xD1);                     // Initialize data read
   second =i2c_read(1);                 // Read seconds from register 0
   minute =i2c_read(1);                 // Read minuts from register 1
   hour = i2c_read(1);                  // Read hour from register 2
   day = i2c_read(1);                   // Read day from register 3
   date = i2c_read(1);                  // Read date from register 4
   month = i2c_read(1);                 // Read month from register 5
   year = i2c_read(0);                  // Read year from register 6
   i2c_stop();                          // Stop I2C
   ds1307_display();                    // Diaplay results   delay_ms(50);
  } 

The read works by:-

Giving the I2C the DS1307's address.
Code:
   i2c_start();                         // Start I2C
   i2c_write(0xD0);                     // DS1307 address

The DS1307 register to read from
Code:
   i2c_write(0);                        // Send register address

Sending the command to start reading
Code:
   i2c_start();                         // Restart I2C
   i2c_write(0xD1);                     // Initialize data read

Then read all the bytes in sequence
Code:
   second =i2c_read(1);                 // Read seconds from register 0
   minute =i2c_read(1);                 // Read minuts from register 1
   hour = i2c_read(1);                  // Read hour from register 2
   day = i2c_read(1);                   // Read day from register 3
   date = i2c_read(1);                  // Read date from register 4
   month = i2c_read(1);                 // Read month from register 5
   year = i2c_read(0);                  // Read year from register 6
   i2c_stop();                          // Stop I2C

It's all in the compiler manual and the DS1307 data sheet.
You need to read both to get a complete picture.

Mike
PROMOD



Joined: 01 Feb 2018
Posts: 42

View user's profile Send private message

PostPosted: Fri Nov 09, 2018 9:29 am     Reply with quote

Code:
second =i2c_read(1);                 // Read seconds from register 0
   minute =i2c_read(1);                 // Read minuts from register 1
   hour = i2c_read(1);                  // Read hour from register 2
   day = i2c_read(1);                   // Read day from register 3
   date = i2c_read(1);                  // Read date from register 4
   month = i2c_read(1);                 // Read month from register 5
   year = i2c_read(0);                  // Read year from register 6
   i2c_stop();   
Why the above code are not like the following?
Code:
second =i2c_read(0);                 // Read seconds from register 0
   minute =i2c_read(1);                 // Read minuts from register 1
   hour = i2c_read(2);                  // Read hour from register 2
   day = i2c_read(3);                   // Read day from register 3
   date = i2c_read(4);                  // Read date from register 4
   month = i2c_read(5);                 // Read month from register 5
   year = i2c_read(6);                  // Read year from register 6
   i2c_stop();         
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Fri Nov 09, 2018 9:34 am     Reply with quote

Because the second can't work....

I2C_read accepts a single _bit_ value. 0 or 1, which determines whether is sends a NACK only. Can't accept numbers like 2, 3, 4, 5....

You really need to read the I2C reference, and get an understanding of how I2C works, since this is vital to understanding what the functions actually do.


Last edited by Ttelmah on Fri Nov 09, 2018 9:41 am; edited 1 time in total
PROMOD



Joined: 01 Feb 2018
Posts: 42

View user's profile Send private message

PostPosted: Fri Nov 09, 2018 9:41 am     Reply with quote

One more question:

ds1307_write(1, minute);

In the above statement:
1=address
minute=data

Am I correct?
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Fri Nov 09, 2018 2:59 pm     Reply with quote

1, gets transferred into the variable called 'address' in the function, when the function is called.
minute, gets transferred into the variable called data in the function when the function is called.

Understand, 'address' and 'data' are variable names 'given to' the values transferred to the function. The values are copied into these variables at the moment you call the function.
PROMOD



Joined: 01 Feb 2018
Posts: 42

View user's profile Send private message

PostPosted: Fri Nov 09, 2018 8:37 pm     Reply with quote

I am all most done with this program. I have learned many things by this program.

One more things I need to know why the following conditions have been used in the program. Is there any problem if I eliminate those conditions?

Code:
if(input(PIN_B2) == 0)
..................
..................
if(input(PIN_B3) == 0)
...................
.................
if(input(PIN_B3) == 0)

......................................
 
if(input(PIN_B3) == 0)

.....................................
if(input(PIN_B3) == 0)

...................................
 if(input(PIN_B2) == 0) 

.................................
if(input(PIN_B3) == 0)
................................
if(input(PIN_B2) == 0)
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Sat Nov 10, 2018 2:26 am     Reply with quote

Depends what you want _your_ program to do?.
These are buttons to allow you to set the clock.
PROMOD



Joined: 01 Feb 2018
Posts: 42

View user's profile Send private message

PostPosted: Wed Nov 14, 2018 9:30 am     Reply with quote

I just want to read time and date from ds1307 and to display it in LCD monitor. To do so I have written the following code. But its not working. Why is it not working?


Code:
void ds1307_display()
{
  second10  =  (second & 0x70) >> 4;
  second = second & 0x0F;
  minute10  =  (minute & 0x70) >> 4;
  minute = minute & 0x0F;
  hour10  =  (hour & 0x30) >> 4;
  hour = hour & 0x0F;
  date10  =  (date & 0x30) >> 4;
  date = date & 0x0F;
  month10  =  (month & 0x10) >> 4;
  month = month & 0x0F;
  year10  =  (year & 0xF0) >> 4;
  year = year & 0x0F;
  time[12]  = second  + 48;
  time[11]  = second10  + 48;
  time[9]  = minute  + 48;
  time[8]  = minute10  + 48;
  time[6]  = hour  + 48;
  time[5]  = hour10  + 48;
  calendar[14]  = year  + 48;
  calendar[13]  = year10  + 48;
  calendar[9]  = month + 48;
  calendar[8]  = month10 + 48;
  calendar[6]  = date + 48;
  calendar[5]  = date10 + 48;
  lcd_gotoxy(1, 1);                     // Go to column 1 row 1
  printf(lcd_putc, time);               // Display time
  lcd_gotoxy(1, 2);                     // Go to column 1 row 2
  printf(lcd_putc, calendar);           // Display calendar
}
void main()
{
  setup_oscillator(OSC_8MHZ);                // Set internal oscillator to 8MHz
  setup_adc_ports(NO_ANALOGS);                // Configure AN pins as digital
  port_b_pullups(TRUE);
  output_b(0);
  set_tris_b(0x0F);
  lcd_init();                          // Initialize LCD module
  lcd_putc('\f');                      // LCD clear
  i2c_start();                         // Start I2C
  i2c_write(0xD0);                     // DS1307 address
  i2c_write(0);                        // Send register address
  i2c_start();                         // Restart I2C
  i2c_write(0xD1);                     // Initialize data read
  second =i2c_read(1);                 // Read seconds from register 0
  minute =i2c_read(1);                 // Read minuts from register 1
  hour = i2c_read(1);                  // Read hour from register 2
  day = i2c_read(1);                   // Read day from register 3
  date = i2c_read(1);                  // Read date from register 4
  month = i2c_read(1);                 // Read month from register 5
  year = i2c_read(0);                  // Read year from register 6
  i2c_stop();                          // Stop I2C
  ds1307_display();                    // Display results 
  delay_ms(50);
}
 
temtronic



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

View user's profile Send private message

PostPosted: Wed Nov 14, 2018 10:55 am     Reply with quote

quick reply..
NOT a complete program !
1) NO processor defined in 1st line of the program
2) call to function lcd_init(); yet lcd_init() is NOT in program. It's probably in an LCD_module driver file, that's not #included...
3) calls to I2C, but again no setup as to WHERE the I2C device is located, speed, type, etc.

You need to post the complete program.
You should get the PIC to flash an LED (the '1Hz LED' program).
THEN get the LCD to work, typically get 'Hello World' to display on it.
THEN proceed with the RTC portion of code.

Build upon known working code, one step at a time.

Jay
PROMOD



Joined: 01 Feb 2018
Posts: 42

View user's profile Send private message

PostPosted: Wed Nov 14, 2018 8:56 pm     Reply with quote

The following is not working!

Code:
#define LCD_RS_PIN PIN_B0
#define LCD_RW_PIN PIN_B1
#define LCD_ENABLE_PIN PIN_B2
#define LCD_DATA4 PIN_B3
#define LCD_DATA5 PIN_B4
#define LCD_DATA6 PIN_B5
#define LCD_DATA7 PIN_B6
#include <18F4431.h>   
#device  adc=10                                                                // setting the adc at 10 bit                                                         
#fuses NOMCLR, INTRC_IO, NOWDT,NOPROTECT,NOLVP                                                       // setting Cristal Clock, No watch dog timer and No low voltage protection
#use     delay(clock=8000000) 
#use fast_io(B)
#use I2C(master, I2C1, FAST = 100000)
#include <lcd.c>
char time[] = "TIME:  :  :  ";
char calendar[] = "DATE:  /  /20  ";
unsigned int8 second, second10, minute, minute10,
               hour, hour10, date, date10, month, month10,
               year, year10, day;
void ds1307_display()
{
  second10  =  (second & 0x70) >> 4;
  second = second & 0x0F;
  minute10  =  (minute & 0x70) >> 4;
  minute = minute & 0x0F;
  hour10  =  (hour & 0x30) >> 4;
  hour = hour & 0x0F;
  date10  =  (date & 0x30) >> 4;
  date = date & 0x0F;
  month10  =  (month & 0x10) >> 4;
  month = month & 0x0F;
  year10  =  (year & 0xF0) >> 4;
  year = year & 0x0F;
  time[12]  = second  + 48;
  time[11]  = second10  + 48;
  time[9]  = minute  + 48;
  time[8]  = minute10  + 48;
  time[6]  = hour  + 48;
  time[5]  = hour10  + 48;
  calendar[14]  = year  + 48;
  calendar[13]  = year10  + 48;
  calendar[9]  = month + 48;
  calendar[8]  = month10 + 48;
  calendar[6]  = date + 48;
  calendar[5]  = date10 + 48;
  lcd_gotoxy(1, 1);                     // Go to column 1 row 1
  printf(lcd_putc, time);               // Display time
  lcd_gotoxy(1, 2);                     // Go to column 1 row 2
  printf(lcd_putc, calendar);           // Display calendar
}
void main()
{
  setup_oscillator(OSC_8MHZ);                // Set internal oscillator to 8MHz
  //setup_adc_ports(NO_ANALOGS);                // Configure AN pins as digital
  port_b_pullups(TRUE);
  output_b(0);
  //set_tris_b(0x00);
  lcd_init();                          // Initialize LCD module
  lcd_putc('\f');
  while(1)
  {
  i2c_start();                         // Start I2C
  i2c_write(0xD0);                     // DS1307 address
  i2c_write(0);                        // Send register address
  i2c_start();                         // Restart I2C
  i2c_write(0xD1);                     // Initialize data read
  second =i2c_read(1);                 // Read seconds from register 0
  minute =i2c_read(1);                 // Read minuts from register 1
  hour = i2c_read(1);                  // Read hour from register 2
  day = i2c_read(1);                   // Read day from register 3
  date = i2c_read(1);                  // Read date from register 4
  month = i2c_read(1);                 // Read month from register 5
  year = i2c_read(0);                  // Read year from register 6
  i2c_stop();                          // Stop I2C
  ds1307_display();                    // Diaplay results   delay_ms(50);
  }
}
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 Previous  1, 2, 3  Next
Page 2 of 3

 
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