'Empfänger Drehbake Rotor Version 2 'Autor: Michael Kinz 'http://www.team-iwan.de/technik/elektronik/drehbake2.php $crystal = 8000000 $regfile = "m8def.dat" $baud = 18150 'Ubrr = 28 'U2x = 0 On Urxc Rec_isr 'ISR Einsprung UART Enable Urxc 'UART einschalten Ucr.2 = 1 Config Timer0 = Timer , Prescale = 64 Config Timer1 = Timer , Prescale = 64 Config Timer2 = Timer , Prescale = 64 Enable Timer0 Enable Timer1 Enable Timer2 Config Int0 = Rising 'Hallsensor On Int0 Interrupt0 'Hallsensor On Ovf0 Tim0_ovfl 'für Ausgabe der seriellen Daten On Ovf1 Tim1_ovfl On Ovf2 Tim2_ovfl Enable Int0 Enable Interrupts 'blaue LED PortB1 OC1A Ddrb = &B00011111 Ddrd = &B11100000 Ledbl Alias Portb.1 Led1 Alias Portb.4 Led2 Alias Portb.3 Led3 Alias Portb.2 Led4 Alias Portb.0 Led5 Alias Portd.7 Led6 Alias Portd.6 Led7 Alias Portd.5 Dim Wartebyte As Byte Dim Tempw As Word Dim Ser_empfang As Word Dim Sicher As Word Dim Rechn As Word Dim Tempx As Word Dim Tempr As Word Dim Templ As Long Dim Umlaufzeit As Word Dim Serwinkel As Word Dim Winkel As Word Dim Pfeilwinkel As Word Dim Pfeilzeit As Long Dim Ovfl1_flag As Bit Dim Ser_flag As Bit Dim Anzeigeflag As Bit Dim Urxc_flag As Bit Dim Empf_flag As Bit Dim Pfeilflag As Byte Dim Stelle(5) As Byte Dim I As Byte Dim A As Byte Dim Flagser As Byte Wartebyte = 7 Do 'Berechnung der Dauer bis der Rotor am Übertragungspunkt 'für die Übermittlung der Daten zum Stator ankommt If Flagser = 0 Then 'wurde schon berechnet? Templ = Umlaufzeit * 88 Templ = Templ / 360 Serwinkel = Templ Sicher = Serwinkel 'Anzeigeflag = 0 Flagser = 1 End If 'Winkel = Ser_empfang * 360 / Umlaufzeit If Ser_flag = 1 Then 'Empfang war ok? Templ = Ser_empfang * 360 Templ = Templ / Umlaufzeit Templ = Templ + 140 If Templ > 359 Then Templ = Templ - 360 End If Winkel = Templ 'hier der Winkel in Grad Ser_flag = 0 'nächsten Empfang erlauben End If 'Zeitpunkt für den angezeigten Pfeil bestimmen If Pfeilflag = 0 Then 'Pfeil schon gezeichnet? ->nein Tempw = Umlaufzeit / 2 'Winkel für Pfeil in Timerticks berechnen Pfeilzeit = Tempw + Ser_empfang Pfeilzeit = Pfeilzeit - 768 If Pfeilzeit > Umlaufzeit Then Pfeilzeit = Pfeilzeit - Umlaufzeit End If Pfeilwinkel = Pfeilzeit Pfeilflag = 1 'Flag für Berechnung fertig End If 'Anzeigeflag = 0 If Anzeigeflag = 0 Then 'nur für Zahlenanzeige 'Gosub Stellenrechnen 'Gosub Stellenanzeige Anzeigeflag = 1 End If Loop End Interrupt0: 'Hallsensor-Interrupt bei 1 vollen Umdrehung Tempw = Timer1 'Timerwert = Umdrehungszeit sichern Timer1 = 0 'alle anderen löschen Timer0 = 0 Timer2 = 0 Flagser = 0 'für Datenübertragung zum Stator If Pfeilflag > 7 Then 'für LED-Pfeil Pfeilflag = 0 End If Anzeigeflag = 1 'Anzeige der Zahlen darf wieder losgehen If Urxc_flag = 0 Then 'seriellen Empfang in der letzten Umdrehung gehab? Winkel.15 = 1 'nein Else Winkel.15 = 0 End If Urxc_flag = 0 'löschen If Ovfl1_flag = 1 Then 'letzte Umdrehung vor Ablauf Timer1 zu Ende gebracht? Ovfl1_flag = 0 'nein Toggle Ledbl Else Umlaufzeit = Tempw 'ja, dann gilt Timerwert End If Return Tim0_ovfl: 'für die Ausgabe der seriellen Daten zum Stator If Flagser = 2 Then 'darf gesendet werden? Ledbl = 1 Printbin Winkel '2Byte-Winkelwert ausgeben Flagser = 3 'Wiederholung vermeiden End If Ledbl = 0 If Flagser = 1 Then 'Zeitpunkt der Übertragung abzählen If Serwinkel < 256 Then Timer0 = 256 - Serwinkel Flagser = 2 'Übertragung einleiten Else Serwinkel = Serwinkel - 256 End If End If Return Tim1_ovfl: 'keine volle Umdrehung geschafft innerhalb der Timer1-Zeit Anzeigeflag = 1 Ovfl1_flag = 1 'es war ein Overflow Winkel = 0 'Winkel-da-Flag (Bit15) löschen Return '-------------------------------------------- U R X C -------------------------- Rec_isr: Ledbl = 1 'blaue LED ein Tempr = Timer1 'Timerwert sichern If Ucr.1 = 0 Then 'Byte richtig empfangen? A = Udr 'dann auslesen Else A = Udr 'sonst auch auslesen, damit der UART wieder bereit ist A = 1 'mit definiertem Falschwert füllen End If If A = 132 Then 'Mein Byte? If Urxc_flag = 0 Then 'schon dagewesen = nein? Ser_empfang = Tempr 'Timerwert abspeichern Ser_flag = 1 Urxc_flag = 1 'schon-da-gewesen-Flag setzen Empf_flag = 1 'Erfolgreich-empfangen-Flag setzen End If End If Ledbl = 0 'blaue LED wieder aus Return '-------------------------------- Timer 2 ------------------------ Tim2_ovfl: Ledbl = 1 'blaue LED ein 'Pfeil zeichnen If Pfeilflag = 7 Then 'Pfeilzeichnen 6.Teil Led3 = 0 Pfeilflag = 8 End If If Pfeilflag = 6 Then 'Pfeilzeichnen 5.Teil Led2 = 0 Led3 = 1 Pfeilflag = 7 End If If Pfeilflag = 5 Then 'Pfeilzeichnen 4.Teil Led1 = 0 Led3 = 0 Led4 = 0 Led5 = 0 Led6 = 0 Led7 = 0 Pfeilflag = 6 End If If Pfeilflag = 4 Then 'Pfeilzeichnen 3.Teil Portb = Portb Or &B00011101 Portd = Portd Or &B11100000 Pfeilflag = 5 End If If Pfeilflag = 3 Then 'Pfeilzeichnen 2.Teil Led3 = 0 Led2 = 1 Pfeilflag = 4 End If If Pfeilflag = 2 Then 'Pfeilzeichnen 1.Teil Led3 = 1 Pfeilflag = 3 End If If Pfeilflag = 1 Then 'Pfeilwinkel fertig berechnet? ->ja If Pfeilwinkel < 256 Then Timer2 = 256 - Serwinkel 'den Rest vom Timer runterzählen Pfeilflag = 2 'Timerticks vorbei, jetzt Pfeil zeichnen Else Pfeilwinkel = Pfeilwinkel - 256 '8Bit-Timer Overflows runterzählen End If End If Ledbl = 0 Return Stellenrechnen: 'nur bei Ausgabe als Zahlenwert statt Pfeil Rechn = Sicher / 10000 Stelle(1) = Rechn Rechn = Stelle(1) * 10000 Sicher = Sicher - Rechn Rechn = Sicher / 1000 Stelle(2) = Rechn Rechn = Stelle(2) * 1000 Sicher = Sicher - Rechn Rechn = Sicher / 100 Stelle(3) = Rechn Rechn = Stelle(3) * 100 Sicher = Sicher - Rechn Rechn = Sicher / 10 Stelle(4) = Rechn Stelle(5) = Sicher Mod 10 Return Stellenanzeige: 'Für Ausgabe als 4-stellige Zahl For I = 5 To 1 Step -1 'I = 1 Select Case Stelle(i) Case 0 : Gosub Null Case 1 : Gosub Eins Case 2 : Gosub Zwei Case 3 : Gosub Drei Case 4 : Gosub Vier Case 5 : Gosub Fuenf Case 6 : Gosub Sechs Case 7 : Gosub Sieben Case 8 : Gosub Acht Case 9 : Gosub Neun End Select Next Return Null: Portb = Portb Or &B00001101 Portd = Portd Or &B11000000 Waitms Wartebyte Portb = Portb And &B11110010 Portd = Portd And &B00111111 Led1 = 1 Led7 = 1 Waitms Wartebyte Waitms Wartebyte Waitms Wartebyte Portb = Portb Or &B00001101 Portd = Portd Or &B11000000 Led1 = 0 Led7 = 0 Waitms Wartebyte Portb = Portb And &B11100010 Portd = Portd And &B00011111 Waitms Wartebyte Return Eins: Portb = Portb Or &B00011101 Portd = Portd Or &B11100000 Waitms Wartebyte Portb = Portb And &B11101010 Portd = Portd And &B00011111 Waitms Wartebyte Led2 = 0 Led3 = 1 Waitms Wartebyte Led3 = 0 Waitms Wartebyte Waitms Wartebyte Waitms Wartebyte Return Zwei: Led1 = 1 Led2 = 1 Led3 = 1 Led4 = 1 Led7 = 1 Waitms Wartebyte Led2 = 0 Led3 = 0 Waitms Wartebyte Waitms Wartebyte Waitms Wartebyte Led5 = 1 Led6 = 1 Waitms Wartebyte Portb = Portb And &B11100010 Portd = Portd And &B00011111 Waitms Wartebyte Return Drei: Portb = Portb Or &B00011101 Portd = Portd Or &B11100000 Waitms Wartebyte Led2 = 0 Led3 = 0 Led5 = 0 Led6 = 0 Waitms Wartebyte Waitms Wartebyte Waitms Wartebyte Waitms Wartebyte Portb = Portb And &B11100010 Portd = Portd And &B00011111 Waitms Wartebyte Return Vier: Portb = Portb Or &B00011101 Portd = Portd Or &B11100000 Waitms Wartebyte Portb = Portb And &B11100011 Portd = Portd And &B00011111 Waitms Wartebyte Waitms Wartebyte Waitms Wartebyte Led1 = 1 Led2 = 1 Led3 = 1 Waitms Wartebyte Portb = Portb And &B11100010 Portd = Portd And &B00011111 Waitms Wartebyte Return Fuenf: Portb = Portb Or &B00010001 Portd = Portd Or &B11100000 Waitms Wartebyte Led5 = 0 Led6 = 0 Waitms Wartebyte Waitms Wartebyte Waitms Wartebyte Led2 = 1 Led3 = 1 Waitms Wartebyte Portb = Portb And &B11100010 Portd = Portd And &B00011111 Waitms Wartebyte Return Sechs: Portb = Portb Or &B00010001 Portd = Portd Or &B11100000 Waitms Wartebyte Led5 = 0 Led6 = 0 Waitms Wartebyte Waitms Wartebyte Waitms Wartebyte Led2 = 1 Led3 = 1 Led5 = 1 Led6 = 1 Waitms Wartebyte Portb = Portb And &B11100010 Portd = Portd And &B00011111 Waitms Wartebyte Return Sieben: Portb = Portb Or &B00011101 Portd = Portd Or &B11100000 Waitms Wartebyte Portb = Portb And &B11110010 Portd = Portd And &B00011111 Waitms Wartebyte Waitms Wartebyte Waitms Wartebyte Waitms Wartebyte Portb = Portb And &B11100010 Portd = Portd And &B00011111 Waitms Wartebyte Return Acht: Portb = Portb Or &B00001100 Portd = Portd Or &B11000000 Waitms Wartebyte Led1 = 1 Led2 = 0 Led3 = 0 Led4 = 1 Led5 = 0 Led6 = 0 Led7 = 1 Waitms Wartebyte Waitms Wartebyte Waitms Wartebyte Led1 = 0 Led2 = 1 Led3 = 1 Led4 = 0 Led5 = 1 Led6 = 1 Led7 = 0 Waitms Wartebyte Portb = Portb And &B11100010 Portd = Portd And &B00011111 Waitms Wartebyte Return Neun: Portb = Portb Or &B00001101 Portd = Portd Or &B11000000 Waitms Wartebyte Led1 = 1 Led2 = 0 Led3 = 0 Led4 = 1 Led5 = 0 Led6 = 0 Led7 = 1 Waitms Wartebyte Waitms Wartebyte Waitms Wartebyte Led2 = 1 Led3 = 1 Led4 = 0 Led7 = 0 Waitms Wartebyte Portb = Portb And &B11100010 Portd = Portd And &B00011111 Waitms Wartebyte Return