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 CCS Technical Support

PIC12F675+PCF8574+LCD 16x2+ RTC DS1307
Goto page 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
mahatia



Joined: 14 Nov 2016
Posts: 23

View user's profile Send private message

PIC12F675+PCF8574+LCD 16x2+ RTC DS1307
PostPosted: Tue Nov 15, 2016 2:41 am     Reply with quote

Hello everybody! Is someone could help me to display date and time from RTC DS1307 on LCD ? I have to use PIC12F675 and my LCD is connected with PCF8574! What should I do! I have already used the code that someone has posted here to display something on lcd by using PCF8574 and PIC12F675 and its works, but now, I have to display time and date, what should I do?
Mike Walne



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

View user's profile Send private message

PostPosted: Tue Nov 15, 2016 3:15 am     Reply with quote

Work out what you want to put on the display, then write your own code.

Mike
mahatia



Joined: 14 Nov 2016
Posts: 23

View user's profile Send private message

PostPosted: Tue Nov 15, 2016 4:13 am     Reply with quote

Ok! This is my schematic capture from Proteus.
[https://drive.google.com/file/d/0BwhZ3_I-f9gham9iMExDUjBNbHc/view?usp=sharing]
and this is my code:
[https://drive.google.com/drive/folders/0BwhZ3_I-f9ghUGFpY3kzeGQyd1k?usp=sharing]
Thank you.
temtronic



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

View user's profile Send private message

PostPosted: Tue Nov 15, 2016 5:57 am     Reply with quote

Well I'd use the 'search' feature of this forum and find the DS1307 'driver'. There's 2 or 3 ... 2 for sure, I know they both work though I modified one for my use. If the Ext_int pin of the 12F675 is available, I'd connect the DS1307 to it and use an ISR to display the date/time information. I use the 1Hz interrupt from the 1307 this way.

Jay
Mike Walne



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

View user's profile Send private message

PostPosted: Tue Nov 15, 2016 6:25 am     Reply with quote

Your link expects me to create or sign in to an account.
I'm not going to do it.

Mike
mahatia



Joined: 14 Nov 2016
Posts: 23

View user's profile Send private message

PostPosted: Tue Nov 15, 2016 10:11 am     Reply with quote

I'm so sorry, I'll post it here soon,thanks
mahatia



Joined: 14 Nov 2016
Posts: 23

View user's profile Send private message

PostPosted: Tue Nov 15, 2016 1:11 pm     Reply with quote

these are my code, i saw them in this forum and I want to add an RTC ds1307.
I2C_LCD.c

Code:
#include <12F675.h>
#device *= 16
#fuses NOWDT, PROTECT, CPD, NOMCLR, INTRC_IO, BROWNOUT, PUT
#use delay (internal = 4MHz)   
#include "lcd.c"                         

void setup();                       
                                                                       
void main()
{                       
   unsigned char i = 0;     
   
    char txt1[] = {"RAMANANTSOA"};
   char txt2[] = {"Projet 2"};
    //char txt[6];
                                   
    setup();                   

    while(TRUE)
    {                                                                                     
        LCD_clear_home();   
             
       for(i = 0; i <= 10; i++)
        {                 
         
           LCD_goto((i), 0);
           LCD_putchar(txt1[i]);
           delay_ms(99);
        }                                               
       for(i = 0; i <= 9; i++)                                   
       {           
           LCD_goto((i), 1);                                                         
           LCD_putchar(txt2[i]);                                         
          // delay_ms(99);
        }         
                                 
       
    };
   

}                                                                             
void setup()
{
    disable_interrupts(GLOBAL);
    setup_comparator(NC_NC_NC_NC);                   
    setup_ADC(ADC_off);
    setup_ADC_ports(no_analogs);
    setup_timer_0(T0_internal);     
    setup_timer_1(T1_disabled);
    set_timer0(0); 
    set_timer1(0);
    LCD_init(); 
    delay_ms(100);       
}                         

lcd.c

Code:
#include "lcd.h"
                             

void LCD_init()
{                                     
    unsigned char t = 0x1A; 

    data_value = 0x08;
    PCF8574_write(data_value);     
    while(t > 0x00)
    {   
            delay_ms(dly);
            t--;
    };     
   
    data_value = 0x30;
    PCF8574_write(data_value); 
                             
    data_value |= 0x08;
    PCF8574_write(data_value);     
    delay_ms(dly);
    data_value &= 0xF7;
    PCF8574_write(data_value); 
    delay_ms(dly);
             
    data_value = 0x30;
    PCF8574_write(data_value); 
                   
    data_value |= 0x08;
    PCF8574_write(data_value); 
    delay_ms(dly);
    data_value &= 0xF7;
    PCF8574_write(data_value); 
    delay_ms(dly);

    data_value = 0x30;
    PCF8574_write(data_value); 

    data_value |= 0x08;
    PCF8574_write(data_value); 
    delay_ms(dly);
    data_value &= 0xF7;
    PCF8574_write(data_value); 
    delay_ms(dly);

    data_value = 0x20;
    PCF8574_write(data_value); 
                   
    data_value |= 0x08;
    PCF8574_write(data_value); 
    delay_ms(dly);
    data_value &= 0xF7;
    PCF8574_write(data_value); 
    delay_ms(dly);

    LCD_command(_4_pin_interface | _2_row_display | _5x7_dots);         
    LCD_command(display_on | cursor_off | blink_off);     
    LCD_command(clear_display);                           
    LCD_command(cursor_direction_inc | display_no_shift);       
}   


void LCD_command(unsigned char value)
{                                   
    data_value &= 0xFB;
    PCF8574_write(data_value); 
    LCD_4bit_send(value);           
}
   

void LCD_send_data(unsigned char value)
{                               
    data_value |= 0x04;
    PCF8574_write(data_value); 
    LCD_4bit_send(value);
}                                       
   

void LCD_4bit_send(unsigned char lcd_data)       
{
    unsigned char temp = 0x00;
     
    temp = (lcd_data & 0xF0);
    data_value &= 0x0F;   
    data_value |= temp;   
    PCF8574_write(data_value); 
                                         
    data_value |= 0x08;
    PCF8574_write(data_value); 
    delay_ms(dly);
    data_value &= 0xF7;
    PCF8574_write(data_value); 
    delay_ms(dly);
   
    temp = (lcd_data & 0x0F);
    temp <<= 0x04;     
    data_value &= 0x0F; 
    data_value |= temp;                           
    PCF8574_write(data_value); 
               
    data_value |= 0x08;
    PCF8574_write(data_value); 
    delay_ms(dly);
    data_value &= 0xF7;
    PCF8574_write(data_value); 
    delay_ms(dly);



void LCD_putstr(char *lcd_string)
{
    while (*lcd_string != '\0')   
    {
        LCD_send_data(*lcd_string);
        lcd_string++;
    };
}


void LCD_putchar(char char_data)
{
    LCD_send_data(char_data);
}


void LCD_clear_home()
{
    LCD_command(clear_display);
    LCD_command(goto_home);
}


void LCD_goto(unsigned char x_pos,unsigned char y_pos)
{                                                   
    if(y_pos == 0)   
    {                             
        LCD_command(0x80 | x_pos);
    }
    else
    {                                             
        LCD_command(0x80 | 0x40 | x_pos);
    }
}

lcd.h
Code:
#include "PCF8574.c"


#define clear_display                           0x01
#define goto_home                               0x02
         
#define cursor_direction_inc                    (0x04 | 0x02)
#define cursor_direction_dec                    (0x04 | 0x00)
#define display_shift                           (0x04 | 0x01)
#define display_no_shift                        (0x04 | 0x00)

#define display_on                              (0x08 | 0x04)
#define display_off                             (0x08 | 0x02)
#define cursor_on                               (0x08 | 0x02)                           
#define cursor_off                              (0x08 | 0x00)
#define blink_on                                (0x08 | 0x01)
#define blink_off                               (0x08 | 0x00)
                                   
#define _8_pin_interface                        (0x20 | 0x10)
#define _4_pin_interface                        (0x20 | 0x00)
#define _2_row_display                          (0x20 | 0x08)
#define _1_row_display                          (0x20 | 0x00)
#define _5x10_dots                              (0x20 | 0x40)
#define _5x7_dots                               (0x20 | 0x00)
                                                   
#define dly                                     0x01


unsigned char data_value = 0x00;
                                   
                                                                         
void LCD_init(); 
void LCD_command(unsigned char value);
void LCD_send_data(unsigned char value);
void LCD_4bit_send(unsigned char lcd_data);                                     
void LCD_putstr(char *lcd_string);               
void LCD_putchar(char char_data);             
void LCD_clear_home();           
void LCD_goto(unsigned char x_pos, unsigned char y_pos);

PCF8574.c
Code:
#include "PCF8574.h"
     
                                       
unsigned char PCF8574_read()
{
    unsigned char port_byte = 0;       
    i2c_start();                             
    i2c_write(PCF8574_read_cmd);       
    port_byte = i2c_read(0);             
    i2c_stop();
    return port_byte;
}                                                         


void PCF8574_write(unsigned char data_byte)                     
{
    i2c_start();
    i2c_write(PCF8574_write_cmd);               
    i2c_write(data_byte);
    i2c_stop();
}                             

PCF8574
Code:
#define PCF8574_address       0x40

#define PCF8574_write_cmd      PCF8574_address             
#define PCF8574_read_cmd      (PCF8574_address + 1)   

#define SDA_pin               pin_A5
#define SCL_pin                 pin_A4     


#use I2C(Master, SDA = SDA_pin, SCL = SCL_pin)

             
unsigned char PCF8574_read();               
void PCF8574_write(unsigned char data_byte);                                   

I use PIC12F675, what should I do if I want to display date and time from RTC?
I found this driver in this forum but I get many errors when I use it with my code
DS1307.c
Code:

#define RTC_SDA  pin_A5
#define RTC_SCL  pin_A4

#use i2c(master, sda=RTC_SDA, scl=RTC_SCL)

BYTE bin2bcd(BYTE binary_value);
BYTE bcd2bin(BYTE bcd_value);

void ds1307_init(void)
{
   BYTE seconds = 0;

   i2c_start();
   i2c_write(0xD0);      // WR to RTC
   i2c_write(0x00);      // REG 0
   i2c_start();
   i2c_write(0xD1);      // RD from RTC
   seconds = bcd2bin(i2c_read(0)); // Read current "seconds" in DS1307
   i2c_stop();
   seconds &= 0x7F;

   delay_us(3);

   i2c_start();
   i2c_write(0xD0);      // WR to RTC
   i2c_write(0x00);      // REG 0
   i2c_write(bin2bcd(seconds));     // Start oscillator with current "seconds value
   i2c_start();
   i2c_write(0xD0);      // WR to RTC
   i2c_write(0x07);      // Control Register
   i2c_write(0x10);     // Disable squarewave output pin
   i2c_stop();

}

void ds1307_set_date_time(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min, BYTE sec)
{
  sec &= 0x7F;
  hr &= 0x3F;

  i2c_start();
  i2c_write(0xD0);            // I2C write address
  delay_us(1);
  i2c_write(0x00);            // Start at REG 0 - Seconds
  i2c_write(bin2bcd(sec));      // REG 0
  i2c_write(bin2bcd(min));      // REG 1
  i2c_write(bin2bcd(hr));      // REG 2
  i2c_write(bin2bcd(dow));      // REG 3
  i2c_write(bin2bcd(day));      // REG 4
  i2c_write(bin2bcd(mth));      // REG 5
  i2c_write(bin2bcd(year));      // REG 6
  //i2c_write(0x80);            // REG 7 - Disable squarewave output pin
  i2c_write(0x10);
  i2c_stop();
}

void ds1307_get_date(BYTE &day, BYTE &mth, BYTE &year, BYTE &dow)
{
  i2c_start();
  i2c_write(0xD0);
  delay_us(1);
  i2c_write(0x03);            // Start at REG 3 - Day of week
  i2c_start();
  i2c_write(0xD1);
  dow  = bcd2bin(i2c_read() & 0x7f);   // REG 3
  day  = bcd2bin(i2c_read() & 0x3f);   // REG 4
  mth  = bcd2bin(i2c_read() & 0x1f);   // REG 5
  year = bcd2bin(i2c_read(0));            // REG 6
  i2c_stop();
}

void ds1307_get_time(BYTE &hr, BYTE &min, BYTE &sec)
{
  i2c_start();
  i2c_write(0xD0);
  i2c_write(0x00);            // Start at REG 0 - Seconds
  i2c_start();
  i2c_write(0xD1);
  sec = bcd2bin(i2c_read() & 0x7f);
  min = bcd2bin(i2c_read() & 0x7f);
  hr  = bcd2bin(i2c_read(0) & 0x3f);
  i2c_stop();

}

BYTE bin2bcd(BYTE binary_value)
{
  BYTE temp;
  BYTE retval;

  temp = binary_value;
  retval = 0;

  //while(1) IO NO TEO
  while(TRUE)
  {
    // Get the tens digit by doing multiple subtraction
    // of 10 from the binary value.
    if(temp >= 10)
    {
      temp -= 10;
      retval += 0x10;
    }
    else // Get the ones digit by adding the remainder.
    {
      retval += temp;
      break;
    }
  };

  return(retval);
}


// Input range - 00 to 99.
BYTE bcd2bin(BYTE bcd_value)
{
  BYTE temp;

  temp = bcd_value;
  // Shifting upper digit right by 1 is same as multiplying by 8.
  temp >>= 1;
  // Isolate the bits for the upper digit.
  temp &= 0x78;

  // Now return: (Tens * 8) + (Tens * 2) + Ones

  return(temp + (temp >> 2) + (bcd_value & 0x0f));

DS1307.h
Code:

#ifndef __DS1307_H
#define __DS1307_H


// Define DS1307 i2c device address
#define Device_Address_DS1307_EEPROM   0xD0

// Define Time Modes
#define AM_Time               0
#define PM_Time               1
#define TwentyFourHoursMode      2

// Define days
#define Monday      1
#define Tuesday      2
#define Wednesday   3
#define Thursday   4
#define Friday      5
#define Saturday   6
#define Sunday      7

// Function Declarations
void Write_Byte_To_DS1307_RTC(unsigned char, unsigned char);
unsigned char Read_Byte_From_DS1307_RTC(unsigned char);
void Write_Bytes_To_DS1307_RTC(unsigned char,unsigned char*,unsigned char);
void Read_Bytes_From_DS1307_RTC(unsigned char,unsigned char*,unsigned int);
void Set_DS1307_RTC_Time(unsigned char,unsigned char,unsigned char,unsigned char);
unsigned char* Get_DS1307_RTC_Time(void);
void Set_DS1307_RTC_Date(unsigned char,unsigned char,unsigned char,unsigned char);
unsigned char* Get_DS1307_RTC_Date(void);

#endif


Thank you!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Nov 15, 2016 2:12 pm     Reply with quote

This block of code will display the date and time from the ds1307 on an lcd:
Code:
ds1307_get_date(day,month,yr,dow);  // Read date
ds1307_get_time(hrs,min,sec);    // Read Time
lcd_gotoxy(1,1);
printf(lcd_Putc,"\%02d/\%02d/\%02d",day,month,yr); // Display date
lcd_gotoxy(1,2);
printf(lcd_Putc," %02d:\%02d:\%02d", hrs,min,sec); // Display time
mahatia



Joined: 14 Nov 2016
Posts: 23

View user's profile Send private message

PostPosted: Tue Nov 15, 2016 5:18 pm     Reply with quote

Thank you for helping me; I have tried the code, I do it like that
I2C_LCD.c
Code:
#include "DS1307.c"
#include <12F675.h>
#device *= 16
#fuses NOWDT, PROTECT, CPD, NOMCLR, INTRC_IO, BROWNOUT, PUT
#use delay (internal = 4MHz)   
#include "lcd.c"                         

void setup();                       
                                                                       
void main()
{                       
   unsigned char i = 0;     
   
    char txt1[] = {"RAMANANTSOA"};
   char txt2[] = {"Projet 2"};
    //char txt[6];
                                   
    setup();                   

    while(TRUE)
    {                                                                                     
        LCD_clear_home();   
             
       for(i = 0; i <= 10; i++)
        {                 
         
           LCD_goto((i), 0);
           LCD_putchar(txt1[i]);
           delay_ms(99);
        }                                               
       for(i = 0; i <= 9; i++)                                   
       {           
           LCD_goto((i), 1);                                                         
           LCD_putchar(txt2[i]);                                         
          // delay_ms(99);
        }         
    ds1307_init();                             
   ds1307_get_date(day,month,yr,dow);  // Read date
   ds1307_get_time(hrs,min,sec);    // Read Time
   lcd_goto(1,1);
   printf(lcd_putchar,"\%02d/\%02d/\%02d",day,month,yr); // Display date
   lcd_goto(1,2);
   printf(lcd_putchar," %02d:\%02d:\%02d", hrs,min,sec); // Display time
    };
   

}                                                                             
void setup()
{
    disable_interrupts(GLOBAL);
    setup_comparator(NC_NC_NC_NC);                   
    setup_ADC(ADC_off);
    setup_ADC_ports(no_analogs);
    setup_timer_0(T0_internal);     
    setup_timer_1(T1_disabled);
    set_timer0(0); 
    set_timer1(0);
    LCD_init(); 
    delay_ms(100);       
}                           

DS1307.c
Code:


#define RTC_SDA  pin_A5
#define RTC_SCL  pin_A4

#use i2c(master, sda=RTC_SDA, scl=RTC_SCL)

BYTE bin2bcd(BYTE binary_value);
BYTE bcd2bin(BYTE bcd_value);

void ds1307_init(void)
{
   BYTE seconds = 0;

   i2c_start();
   i2c_write(0xD0);      // WR to RTC
   i2c_write(0x00);      // REG 0
   i2c_start();
   i2c_write(0xD1);      // RD from RTC
   seconds = bcd2bin(i2c_read(0)); // Read current "seconds" in DS1307
   i2c_stop();
   seconds &= 0x7F;

   delay_us(3);

   i2c_start();
   i2c_write(0xD0);      // WR to RTC
   i2c_write(0x00);      // REG 0
   i2c_write(bin2bcd(seconds));     // Start oscillator with current "seconds value
   i2c_start();
   i2c_write(0xD0);      // WR to RTC
   i2c_write(0x07);      // Control Register
   i2c_write(0x10);     // Disable squarewave output pin
   i2c_stop();

}

void ds1307_set_date_time(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min, BYTE sec)
{
  sec &= 0x7F;
  hr &= 0x3F;

  i2c_start();
  i2c_write(0xD0);            // I2C write address
  delay_us(1);
  i2c_write(0x00);            // Start at REG 0 - Seconds
  i2c_write(bin2bcd(sec));      // REG 0
  i2c_write(bin2bcd(min));      // REG 1
  i2c_write(bin2bcd(hr));      // REG 2
  i2c_write(bin2bcd(dow));      // REG 3
  i2c_write(bin2bcd(day));      // REG 4
  i2c_write(bin2bcd(mth));      // REG 5
  i2c_write(bin2bcd(year));      // REG 6
  //i2c_write(0x80);            // REG 7 - Disable squarewave output pin
  i2c_write(0x10);
  i2c_stop();
}

void ds1307_get_date(BYTE &day, BYTE &mth, BYTE &year, BYTE &dow)
{
  i2c_start();
  i2c_write(0xD0);
  delay_us(1);
  i2c_write(0x03);            // Start at REG 3 - Day of week
  i2c_start();
  i2c_write(0xD1);
  dow  = bcd2bin(i2c_read() & 0x7f);   // REG 3
  day  = bcd2bin(i2c_read() & 0x3f);   // REG 4
  mth  = bcd2bin(i2c_read() & 0x1f);   // REG 5
  year = bcd2bin(i2c_read(0));            // REG 6
  i2c_stop();
}

void ds1307_get_time(BYTE &hr, BYTE &min, BYTE &sec)
{
  i2c_start();
  i2c_write(0xD0);
  i2c_write(0x00);            // Start at REG 0 - Seconds
  i2c_start();
  i2c_write(0xD1);
  sec = bcd2bin(i2c_read() & 0x7f);
  min = bcd2bin(i2c_read() & 0x7f);
  hr  = bcd2bin(i2c_read(0) & 0x3f);
  i2c_stop();

}

BYTE bin2bcd(BYTE binary_value)
{
  BYTE temp;
  BYTE retval;

  temp = binary_value;
  retval = 0;

  //while(1) IO NO TEO
  while(TRUE)
  {
    // Get the tens digit by doing multiple subtraction
    // of 10 from the binary value.
    if(temp >= 10)
    {
      temp -= 10;
      retval += 0x10;
    }
    else // Get the ones digit by adding the remainder.
    {
      retval += temp;
      break;
    }
  };

  return(retval);
}


// Input range - 00 to 99.
BYTE bcd2bin(BYTE bcd_value)
{
  BYTE temp;

  temp = bcd_value;
  // Shifting upper digit right by 1 is same as multiplying by 8.
  temp >>= 1;
  // Isolate the bits for the upper digit.
  temp &= 0x78;

  // Now return: (Tens * 8) + (Tens * 2) + Ones

  return(temp + (temp >> 2) + (bcd_value & 0x0f));

And after compiling, he asked me to put #DEVICE before this line
Code:
#use i2c(master, sda=RTC_SDA, scl=RTC_SCL)

thank you! I am losed Crying or Very sad Crying or Very sad
jeremiah



Joined: 20 Jul 2010
Posts: 1358

View user's profile Send private message

PostPosted: Wed Nov 16, 2016 7:58 am     Reply with quote

Your program's first include MUST always be the the chip's header file. In your posted code it is not. General order:

Chip include file
#device statments
#fuses
#use delay() setup
other #use or includes
remaining code.
mahatia



Joined: 14 Nov 2016
Posts: 23

View user's profile Send private message

PostPosted: Wed Nov 16, 2016 9:19 am     Reply with quote

ok! let me try it! thank you very much for the idea!
mahatia



Joined: 14 Nov 2016
Posts: 23

View user's profile Send private message

PostPosted: Wed Nov 16, 2016 3:02 pm     Reply with quote

I have done what you suggest me to do:
I2C_LCD.c
Code:
#include <12F675.h>
#device *= 16
#fuses NOWDT, PROTECT, CPD, NOMCLR, INTRC_IO, BROWNOUT, PUT
#use delay (internal = 4MHz)

#include "lcd.c"                         
#include "DS1307.c"
                   
 void setup();                                                                       
void main()
{                       
   unsigned char i = 0;     
   
    char txt1[] = {"hello"};
   char txt2[] = {"Projet 2"};
   
         byte day;
        byte mth;
        byte year;
        byte dow;
        byte hr;
        byte min;
        byte sec;
       
    setup();                   

    while(TRUE)
    {                                                                                     
        LCD_clear_home();   
             
       for(i = 0; i <= 10; i++)
        {                 
         
           LCD_goto((i), 0);
           LCD_putchar(txt1[i]);
           delay_ms(99);
        }                                               
       for(i = 0; i <= 9; i++)                                   
       {           
           LCD_goto((i), 1);                                                         
           LCD_putchar(txt2[i]);                                         
           delay_ms(99);
        }
         
       
   while(TRUE)
    {     
    ds1307_init();                             
   ds1307_get_date(day,mth,year,dow);  // Read date
   ds1307_get_time(hr,min,sec);    // Read Time
   LCD_goto(1,1);
   printf(LCD_putchar,"\%02d/\%02d/\%02d",day,mth,year); // Display date
   LCD_goto(1,2);
   printf(LCD_putchar," %02d:\%02d:\%02d",hr,min,sec); // Display time
    };
   };

}                                                                             
 void setup()
{
    disable_interrupts(GLOBAL);
    setup_comparator(NC_NC_NC_NC);                   
    setup_ADC(ADC_off);
    setup_ADC_ports(no_analogs);
    setup_timer_0(T0_internal);     
    setup_timer_1(T1_disabled);
    set_timer0(0); 
    set_timer1(0);
    LCD_init(); 
    delay_ms(100);       
}         
                 


After running and building, they send me this error message: Functions may not be nested. He underlines in red the void setup(); and void main() and setup();
I have tried to solve the problem but I couldn't find it! I am a beginner in CCS, could you help me for the last time please?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Nov 16, 2016 4:59 pm     Reply with quote

Look at this function at the end of ds1307.c
Quote:
BYTE bcd2bin(BYTE bcd_value)

You left off the ending brace on that function. You left off this:
Quote:
}
mahatia



Joined: 14 Nov 2016
Posts: 23

View user's profile Send private message

PostPosted: Mon Nov 21, 2016 10:32 am     Reply with quote

thank you PCM programmer. I have tried the code and it works! But could tell me why the seconds takes a long time to increments when I simulate it with proteus? this is my code:
I2C_LCD.c
Code:
#include <12F675.h>
#device *= 16
#fuses NOWDT, PROTECT, CPD, NOMCLR, INTRC_IO, BROWNOUT, PUT
#use delay (internal = 4MHz)

#include "lcd.c"                         
#include "DS1307.c"
                   
 void setup();                                                                       
void main()
{   
 byte day;
        byte mth;
        byte year;
        byte hr;
        byte min;
        byte sec;
       
    setup();   
 while(TRUE)
    {
   
    ds1307_init();
   
   ds1307_get_date(day,mth,year);  // Read date
   ds1307_get_time(hr,min,sec);    // Read Time
   LCD_goto(0,0);
   printf(LCD_putchar," %02d:\%02d:\%02d",hr,min,sec); // Display time
   LCD_goto(0,1);
   printf(LCD_putchar,"\%02d/\%02d/20\%02d",day,mth,year); // Display date
   
    };
while(TRUE)
    {
   
    };
}                                                                             
 void setup()
{
    disable_interrupts(GLOBAL);
    setup_comparator(NC_NC_NC_NC);                   
    setup_ADC(ADC_off);
    setup_ADC_ports(no_analogs);
    setup_timer_0(T0_internal);     
    setup_timer_1(T1_disabled);
    set_timer0(0); 
    set_timer1(0);
    LCD_init(); 
    delay_ms(100);       
}         

lcd.c
Code:
#include "lcd.h"
                             

void LCD_init()
{   
data_value = 0x30;
    PCF8574_write(data_value); 
 data_value = 0x20;
    PCF8574_write(data_value); 
                   
    data_value |= 0x08;
    PCF8574_write(data_value); 
    delay_ms(dly);
    data_value &= 0xF7;
    PCF8574_write(data_value); 
    delay_ms(dly);

    LCD_command(_4_pin_interface | _2_row_display | _5x7_dots);         
    LCD_command(display_on | cursor_off | blink_off);     
    LCD_command(clear_display);                           
    LCD_command(cursor_direction_inc | display_no_shift);       
}   


void LCD_command(unsigned char value)
{                                   
    data_value &= 0xFB;
    PCF8574_write(data_value); 
    LCD_4bit_send(value);           
}
   

void LCD_send_data(unsigned char value)
{                               
    data_value |= 0x04;
    PCF8574_write(data_value); 
    LCD_4bit_send(value);
}                                       
   

void LCD_4bit_send(unsigned char lcd_data)       
{
    unsigned char temp = 0x00;
     
    temp = (lcd_data & 0xF0);
    data_value &= 0x0F;   
    data_value |= temp;   
    PCF8574_write(data_value); 
                                         
    data_value |= 0x08;
    PCF8574_write(data_value); 
    delay_ms(dly);
    data_value &= 0xF7;
    PCF8574_write(data_value); 
    delay_ms(dly);
   
   
   
    temp = (lcd_data & 0x0F);
    temp <<= 0x04;     
    data_value &= 0x0F; 
    data_value |= temp;                           
    PCF8574_write(data_value); 
               
     data_value |= 0x08;
    PCF8574_write(data_value); 
    delay_ms(dly);
    data_value &= 0xF7;
    PCF8574_write(data_value); 
    delay_ms(dly);



//void LCD_putstr(char *lcd_string)
//{
   // while (*lcd_string != '\0')   
   // {
       // LCD_send_data(*lcd_string);
       // lcd_string++;
   // };
//}


void LCD_putchar(char char_data)
{
    LCD_send_data(char_data);
}


void LCD_clear_home()
{
    LCD_command(clear_display);
    LCD_command(goto_home);
}


void LCD_goto(unsigned char x_pos,unsigned char y_pos)
{                                                   
    if(y_pos == 0)   
    {                             
        LCD_command(0x80 | x_pos);
       
    }
    else
    {                                             
        LCD_command(0x80 | 0x40 | x_pos);
    }
}

lcd.h
Code:
#include "PCF8574.c"


#define clear_display                           0x01
#define goto_home                               0x02
         
#define cursor_direction_inc                    (0x04 | 0x02)
#define cursor_direction_dec                    (0x04 | 0x00)
#define display_shift                           (0x04 | 0x01)
#define display_no_shift                        (0x04 | 0x00)

#define display_on                              (0x08 | 0x04)
#define display_off                             (0x08 | 0x02)
#define cursor_on                               (0x08 | 0x02)                           
#define cursor_off                              (0x08 | 0x00)
#define blink_on                                (0x08 | 0x01)
#define blink_off                               (0x08 | 0x00)
                                   
#define _8_pin_interface                        (0x20 | 0x10)
#define _4_pin_interface                        (0x20 | 0x00)
#define _2_row_display                          (0x20 | 0x08)
#define _1_row_display                          (0x20 | 0x00)
#define _5x10_dots                              (0x20 | 0x40)
#define _5x7_dots                               (0x20 | 0x00)
                                                   
#define dly                                     0x01
       


unsigned char data_value = 0x00;
                                   
                                                                         
void LCD_init(); 
void LCD_command(unsigned char value);
void LCD_send_data(unsigned char value);
void LCD_4bit_send(unsigned char lcd_data);                                     
void LCD_putstr(char *lcd_string);               
void LCD_putchar(char char_data);             
void LCD_clear_home();           
void LCD_goto(unsigned char x_pos, unsigned char y_pos);

PCF8574.c
Code:

#include "PCF8574.h"
                                       
unsigned char PCF8574_read()
{
    unsigned char port_byte = 0;       
    i2c_start();                             
    i2c_write(PCF8574_read_cmd);       
    port_byte = i2c_read(0);             
    i2c_stop();
    return port_byte;
}                                                         


void PCF8574_write(unsigned char data_byte)                     
{
    i2c_start();
    i2c_write(PCF8574_write_cmd);               
    i2c_write(data_byte);
    i2c_stop();
}     

PCF8574.h
Code:
#define PCF8574_address       0x40 //A2, A1, A0, vers la masse

#define PCF8574_write_cmd      PCF8574_address             
#define PCF8574_read_cmd      (PCF8574_address + 1)   

#define SDA_pin               pin_A5
#define SCL_pin                 pin_A4     


#use I2C(Master, SDA = SDA_pin, SCL = SCL_pin)

             
unsigned char PCF8574_read();               
void PCF8574_write(unsigned char data_byte);

DS1307.C
Code:
#include <DS1307.h>
#define RTC_SDA  pin_A5
#define RTC_SCL  pin_A4

#use i2c(master, sda=RTC_SDA, scl=RTC_SCL)

byte bin2bcd(byte binary_value);
byte bcd2bin(byte bcd_value);

void ds1307_init(void)
{
 output_float(RTC_SCL);
   output_float(RTC_SDA);
}
void ds1307_get_date(byte &day, byte &mth, byte &year)
{
  i2c_start();
  i2c_write(0xD0);
  delay_us(1);
  i2c_write(0x04);            // Start at REG 3 - Day of week
  i2c_start();
  i2c_write(0xD1);
 // dow  = bcd2bin(i2c_read(1) & 0x7f);   // REG 3
  day  = bcd2bin(i2c_read(1) & 0x3f);   // REG 4
  mth  = bcd2bin(i2c_read(1) & 0x1f);   // REG 5
  year = bcd2bin(i2c_read(0));            // REG 6
  i2c_stop();
}

void ds1307_get_time(byte &hr, byte &min, byte &sec)
{
  i2c_start();
  i2c_write(0xD0);
  i2c_write(0x00);            // Start at REG 0 - Seconds
  i2c_start();
  i2c_write(0xD1);
  sec = bcd2bin(i2c_read(1) & 0x7f);
  min = bcd2bin(i2c_read(1) & 0x7f);
  hr  = bcd2bin(i2c_read(0) & 0x3f);
  i2c_stop();

}

byte bin2bcd(byte binary_value)
{
  byte temp;
  byte retval;

  temp = binary_value;
  retval = 0;

  //while(1) IO NO TEO
  while(TRUE)
  {
    // Get the tens digit by doing multiple subtraction
    // of 10 from the binary value.
    if(temp >= 10)
    {
      temp -= 10;
      retval += 0x10;
    }
    else // Get the ones digit by adding the remainder.
    {
      retval += temp;
      break;
    }
  };

  return(retval);
}


// Input range - 00 to 99.
byte bcd2bin(byte bcd_value)
{
  byte temp;

  temp = bcd_value;
  // Shifting upper digit right by 1 is same as multiplying by 8.
  temp >>= 1;
  // Isolate the bits for the upper digit.
  temp &= 0x78;

  // Now return: (Tens * 8) + (Tens * 2) + Ones

  return(temp + (temp >> 2) + (bcd_value & 0x0f));
  }

DS1307.h
Code:
#ifndef __DS1307_H
#define __DS1307_H


// Define DS1307 i2c device address
#define Device_Address_DS1307_EEPROM   0xD0
void Write_Byte_To_DS1307_RTC(unsigned char, unsigned char);
unsigned char Read_Byte_From_DS1307_RTC(unsigned char);
void Write_Bytes_To_DS1307_RTC(unsigned char,unsigned char*,unsigned char);
void Read_Bytes_From_DS1307_RTC(unsigned char,unsigned char*,unsigned int);
void Set_DS1307_RTC_Time(unsigned char,unsigned char,unsigned char,unsigned char);
unsigned char* Get_DS1307_RTC_Time(void);
void Set_DS1307_RTC_Date(unsigned char,unsigned char,unsigned char,unsigned char);
unsigned char* Get_DS1307_RTC_Date(void);

#endif

Any help is appreciated! thank you!
And what should I do if I want to add some push button connected to the PIC to adjust time and date?
mahatia



Joined: 14 Nov 2016
Posts: 23

View user's profile Send private message

PostPosted: Mon Nov 21, 2016 10:38 am     Reply with quote

Crying or Very sad Crying or Very sad Crying or Very sad Rolling Eyes
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, 3  Next
Page 1 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