Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python/Grafana Coding
#81
I should be able to test Tomorrow. Il get back with results. I have to fake the sensors but should not matter Smile
The Ultimate DIY Solar and build place
YouTube / Forum system setup / My webpage  Diy Tech & Repairs

Current: 10kW Mpp Hybrid | 4kW PIP4048 | 2x PCM60x | 83kWh LiFePo4 | 10kWh 14s 18650 |  66*260W Poly
Upcoming: 14S 18650~30kWh | Automatic trip breakers, and alot more
Reply
#82
This was my microcontroller code for handling the serial port from some code I wrote in 2001 for an ATmega323, 8bit 8Mhz, found today while sorting some folders out....

The hardware handled the bit buffering to a byte and then raised an interrupt for you to catch in code. Handing the incomming stream was byte by byte increasing a pointer for each byte.... only the same as reading bytes into an array with current higher level code you have.

This bit was handling a GPS input stream, lighting up an LED for each satellite visible and then outputting the data via the SPI port to the other controller. With this approach I was just reading for a fixed number of bytes to detect a recieved position packet/message and relying on the gap between messages to reset the counter.. so if you can catch the "silent" gaps between the incomming data you can also use this as a means of tracking the data packets.

Handling had to be byte by byte as there was litterally no other way... development system was basic to say the least in 2001.... 

Separate code to track the GPS pps signal to 1uS timing accuracy relative to a separate analog signal... with 8bit MCU at 8MHz. Cool

Code:
.cseg

;----------------------------------------------------------------------
ClearMemory:
push temp
push Xl
push Xh
clr zero
ldi Xl, low(MemStart)
ldi Xh, high(MemStart)
ldi temp, 0xff
ClearMemoryLoop1:
dec temp
st X+, zero
cpse zero, temp
rjmp ClearMemoryLoop1
ldi temp, 0xD0
ClearMemoryLoop2:
dec temp
st X+, zero
cpse zero, temp
rjmp ClearMemoryLoop2
pop Xh
pop Xl
pop temp
ret

;----------------------------------------------------------------------
SystemStartupLights:
push temp
ldi temp,  255
out DDRA, temp
out PORTA, temp
ldi temp, 0b00011111
out PORTB, temp
;Setup status lights and SPI port settings
ldi temp, 0b10111111    ;Set b0, b1, b2, b3 output, rest input
            ;b4=SS, b5=mosi, b6=miso, b7=sck
out DDRB, temp
pop temp
ret

;----------------------------------------------------------------------
BufferReset:
push temp
ldi temp, low(AddrRX)
sts AddrRX_PtrLr, temp
sts AddrRX_PtrLw, temp
ldi temp, high(AddrRX)
sts AddrRX_PtrHr, temp
sts AddrRX_PtrHw, temp
sts AddrRX_Bytes, zero
pop temp
ret

;----------------------------------------------------------------------
BufferRead:
push Zl                ;Push Z registers to stack
push Zh
lds Zl, AddrRX_PtrLr            ;Load pointer address
lds Zh, AddrRX_PtrHr
ld temp, Z+                ;Read byte, post increment
sts AddrRX_PtrLr, Zl            ;Save address
sts AddrRX_PtrHr, Zh            ;Save address
lds Zl, AddrRX_Bytes            ;Load and decrement byte count
dec Zl
sts AddrRX_Bytes, Zl            ;Save byte count
pop Zh                    ;Pop registers from stack
pop Zl
ret

;----------------------------------------------------------------------
BufferWrite:
push Zl                ;Push Z registers to stack
push Zh
push calc
lds Zl, AddrRX_PtrLw            ;Load pointer address
lds Zh, AddrRX_PtrHw            ;Load pointer address
ldi calc, 1
cpse calc, Zh                ;Check for buffer overun
rjmp BufWNext
ldi calc, 0xAE                ;0x1A0 max == 318 bytes
cpse calc, Zl
rjmp BufWNext
rcall BufferReset            
lds Zl, AddrRX_PtrLw            ;Load pointer address
lds Zh, AddrRX_PtrHw            ;Load pointer address
BufWNext:
st Z+, temp                ;Store byte, post increment
sts AddrRX_PtrLw, Zl            ;Save pointer address (low byte only)
sts AddrRX_PtrHw, Zh            ;Save pointer address (low byte only)
lds calc, AddrRX_Bytes            ;Load and increment byte count
inc calc
sts AddrRX_Bytes, calc
pop calc
pop Zh                    ;Pop registers from stack
pop Zl
ret

;----------------------------------------------------------------------
ReadGPSBitSet:
lsl temp
tst SatS
breq NoSatSignal
tst SatV
breq SatValid
eor temp, calc
rjmp SatExit
SatValid:
ori temp, 0x01
rjmp SatExit
NoSatSignal:
andi temp, 0xFE
SatExit:
ret


;----------------------------------------------------------------------
WriteSPITestData:
push temp
push calc
push Xl
push Xh

ldi calc, 78
ldi Xl, low(AddrRX)
ldi Xh, high(AddrRX)
WriteSPITestDataNext:
ld temp, X+
rcall SPIWrite
dec calc
clr temp
cpse temp, calc
rjmp WriteSPITestDataNext

pop Xh
pop Xl
pop calc
pop temp
ret

;----------------------------------------------------------------------
ExtractGPSDataToSPIBuffer:

push temp

;Fix type
lds temp, (AddrRX + 251)
rcall SPIWrite
lds temp, (AddrRX + 252)
rcall SPIWrite
 
;Signal strengths
lds temp, (AddrRX + 28)
rcall SPIWrite
lds temp, (AddrRX + 46)
rcall SPIWrite
lds temp, (AddrRX + 64)
rcall SPIWrite
lds temp, (AddrRX + 82)
rcall SPIWrite
lds temp, (AddrRX + 100)
rcall SPIWrite
lds temp, (AddrRX + 118)
rcall SPIWrite
lds temp, (AddrRX + 136)
rcall SPIWrite
lds temp, (AddrRX + 154)
rcall SPIWrite
lds temp, (AddrRX + 172)
rcall SPIWrite
lds temp, (AddrRX + 190)
rcall SPIWrite
lds temp, (AddrRX + 208)
rcall SPIWrite
lds temp, (AddrRX + 226)
rcall SPIWrite

;Satelite numbers
lds temp, (AddrRX + 29)
rcall SPIWrite
lds temp, (AddrRX + 47)
rcall SPIWrite
lds temp, (AddrRX + 65)
rcall SPIWrite
lds temp, (AddrRX + 83)
rcall SPIWrite
lds temp, (AddrRX + 101)
rcall SPIWrite
lds temp, (AddrRX + 119)
rcall SPIWrite
lds temp, (AddrRX + 137)
rcall SPIWrite
lds temp, (AddrRX + 155)
rcall SPIWrite
lds temp, (AddrRX + 173)
rcall SPIWrite
lds temp, (AddrRX + 191)
rcall SPIWrite
lds temp, (AddrRX + 209)
rcall SPIWrite
lds temp, (AddrRX + 227)
rcall SPIWrite

;Cycle Slip
lds temp, (AddrRX + 27)
rcall SPIWrite
lds temp, (AddrRX + 45)
rcall SPIWrite
lds temp, (AddrRX + 63)
rcall SPIWrite
lds temp, (AddrRX + 81)
rcall SPIWrite
lds temp, (AddrRX + 99)
rcall SPIWrite
lds temp, (AddrRX + 117)
rcall SPIWrite
lds temp, (AddrRX + 135)
rcall SPIWrite
lds temp, (AddrRX + 153)
rcall SPIWrite
lds temp, (AddrRX + 171)
rcall SPIWrite
lds temp, (AddrRX + 189)
rcall SPIWrite
lds temp, (AddrRX + 207)
rcall SPIWrite
lds temp, (AddrRX + 225)
rcall SPIWrite

;GPS Valid
lds temp, (AddrRX + 30)
rcall SPIWrite
lds temp, (AddrRX + 48)
rcall SPIWrite
lds temp, (AddrRX + 66)
rcall SPIWrite
lds temp, (AddrRX + 84)
rcall SPIWrite
lds temp, (AddrRX + 102)
rcall SPIWrite
lds temp, (AddrRX + 120)
rcall SPIWrite
lds temp, (AddrRX + 138)
rcall SPIWrite
lds temp, (AddrRX + 156)
rcall SPIWrite
lds temp, (AddrRX + 174)
rcall SPIWrite
lds temp, (AddrRX + 192)
rcall SPIWrite
lds temp, (AddrRX + 210)
rcall SPIWrite
lds temp, (AddrRX + 228)
rcall SPIWrite

;Time of week
lds temp, (AddrRX + 3)
rcall SPIWrite
lds temp, (AddrRX + 4)
rcall SPIWrite
lds temp, (AddrRX + 5)
rcall SPIWrite
lds temp, (AddrRX + 6)
rcall SPIWrite
lds temp, (AddrRX + 7)
rcall SPIWrite
lds temp, (AddrRX + 8)
rcall SPIWrite
lds temp, (AddrRX + 9)
rcall SPIWrite
lds temp, (AddrRX + 10)
rcall SPIWrite

;Longitude
lds temp, (AddrRX + 269)
rcall SPIWrite
lds temp, (AddrRX + 270)
rcall SPIWrite
lds temp, (AddrRX + 271)
rcall SPIWrite
lds temp, (AddrRX + 272)
rcall SPIWrite
lds temp, (AddrRX + 273)
rcall SPIWrite
lds temp, (AddrRX + 274)
rcall SPIWrite
lds temp, (AddrRX + 275)
rcall SPIWrite
lds temp, (AddrRX + 276)
rcall SPIWrite

;Latitude
lds temp, (AddrRX + 261)
rcall SPIWrite
lds temp, (AddrRX + 262)
rcall SPIWrite
lds temp, (AddrRX + 263)
rcall SPIWrite
lds temp, (AddrRX + 264)
rcall SPIWrite
lds temp, (AddrRX + 265)
rcall SPIWrite
lds temp, (AddrRX + 266)
rcall SPIWrite
lds temp, (AddrRX + 267)
rcall SPIWrite
lds temp, (AddrRX + 268)
rcall SPIWrite

;Altitude
lds temp, (AddrRX + 235)
rcall SPIWrite
lds temp, (AddrRX + 236)
rcall SPIWrite
lds temp, (AddrRX + 237)
rcall SPIWrite
lds temp, (AddrRX + 238)
rcall SPIWrite
 
pop temp

ret

;----------------------------------------------------------------------
SetLEDStatusIndicators:

push temp
push calc

ldi calc, 1
ldi temp, 0

lds SatS, (28  + AddrRX)        ;Sat 1 signal
lds SatV, (30  + AddrRX)        ;Sat valid
rcall ReadGPSBitSet

lds SatS, (46  + AddrRX)        ;Sat 2 signal
lds SatV, (48  + AddrRX)        ;Sat valid
rcall ReadGPSBitSet

lds SatS, (64  + AddrRX)        ;Sat 3 signal
lds SatV, (66  + AddrRX)        ;Sat valid
rcall ReadGPSBitSet

lds SatS, (82  + AddrRX)        ;Sat 4 signal
lds SatV, (84  + AddrRX)        ;Sat valid
rcall ReadGPSBitSet

lds SatS, (100 + AddrRX)        ;Sat 5 signal
lds SatV, (102 + AddrRX)        ;Sat valid
rcall ReadGPSBitSet

lds SatS, (118 + AddrRX)        ;Sat 6 signal
lds SatV, (120 + AddrRX)        ;Sat valid
rcall ReadGPSBitSet

lds SatS, (136 + AddrRX)        ;Sat 7 signal
lds SatV, (138 + AddrRX)        ;Sat valid
rcall ReadGPSBitSet

lds SatS, (154 + AddrRX)        ;Sat 8 signal
lds SatV, (156 + AddrRX)        ;Sat valid
rcall ReadGPSBitSet

out PORTA, temp
ldi temp, 0

lds SatS, (172 + AddrRX)        ;Sat 9 signal
lds SatV, (174 + AddrRX)        ;Sat valid
rcall ReadGPSBitSet

lds SatS, (190 + AddrRX)        ;Sat 10 signal
lds SatV, (192 + AddrRX)        ;Sat valid
rcall ReadGPSBitSet

lds SatS, (208 + AddrRX)        ;Sat 11 signal
lds SatV, (210 + AddrRX)        ;Sat valid
rcall ReadGPSBitSet

lds SatS, (226 + AddrRX)        ;Sat 12 signal
lds SatV, (228 + AddrRX)        ;Sat valid
rcall ReadGPSBitSet

in calc, PINB

sbrc temp, 0                ;Check for conflicts with SPI
sbr calc, bit0
sbrs temp, 0
cbr calc, bit0

sbrc temp, 1
sbr calc, bit1
sbrs temp, 1
cbr calc, bit1

sbrc temp, 2
sbr calc, bit2
sbrs temp, 2
cbr calc, bit2

sbrc temp, 3
sbr calc, bit3
sbrs temp, 3
cbr calc, bit3

out PORTB, calc

pop calc
pop temp

ret
DCkiwi likes this post
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)