Serial data question
Ok here goes i have a program that works ok right now but i know it could be alot better if i could figure this out, the program sends data to a instrument panel on a motorcycle, it is 8 bits no parity and 1 stop bit but right now im doing it all wrong i would like some incite on how to go about fixing it.
The program works right now but i have to manually go to a data table to get a different string each time a temperature reading changes and this means i have a huge data table in my code witch eats up valuable space.
Here is some of my code that works right now this is a small fraction of the data im sending out.
You will also notice that the data always has to add up to 512 in order for the dash to work if it 511 or 513 or anything other than 512 it wont work so i need to do a checksum ( i think) to make it work better.
The first bit in the data is for temperature control , so it changes all the time depending on the input from a sensor , and the second bit changes sometimes depending on the status ( ie. error codes) other wise it stays Hex 40 the rest of the bits can be anything as long as it adds up to 512 before it sends it out.
Any help would be appreciated.
The program works right now but i have to manually go to a data table to get a different string each time a temperature reading changes and this means i have a huge data table in my code witch eats up valuable space.
Here is some of my code that works right now this is a small fraction of the data im sending out.
You will also notice that the data always has to add up to 512 in order for the dash to work if it 511 or 513 or anything other than 512 it wont work so i need to do a checksum ( i think) to make it work better.
The first bit in the data is for temperature control , so it changes all the time depending on the input from a sensor , and the second bit changes sometimes depending on the status ( ie. error codes) other wise it stays Hex 40 the rest of the bits can be anything as long as it adds up to 512 before it sends it out.
Any help would be appreciated.
C0: DATA $C8,$40,$00, $00, $00, $00, $00, $F8,$00 C1: DATA $78,$40,$00, $00, $00, $00, $4A, $FE,$00 C2: DATA $6E,$40,$00, $00, $00, $00, $54, $FE,$00 C3: DATA $64,$40,$00, $00, $00, $00, $5E, $FE,$00 c4: DATA $4B,$40,$00, $00, $00, $00, $77, $FE,$00 C5: DATA $41,$40,$00, $00, $00, $00, $81, $FE,$0 'DATA $64, $F5, $00, $1, $00, $00, $B, $9B,$00 'DATA $64, $F5, $00, $2, $00, $00, $B, $9A,$00 'DATA $64, $F5, $00, $3, $00, $00, $B, $99,$00 'DATA $64, $FF, $00, $4, $00, $00, $B, $8E,$00 'DATA $64, $FF, $00, $5, $00, $00, $B, $8D,$00 'DATA $64, $FF, $00, $6, $00, $00, $B, $8C,$00 'DATA $64, $FF, $00, $7, $00, $00, $B, $8B,$00 'DATA $64, $FF, $00, $9, $00, $00, $B, $89,$00 'DATA $64, $FF, $00, $A, $00, $00, $B, $88,$00 'DATA $64, $FF, $00, $B, $00, $00, $B, $87,$00 'DATA $64, $FF, $00, $D, $00, $00, $B, $85,$00 'DATA $64, $FF, $00, $F, $00, $00, $B, $83,$00 'DATA $64, $FF, $00, $11, $00, $00, $B, $81,$0
Comments
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"The welfare of the people in particular has always been the alibi of tyrants." ~ Camus
www.iElectronicDesigns.com
·
Post Edited (JonnyMac) : 12/21/2008 6:06:30 PM GMT
Basicaly i take a reading from a temp sensor then depending on the reading i go find the "DATA" string that matches the position i need the gauge to read when all i really need to do is change the first bit in the string since the other 7 bits can stay the same now in order to do that i need a way for the program to automatically calculate the checksum of the data to go out.
This program just keeps sending the same string to th dash unless potval goes above 0 then it changes to another string.
Now in the real program i use there are MANY strings depending on the temp reading i get from the sensor so i have to keep changing the first bit and then recalculating the total bits to make sure the gauge accepts the string
[noparse][[/noparse]code] DEVICE SX48, oscxt2'SX28, OSChs1 , TURBO, STACKX, OPTIONX,
FREQ 20_000_000
'DEVICE SX28, OSCxt1 , TURBO, STACKX, OPTIONX, 'SX48, OSchs1
'FREQ 4_000_000
'DEVICE SX28, OSC4MHZ , TURBO, STACKX, OPTIONX, 'SX48, OSchs1
'FREQ 4_000_000
ID "SGI_V1.0"
'
' I/O Pins
'
TX PIN RE.4 OUTPUT
inpin PIN RA.2 INPUT CMOS
outpin PIN RA.1 OUTPUT
neutralin PIN RD.7 INPUT
FI PIN RC.3 INPUT PULLUP
Potpin PIN RA.0 INPUT
pot PIN re.6 input pullup
' 7 SEGMENT DISPLAY VARIABLES
A PIN RE.5 OUTPUT
B PIN RE.3 OUTPUT
C22 PIN RE.0 OUTPUT
D PIN RE.1 OUTPUT
E PIN RE.2 OUTPUT
F PIN RE.6 OUTPUT
G PIN RD.7 OUTPUT
' GEAR BASED BOOST CONTROL OUTPUTS
GB1 PIN RB.1 OUTPUT
GB2 PIN RB.2 OUTPUT
GB3 PIN RB.3 OUTPUT
GB4 PIN RB.4 OUTPUT
GB5 PIN RB.5 OUTPUT
GB6 PIN RB.6 OUTPUT
'
' Constants
'
Baud CON "T7812"
SAMPLES CON $07
LcdCC0 CON $F0
LcdCC1 CON $F1
LcdCC2 CON $F2
LcdCC3 CON $F3
LcdCC4 CON $F4
LcdCC5 CON $F5
LcdCC6 CON $F6
LcdCC7 CON $F7
LcdCC8 CON $F8 ' define custom char 0
LcdCC9 CON $F9 ' define custom char 1
LcdCC10 CON $FA ' define custom char 2
LcdCC11 CON $FB ' define custom char 3
LcdCC12 CON $FC ' define custom char 4
LcdCC13 CON $FD ' define custom char 5
LcdCC14 CON $FE ' define custom char 6
LcdCC15 CON $FF ' define custom char 7
'
' Variables
'
DATABUFFER VAR BYTE
AVG VAR BYTE
tmpW1 VAR Word
ADC VAR Byte ' adc in from clt sensor
idx1 VAR Byte ' loop control
temp1 VAR Byte ' subroutine work vars
temp2 VAR Byte
temp3 VAR Byte
temp4 VAR Byte
temp5 VAR Byte
NEW VAR BYTE
potval VAR Byte
neutral VAR Byte
' =========================================================================
PROGRAM Start
' =========================================================================
'
' Subroutine / Function Declarations
'
CLT_STEP SUB 2 ' define custom character
DELAY_MS SUB 1, 2 ' shell for PAUSE
CLT_OUT SUB 1, 2 ' send byte {+ count} to Dash
'
' Program Code
'
START:
High potpin
Pauseus 35
RCTIME Potpin, 1, potval, 3 '
PAUSE 0200
IF POTVAL = 0 THEN
clt_step c15
PAUSE 500
ENDIF
if POTVAL >0 THEN
CLT_STEP C7
ENDIF
GOTO start
'
' Subroutine / Function Code
'
' Use: DELAY_MS milliseconds
' -- shell for PAUSE
SUB DELAY_MS
IF __PARAMCNT = 1 THEN
tmpW1 = __PARAM1
ELSE
tmpW1 = __WPARAM12
ENDIF
PAUSE tmpW1
ENDSUB
'
' Use: CLT_OUT theByte {, count }
' -- sends "theByte" to the dash for data [noparse][[/noparse]optional] "count" times
' -- "count" defaults to 1 if not specified
CLT_OUT:
temp1 = __PARAM1 ' save the byte
IF __PARAMCNT = 9 THEN ' "count" specified?
temp2 = __PARAM2 ' yes, save
ELSE
temp2 = 1 ' no, set to 1
ENDIF
DO WHILE temp2 > 0
SEROUT Tx, Baud, temp1 ' transmit to LCD
DEC temp2
LOOP
RETURN
'
'
' Use: CLT_STEP CharDef
' -- loads custom character definition into DATA TO DASH FOR CLT GAUGE
' -- "CharDef" is DATA table that holds character number and definition
' bytes
CLT_STEP:
temp3 = __PARAM1 ' get definition offset
temp4 = __PARAM2 ' get definition base
FOR idx1 = 1 TO 8
READ temp4 + temp3, temp5 ' read a byte
CLT_OUT temp5
PAUSEUS 3700 ' send the byte
INC temp3 ' point to next
temp4 = temp4 + Z ' update base on overflow
NEXT
RETURN
'
' User Data
'
C0:
DATA $C8,$40,$00, $00, $00, $00, $00, $F8,$00
C1:
DATA $78,$40,$00, $00, $00, $00, $4A, $FE,$00
C2:
DATA $6E,$40,$00, $00, $00, $00, $54, $FE,$00
C3:
DATA $64,$40,$00, $00, $00, $00, $5E, $FE,$00 'CLEARS THE CLOCK FOR USE clt low
c4:
DATA $4B,$40,$00, $00, $00, $00, $77, $FE,$00 'CLEARS THE CLOCK FOR USE clt 1/8
C5:
DATA $41,$40,$00, $00, $00, $00, $81, $FE,$00
c6:
DATA $32,$40,$00, $00, $00, $00, $90, $FE,$00 'CLEARS THE CLOCK FOR USE clt 3/8
c7:
DATA $2D,$40,$00, $00, $00, $00, $95, $FE,$00 'CLEARS THE CLOCK FOR USE clt ALMOST MIDDLE
c8:
DATA $1E,$40,$00, $00, $00, $00, $A4, $FE,$00 'CLEARS THE CLOCK FOR USE clt middle
C9:
DATA $19,$40,$00, $00, $00, $00, $A9, $FE,$00
C10:
DATA $16,$40,$00, $00, $00, $00, $AC, $FE,$00
c11:
DATA $12,$40,$00, $00, $00, $00, $B0, $FE,$00 'CLEARS THE CLOCK FOR USE clt 5/8
c12:
DATA $11,$40,$00, $00, $00, $00, $B1, $FE,$00 'CLEARS THE CLOCK FOR USE clt 3/4
C13:
'DATA $A ,$40,$00, $00, $00, $00, $B8, $FE,$00 'CLEARS THE CLOCK FOR USE clt high light on
'DATA $F ,$40,$00, $00, $00, $00, $B3, $FE,$00 'CLEARS THE CLOCK FOR USE clt AT RED MARK
DATA $1E,$80, $00, $E4, $00, $00, $00, $7E,$00 'CLT FULL CLOCK BLINK FI LIGHT ON
c14:
DATA $A ,$40,$00, $00, $00, $00, $B8, $FE,$00 'CLEARS THE CLOCK FOR USE clt high light on
'DATA $1E, %11010000, $00, $00, $00, $00, $51, $C1,$00 'CLT LOW AND COO ACROSS DISPLAY
'DATA $F,$80, $00, $00, $00, $00, $73, $FE,$00 'CLT 1/8 CLOCK BLINK
c15:
DATA $64, $A, $00, $E5, $00, $00, $B, $A2,$00 'CLT LOW SETS THE FI ON THE DISPLAY AND BLINKS THE LIGHT
[noparse][[/noparse]code]
For example for DATA C11 the bit $12 will make my coolant gauge read 5/8, now on DATA C3 $64 will make my gauge read low , the second bit $40 clears error codes off a lcd in the dash it is the same through all my strings unless there is a error code to be displayed then it will change to whatever the error code that needs to be displayed is ( not totally necessary i dont have to have this) C15 you will see the second bit is $A this will set a error code on the lcd.
It never dawned on me till i looked at the example code from you Jonny how you calculated the check sum how to do it.
Anyway thanks for the great help !!!