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

Problem with DS3231 and PIC16F1847
Goto page Previous  1, 2, 3, 4  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jake1272



Joined: 15 Mar 2021
Posts: 37

View user's profile Send private message

PostPosted: Mon Apr 12, 2021 7:53 am     Reply with quote

In the main code :


I removed the I2C1 and the LCD does display the date and time in a wrong format and the time it is not running as well
Code:
 #use I2C(MASTER, SLOW = 100000, STREAM = DS3231_STREAM)
Code:
  #use I2C(MASTER, I2C1, SLOW = 100000, STREAM = DS3231_STREAM)
temtronic



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

View user's profile Send private message

PostPosted: Mon Apr 12, 2021 8:14 am     Reply with quote

That RTC has an 'osc-enable' bit, maybe the driver doesn't enable the enable ?? It 'should', by default be enabled, at least for a 5 volt PIC, if my fuzzy memory serves me right. You do HAVE a battery installed on the module ??
jake1272



Joined: 15 Mar 2021
Posts: 37

View user's profile Send private message

PostPosted: Mon Apr 12, 2021 8:19 am     Reply with quote

temtronic wrote:
That RTC has an 'osc-enable' bit, maybe the driver doesn't enable the enable ?? It 'should', by default be enabled, at least for a 5 volt PIC, if my fuzzy memory serves me right. You do HAVE a battery installed on the module ??


Yes, I do have a 3.6V battery installed on the module.
jake1272



Joined: 15 Mar 2021
Posts: 37

View user's profile Send private message

PostPosted: Mon Apr 12, 2021 8:25 am     Reply with quote

Jerson wrote:
To begin with debugging your code, I would

* comment all calls to the RTC driver functions.
* Replace the print statement with dummy values for each variable you print
* If you succeed with this, then re-enable RTC_get and check if it prints some values

Unless this works, do not check other RTC functions.
Thank you very much for the suggestion
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Apr 12, 2021 4:27 pm     Reply with quote

jake1272 wrote:
I
removed the I2C1 and the LCD does display the date and time in a wrong format and the time it is not running as well.

Post what you see on the LCD.
Post the date which shows in a wrong format.
If you see 00:00:00 for the time, then post that to us.
The more information the better. Withholding information makes it
harder for people to help you.

Also, I think you missed my post here, where I asked you to use
the example main() from the simple circuits website, instead of
your modified version. The simple circuits code initializes the ds3231
with a date and time. Your code does not do this.
http://www.ccsinfo.com/forum/viewtopic.php?t=59278&start=24
jake1272



Joined: 15 Mar 2021
Posts: 37

View user's profile Send private message

PostPosted: Tue Apr 13, 2021 12:13 am     Reply with quote

PCM programmer wrote:
jake1272 wrote:
I
removed the I2C1 and the LCD does display the date and time in a wrong format and the time it is not running as well.

Post what you see on the LCD.
Post the date which shows in a wrong format.
If you see 00:00:00 for the time, then post that to us.
The more information the better. Withholding information makes it
harder for people to help you.

Also, I think you missed my post here, where I asked you to use
the example main() from the simple circuits website, instead of
your modified version. The simple circuits code initializes the ds3231
with a date and time. Your code does not do this.
http://www.ccsinfo.com/forum/viewtopic.php?t=59278&start=24


I did try his code, it doesnt change anything...I think there is a problem between the master and slave.I am not really sure
So the LCD print:
165:165:16 T:-00.25°C
Sat: 165/165/20165
A1: 45:85:00 ON
A2: 45:85:00 ON
Jerson



Joined: 31 Jul 2009
Posts: 125
Location: Bombay, India

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

Re: Problem with DS3231 and PIC16F1847
PostPosted: Tue Apr 13, 2021 12:55 am     Reply with quote

I have made a few changes that will allow you to test the code independent of the RTC part. At the very least, you will be able to set the date/time and see it on LCD. Of course, since the RTC driver is not being called, the reading will not change automatically. You will be able to edit it via the editing routines.

Once you're sure that part works, you can concentrate your effort on the RTC driver and why all bytes are reading 165 (0xA5)

Code:

// LCD module connections
#define LCD_RS_PIN      PIN_A2
#define LCD_RW_PIN      PIN_A3
#define LCD_ENABLE_PIN  PIN_A4
#define LCD_DATA4       PIN_A7
#define LCD_DATA5       PIN_A6
#define LCD_DATA6       PIN_B5
#define LCD_DATA7       PIN_B4
// end LCD module connections
 
// pin definitions
#define button1      PIN_B1
#define button2      PIN_B2
#define button3      PIN_B3
#define LED_PIN      PIN_B6
 
#include <16F1847.h>
#fuses  INTRC_IO ,NOMCLR,NOLVP,NOBROWNOUT,PUT,NOWDT
#use delay(internal = 8MHz)
#use I2C(MASTER, I2C1, SLOW = 100000, STREAM = DS3231_STREAM)
#use fast_io(b)
#use fast_io(a)
#include <lcd.c>     // include LCD driver source file
#include <DS3231.c>  // include DS3231 driver source file
 
int8 i;
// DS3231 library variable declaration
RTC_Time *mytime, *alarm1, *alarm2;
 
// external interrupt routine
#INT_EXT
void ext_isr(void)
{
  output_high(LED_PIN);
  clear_interrupt(INT_EXT);
}
 
// function for displaying day of the week
void dow_print()
{
  lcd_gotoxy(1, 2);
  switch(mytime->dow)
  {
    case SUNDAY   :  printf(lcd_putc, "Sun");  break;
    case MONDAY   :  printf(lcd_putc, "Mon");  break;
    case TUESDAY  :  printf(lcd_putc, "Tue");  break;
    case WEDNESDAY:  printf(lcd_putc, "Wed");  break;
    case THURSDAY :  printf(lcd_putc, "Thu");  break;
    case FRIDAY   :  printf(lcd_putc, "Fri");  break;
    default       :  printf(lcd_putc, "Sat");
  }
}
 
// a small function for buttons debounce
int1 debounce(int16 button)
{
  int8 count = 0;
  for(int8 i = 0; i < 5; i++)
  {
    if ( !input(button) )
      count++;
    delay_ms(10);
  }
 
  if(count > 2)  return 1;
  else           return 0;
}
 
void wait()
{
  set_timer0(0);
  while( (get_timer0() < 62500L) && (input(button1) || i >= 5) && input(button2) && (input(button3) || i < 5) ) ;
}
 
int8 edit(int8 x_pos, int8 y_pos, int8 parameter)
{
  if(i < 5)
    while( debounce(button1) );  // call debounce function (wait for B1 to be released)
  else
    while( debounce(button3) );  // call debounce function (wait for B3 to be released)
 
  lcd_gotoxy(x_pos, y_pos);  // move cursor to row y_pos, column x_pos
  while(TRUE)
  {
    while(!input(button2))
    {
      parameter++;
      if( (i == 0 || i == 5) && parameter > 23)   // if hours > 23 ==> hours = 0
        parameter = 0;
      if( (i == 1 || i == 6) && parameter > 59)   // if minutes > 59 ==> minutes = 0
        parameter = 0;
      if(i == 2 && parameter > 31)   // if day > 31 ==> day = 1
        parameter = 1;
      if(i == 3 && parameter > 12)   // if month > 12 ==> month = 1
        parameter = 1;
      if(i == 4 && parameter > 99)   // if year > 99 ==> year = 0
        parameter = 0;
 
      printf(lcd_putc, "%02u\b\b", parameter);
      delay_ms(200);
    }
 
    lcd_putc("  \b\b");
    wait();
    printf(lcd_putc, "%02u\b\b", parameter);
    wait();
 
    if( (!input(button1) && i < 5) || (!input(button3) && i >= 5) )
    {
      i++;                // increment 'i' for the next parameter
      return parameter;   // return parameter value and exit
    }
  }
}
 
void alarms_edit(int8 _alarm)
{
  int1 alarm_c;
  if(_alarm == 1) {
    lcd_gotoxy(38, 1);
    alarm_c = Alarm1_Status();
  }
  else {
    lcd_gotoxy(38, 2);
    alarm_c = Alarm2_Status();
  }
 
  while( debounce(button3) );  // call debounce function (wait for B3 to be released)
  while(TRUE)
  {
    while( !input(button2) )
    {
      alarm_c = !alarm_c;
      if(alarm_c)
        lcd_putc("ON \b\b\b");
      else
        lcd_putc("OFF\b\b\b");
      delay_ms(500);
    }
 
    lcd_putc("   \b\b\b");
    wait();
    if(alarm_c)  lcd_putc("ON \b\b\b");
    else         lcd_putc("OFF\b\b\b");
    wait();
 
    if( !input(button3) )
    {
      if(_alarm == 1)
      {
        if(alarm_c)  Alarm1_Enable();   // enable alarm1
        else         Alarm1_Disable();  // disable alarm1
      }
      else
      {
        if(alarm_c)  Alarm2_Enable();   // enable alarm2
        else         Alarm2_Disable();  // disable alarm2
      }
      return;
    }
  }
}

// main function
void main()
{
  setup_oscillator(OSC_8MHZ);  // set internal oscillator to 8MHz
  set_tris_b(0x0F);            // configure RB0~4 ins as inputs
  port_b_pullups(0x0E);        // enable RB1, RB2 & RB3 internal pull-ups
  enable_interrupts(GLOBAL);       // enable global interrupts
  enable_interrupts(INT_EXT_H2L);  // enable external interrupt (INT0) with edge from high to low
 
  delay_ms(1000);  // wait a second
  lcd_init();      // initialize LCD module
  IntSqw_Set(OUT_INT);  // DS3231 INT/SQW pin configuration (interrupt when alarm)
  Disable_32kHZ();      // disable DS3231 32kHz output
 
  while(TRUE)
  {
    // read current time and date
//-    mytime = RTC_Get();
    // print time
    lcd_gotoxy(1, 1);
    printf(lcd_putc, "%02u:%02u:%02u", mytime->hours, mytime->minutes, mytime->seconds);
    // print date
    dow_print();       // print week day
    lcd_gotoxy(5, 2);
    printf(lcd_putc, "%02u/%02u/20%02u", mytime->day, mytime->month, mytime->year);
 
    // read alarm1
//-    alarm1 = Alarm1_Get();
    // print alarm1
    lcd_gotoxy(21, 1);
    printf(lcd_putc, "A1: %02u:%02u:00", alarm1->hours, alarm1->minutes);
    lcd_gotoxy(38, 1);
    if( Alarm1_Status() )
      lcd_putc("ON ");
    else
      lcd_putc("OFF");
 
    // read alarm2
//-    alarm2 = Alarm2_Get();
    // print alarm2
    lcd_gotoxy(21, 2);
    printf(lcd_putc, "A2: %02u:%02u:00", alarm2->hours, alarm2->minutes);
    lcd_gotoxy(38, 2);
    if( Alarm2_Status() )
      lcd_putc("ON ");
    else
      lcd_putc("OFF");
 
    // read chip temperature
    signed int16 chip_temp = Get_Temperature();
    // print chip temperature
    lcd_gotoxy(11, 1);
    if (chip_temp < 0)  // if temperature is negative
        printf(lcd_putc, "T:-%02Lu.%02Lu%cC", abs(chip_temp) / 100, abs(chip_temp) % 100, 223);
    else
    printf(lcd_putc, "T: %02Lu.%02Lu%cC", chip_temp / 100, chip_temp % 100, 223);
 
    // if button B1 is pressed
    if( !input(button1) )
    if( debounce(button1) )        // call debounce function (make sure if B1 is pressed)
    {
      i = 0;
      setup_timer_0(T0_INTERNAL | T0_DIV_16);  // start Timer0 with internal clock & prescaler=16
 
      mytime->hours   = edit(1,  1, mytime->hours);    // edit hours
      mytime->minutes = edit(4,  1, mytime->minutes);  // edit minutes
      mytime->seconds = 0;                             // reset seconds
      while( debounce(button1) );  // call debounce function (wait for button B1 to be released)
 
      while(TRUE)
      {
        while( !input(button2) )  // if button B2 button is pressed
        {
          mytime->dow++;
          if(mytime->dow > 7) mytime->dow = 1;
          dow_print();     // print week day
          delay_ms(500);
        }
        lcd_gotoxy(1, 2);
        lcd_putc("   ");  // print 3 spaces
        wait();           // call wait() function
        dow_print();      // print week day
        wait();           // call wait() function
        if( !input(button1) )  // if button B1 is pressed
          break;
      }
 
      mytime->day     = edit(5,  2, mytime->day);    // edit day
      mytime->month   = edit(8,  2, mytime->month);  // edit month
      mytime->year    = edit(13, 2, mytime->year);   // edit year
 
      while( debounce(button1) );  // call debounce function (wait for button B1 to be released)
           // disable Timer0
     
      // write data to the RTC chip
//-      RTC_Set(mytime);
    }
 
    // if button B3 is pressed
    if( !input(button3) )
    if( debounce(button3) )  // call debounce function (make sure if B3 is pressed)
    {
      i = 5;
      setup_timer_0(T0_INTERNAL | T0_DIV_16);  // start Timer0 with internal clock & prescaler=16
 
      alarm1->hours   = edit(25,  1, alarm1->hours);    // edit alarm1 hours
      alarm1->minutes = edit(28,  1, alarm1->minutes);  // edit alarm1 minutes
      alarm1->seconds = 0;                              // reset alarm1 seconds
      Alarm1_Set(alarm1, HOURS_MINUTES_SECONDS_MATCH);  // alarm when hours, minutes and seconds match
      Alarm1_IF_Reset();  // reset alarm1 interrupt flag
      alarms_edit(1);     // edit alarm1 ON & OFF
 
      i = 5;
      alarm2->hours   = edit(25,  2, alarm2->hours);    // edit alarm2 hours
      alarm2->minutes = edit(28,  2, alarm2->minutes);  // edit alarm2 minutes
      Alarm2_Set(alarm2, HOURS_MINUTES_MATCH);          // alarm when hours and minutes match
      Alarm2_IF_Reset();  // reset alarm2 interrupt flag
      alarms_edit(2);     // edit alarm2 ON & OFF
 
      while( debounce(button3) );  // call debounce function (wait for button B3 to be released)
   
    }
 
    if( input(LED_PIN) && !input(button2) )
    {
      output_low(LED_PIN);
      if( Alarm1_IF_Check() )
      {
        Alarm1_IF_Reset();  // reset alarm1 interrupt flag
        Alarm1_Disable();   // disable alarm1
      }
      if( Alarm2_IF_Check() )
      {
        Alarm2_IF_Reset();  // reset alarm2 interrupt flag
        Alarm2_Disable();   // disable alarm2
      }
    }
 
    delay_ms(100);
  }
}
 
// end of code.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Apr 13, 2021 1:02 am     Reply with quote

Do a web search for:
Quote:
rtc 165:165:

Read all the hits and see if any of it might apply to your problem.
jake1272



Joined: 15 Mar 2021
Posts: 37

View user's profile Send private message

PostPosted: Tue Apr 13, 2021 1:58 am     Reply with quote

Hi Jerson, If I don't include I2C1 in #use I2C(MASTER, I2C1, SLOW = 100000, STREAM = DS3231_STREAM), I can edit date, time alarms. But with I2C1 included the LCD is blank.
Ttelmah



Joined: 11 Mar 2010
Posts: 19537

View user's profile Send private message

PostPosted: Tue Apr 13, 2021 2:48 am     Reply with quote

Er. On the 16F1847, the default pin location for I2C1, is pins B1 & B4,
which clashes with the button locations shown....
You are going to have to switch to using software I2C, or move some of
the other peripherals around.... Sad
Not surprised it doesn't work...
jake1272



Joined: 15 Mar 2021
Posts: 37

View user's profile Send private message

PostPosted: Tue Apr 13, 2021 3:55 am     Reply with quote

PCM programmer wrote:
Do a web search for:
Quote:
rtc 165:165:

Read all the hits and see if any of it might apply to your problem.


Thanks, PCM programmer here is what I have found:
The I2C interface is accessible whenever either VCC or VBAT is at a valid level. If a microcontroller connected to the DS3231 resets because of a loss of VCC or other event, it is possible that the microcontroller and DS3231 I2C communications could become unsynchronized, e.g., the microcontroller resets while reading data from the DS3231. When the microcontroller resets, the DS3231 I2C interface may be placed into a known state by toggling SCL until SDA is observed to be at a high level. At that point the microcontroller should pull SDA low while SCL is high, generating a START condition.

Some people claimed (Adruino forum) there might be a problem with the battery
jake1272



Joined: 15 Mar 2021
Posts: 37

View user's profile Send private message

PostPosted: Tue Apr 13, 2021 4:00 am     Reply with quote

Ttelmah wrote:
Er. On the 16F1847, the default pin location for I2C1 is pins B1 & B4,
which clashes with the button locations shown...
You are going to have to switch to using software I2C or move some of
the other peripherals around.... Sad
Not surprised it doesn't work...


I switch the pins, waiting for the magic to be operated but nothing happened Sad
Code:
#define LCD_RS_PIN      PIN_A2
#define LCD_RW_PIN      PIN_A3
#define LCD_ENABLE_PIN  PIN_A4
#define LCD_DATA4       PIN_A7
#define LCD_DATA5       PIN_A6
#define LCD_DATA6       PIN_A0
#define LCD_DATA7       PIN_A1
// end LCD module connections
 
// pin definitions
#define button1      PIN_B5
#define button2      PIN_B2
#define button3      PIN_B3
#define LED_PIN      PIN_A5
 
#include <16F1847.h>
#fuses  INTRC_IO ,NOMCLR,NOLVP,NOBROWNOUT,PUT,NOWDT
#use delay(internal = 8MHz)
#use I2C(MASTER,I2C1, SLOW = 100000, STREAM = DS3231_STREAM)
#include <lcd.c>     // include LCD driver source file
#include <DS3231.c>  // include DS3231 driver source file
 


Last edited by jake1272 on Tue Apr 13, 2021 4:00 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19537

View user's profile Send private message

PostPosted: Tue Apr 13, 2021 4:00 am     Reply with quote

That is the standard I2C 're-init' sequence.
Basically use the 'no init' option in #USE I2C.
Then at the start simply toggle the SCL line, till you see the data line is high.
Once this is done, init the I2C interface.
Jerson



Joined: 31 Jul 2009
Posts: 125
Location: Bombay, India

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

PostPosted: Tue Apr 13, 2021 4:26 am     Reply with quote

jake1272 wrote:
Hi Jerson, If I don't include I2C1 in #use I2C(MASTER, I2C1, SLOW = 100000, STREAM = DS3231_STREAM), I can edit date, time alarms. But with I2C1 included the LCD is blank


This indicates you're having trouble with the I2C communications with RTC
temtronic



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

View user's profile Send private message

PostPosted: Tue Apr 13, 2021 4:59 am     Reply with quote

FWIW...
I posted my 'basic DS3231 driver' code in the code library. While it doesn't do 'alarms', I know it works,at least on the 18F46k22, 26k22 devices. I modified DS1307 code( device address, some register settings).
It's my RTC for all projects as the 'RTC module' I buy also have a 24lc32 EEProm on it and for $2 I can't resist buying them.

If it works for you, then you know the current driver has a 'bug' in it somewhere.....

Jay
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, 4  Next
Page 3 of 4

 
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