|
|
View previous topic :: View next topic |
Author |
Message |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Apr 12, 2021 1:01 am |
|
|
I looked at the simple circuits page that has CCS code for the ds3231.
https://simple-circuit.com/ds3231-rtc-driver-ccs-c-compiler/
It shows the following main() function. Notice that he initializes the RTC
chip with the date and time. You cut that out.
Why not just copy his code exactly ?
Code: |
void main()
{
delay_ms(1000); // wait a second
lcd_init();
lcd_putc('\f');
// read current time and date from the RTC chip
mytime = RTC_Get();
// print them
rtc_print();
delay_ms(1000); // wait a second
// set RTC time to 21:08:47 (hh:mm:ss) and date to 03-01-19 (dd-mm-yy)
mytime->hours = 21;
mytime->minutes = 8;
mytime->seconds = 47;
mytime->dow = THURSDAY;
mytime->day = 3;
mytime->month = JANUARY;
mytime->year = 19;
// write time and date to the RTC chip
RTC_Set(mytime);
// enable SQW output with frequency of 1Hz
IntSqw_Set(OUT_1Hz);
while(TRUE)
{
// read current time and date from the RTC chip
mytime = RTC_Get();
// read chip temperature
chip_temp = Get_Temperature();
// print all data
rtc_print();
delay_ms(100); // wait 100 ms
}
} |
|
|
|
jake1272
Joined: 15 Mar 2021 Posts: 37
|
|
Posted: Mon Apr 12, 2021 1:01 am |
|
|
PCM programmer wrote: | I think you mean that you're using a module with a battery installed on
the back side of the board.
Is there a battery installed ? Can you measure the voltage of it ? |
Yes there is a battery installed on the back with 3.6V |
|
|
jake1272
Joined: 15 Mar 2021 Posts: 37
|
|
|
Jerson
Joined: 31 Jul 2009 Posts: 125 Location: Bombay, India
|
|
Posted: Mon Apr 12, 2021 4:24 am |
|
|
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. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Mon Apr 12, 2021 5:05 am |
|
|
a comment...
I do the lcd_init() THEN the delay_ms(1000).
This ensures the LCD module has more than enough time to do it's 'setup'.
Depending on who wrote the LCD driver, there may or may not be a long enough delay for the LCD module being used.
It also allows external peripherals time to properly 'powerup'. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Mon Apr 12, 2021 7:19 am |
|
|
Actually, delaying first is more normally what is needed....
Problem is that the standard Hitachi LCD controller carefully specifies that
it can accept commands just 15mSec after the supply comes on. However
a lot of the 'clone' LCD's need longer, and also many of the LCD's don't
actually 'start' this time, till the supply has risen further than the PIC needs
to start.
I suspect you use PCM Programmer's flex LCD driver. this allows 35mSec
for the display boot, so normally works. However the poster here is using the
CCS driver, and this often has problems unless a little extra time is allowed
before lcd_init is called. |
|
|
jake1272
Joined: 15 Mar 2021 Posts: 37
|
|
Posted: Mon Apr 12, 2021 7:53 am |
|
|
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
|
|
Posted: Mon Apr 12, 2021 8:14 am |
|
|
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
|
|
Posted: Mon Apr 12, 2021 8:19 am |
|
|
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
|
|
Posted: Mon Apr 12, 2021 8:25 am |
|
|
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
|
|
Posted: Mon Apr 12, 2021 4:27 pm |
|
|
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
|
|
Posted: Tue Apr 13, 2021 12:13 am |
|
|
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
|
Re: Problem with DS3231 and PIC16F1847 |
Posted: Tue Apr 13, 2021 12:55 am |
|
|
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
|
|
Posted: Tue Apr 13, 2021 1:02 am |
|
|
Do a web search for:
Read all the hits and see if any of it might apply to your problem. |
|
|
jake1272
Joined: 15 Mar 2021 Posts: 37
|
|
Posted: Tue Apr 13, 2021 1:58 am |
|
|
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. |
|
|
|
|
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
|