Shop OBEX P1 Docs P2 Docs Learn Events
Nju6355 clock/calendar subroutines — Parallax Forums

Nju6355 clock/calendar subroutines

ArchiverArchiver Posts: 46,084
edited 2001-03-30 03:56 in General Discussion
Here is some code to program a clock chip with the stamp. I have tried
and it seems to work. I have some of the clock chips, if anyone is
interested.

Regards

Leroy


'=====================================================================
' PIN ASSIGNMENTS, SYSTEM CONSTANTS, TEMPORARY VARIABLES
'=====================================================================
' The BS2's upper port, pins 8 through 15, is dedicated to the
' operation of the data-collection peripherals. The lower port,
' pins 0 through 7, is available for your application.

CLK con 15 ' Clock line for all serial peripherals.

DATA_ con 14 ' Data line for all serial peripherals.

NJU_CE con 11 ' Chip-enable for NJU6355 clock/calendar.

NJU_IO con 10 ' IO (read/write) for NJU6355; 1=write.

Shtdwn con 9 ' Shutdown for peripheral power supply; 1=off.
b96 con $54 ' Baudmode for 9600 bps.
timeout con 60000 ' At startup, wait 60 seconds for serial command.
pwrOn con $31 ' Initial state of OUTH at peripheral power-up.
' Turns on LP2951, but deselects all chips.
temp var byte ' Temporary variable used in several routines.


'=====================================================================
' SAMPLING CONSTANT(S)
'=====================================================================
smplSiz con 8 ' Each sample consists of 8 bytes--
' 4 of data (2 ADC results in 16-bit words)
' 3 of time (12 BCD digits for hh[noparse]:mm:[/noparse]ss)
' 1 checksum.

'=====================================================================
' NJU6355 CLOCK/CALENDAR CONSTANTS AND VARIABLES
'=====================================================================
' The NJU6355ED clock/calendar chip maintains a 13-digit BCD account
' of the current year, month, day, day of week, hour, minute, and
' second. The clock subroutines transfer this data to/from a 13-nibble
' array in the BS2's RAM called "DTG" for "date-time group." The
' constants below allow you to refer to the digits by name; e.g.,
' "Y10s" is the tens digit of the year. Note that there's no "am/pm"
' indicator--the NJU6355 uses the 24-hour clock. For instance, 2:00 pm
' is written or read as 14:00 (without the colon, of course).
Y10s con 1 ' Array position of year 10s digit.
Y1s con 0 ' " " " year 1s "
Mo10s con 3 ' " " " month 10s "
Mo1s con 2 ' " " " month 1s "
D10s con 5 ' " " " day 10s "
D1s con 4 ' " " " day 1s "
H10s con 8 ' " " " hour 10s "
H1s con 7 ' " " " hour 1s "
M10s con 10 ' " " " minute 10s "
M1s con 9 ' " " " minute 1s "
S10s con 12 ' " " " second 10s "
S1s con 11 ' " " " second 1s "
day con 6 ' " " " day-of-week (1-7) digit.
digit var nib ' Number of 4-bit BCD digits read/written.
DTG var nib(13) ' Array to hold "date/time group" BCD digits.


' Initial setup.
OUTH = pwrOn ' Get ready to turn on peripherals.
DIRH = $FF ' Set all bits of high port to output.
DIRL = $FF ' Set all bits of low port to output.

' setup =============
' When you apply power to the board, the BS2 will send a message at
' 9600 bps through the built-in serial port asking for setup
' instructions. If you don't press a key within timeout seconds, the
' program will begin logging data.

setup:

debug ? DTG(H10s), DTG(H1s),DTG(M10s), DTG(M1s), DTG(S10s), DTG(S1s)

' serout 16,b96,[noparse][[/noparse]CR,CR,"BS2 Data Logger"] ' Sign-on and choices.
' serout 16,b96,[noparse][[/noparse]CR,"Get (T)ime Set (C)lock",CR,">"]
' serin 16,b96,timeout,time,[noparse][[/noparse]temp] ' Look for instruction.

' If no instruction arrives within timeout milliseconds, then log data,
' else get choose from setup menu.
temp = temp & $0DF ' Convert to uppercase.
IF temp = "T" THEN time ' Show the current time/date.
IF temp = "C" THEN clock ' Set the clock/calendar.

gosub read_clock ' Update DTG data.

if DTG(S1s) = 1 then turn_on
if DTG(S1s) = 8 then turn_off


goto setup ' Unrecognized entry; try again.

' time =============
' Put the time and date from the NJU clock chip on the screen and
' go back to the setup routine.
time:
gosub read_clock ' Update DTG data.
gosub show_time ' Display the time.
gosub show_date ' Display the date.
goto setup ' Back to setup.

' clock =============
' Let the user set the current time and date via the set_clock
subroutine.
' This code just jury-rigs an IF ... THEN GOSUB ... instruction, which
' PBASIC lacks. When done, goes back to setup.
clock: ' Set the internal clock.
gosub set_clock
goto setup ' Return to setup for more instructions.

invalid_blank: ' Display message when blanks encountered.
serout 16,b96,[noparse][[/noparse]CR,"Stopped. End of data.",CR]
goto setup

invalid_sum: ' Display messgage when checksums don't match.
serout 16,b96,[noparse][[/noparse]CR,"Stopped. Invalid checksum.",CR]
goto setup

' ==================================================================
' NJU6355 CLOCK/CALENDAR SUBROUTINES
' ==================================================================

' read_clock =============
' Get the current date/time group from the NJU6355 clock and store
' it in the array DTG(n).
read_clock:
low NJU_IO ' Set for read.
high NJU_CE ' Select the chip.
for digit = 0 to 12 ' Get 13 digits.
shiftin DATA_,CLK,lsbpre,[noparse][[/noparse]DTG(digit)\4] ' Shift in a digit.
next ' Next digit.
low NJU_CE ' Deselect the chip.
return ' Return to program.

' write_clock =============
' Get the time stored in DTG(n) and write it to the NJU6355 clock.
' Note that the NJU6355 does not allow you to write the seconds digits.
' If clears the seconds digits when written, so if you set it for
' 08:30 (hh:mm), when the write is complete, the NJU6355 starts at
' 08:30:00 (hh[noparse]:mm:[/noparse]ss).
write_clock:
high NJU_IO ' Set for write.
high NJU_CE ' Select the chip.
for digit = 0 to 10 ' Write 11 digits.
shiftout DATA_,CLK,lsbfirst,[noparse][[/noparse]DTG(digit)\4] ' Shift out a digit.
next ' Next digit.
low NJU_CE ' Deselect the chip.
return ' Return to program.

' set_clock =============
' Set the clock/calendar based on data entered by the user at
' 9600 bps through the built-in serial-port connector.
set_clock:
serout 16,b96,[noparse][[/noparse]CR,"Year (YY): "]: gosub get2BCD ' Get year.
DTG(Y10s) = temp.highnib[noparse]:D[/noparse]TG(Y1s) = temp.lownib ' Store.
serout 16,b96,[noparse][[/noparse]CR,"Month (MM): "]: gosub get2BCD ' Get month.
DTG(Mo10s) = temp.highnib[noparse]:D[/noparse]TG(Mo1s) = temp.lownib ' Store.
serout 16,b96,[noparse][[/noparse]CR,"Day (DD): "]: gosub get2BCD ' Get day.
DTG(D10s) = temp.highnib[noparse]:D[/noparse]TG(D1s) = temp.lownib ' Store.
serout 16,b96,[noparse][[/noparse]CR,"DOW (1-7): "] ' Get day of wk.
serin 16,b96,[noparse][[/noparse]HEX1 temp]
DTG(day) = temp.lownib ' Store.
serout 16,b96,[noparse][[/noparse]CR,"Hour (HH): "]: gosub get2BCD ' Get year.
DTG(H10s) = temp.highnib[noparse]:D[/noparse]TG(H1s) = temp.lownib ' Store.
serout 16,b96,[noparse][[/noparse]CR,"Minute (MM): "]: gosub get2BCD ' Get month.
DTG(M10s) = temp.highnib[noparse]:D[/noparse]TG(M1s) = temp.lownib ' Store.
gosub write_clock
return

' show_date =============
' Display the date stored in DTG.
show_date:
serout 16,b96,[noparse][[/noparse]CR,HEX DTG(Mo10s), HEX DTG(Mo1s),"/",HEX DTG(D10s), HEX
DTG(D1s),"/",HEX DTG(Y10s), HEX DTG(Y1s)]
return

' show_time =============
' Display the time stored in DTG.

show_time:
serout 16,b96,[noparse][[/noparse]CR,HEX DTG(H10s), HEX DTG(H1s),":",HEX DTG(M10s), HEX
DTG(M1s),":",HEX DTG(S10s), HEX DTG(S1s)]
return

' get2BCD =============
' Get two BCD digits using the HEX2 modifier. Within the range 0-9,
' hex and BCD are the same, so this is useful for setting the clock
' in its BCD format.

get2BCD:
serin 16,b96,[noparse][[/noparse]HEX2 temp]
return

turn_off

low 8

goto setup

turn_on

high 8

goto setup
Sign In or Register to comment.