PDA

View Full Version : RTC - code not working - Mel's example



malc-c
- 22nd March 2010, 22:15
Ok so now I've sussed out why the RTC module wasn't working, I've used Mel's code to test the RTC and have stumbled into a problem which for the life of me can't get working.

When powered up it displays the time on the 1st line and date on the second (incidentally it seems to skip the pre-sets in the code and use the date set in a previous bit of code used to test the module). The code responds to RA5 going low and jumps to set mode. However the LCD just sits there at Set Mode with the 12HR / 24HR overwriting itself every half a second. Pressing any of the buttons on PORT A has no affect, and the code doesn't jump back to normal clock mode if left for several seconds with out input.

Your help would be appreciated... Code attached :)

malc-c
- 22nd March 2010, 22:36
Sorted...

I'm gonna give up tonight....

I had the LEDs set to confirm the switches were working, and it must of been taking the port low enough to register as an input.... turning these off and it is now stable !

malc-c
- 22nd March 2010, 22:50
Maybe I spoke too soon...

I can now run through the set up, however setting it up for 10:45PM on 22nd March 2010, however pressing RA5 the display states "Memorised" but then displays the current time and date as : 4:46 AM Sat 28 Apr 2018 :confused:

malc-c
- 23rd March 2010, 10:55
Anyone have any ideas ?

Melanie
- 23rd March 2010, 12:08
If I have time in the next day or two I'll revisit this... but I'd have thought if there was a problem someone would have mentioned it in the eight years it's been around.

Despite the fact it's incorrect - is the Time incrementing?

Looks to me like the Data to/from the RTC is corrupt.

You got correct pull-up's on the CLK and Data lines to the RTC?

Download a fresh source copy and recompile... you may have changed something accidentally without noticing.

malc-c
- 23rd March 2010, 14:53
Mel, thank for replying.

Yes the clock runs fine. The module has the pull ups fitted
http://www.mcustore.com/acatalog/rtc2_150.jpg

I'll try your suggestion when I get home from work in a couple of hours. Maybe I did edit something I shouldn't have, but AFAIK the only changes were to portB for the LCD and port A for the switches

malc-c
- 23rd March 2010, 19:00
Download a fresh source copy and recompile... you may have changed something accidentally without noticing.

Mel,

I've downloaded a fresh copy of the code from http://www.melabs.com/resources/samples/submitted/MN1307.txt

The only things I've changed are making PORTA all input, PORTB output, the LCD to work on PORTB and the config setting s so it compiles on an 16F877A. - all the code that handles the setup of the clock is unchanged. I have all LEDS turned off so there is nothing else connected to either the RTC lines or switch inputs. RA4,5 and 6 are tied high via pull-up resistors, with switch's to gnd when pressed.

Code compiles and loads into PIC. time is shown on line 1 Hrs:min:sec, a date is shown on line two (which was the date set using a very basic example found on the web that simply writes values in a "string" to the device eg I2CWRITE SDA,SCL,$D0,$00,[$00,$00,$19,$7,$14,$2,$10,$90] )

All seems as it should, however pressing RA5 the display changes to "SET MODE :" with the 12 / 24 HR constantly over-writing itself. Pressing RA4, 5 or 6 has no effect, and if left doesn't auto switch back to display the clock.

Code attached

Any ideas ?

malc-c
- 23rd March 2010, 19:55
Well I compiled and loaded this example




@ __config _HS_OSC & _WDT_ON & _LVP_OFF & _CP_OFF

DEFINE OSC 20
CLEAR

;----[LCD definitions]------------------------------------------------------
DEFINE LCD_DREG PORTB ' LCD Data port
DEFINE LCD_DBIT 0 ' starting Data bit (0 or 4)
DEFINE LCD_EREG PORTB ' LCD Enable port
DEFINE LCD_EBIT 5 ' Enable bit (on EasyPIC 5 LCD)
DEFINE LCD_RSREG PORTB ' LCD Register Select port
DEFINE LCD_RSBIT 4 ' Register Select bit (on EasyPIC 5 LCD)
DEFINE LCD_BITS 4 ' LCD bus size (4 or 8 bits)
DEFINE LCD_LINES 4 ' number of lines on LCD
DEFINE LCD_COMMANDUS 2000 ' Command delay time in us
DEFINE LCD_DATAUS 50 ' Data delay time in us

'RTC pins on 877a
SDA Var PORTC.4
SCL Var PORTC.3

' Allocate variables
RTCYear Var Byte
RTCMonth Var Byte
RTCDate Var Byte
RTCDay Var Byte
RTCHour Var Byte
RTCMin Var Byte
RTCSec Var Byte
RTCCtrl Var Byte

TRISC= %11111111
;DB0 var byte[8]
CMCON = %00000111 ' Comparators = off

' Initialize LCD
LCDOUT $FE,1:FLAGS=0:PAUSE 250:LCDOUT $FE,1:PAUSE 250

' Set initial time
RTCYear = $10
RTCMonth = $03
RTCDate = $23
RTCDay = $02
RTCHour = $19
RTCMin = $35
RTCSec = 0
RTCCtrl = 0

Gosub set ' Set the time

Goto mainloop ' Skip over subroutines


' Subroutine to write time to RTC
set:
I2CWrite SDA,SCL,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCDay,RTCDate,RTCMonth,RTCY ear,RTCCtrl]
Return

' Subroutine to read time from RTC
gettime:
I2CRead SDA,SCL,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCDay,RTCDate,RTCMonth,RTCY ear,RTCCtrl]
Return

mainloop:
Gosub gettime ' Read the time from the RTC

' Display time on LCD
Lcdout $fe, 1,hex2 RTCDate, "/",hex2 RTCMonth, "/" , hex2 RTCYear,_
" ", hex2 RTCHour, ":", hex2 RTCMin, ":", hex2 RTCSec

Pause 500

Goto mainloop


The correct date and time as per code was shown on the LCD. I then recompiled the melrtc.pbp code and this time I was able to run through the set up menu, and kept the date (today's date) and after the "memorise" was displayed the following was displayed:

2:59:01 AM
Wed 25 sep 2013

I have no idea what's screwing the code up, and as you said, given the age of the file, would of though that this would of been discussed years ago !

chuck
- 23rd March 2010, 21:33
Malc I brought one of those for my easy-pic spent hours and over severals day's thinking it was my code. Which I knew it did work in another project, At the time I was unsure about the code but when I loaded the hex file into my running project everything worked ok. so this led me to the RTC moudle (the one shown in the picture), So I built the curcuit on vero board and that worked percfet, There was one connection I never used, So in the end atfer waisting days hunting I just threw it in the bin and could not be botherd to send it back. I did look at but not found nothing

Have you tried your own simple circuit ?

malc-c
- 23rd March 2010, 22:59
Thanks for the reply Chuck,

To be honest I've not tried breadboarding the chip like you suggest. I'll try that tomorrow and see if it makes any difference. Are you suggesting that there is a flaw with the EasyPIC5 board or just the RTC2 board... If the latter, why does it seem to work with other examples that write once to the module and then read the time back from that point... IMO it seems as if the code is doing strange things. I noticed that if I press the increment button it doesn't decrease by one unit, it seems to jump. Maybe the value on the LCD isn't the actual value that the counter is. EG, the hour displayed on the LCD hasn't refreshed to show the value the code has been set to by the button..

Darrel Taylor
- 24th March 2010, 00:05
Malc,

In the modified version of Melanie's code, you have ...


IncButton var PORTA.6 ' Press to Increment Button

The 16F877A doesn't have a PORTA.6, and to the program it probably looks like the button is always pressed.
<br>

malc-c
- 24th March 2010, 07:21
How did I miss that ! (embarrassed smile goes here !)

I'll re-compile tonight after work and see what happens

malc-c
- 24th March 2010, 19:29
Well must be something with the configuration of port A.



DecButton var PORTD.0 ' Press to Decrement Button
SetButton var PORTD.1 'Press to Set/memorise Button
IncButton var PORTD.2 ' Press to Increment Button


The above works ! as does



DecButton var PORTE.0 ' Press to Decrement Button
SetButton var PORTE.1 'Press to Set/memorise Button
IncButton var PORTE.2 ' Press to Increment Button


Thanks to all those who posted... at least I've proved my hardware is ok, just need to learn how to set up these devices correctly !

Darrel Taylor
- 24th March 2010, 19:33
Well must be something with the configuration of port A.
PORTA ... well, you have ...

ADCON1 = $0F

For a 16F877A it should be ...

ADCON1 = 7

Unless you want some analog channels.
<br>

malc-c
- 24th March 2010, 19:56
I'm looking at using the code to set a photo period, ie at a set time, say 11:00 pm lights go off, then at 06:00 am lights come on.

Is it a simple case of : (not actual code, more logic)



set off time
set on time

I2CRead SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCWDay,RTCDay,RTCMonth,RTCY ear,RTCCtrl]

If RTCHour=>off time or <=on time then lights off
Else lights on

malc-c
- 24th March 2010, 19:58
PORTA ... well, you have ...

ADCON1 = $0F

For a 16F877A it should be ...

ADCON1 = 7

Unless you want some analog channels.
<br>

I tried that :) well, ADCON1 = $0F but that didn't work... but then I'm currently using an 16f877a :o

malc-c
- 24th March 2010, 20:03
For a 16F877A it should be ...

ADCON1 = 7

Unless you want some analog channels.
<br>

Uhmmm.. tried that and then changed the code to



DecButton var PORTA.0 ' Press to Decrement Button
SetButton var PORTA.1 'Press to Set/memorise Button
IncButton var PORTA.2 ' Press to Increment Button


but that still left the 12/24 hr set mode over-writing each other again !

jellis00
- 24th March 2010, 20:32
Unless you want some analog channels.
<br>

Malc-C, since it doesn't appear you are using any analog channels in your application I recommend you use DT's "AllDigital.inc" routine as an INCLUDE in your program. It works great and takes care of all the register settings you are trying to figure out so that you don't have a conflict of your digital processing with ports that are set for analog input.

malc-c
- 24th March 2010, 20:40
Many thanks for the suggestion. At the moment I'm just verifying that the hardware works, and will (hopefully) be combining this code with some existing code for other functions, one of which has 4 pots on AN1, AN2, AN3 and AN4, so I'm really looking at setting up PORTA for a 50/50 split of analogue / digital when I come to migrating this all to a new PIC (18F4580) when my samples arrive :)