Code: Select all
{
*****************************************************************************
* Name : UNTITLED.BAS *
* Author : [select VIEW...EDITOR OPTIONS] *
* Notice : Copyright (c) 2023 [select VIEW...EDITOR OPTIONS] *
* : All Rights Reserved *
* Date : 2/5/2023 *
* Version : 1.0 *
* Notes : *
* : *
*****************************************************************************
FRAM MEMORY ADDRESSES ($0000 - $1FFF)
ALARM 0000 - 0003 - 1 = SECONDS, 2 = MINUITES, 3 = HOURS
ALARM VALUE 0004 - 0007
}
Module DS1394
#option SPI_SDO = PORTC.5
Include "USART.bas"
Include "SPI2xv.bas"
Include "convert.bas"
Include "system.bas"
Include "utils.bas"
Include "MEM_64K.BAS"
Private Dim
b1 As Byte,
b2 As Byte,
b3 As Byte,
b4 As Byte
Private Structure m_Date_Time
dt_seconds As Byte,
dt_minutes As Byte,
dt_hours As Byte,
dt_dow As Byte,
dt_date As Byte,
dt_month As Byte,
dt_year As Byte
End Structure
Private Structure m_Time
t_seconds As Byte,
t_minutes As Byte,
t_hours As Byte
End Structure
Private Structure m_Date
d_dow As Byte,
d_date As Byte,
d_month As Byte,
d_year As Byte
End Structure
Private Dim
red As PORTA.7,
green As PORTA.6,
mem_cs As PORTB.2,
rtc_cs As PORTB.3,
time As m_Time,
date As m_Date
Private Const
sec_10_100_read = $00,
sec_10_100_write = $80,
sec_read = $01,
sec_write = $81,
min_read = $02,
min_write = $82,
hour_read = $03,
hour_write = $83,
dow_read = $04,
dow_write = $84,
dom_read = $05,
dom_write = $85,
month_read = $06,
month_write = $86,
year_read = $07,
year_write = $87,
alm_10_100_read = $08,
alm_10_100_write = $88,
alm_sec_read = $09,
alm_sec_write = $89,
alm_min_read = $0A,
alm_min_write = $8A,
alm_hour_read = $0B,
alm_hour_write = $8B,
alm_dow_read = $0C,
alm_dow_write = $8C,
control_read = $0D,
control_write = $8D,
status_read = $0E,
status_write = $8E,
trickle_read = $0F,
trickel_write = $8F
'****************************************************************************
Public Sub RTC_SPI()
PPSLOCK.0 = 0
RC5PPS = $32 ' RC5 set to SPI1 SDO
PPSLOCK.0 = 1
SPI2.Open(SPI_MODE_0,spiOscDiv64,spiSampleEnd) '
DelayUS(5)
Low(rtc_cs)
End Sub
'****************************************************************************
Private Sub SET_TERM_SERIAL() ' Term emulator rs232
PPSLOCK.0 = 0
U1RXPPS = $17 ' USART1 RCV = RC6
RC6PPS = $20 ' RC7 = USART1 SND
PPSLOCK.0 = 1
USART.SetBaudrate(br9600) ' Baud rate
'USART.ReadTerminator = 13
End Sub
'****************************************************************************
Public Sub INIT_RTC()
RTC_SPI
SPI2.Transfer(status_write) '
SPI2.Transfer($00) ' Clear Osc and Alarm flags
High(rtc_cs)
RTC_SPI
SPI2.Transfer(control_write) ' Enable osc, int on batt, enable int, AIE disabled (enabled on alarm set)
SPI2.Transfer($24)
High(rtc_cs)
RTC_SPI
SPI2.Transfer(trickel_write)
' SPI2.Transfer($A5) ' No diode, 250 ohm resistor
SPI2.Transfer($A6) ' No diode, 2K resistor
' SPI2.Transfer($A7) ' No diode, 4K resistor
High(rtc_cs)
End Sub
'****************************************************************************
Public Function READ_SECONDS() As Byte
RTC_SPI
SPI2.Transfer(sec_read)
result = SPI2.Transfer
High(rtc_cs)
End Function
'****************************************************************************
Public Sub SET_SECONDS(pVal As Byte)
RTC_SPI
SPI2.Transfer(sec_10_100_write)
SPI2.Transfer($00) ' Set 10ths and 100ths seconds to 0
SPI2.Transfer(pVal)
High(rtc_cs)
READ_SECONDS
End Sub
'****************************************************************************
Public Function READ_MINUITES() As Byte
RTC_SPI
SPI2.Transfer(min_read)
result = SPI2.Transfer
High(rtc_cs)
End Function
'****************************************************************************
Public Sub SET_MINUITES(pVal As Byte)
RTC_SPI
SPI2.Transfer(min_write)
SPI2.Transfer(pVal)
High(rtc_cs)
READ_MINUITES
End Sub
'****************************************************************************
Public Function READ_HOUR() As Byte
RTC_SPI
SPI2.Transfer(hour_read)
result = SPI2.Transfer
High(rtc_cs)
End Function
'****************************************************************************
Public Sub SET_HOUR(pVal As Byte)
RTC_SPI
SPI2.Transfer(hour_write)
SPI2.Transfer(pVal)
High(rtc_cs)
READ_HOUR
End Sub
'****************************************************************************
Public Function READ_DOW() As Byte
RTC_SPI
SPI2.Transfer(dow_read)
result = SPI2.Transfer
High(rtc_cs)
End Function
'****************************************************************************
Public Sub SET_DOW(pVal As Byte)
RTC_SPI
SPI2.Transfer(dow_write)
SPI2.Transfer(pVal)
High(rtc_cs)
READ_DOW
End Sub
'****************************************************************************
Public Function READ_DOM() As Byte ' Day of the month
RTC_SPI
SPI2.Transfer(dom_read)
result = SPI2.Transfer
High(rtc_cs)
End Function
'****************************************************************************
Public Sub SET_DOM(pVal As Byte)
RTC_SPI
SPI2.Transfer(dom_write)
SPI2.Transfer(pVal)
High(rtc_cs)
READ_DOM
End Sub
'****************************************************************************
Public Function READ_MONTH() As Byte
RTC_SPI
SPI2.Transfer(month_read)
result = SPI2.Transfer
High(rtc_cs)
End Function
'****************************************************************************
Public Sub SET_MONTH(pVal As Byte)
RTC_SPI
SPI2.Transfer(month_write)
SPI2.Transfer(pVal)
High(rtc_cs)
READ_MONTH
End Sub
'****************************************************************************
Public Function READ_YEAR() As Byte
RTC_SPI
SPI2.Transfer(year_read)
result = SPI2.Transfer
High(rtc_cs)
End Function
'****************************************************************************
Public Sub SET_YEAR(pVal As Byte)
RTC_SPI
SPI2.Transfer(year_write)
SPI2.Transfer(pVal)
High(rtc_cs)
READ_YEAR
End Sub
'****************************************************************************
Public Sub SET_H_M_S(pHour As Byte,pMin As Byte,pSec As Byte)
SET_HOUR(pHour)
SET_MINUITES(pMin)
SET_SECONDS(pSec)
End Sub
'****************************************************************************
Public Sub SET_D_M_Y(pDate As Byte,pMonth As Byte,pYear As Byte)
SET_DOM(pDate)
SET_MONTH(pMonth)
SET_YEAR(pYear)
End Sub
'****************************************************************************
Public Sub SHOW_TIME()
SET_TERM_SERIAL
time.t_seconds = READ_SECONDS
time.t_minutes = READ_MINUITES
time.t_hours = READ_HOUR
USART.Write(DecToStr(HighNibble(time.t_hours)),DecToStr(LowNibble(time.t_hours)),":",DecToStr(HighNibble(time.t_minutes)),
DecToStr(LowNibble(time.t_minutes)),":",DecToStr(HighNibble(time.t_seconds)),DecToStr(LowNibble(time.t_seconds)),
" ")
DelayMS(2)
End Sub
'****************************************************************************
Public Sub SHOW_DATE()
SET_TERM_SERIAL
date.d_month = READ_MONTH
date.d_date = READ_DOM
date.d_year = READ_YEAR
USART.Write(DecToStr(HighNibble(date.d_month)),DecToStr(LowNibble(date.d_month)),"/",DecToStr(HighNibble(date.d_date)),
DecToStr(LowNibble(date.d_month)),"/",DecToStr(HighNibble(date.d_year)),DecToStr(LowNibble(date.d_year)),
" ")
DelayMS(2)
End Sub
'****************************************************************************
Public Sub SET_TIME()
USART.Write("ENTER HOUR IN 24 HOUR FORMAT",10,13)
DelayMS(2)
b1 = USART.ReadByte
b2 = USART.ReadByte
b2 = b2 - 48
b1 = ((b1 - 48) * 16) + b2
SET_HOUR(b1)
USART.Write("ENTER MINUITES",10,13)
DelayMS(2)
b1 = USART.ReadByte
b2 = USART.ReadByte
b2 = b2 - 48
b1 = ((b1 - 48) * 16) + b2
SET_MINUITES(b1)
USART.Write("SECONDS",10,13)
DelayMS(2)
b1 = USART.ReadByte
b2 = USART.ReadByte
b2 = b2 - 48
b1 = ((b1 - 48) * 16) + b2
SET_SECONDS(b1)
End Sub
'****************************************************************************
'****************************************************************************
Public Function READ_ALM_10_100() As Byte
RTC_SPI
SPI2.Transfer(alm_10_100_read)
result = SPI2.Transfer
High(rtc_cs)
End Function
'****************************************************************************
Public Sub SET_ALM_10_100(pVal As Byte)
RTC_SPI
SPI2.Transfer(alm_10_100_write)
SPI2.Transfer(pVal)
High(rtc_cs)
READ_ALM_10_100
End Sub
'****************************************************************************
Public Function READ_ALM_SEC() As Byte
RTC_SPI
SPI2.Transfer(alm_sec_read)
result = SPI2.Transfer
High(rtc_cs)
End Function
'****************************************************************************
Public Sub SET_ALM_SEC(pVal As Byte)
RTC_SPI
SPI2.Transfer(alm_sec_write)
SPI2.Transfer(pVal)
High(rtc_cs)
READ_ALM_SEC
End Sub
'****************************************************************************
Public Function READ_ALM_MIN() As Byte
RTC_SPI
SPI2.Transfer(alm_min_read)
result = SPI2.Transfer
High(rtc_cs)
End Function
'****************************************************************************
Public Sub SET_ALM_MIN(pVal As Byte)
RTC_SPI
SPI2.Transfer(alm_min_write)
SPI2.Transfer(pVal)
High(rtc_cs)
READ_ALM_MIN
End Sub
'****************************************************************************
Public Function READ_ALM_HOUR() As Byte
RTC_SPI
SPI2.Transfer(alm_hour_read)
result = SPI2.Transfer
High(rtc_cs)
End Function
'****************************************************************************
Public Sub SET_ALM_HOUR(pVal As Byte)
RTC_SPI
SPI2.Transfer(alm_hour_write)
SPI2.Transfer(pVal)
High(rtc_cs)
READ_ALM_HOUR
End Sub
'****************************************************************************
Public Function READ_ALM_DOW() As Byte
RTC_SPI
SPI2.Transfer(alm_dow_read)
result = SPI2.Transfer
High(rtc_cs)
End Function
'****************************************************************************
Public Sub SET_ALM_DOW(pVal As Byte)
pVal = pVal + $40 ' Make the register DOW instead of Date
RTC_SPI
SPI2.Transfer(alm_dow_write)
SPI2.Transfer(pVal)
High(rtc_cs)
READ_ALM_DOW
End Sub
'****************************************************************************
Public Sub CLEAR_OSC_FLAG()
RTC_SPI
SPI2.Transfer(status_read)
b1 = SPI2.Transfer
High(rtc_cs)
If b1.7 = 1 Then ' Osc flag set?
b1.7 = 0
EndIf
Low(rtc_cs)
SPI2.Transfer(status_write) ' Clear it
SPI2.Transfer(b1)
High(rtc_cs)
Low(rtc_cs)
SPI2.Transfer(status_read)
b1 = SPI2.Transfer
High(rtc_cs)
End Sub
'****************************************************************************
Public Sub CLEAR_ALM_FLAG()
RTC_SPI
SPI2.Transfer(status_read)
b1 = SPI2.Transfer
High(rtc_cs)
If b1.0 = 1 Then ' Alm flag set?
b1.0 = 0
EndIf
Low(rtc_cs)
SPI2.Transfer(status_write) ' Clear it
SPI2.Transfer(b1)
High(rtc_cs)
Low(rtc_cs)
SPI2.Transfer(status_read)
b1 = SPI2.Transfer
High(rtc_cs)
End Sub
'****************************************************************************
Public Function READ_ALM_Flag() As Boolean
RTC_SPI
SPI2.Transfer(status_read)
b1 = SPI2.Transfer
High(rtc_cs)
If b1.0 = 1 Then ' Alm flag set?
result = true
Else
result = false
EndIf
End Function
'****************************************************************************
Public Sub ENABLE_ALM()
CLEAR_ALM_FLAG
RTC_SPI
SPI2.Transfer(control_read)
b1 = SPI2.Transfer
High(rtc_cs)
' USART.Write("ENABLE ALARM = ",BinToStr(b1,8),". NEEDS TO BE 0010 0101.",10,13)
' DelayMS(2)
If b1.0 = 0 Then ' Enable ALM if need be
b1.0 = 1
Low(rtc_cs)
SPI2.Transfer(control_write)
SPI2.Transfer(b1)
High(rtc_cs)
EndIf
End Sub
'****************************************************************************
Public Sub DISABLE_ALM()
RTC_SPI
SPI2.Transfer(control_read)
b1 = SPI2.Transfer
High(rtc_cs)
' USART.Write("DISABLE ALARM = ",BinToStr(b1,8),". NEEDS TO BE 0010 0100.",10,13)
' DelayMS(2)
If b1.0 = 1 Then
b1.0 = 0
Low(rtc_cs)
SPI2.Transfer(control_write)
SPI2.Transfer(b1)
High(rtc_cs)
EndIf
End Sub
'****************************************************************************
Public Sub ALARM_ON_HOUR(pHr As Byte) ' Number of hours to delay
DISABLE_ALM
b1 = READ_ALM_DOW ' Set AM4 to 1
If b1.7 = 0 Then
b1.7 = 1
Low(rtc_cs)
SPI2.Transfer(alm_dow_write)
SPI2.Transfer(b1)
High(rtc_cs)
EndIf
SET_ALM_10_100($00) ' Set second registers to 0
SET_ALM_SEC($00)
SET_ALM_MIN($00) ' Set alarm minuites to 0
b1 = READ_ALM_HOUR '
b1 = b1 + pHr
If HighNibble(b1) > 2 Then
b1 = LowNibble(b1)
EndIf
SET_ALM_HOUR(b1) ' Set the hour alarm
ENABLE_ALM ' Start the timer
End Sub
'****************************************************************************
Public Sub ALARM_ON_MIN(pMin As Byte) ' Number of minuites to delay
DISABLE_ALM
b1 = READ_ALM_DOW ' Set AM4 to 1
If b1.7 = 0 Then
b1.7 = 1
Low(rtc_cs)
SPI2.Transfer(alm_dow_write)
SPI2.Transfer(b1)
High(rtc_cs)
EndIf
b1 = READ_ALM_HOUR ' Set AM3 to 1
If b1.7 = 0 Then
b1.7 = 1
Low(rtc_cs)
SPI2.Transfer(alm_hour_write)
SPI2.Transfer(b1)
High(rtc_cs)
EndIf
SET_ALM_10_100($00) ' Set second registers to 0
SET_ALM_SEC($00)
b1 = READ_MINUITES
' USART.write("CURRENT MINUITES - ",HexToStr(b1),10,13)
' DelayMS(1)
b1 = b1 + pMin
If LowNibble(b1) > 9 Then ' Adjust the minuites alarm reg
b1 = (b1 + $10) - 10
EndIf
If HighNibble(b1) > 5 Then
b1 = b1 - (HighNibble(b1) * $10)
EndIf
' USART.write("CALCULATED ALARM MINUITES - ",HexToStr(b1),10,13)
' DelayMS(1)
SET_ALM_MIN(b1)
ENABLE_ALM ' Start the timer
End Sub
'****************************************************************************
Public Sub ALARM_ON_SEC(pSec As Byte) ' Number of minuites to delay
DISABLE_ALM
b1 = READ_ALM_DOW ' Set AM4 to 1
If b1.7 = 0 Then
b1.7 = 1
Low(rtc_cs)
SPI2.Transfer(alm_dow_write)
SPI2.Transfer(b1)
High(rtc_cs)
EndIf
b1 = READ_ALM_HOUR ' Set AM3 to 1
If b1.7 = 0 Then
b1.7 = 1
Low(rtc_cs)
SPI2.Transfer(alm_hour_write)
SPI2.Transfer(b1)
High(rtc_cs)
EndIf
b1 = READ_ALM_MIN ' Set AM2 to 1
If b1.7 = 0 Then
b1.7 = 1
Low(rtc_cs)
SPI2.Transfer(alm_min_write)
SPI2.Transfer(b1)
High(rtc_cs)
EndIf
SET_ALM_10_100($00) ' Set second registers to 0
SET_ALM_SEC($00)
SET_ALM_MIN($00) ' Set alarm minuites to 0
' b1 = READ_MINUITES
' USART.write("CURRENT MINUITES - ",HexToStr(b1),10,13)
' DelayMS(1)
b1 = b1 + pSec
b1 = READ_ALM_SEC '
b1 = b1 + pSec
If LowNibble(b1) > 9 Then ' Adjust the minuites alarm reg
b1 = (b1 + $10) - 10
EndIf
If HighNibble(b1) > 5 Then
b1 = b1 - (HighNibble(b1) * $10)
EndIf
SET_ALM_SEC(b1) ' Set the second alarm
ENABLE_ALM ' Start the timer
End Sub
'****************************************************************************
Public Sub WAKE_RTC_ALARM()
INIT_RTC
b3 = MEM_64K.READ_BYTE($0000)
b4 = MEM_64K.READ_BYTE($0004)
' USART.write(10,13,"H M S - ",DecToStr(b3)," DELAY - ",DecToStr(b4),10,13)
' DelayMS(2)
Select b3
Case 1
ALARM_ON_SEC(b4)
Case 2
ALARM_ON_MIN(b4)
Case 3
ALARM_ON_HOUR(b4)
End Select
End Sub
'****************************************************************************
SET_TERM_SERIAL