Hallo,
Hier mal ein Code für einen Spannungssensor mit einem Attiny85.
Da dieser keinen UART hat, und die Bascom softserial nur 8bit beherscht, mit eigenbaubasteluart für 9o2 :-)
Benötigt wird nur ein Attiny 85 und ein Festspannungsregler + Spannungsteiler.
Ausgabe der Spannung als EX Telemetrie und als Text Telemetrie.
Es ist auch kein Quarz notwendig. Der Sensor ist kleiner als ein 50cent stück
gruß Ralf
$regfile = "attiny85.dat"
Const Produkt_id_firstbyte = $1
Const Produkt_id_secondbyte = $a4
Const Device_id_firstbyte = $00
Const Device_id_secondbyte = 5
Const Poly = 7
Const Version1 = 1 '1'
Const Version2 = 1
Const Version3 = "a"
$crystal = 8000000
$hwstack = 30
$swstack = 30
$framesize = 24
'Const Teiler = 2
'Zelle 0% v*10
Const Zelleleer = 35.4
'Wert 1% Pro zelle "(42-Zelleleer) /100
Const 0prozent = 0.066
Const Urart_reload = 159
Const Kalibrierspannung = 280
Const Alarmspannung = 31
Dim Teiler As Single
Teiler = 1
Waitms 100
'Jeti
Dim Taster As Byte
'Taster = Osccal
'Osccal = Taster
Dim Text(32) As Byte
Dim Zahlenstartposition As Byte
Dim Alarmsendenbit As Bit
'Dim's Jeti Soft Uart
Dim Urart_sendebyte As Byte
Dim Urart_fertig As Bit
Urart_fertig = 0
Dim Parity As Bit
Dim Comand As Bit
Dim Urart_step As Byte
Urart_step = 0
Dim Urart_x As Byte
'Declaration JETI_Subs
Declare Sub Jeti_out
Declare Sub Jeti_in
Declare Sub Usart_cmd(byval A2 As Byte)
Declare Sub Usart_txt(byval A1 As Byte)
Dim Ex_byte(32) As Byte
Dim Crc As Byte
Dim Calc_temp As Byte
Dim Ex_bytes As Byte
Dim Calc_z As Byte
Dim Calc_y As Byte
Dim Buffersize As Byte
Dim Identifier As Byte
Dim Zahlentyp As Byte
Dim Ex_auswahl As Byte
Ex_auswahl = 0
Dim Lowbyte As Byte
Dim Highbyte As Byte
Dim Ex_steuern As Bit
Dim Alarm As Byte
'Timer für Software-Uart
Config Timer0 = Timer , Prescale = 8
Enable Timer0
On Timer0 Urart_isr
Enable Interrupts
Timer0 = Urart_reload
'Global
Dim X As Word
Dim Z As Word
Dim Temp As Word
Dim Y As Word
'Dim Summ As Single
Dim N As Single
Dim E As Single
Dim Wert As Word
Wert = 0
Dim Zahler As Byte
Zahler = 0
Dim Zellenanzahl As Byte
Dim Zellenerkannt As Bit
Zellenerkannt = 0
Dim Einprozent As Single
Dim Prozent As Byte
Config Adc = Single , Prescaler = Auto , Reference = Avcc
Dim Spannung As Word
Start Adc
Dim Teiler_e As Eram Single
Teiler = Teiler_e
Config Pinb.4 = Input
Teiler = Teiler_e
'Wait 2
Do
' spannungsmessung
X = Getadc(1)
'Spannung ausrechen
N = X
N = N / Teiler
E = Round(n)
Spannung = E
'zellenanzahl erkennen
If Spannung > 60 And Zellenerkannt = 0 Then
Zellenerkannt = 1
X = Spannung / 38
Zellenanzahl = X
End If
'einprozent/przentakku
Einprozent = 0prozent * Zellenanzahl
E = Zellenanzahl
N = Zelleleer * E
E = Spannung
If N > E Then
E = 0
Else
E = E - N
End If
E = E / Einprozent
N = Round(e)
Prozent = N
'Alarm
If Zellenerkannt = 1 Then
N = Spannung / Zellenanzahl
If N =< Alarmspannung Then Alarm = "Z"
If N > Alarmspannung Then Alarm = 0
End If
'Textausgabe
Zahlenstartposition = 1
Temp = Spannung
Gosub Zahlenanzeigenhundert
Text(4) = Text(3)
Text(3) = ","
Text(5) = "V"
Text(6) = " "
Temp = Prozent
Zahlenstartposition = 7
Gosub Zahlenanzeigenhundert
Text(10) = "%"
Text(11) = " "
Text(12) = Zellenanzahl + 48
Text(13) = "S"
Text(14) = " "
Text(15) = " "
Text(16) = " "
Text(17) = "V"
Text(18) = "e"
Text(19) = "r"
Text(20) = "s"
Text(21) = "i"
Text(22) = "o"
Text(23) = "n"
Text(24) = "1"
Text(25) = "."
Text(26) = "4"
Text(27) = "a"
Text(28) = " "
Text(29) = " "
Text(30) = " "
Text(31) = " "
Text(32) = " "
Waitms 100
Gosub Jeti_data
Loop
'*******************************************************************************
'--------------------------------Jeti-Telemetrie -------------------------------
'*******************************************************************************
Jeti_data:
Call Jeti_out
Incr Ex_auswahl
Ex_steuern = 0
If Ex_auswahl = 20 Then Gosub Identifier_0
If Ex_auswahl = 40 Then Gosub Identifier_1
If Ex_auswahl = 60 Then Gosub Identifier_2
'If Ex_auswahl = 80 Then Gosub Identifier_3
If Ex_auswahl = 220 Then Ex_auswahl = 0
If Ex_steuern = 0 Then Gosub Exdaten
Call Usart_cmd($7e)
If Alarm > 0 Then Toggle Alarmsendenbit
If Alarm > 0 And Alarmsendenbit = 1 Then
Call Usart_txt($92)
Call Usart_txt($23)
Call Usart_txt(alarm)
Goto Alarmende
End If
Call Usart_txt($9f)
Incr Ex_bytes
For X = 1 To Ex_bytes Step 1
Call Usart_txt(ex_byte(x))
Next
Alarmende:
Call Usart_cmd($fe)
For Z = 1 To 32
Call Usart_txt(text(z))
Next
Call Usart_cmd($ff)
Call Jeti_in
Return
Sub Jeti_out
Config Portb.5 = Output
End Sub
Sub Jeti_in
' Pinb.5 = 1
Config Pinb.5 = Input
' Pinb.5 = 1
Waitms 6
End Sub
Sub Usart_cmd(byval A2 As Byte) ' sendet Textzeichen
Parity = 1
Comand = 0
Urart_sendebyte = A2
Gosub Urart_calc_parity
Start Timer0
Do
If Urart_fertig = 1 Then
Urart_fertig = 0
Exit Do
End If
Loop
End Sub
Sub Usart_txt(byval A1 As Byte) ' sendet Steuerzeichen
Parity = 0
Comand = 1
Urart_sendebyte = A1
Gosub Urart_calc_parity
Start Timer0
Do
If Urart_fertig = 1 Then
Urart_fertig = 0
Exit Do
End If
Loop
End Sub
Urart_calc_parity:
For Urart_x = 0 To 7 Step 1
If Urart_sendebyte.urart_x = 1 Then Toggle Parity
Next Urart_x
Return
Urart_isr:
Timer0 = Urart_reload
'Start,0,1,2,3,4,5,9,7,C,P, Stopp
If Urart_step = 0 Then Portb.5 = 0
If Urart_step = 1 Then Portb.5 = Urart_sendebyte.0
If Urart_step = 2 Then Portb.5 = Urart_sendebyte.1
If Urart_step = 3 Then Portb.5 = Urart_sendebyte.2
If Urart_step = 4 Then Portb.5 = Urart_sendebyte.3
If Urart_step = 5 Then Portb.5 = Urart_sendebyte.4
If Urart_step = 6 Then Portb.5 = Urart_sendebyte.5
If Urart_step = 7 Then Portb.5 = Urart_sendebyte.6
If Urart_step = 8 Then Portb.5 = Urart_sendebyte.7
If Urart_step = 9 Then Portb.5 = Comand
If Urart_step = 10 Then Portb.5 = Parity
If Urart_step = 11 Then Portb.5 = 1
If Urart_step = 13 Then
Urart_fertig = 1
Urart_step = 0
Stop Timer0
Timer0 = Urart_reload
Else
Incr Urart_step
End If
Return
'*******************************************************************************
'----------------- Jeti-EX- Subs -----------------------------------------------
'*******************************************************************************
'Identifier = 1 Geschwindigkeit m/s eine Kommastelle
'Identifier = 2 Gesamtflugstrecke
'Identifier = 3 Anzahl Sat
'Identifier = 4 GPS FIX
'Identifier = 5 Mwc_altitude_baro eine Kommastelle
'Identifier = 6 Maxspeed
'Identifier = 7 Spannung
Exdaten:
Ex_bytes = 12
Ex_byte(1) = Ex_bytes
Ex_byte(1).6 = 1
Ex_byte(2) = Produkt_id_firstbyte
Ex_byte(3) = Produkt_id_secondbyte
Ex_byte(4) = Device_id_firstbyte
Ex_byte(5) = Device_id_secondbyte
Ex_byte(6) = 0
Identifier = 1
Zahlentyp = 1
Shift Identifier , Left , 4
Ex_byte(7) = Identifier + Zahlentyp
Lowbyte = 5
Highbyte = 0
Highbyte.7 = 0
Highbyte.6 = 0
Highbyte.5 = 1
Ex_byte(8) = Spannung
Ex_byte(9) = Highbyte
Identifier = 2
Zahlentyp = 1
Shift Identifier , Left , 4
Ex_byte(10) = Identifier + Zahlentyp
Highbyte = 0
'Highbyte.7 = 0
'Highbyte.6 = 0
'Highbyte.5 = 0
Ex_byte(11) = Prozent
Ex_byte(12) = 0
Gosub Calc_crc8
Ex_byte(13) = Crc
Return
'Sensorname
Identifier_0:
Ex_steuern = 1
'anzahl der EX-Bytes in der sub
Ex_bytes = 18
Ex_byte(1) = Ex_bytes
Ex_byte(2) = Produkt_id_firstbyte
Ex_byte(3) = Produkt_id_secondbyte
Ex_byte(4) = Device_id_firstbyte
Ex_byte(5) = Device_id_secondbyte
'Dummy immer 0
Ex_byte(6) = 0
'Indentifizierung ex Typ 0 = sensorname, 1 = name wert1 usw
Ex_byte(7) = 0
'Länge in bytes des sensornamens oder des wertes Bit 7-5 Länge des zeichens bit 0-2 (beim sensornamen 0)
X = 8
Shift X , Left , 3
Ex_byte(8) = X
'Sensorname
Ex_byte(9) = "V"
Ex_byte(10) = "o"
Ex_byte(11) = "l"
Ex_byte(12) = "t"
Ex_byte(13) = "m"
Ex_byte(14) = "a"
Ex_byte(15) = "s"
Ex_byte(16) = "t"
Ex_byte(17) = "e"
Ex_byte(18) = "r"
Gosub Calc_crc8
Ex_byte(19) = Crc
Return
'Name Wert 1 Spannung
Identifier_1:
Ex_steuern = 1
'anzahl der bytes bis vor crc
Ex_bytes = 17
Ex_byte(1) = Ex_bytes
Ex_byte(2) = Produkt_id_firstbyte
Ex_byte(3) = Produkt_id_secondbyte
Ex_byte(4) = Device_id_firstbyte
Ex_byte(5) = Device_id_secondbyte
Ex_byte(6) = 0
'Identifernummer
Ex_byte(7) = 1
'Anzahl Zeichen wertname
X = 8
'anzahl zeichen werteinheit
Y = 1
Shift X , Left , 3
Ex_byte(8) = X
Ex_byte(8) = Ex_byte(8) + Y
Ex_byte(9) = "S"
Ex_byte(10) = "p"
Ex_byte(11) = "a"
Ex_byte(12) = "n"
Ex_byte(13) = "n"
Ex_byte(14) = "u"
Ex_byte(15) = "n"
Ex_byte(16) = "g"
Ex_byte(17) = "V"
Gosub Calc_crc8
Ex_byte(18) = Crc
Return
'Name Wert 2 Distanz m
Identifier_2:
Ex_steuern = 1
'anzahl der bytes bis vor crc
Ex_bytes = 18
Ex_byte(1) = Ex_bytes
Ex_byte(2) = Produkt_id_firstbyte
Ex_byte(3) = Produkt_id_secondbyte
Ex_byte(4) = Device_id_firstbyte
Ex_byte(5) = Device_id_secondbyte
Ex_byte(6) = 0
'Identifernummer
Ex_byte(7) = 2
'Anzahl Zeichen wertname
X = 9
'anzahl zeichen werteinheit
Y = 1
Shift X , Left , 3
Ex_byte(8) = X
Ex_byte(8) = Ex_byte(8) + Y
Ex_byte(9) = "K"
Ex_byte(10) = "a"
Ex_byte(11) = "p"
Ex_byte(12) = "a"
Ex_byte(13) = "z"
Ex_byte(14) = "i"
Ex_byte(15) = "t"
Ex_byte(16) = "a"
Ex_byte(17) = "t"
Ex_byte(18) = "%"
Gosub Calc_crc8
Ex_byte(19) = Crc
Return
'*******************************************************************************
'----------------- CRC poly7 Prüfsumme-sub -------------------------------------
'*******************************************************************************
Calc_crc8:
Calc_z = 0
Crc = 0
Do
Calc_z = Calc_z + 1
Crc = Ex_byte(calc_z) Xor Crc
Calc_y = 0
Do
Calc_y = Calc_y + 1
Calc_temp = Crc And 128
If Calc_temp = 128 Then
Crc = Crc * 2
Crc = Crc Xor Poly
Else
Crc = Crc * 2
End If
Loop Until Calc_y = 8
Loop Until Calc_z = Ex_bytes
Return
Zahlenanzeigenhundert:
'temp = anzuzeigender wert (Max.999)
'zahlenstartposition = erste stelle (hunderter)
Z = Temp / 100
If Z = 0 Then
Text(zahlenstartposition) = " "
Else
Text(zahlenstartposition) = Z + 48
End If
X = Z * 100
Temp = Temp - X
Z = Temp / 10
If Z = 0 Then
If Text(zahlenstartposition) = " " Then
Text(zahlenstartposition + 1) = " "
Else
Text(zahlenstartposition + 1) = Z + 48
End If
Else
Text(zahlenstartposition + 1) = Z + 48
End If
X = Z * 10
Temp = Temp - X
Text(zahlenstartposition + 2) = Temp + 48
Return