Problem with DS1307 and dirs command

JSDL

Senior Member
I am building a digital clock using an HD44780 LCD display, Picaxe 20M2 and a ds1307 RTC. I got the display working in 4-bit mode thanks to the help of others on the forum. My question is I have connected the LCD to pins B.0 - B.3 on the 20M2 and everything is working fine.

I can send text and commands and it works perfectly. However, once I connected the DS1307 to the SDA (b.5) and SCL (b.7) pins on portB and I can not read anything from the RTC and display it. I am guessing this has something to do with the fact I am using the pinsB= command to send data/commands to the LCD and the RTC is connected to 2 of those pins. I used the dirsB=%01011111 to set all pins but SDA and SCL as outputs. I understood that issuing a pinsB=%value would not affect the 2 pins configured as inputs, but I get no reading. I'm not sure if this is a limitation and I have to re-design around this, or there is a fix. Here is my code. Any suggestions would be much appreciated. I have also added to two 4K7 pullup resistors on the SDA and SCL lines.

Code:
pause 100
hi2csetup i2cmaster, %11010000, i2cslow, i2cbyte

'###################Define Sysmbols HD44780 pins

symbol LCD_ENABLE=c.4		'Enable pin on HD44780 LCD module
symbol LCD_RS=c.3		'Register Select pin on HD77480, HIGH=Data, LOW=Command
dirsB=%01011111

symbol outbyte =b0
symbol counter = b1
symbol bitcount = b2
symbol nibblecount=b3

'Time Variables
symbol seconds = b4
symbol mins = b5
symbol hour = b6
symbol day = b7
symbol date = b8
symbol month = b9
symbol year = b10
symbol control = b11 'Square wave output

'Set initial values for date and time
let seconds = $00
let mins = $00
let hour = $12
let day = $01
let date = $01
let month = $12
let year = $16
let control = %00010000

hi2cout 0, (seconds, mins, hour, day, date, month, year, control)

LOW LCD_ENABLE
LOW LCD_RS

'############Initialization sequence for HD44780 Module#############

for counter = 0 to 6
	lookup counter ,($33, $32, $28, $10, $01, $06, $0E), outbyte	
	gosub WRITE_LCD_INST
	pause 10
next counter


'#####################################################################

outbyte=$80
gosub WRITE_LCD_INST

eeprom 0,("Time: ")
eeprom 12,("Date: ")

main:

hi2cin 0, (seconds, mins, hour, day, date, month, year, control)

bcdtoascii seconds, b20, b21
b20 = b20 - $30
b21 = b21 - $30
outbyte=b21    'Test to see output of second but appears blank
gosub WRITE_LCD_DATA




for counter = 0 to 5
	read counter,outbyte
	gosub WRITE_LCD_DATA
next counter

outbyte=$C0
gosub WRITE_LCD_INST


for counter = 12 to 16
	read counter,outbyte
	gosub WRITE_LCD_DATA
next counter



end



WRITE_LCD_INST:	'Write Commands to LCD

low LCD_RS
outbyte=outbyte * 256 | outbyte / 16

for nibblecount= 1 to 2
	pinsB=outbyte
	pulsout LCD_ENABLE,1
	pause 4
	outbyte=outbyte / 16	
next nibblecount
return


WRITE_LCD_DATA:	'Write Data to LCD

high LCD_RS
outbyte=outbyte * 256 | outbyte / 16

for nibblecount= 1 to 2
	pinsB=outbyte
	pulsout LCD_ENABLE,1
	pause 4
	outbyte=outbyte / 16	
next nibblecount
return
The only thing that appears in the LCD is Time: and Date:(below it), but a blank space for what should be the second
 

westaust55

Moderator
Try setting up an additional subroutine along the lines:
Copy the data to variable b0.
Then use the bit variables bit0 to bit3 to set the outputs B.0 to B.3 respectively high or low.

Such schemes has been discussed on the forum in the past.
Particularly useful when the output pins are not consecutive.
 

hippy

Technical Support
Staff member
As I understand it, enabling I2C will prevent the use of B.5 and B.7 for I/O; dirsB= and pinsB= will have no affect upon those pins. Because you are not using those pins to control the LCD then your LCD code should continue to work with LCD enabled, and I2C should continue to work even if using dirsB= and pinsB=.

It might be worth moving the dirsB= to before the HI2CSETUP just in case I am wrong about that one, but I am confident I am right about pinsB= being okay.

Perhaps there is something else which is preventing the DS1307 being read. When you say you get no reading; what does happen, what values are received ?

Does test code read the DS1307 correctly when you are not accessing the LCD, not using dirsB= and pinsB= ? If so you could add those to that test to confirm they do or do not affect access to the DS1307.
 

JSDL

Senior Member
Try setting up an additional subroutine along the lines:
Copy the data to variable b0.
Then use the bit variables bit0 to bit3 to set the outputs B.0 to B.3 respectively high or low.

Such schemes has been discussed on the forum in the past.
Particularly useful when the output pins are not consecutive.

Are you saying to forget about using the pinsB= to set the output state of the pins, and instead create another subroutine to manually set each individual bit? I would prefer to use the pinsB= to set the state if possible.
 
Last edited:

westaust55

Moderator
@JSDL,
See the information above that hippy has provided.

You indicate that the LCD is working but you cannot get the RTC to work at the same time.

Have you ever had the RTC working?
How is it connected:
1. What is the voltage for the supply?
2. Do you have a backup battery?
3. If no backup battery, is the battery pin pulled low to ground / 0 volts?
 

JSDL

Senior Member
I am running the entire thing in Picaxe VSM. Everything is connected properly as I am able to display text read from the 20M2 eeprom. When I just isolate, for example, the seconds byte, then convert to decimal, then store in outbyte and call the WRITE_LCD_DATA subroutine, I just get a blank space.
 

hippy

Technical Support
Staff member
Perhaps post your VSM .DSN file and the .BAS file then people can see exactly what you have and can check if they get the same results as you do.

The problem seems to be here -

bcdtoascii seconds, b20, b21
b20 = b20 - $30
b21 = b21 - $30
outbyte=b21 'Test to see output of second but appears blank
gosub WRITE_LCD_DATA

You are converting your ASCII digits to binary values 0 to 9, and sending those out. You should be sending out the ASCII digits ....

bcdtoascii seconds, b20, b21
outbyte=b21
gosub WRITE_LCD_DATA
 

JSDL

Senior Member
Hippy you once again solved the problem. Thank you both for your insights, I took the code from an older project I did with 7 segment displays, and forgot that I did not have to convert to binary to send out the 7-segment pattern for the corresponding decimal digit. Should have spotted that one :)

This is honestly one of the best forums for sharing knowledge in this area I have come across, which is one of the reasons I chose Picaxe!
 
Top