PDA

View Full Version : Issue with single string character



Amoque
- 27th November 2014, 14:39
So, I have been working on a controller for my aquarium - to turn on lights, control temperature... The project progresses well and a fair number of the features work flawlessly; however a couple of unrelated issues - some PicBasic related and some not - continue to plague me. I'm hoping for some help...

First, I use a RTC1307 clock module to track time. Each five seconds and each one minute, routines run to update the display and to adjust various parameters. A part of the output is to display the time and date in the format: TH, NOV 27, 14. I store characters in DATA statements and, using the clock registers and READ statements, compose the required strings with the following code:

'-----SET CALENDAR
EXPAND_DATE:
READ 097 + (reg_val[5] * 3) + 0, CA_MONTH[0]
READ 097 + (reg_val[5] * 3) + 1, CA_MONTH[1]
READ 097 + (reg_val[5] * 3) + 2, CA_MONTH[2]
READ 137 + (reg_val[3] * 2) + 0, CA_DAY[0]
READ 137 + (reg_val[3] * 2) + 1, CA_DAY[1]
RETURN

These are the associated DATA statements:

DATA @ 100, "J", "A", "N", "F", "E", "B", "M", "A", "R", "A", "P", "R", "M", "A", "Y", "J", "U", "N", "J", "L", "Y", "A", "U", "G", "S", "E", "P", "O", "C", "T", "N", "O", "V", "D", "E", "C"
DATA @ 137, "S", "U", "M", "O", "T", "U", "W", "E", "T", "H", "F", "R", "S", "A", "S", "U"

And here is the code that puts it together on the 4X20 LCD operating in 4-bit mode:

'-----UPDATE DISPLAY
UPDATE_DISPLAY:
'-----LINE1
LCDOUT $FE, LINE1, CA_DAY[0], CA_DAY[1], ", ", STR CA_MONTH[0]\1, STR CA_MONTH[1]\1, STR CA_MONTH[2]\1, " ", dec2 REG_VAL[4]

REG_VAL[3] is the day of the week, REG_VAL[5] is the month and REG_VAL[4] is the 2 digit year. CA_DAY is defined as a byte array [2] and CA_MONTH as a byte array [3]; LINE1 a constant representing position 1 on the first display line.

Everything works as expected, EXCEPT that the fist day of the month, any month, is always munged - appearing as some unintelligible pattern of dots rather than the first day of the month. It is hard to demonstrate because the pattern of dots is not one defined on the keyboard, but similar to $OV - where "$" would be a scatter-shot of dots. Changing the month, it might be $AN or $EB... Always the first character of the month. The two character day appears as expected (even without the string designator) - added to the month without beneficial effect.

I've tried PAUSING between reads, reversing the order of the reads... no difference. I've verified the function of the LCD by moving the date to the second line - items on the first line display properly - the date, on the second line, remains munged in the same way as before.

Anyone see the issue?

richard
- 27th November 2014, 21:34
since the RTC1307 clock module gives its output data in bcd why not just
lcdout hex2 reg_val[3]

also if you need to use it as a binary/dec number for comparisons etc.
day = ((reg_val[3] >> 4) * 10) + (reg_val[3]$0f) ; bcd2bin conversion

lcdout dec2 reg_val[3]

richard
- 27th November 2014, 22:58
that last line was wrong should be


day = ((reg_val[3] >> 4) * 10) + (reg_val[3]$0f) ; bcd2bin conversion

lcdout dec2 day

must have been too early in the morning for me

Amoque
- 28th November 2014, 12:40
that last line was wrong should be


day = ((reg_val[3] >> 4) * 10) + (reg_val[3]$0f) ; bcd2bin conversion

lcdout dec2 day

must have been too early in the morning for me


It took me a moment... I'm blaming it on mashed potato poisoning - self inflicted, don't call the police.


In fact, I use a similar function to convert the calendar registers to decimal (and back) as part of each read and write routine. The problem comes in trying to use that decimal value to retrieve text characters. The math:

REG_VAL[5] = 1 in January... So:

READ 097 + (REG_VAL[5] * 3) + 0 = DATA position 100; CA_MONTH[0] = "J"
READ 097 + (REG_VAL[5] * 3) + 1 = DATA position 101; CA_MONTH[1] = "A"
READ 097 + (REG_VAL[5] * 3) + 2 = DATA position 102; CA_MONTH[2] = "N"

The problem... On the LCD the first character in munged - displays as "$AN", where $ represents some random pattern of dots. When REG_VAL[5] = 2, I get $EB; 3 = $AR... Indicating that the month is correctly decoded and the DATA located properly. As they say, "Everything is perfect, it just don't work".

The day (MO, TU, WE...) is decoded similarly from REG_VAL[3] (starting at DATA location 137) and displays as it should.

richard
- 28th November 2014, 22:00
looks like I got hold of the wrong end of the stick again


tried this


'************************************************* ***************
'* Name : UNTITLED.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 29/11/2014 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
#CONFIG
cfg1 = _FOSC_INTOSC
cfg1&= _WDTE_ON
cfg1&= _PWRTE_OFF
cfg1&= _MCLRE_ON
cfg1&= _CP_OFF
cfg1&= _CPD_OFF
cfg1&= _BOREN_ON
cfg1&= _CLKOUTEN_OFF
cfg1&= _IESO_ON
cfg1&= _FCMEN_ON
__CONFIG _CONFIG1, cfg1
cfg2 = _WRT_OFF
cfg2&= _PLLEN_OFF
cfg2&= _STVREN_ON
cfg2&= _BORV_19
cfg2&= _LVP_OFF
__CONFIG _CONFIG2, cfg2
#ENDCONFIG

DATA @ 100, "J", "A", "N", "F", "E", "B", "M", "A", "R", "A", "P", "R", "M", "A", "Y", "J", "U", "N", "J", "L", "Y", "A", "U", "G", "S", "E", "P", "O", "C", "T", "N", "O", "V", "D", "E", "C"
;DATA @ 137, "S", "U", "M", "O", "T", "U", "W", "E", "T", "H", "F", "R", "S", "A", "S", "U"
CA_MONTH var byte[3 ]
reg_val var byte
osccon=$6a '4 mhz
anselA=0


main:
for reg_val =1 to 12
gosub expand_date
Serout2 PORTA.0,84,["m",#reg_val ," ",str ca_month \3 ,13,10]
next
goto main
EXPAND_DATE:
READ 097 + (reg_val * 3) + 0, CA_MONTH[0]
READ 097 + (reg_val * 3) + 1, CA_MONTH[1]
READ 097 + (reg_val * 3) + 2, CA_MONTH[2]
RETURN

result




m1 JAN
m2 FEB
m3 MAR
m4 APR
m5 MAY
m6 JUN
m7 JLY
m8 AUG
m9 SEP
m10 OCT
m11 NOV
m12 DEC


so the method is sound . reg_val[5] is binary not BCD right ?
otherwise post entire code , I suspect an array overrun is most likely problem


ps another method using flash mem not eprom :- for more ideas
lookup pkt[3],["xmtwtfss"], day[0]
lookup pkt[3],["xouehrau"], day[1]
lookup pkt[3],["xnenuitn"], day[2]