ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 1806
Скачиваний: 0
Experiment #7: Real-time Control and Data Logging
'Define RTC Constants 'Register values in the RTC
SecReg |
CON |
%00000 |
MinReg |
CON |
%00001 |
HrsReg |
CON |
%00010 |
CtrlReg |
CON |
%00111 |
BrstReg |
CON |
%11111 |
'Constant for BS2 Pin connections to RTC
RTC_CLK |
CON |
12 |
RTC_IO |
CON |
13 |
RTCReset |
CON |
14 |
'Real Time variables |
BYTE |
|
RTCCmd |
VAR |
|
RTemp |
VAR |
BYTE |
' Current Time |
Variables |
|
Time |
VAR |
WORD |
Hours |
VAR |
Time.HIGHBYTE |
Minutes |
VAR |
Time.LOWBYTE |
Seconds |
VAR |
BYTE |
' Log-Time variables |
WORD |
|
LTime |
VAR |
|
LHours |
VAR |
LTime.HIGHBYTE |
LMinutes |
VAR |
LTime.LOWBYTE |
LSeconds |
VAR |
BYTE |
' Start time variables |
WORD |
|
STime |
VAR |
|
SHours |
VAR |
STime.HIGHBYTE |
SMinutes |
VAR |
STime.LOWBYTE |
MemAddr VAR |
WORD |
|
' Define A/D constants & variables |
||
CS |
CON |
3 |
CLK |
CON |
4 |
Dout |
CON |
5 |
Datain |
VAR |
BYTE |
Temp |
VAR |
WORD |
TempSpan |
CON |
5000 |
Offset |
CON |
700 |
DIR15 = 0 |
||
DIR8 = 1 |
VAR |
IN15 |
PB |
||
LED |
CON |
8 |
LOW LED |
||
'***** Initialize *******
'Current memory location for storage
'0831 chip select active low from BS2 (P3)
'Clock pulse from BS2 (P4) to 0831
'Serial data output from 0831 to BS2 (P5)
'Variable to hold incoming number (0 to 255)
'Hold the converted value representing temp
'Full Scale input span (5000 = 50 degrees span)
'Minimum temp. Offset. (700 = 70 degrees)
'Pushbutton
'LED
'LED off
Industrial Control Version 1.1 •Page 207
Experiment #7: Real-time Control and Data Logging
INIT:
' To set the new time, hold down PB until LED goes off
DEBUG |
"Hold button now to initialize clock and logging",cr |
||
HIGH 8 |
2000 |
' |
LED On |
PAUSE |
' |
If PB not pressed, don’t store time or restart |
|
IF PB |
= 1 THEN SkipSet |
||
Seconds = $00 |
|||
GOSUB |
SetTime |
||
GOSUB |
RecoveryData |
||
DEBUG |
"Release button",CR |
||
SkipSet: |
' |
LED OFF |
|
LOW LED |
|||
IF PB |
= 0 THEN SkipSet |
' |
Wait for PB release |
READ 0,MemAddr.HIGHBYTE |
' |
Read recovery data of memory address and time |
|
READ 1,MemAddr.LOWBYTE |
|||
READ 2,SHours |
|||
Read 3,SMinutes |
|||
' Initialize time keeping variables |
' |
Read current time |
|
GOSUB |
ReadRTCBurst |
||
LHours = Hours |
' |
Set change hours to current |
|
LMinutes = Minutes |
' |
Set change minutes to current |
|
LSeconds = $00 |
' |
Set change seconds to 00 |
|
LMinutes = LMinutes + Interval |
' |
Add interval to get first log time |
|
GOSUB |
AdjustTime |
' |
Decimal adjust new time |
'***** Main Loop ******* |
|||
Main: |
500 |
||
PAUSE |
|||
GOSUB |
Control |
||
GOSUB |
Getdata |
||
GOSUB |
Calc_Temp |
||
GOSUB |
ReadRTCBurst |
||
GOSUB |
Display |
||
GOSUB |
TimeControl |
||
GOTO Main |
|||
Getdata: |
' |
Acquire conversion from 0831 |
|
LOW CS |
' |
Select the chip |
|
LOW CLK |
' |
Ready the clock line. |
|
SHIFTIN Dout, CLK, MSBPOST,[Datain\9] ' |
Shift in data |
||
HIGH CS |
' |
conversion |
|
RETURN |
|||
Calc_Temp: |
' |
Convert digital value to |
|
Temp = TempSpan/255 * Datain/10 + Offset ' temp based on Span & |
|||
RETURN |
' |
Offset variables. |
|
Control: |
= 0 THEN DumpData |
' |
If PB pressed, plot recorded data |
IF PB |
|||
Page 208 •Industrial Control Version 1.1
Experiment #7: Real-time Control and Data Logging |
|
RETURN |
|
TimeControl: |
' Check if time for reading |
IF (Time = LTime) AND (MemAddr-4 < Samples) THEN SaveData |
|
RETURN |
|
SaveData: |
' Write ADC reading to memory |
WRITE MemAddr, DataIn |
' Store data into EEPROM |
HIGH 8:PAUSE 250:LOW 8 |
' Blink LED |
MemAddr = MemAddr + 1 |
' Increment memory location for next reading |
WRITE 0,MemAddr.HIGHBYTE |
' Update recovery data |
WRITE 1,MemAddr.LOWBYTE |
|
LMinutes = LMinutes + Interval |
' Update for next interval |
IF MemAddr-4 < Samples THEN AdjustTime' If samples not full, continue |
|
IF Stop_Reset = 1 THEN Dont_Reset |
' If samples full, restart or end logging |
GOSUB RecoveryData |
|
Dont_Reset: |
|
AdjustTime: |
'Decimal adjust Time |
IF LSeconds.LOWNIB < $A THEN HighSec |
|
LSeconds = LSeconds + 6 |
|
HighSec: |
|
If LSeconds < $60 THEN LowMin |
|
LSeconds = LSeconds - $60 |
|
LMinutes = LMinutes + 1 |
|
LowMin: |
|
IF LMinutes.LOWNIB < $A THEN HighMin |
|
LMinutes = LMinutes + 6 |
|
HighMin: |
|
IF LMinutes < $60 THEN LowHours |
|
LMinutes = LMinutes - $60 |
|
LHours = LHours + 1 |
|
LowHours: |
|
IF LHours.LOWNIB < $A THEN HighHours |
|
LHours = LHours + 6 |
|
HighHours: |
|
IF LHours < $24 THEN AdjustDone |
|
LHours = LHours - $24 |
|
AdjustDone: |
|
RETURN |
|
Display: |
'Display real time and time for next log reading |
DEBUG "!USRS Time:", HEX2 hours,":",HEX2 minutes,":",HEX2 seconds
DEBUG " Sample Due:",HEX2 LHours,":",HEX2 LMinutes,":",HEX2 LSeconds
DEBUG " # ",DEC MemAddr-4, " Temp now = ", DEC Temp,CR
DEBUG DEC Temp,CR
Return
SetTime:
'****** Initialize the real time clock to start time RTemp = $10 : RTCCmd = CtrlReg : GOSUB WriteRTC
'Clear Write Protect bit in control register
Industrial Control Version 1.1 •Page 209
Experiment #7: Real-time Control and Data Logging
RTemp = |
Hours : |
RTCCmd = |
HrsReg : |
GOSUB WriteRTC ' |
Set initial hours |
RTemp = |
Minutes |
: RTCCmd |
= MinReg |
: GOSUB WriteRTC ' |
Set initial minutes |
RTemp = |
Seconds |
: RTCCmd |
= SecReg |
: GOSUB WriteRTC ' |
Set initial seconds |
RTemp = |
$80 : RTCCmd = CtrlReg : GOSUB WriteRTC |
||||
' Set write-protect bit in control register |
|||||
Return |
|||||
WriteRTC: |
' |
Write to DS1302 RTC |
|||
HIGH RTCReset
SHIFTOUT RTC_IO, RTC_CLK, LSBFIRST, [%0\1,RTCCmd\5,%10\2,RTemp]
LOW RTCReset
RETURN |
|||
ReadRTCBurst: |
' Read all data from RTC |
||
HIGH RTCReset |
|||
SHIFTOUT RTC_IO, RTC_CLK, LSBFIRST, [%1\1,BrstReg\5,%10\2] |
|||
SHIFTIN RTC_IO, RTC_CLK, LSBPRE, [Seconds,Minutes,Hours] |
|||
LOW RTCReset |
|||
RETURN |
|||
RecoveryData: |
' Stores data for recovery from restart |
||
MemAddr = 4 |
' Set starting location |
||
WRITE 0,MemAddr.HIGHBYTE |
' Write to EEPROM |
||
WRITE 1,MemAddr.LOWBYTE |
' Save start time in EEPROM |
||
WRITE 2,Hours |
|||
WRITE 3,Minutes |
|||
Return |
|||
'*** Download and siplay logged data *** |
|||
DumpData: |
|||
'Configure Plot |
' Allow buffer to clear |
||
PAUSE 500 |
|||
DEBUG "!RSET",CR |
' Reset plot to clear data |
||
DEBUG "!TITL Interval Data Logging",CR |
' Title the plot |
||
DEBUG "!PNTS 2000",CR |
' 2000 sample data points |
||
DEBUG "!TMAX ", DEC MemAddr/7+1,CR |
' Time based on number of samples |
||
DEBUG "!SPAN ",DEC offset/10,",",DEC (TempSpan/10 + Offset) / 10,CR |
|||
DEBUG "!AMUL .1",cr |
' Multiply data by 0.1 |
||
DEBUG "!CLMM",CR |
' Clear Min/Max |
||
DEBUG "!CLRM",CR |
' Clear messages |
||
DEBUG "!TSMP OFF",CR |
' Time Stamping off |
||
DEBUG "!SHFT ON",CR |
' Enable plot shift |
||
DEBUG "!DELM",CR |
' Delete message file |
||
DEBUG "!SAVM ON",CR |
' Save messages (logged data) to file |
||
DEBUG "!PLOT ON",CR |
' Start plotting |
||
PAUSE 500 |
' Reset plot to time 0 |
||
DEBUG "!RSET",CR |
|||
X |
VAR |
Word |
' Set log time = start time |
LTime = STime |
|||
DEBUG "Point,Time,Temperature",CR |
' message header |
||
Page 210 •Industrial Control Version 1.1