'---------------------------------------------------------- ' Programm initialisieren '---------------------------------------------------------- ' ' ACHTUNG JTAG per Fuse abschalten !!!! ' ' SPI RTC an MISO MOSI CS0 = PortC.7 CS1 = PortC.6 RW = PortC.5 ' Habe noch SRF04 Ultraschallenfernungssensor an PortC.2 und PortC.3 ' Ist nicht besonders elegant gecodet, aber kann ja je nach Bedarf ' eventbasierend individuell umgebaut werden. ' ' Nur zur Verdeutlicheung der Grundfunktionen der RTC-4553 Uhrenfunktion $regfile = "m16def.dat" $crystal = 8000000 $baud = 19200 Declare Function Ultrasonic() As Integer ' Funktionen deklarieren Declare Function Spitransfer(byval Outbyte As Byte ) As Byte Declare Sub Glcd_wdata(byval Outbyte As Byte) Declare Function Glcd_rdata(byval Outbyte As Byte)as Byte Declare Sub Glcd_busycheck Declare Sub Glcd_pixpos(byval Pixpos_x As Byte , Byval Pixpos_y As Byte , Byval Modus As Byte) Declare Sub Glcd_framepos(byval Pixpos_x As Byte , Byval Pixpos_y As Byte , Byval Framedata As Byte) Declare Sub Glcd_line(byval Sx As Byte , Byval Sy As Byte , Byval Ex As Byte , Byval Ey As Byte) Glcd_db0 Alias Portd.7 Glcd_pindb0 Alias Pind.7 Glcd_db1 Alias Porta.1 Glcd_pindb1 Alias Pina.1 Glcd_db2 Alias Porta.2 Glcd_pindb2 Alias Pina.2 Glcd_db3 Alias Porta.3 Glcd_pindb3 Alias Pina.3 Glcd_db4 Alias Porta.4 Glcd_pindb4 Alias Pina.4 Glcd_db5 Alias Porta.5 Glcd_pindb5 Alias Pina.5 Glcd_db6 Alias Porta.6 Glcd_pindb6 Alias Pina.6 Glcd_db7 Alias Porta.7 Glcd_pindb7 Alias Pina.7 Glcd_cs0 Alias Portb.0 Glcd_cs1 Alias Portb.1 Glcd_e Alias Portb.2 Glcd_rs Alias Portb.4 Glcd_res Alias Portb.3 Glcd_rw Alias Portd.3 Config Glcd_db0 = Output Config Glcd_db1 = Output Config Glcd_db2 = Output Config Glcd_db3 = Output Config Glcd_db4 = Output Config Glcd_db5 = Output Config Glcd_db6 = Output Config Glcd_db7 = Output Config Glcd_cs0 = Output Config Glcd_cs1 = Output Config Glcd_e = Output Config Glcd_rs = Output Config Glcd_res = Output Config Glcd_rw = Output Config Porta.0 = Input Config Adc = Single , Prescaler = Auto Start Adc Dim A As Byte ' variablen deklarieren Dim I As Byte Dim X As Byte Dim Temp As Word Dim Tempint As Integer Dim Tempstr As String * 16 Dim Sx As Byte , Sy As Byte , Ex As Byte , Ey As Byte , Xsingle As Single , Ysingle As Single Dim Framedata As Byte Dim Pixpos_x As Byte Dim Pixpos_y As Byte Dim Tmp As Byte Dim Outbyte As Byte Dim Modus As Byte Dim Y_alt As Byte Dim X_alt As Byte Dim Y_neu As Byte Dim X_neu As Byte Dim Y_alt2 As Byte Dim X_alt2 As Byte Dim Y_neu2 As Byte Dim X_neu2 As Byte X_alt = 1 Y_alt = 1 X_neu = 1 Y_neu = 1 Enable Interrupts Config Pinc.2 = Output Config Pinc.3 = Input Config Portc.5 = Output Config Portc.6 = Output Config Portc.7 = Output ' Konstanten deklarieren Const Setyear = &B00001011 Const Setmonth = &B00001001 Const Setday = &B00000111 Const Sethour = &B00000100 Const Setminute = &B00000010 Rtcwr Alias Portc.5 Rtccs1 Alias Portc.6 Rtccs0 Alias Portc.7 Rtccs1 = 1 Rtccs0 = 0 ' SPI konfigurieren und initialisieren Config Spi = Hard , Interrupt = Off , Data Order = Lsb , Master = Yes , Polarity = Low , Phase = 0 , Clockrate = 4 , Noss = 0 Spiinit ' RTC-4553 initialisieren Rtcwr = 0 X = Spitransfer(&B00001111) 'c3 (Initialisierung) X = Spitransfer(&B00011101) 'c1 (Initialisierung) Rtcwr = 1 Gosub Glcd_init Gosub Glcd_cls Call Glcd_line(1 , 1 , 126 , 62) Call Glcd_line(1 , 62 , 126 , 1 ) Call Glcd_line(20 , 20 , 106 , 42) Call Glcd_line(20 , 42 , 106 , 20) Wait 2 Gosub Glcd_cls Do 'Tempint = Ultrasonic() ' Entfernung per SRF04 messen 'Tempint = Tempint * 10 ' in cm umrechnen 'Tempint = Tempint / 62 'Print Tempint ; " " 'A = Tempint 'Shift A , Right , 1 'Print A X_neu = X_neu + 7 A = Getadc(0) Shift A , Right , 2 Call Glcd_line(x_alt , A , X_neu , Y_alt2 ) 'Enfernung ausgeben X_alt = X_neu Y_alt2 = A If X_neu > 125 Then X_neu2 = 1 X_alt2 = 1 X_neu = 1 X_alt = 1 waitms 200 Gosub Glcd_cls End If Loop End Rtc_auslesen: Tempstr = "" For A = &B00001100 To 0 Step -1 ' absteigende Reihenfolge X = Spitransfer(a) ' sena Anfrage X = Spitransfer(0) ' empfange Antwort X = X And &B11110000 Shift X , Right , 4 Tempstr = Tempstr + Str(x) ' Zahlenwert in String umwandeln und an String anreihen Next A I = Inkey() ' UART abfragen Select Case I ' Je nach Zeichen Funktion auslösen Case &H79: ' wenn y Rtcwr = 0 X = Spitransfer(setyear) Rtcwr = 1 Case &H6D: ' wenn m Rtcwr = 0 X = Spitransfer(setmonth) Rtcwr = 1 Case &H64: ' wenn d Rtcwr = 0 X = Spitransfer(setday) Rtcwr = 1 Case &H68: ' wenn h Rtcwr = 0 X = Spitransfer(sethour) Rtcwr = 1 Case &H69: ' wenn i Rtcwr = 0 X = Spitransfer(setminute) Rtcwr = 1 End Select Waitms 100 Print Tempstr ; " "; ' Ausgabe Datum und Uhrzeitstring Return ' -------------------------------------------------------------------- Function Ultrasonic() As Integer Portc.2 = 0 Pulseout Portc , 2 , 20 Pulsein Ultrasonic , Pinc , 3 , 1 End Function Function Spitransfer(byval Outbyte As Byte) As Byte Spdr = Outbyte ' SPI-Ausgabe Do Loop Until Spsr.spif = 1 ' Warten bis Byte raus ist Spitransfer = Spdr ' Rückgabewert auslesen End Function End ' -------------------------------------------------------------------- Sub Glcd_pixpos(byval Pixpos_x As Byte , Byval Pixpos_y As Byte , Modus As Byte) Local Templocal As Integer If Pixpos_y < 62 Then If Pixpos_x < 126 Then Glcd_rs = 1 Glcd_rw = 0 If Modus = 1 Then Tmp = Pixpos_y Shift Tmp , Right , 3 ' pixel im Frame setzen Shift Tmp , Left , 3 Pixpos_y = Pixpos_y - Tmp Tmp = 0 Tmp.pixpos_y = 1 Tmp = Framedata Or Tmp Call Glcd_wdata(tmp) ' pixel schreiben Else Call Glcd_wdata(0) ' frame löschen End If Glcd_rs = 0 End If End If End Sub '------------------------------------------------------------------ Sub Glcd_framepos(byval Pixpos_x As Byte , Byval Pixpos_y As Byte , Byref Framedata As Byte) Local Tempbyte As Byte If Pixpos_y < 62 Then If Pixpos_x < 126 Then Glcd_cs0 = 0 Glcd_cs1 = 0 Glcd_rs = 0 Glcd_rw = 0 If Pixpos_x > 61 Then ' chip auswählen Glcd_cs1 = 1 Pixpos_x = Pixpos_x And &B00111111 Else Glcd_cs0 = 1 Pixpos_x = Pixpos_x And &B00111111 End If Tmp = Pixpos_x Tmp = Tmp And &B00111111 Tmp = Tmp Or &B01000000 Call Glcd_wdata(tmp) ' x-Adresse setzen Shift Pixpos_y , Right , 3 Tmp = Pixpos_y And &B00011111 '/ 8 Tmp = Tmp Or &B10111000 ' frame setzen für Abfrage Call Glcd_wdata(tmp) Glcd_rs = 1 Glcd_rw = 1 Framedata = Glcd_rdata(0) ' Frame auslesen Glcd_rw = 0 Glcd_rs = 0 Call Glcd_wdata(tmp) End If End If End Sub End ' -------------------------------------------------------------------- Sub Glcd_wdata(byval Outbyte As Byte) nop nop Glcd_e = 0 Glcd_db0 = Outbyte.0 Glcd_db1 = Outbyte.1 Glcd_db2 = Outbyte.2 Glcd_db3 = Outbyte.3 Glcd_db4 = Outbyte.4 Glcd_db5 = Outbyte.5 Glcd_db6 = Outbyte.6 Glcd_db7 = Outbyte.7 nop nop Glcd_e = 1 nop nop nop Glcd_e = 0 End Sub End ' -------------------------------------------------------------------- Function Glcd_rdata(byval Outbyte As Byte)as Byte Config Glcd_db0 = Input Config Glcd_db1 = Input Config Glcd_db2 = Input Config Glcd_db3 = Input Config Glcd_db4 = Input Config Glcd_db5 = Input Config Glcd_db6 = Input Config Glcd_db7 = Input Glcd_e = 0 nop nop nop Glcd_e = 1 'NOP nop nop nop Glcd_e = 0 nop nop nop Glcd_e = 1 nop nop nop Outbyte.0 = Glcd_pindb0 Outbyte.1 = Glcd_pindb1 Outbyte.2 = Glcd_pindb2 Outbyte.3 = Glcd_pindb3 Outbyte.4 = Glcd_pindb4 Outbyte.5 = Glcd_pindb5 Outbyte.6 = Glcd_pindb6 Outbyte.7 = Glcd_pindb7 Glcd_rdata = Outbyte Framedata = Outbyte NOP NOP nop Config Glcd_db0 = Output Config Glcd_db1 = Output Config Glcd_db2 = Output Config Glcd_db3 = Output Config Glcd_db4 = Output Config Glcd_db5 = Output Config Glcd_db6 = Output Config Glcd_db7 = Output nop Glcd_e = 0 End Function End ' -------------------------------------------------------------------- Glcd_init: Glcd_res = 1 Waitms 500 Glcd_res = 0 Waitms 500 Glcd_res = 1 Waitms 500 Glcd_rs = 0 Glcd_rw = 0 Glcd_cs0 = 1 ' Instruktionen Glcd_cs1 = 0 Call Glcd_wdata(&B00111111 ) Call Glcd_wdata(&B11000000 ) Call Glcd_wdata(&B10111000 ) Call Glcd_wdata(&B01000000 ) Glcd_rs = 0 Glcd_rw = 0 Glcd_cs0 = 0 ' Instruktionen Glcd_cs1 = 1 Call Glcd_wdata(&B00111111) Call Glcd_wdata(&B11000000 ) Call Glcd_wdata(&B10111000 ) Call Glcd_wdata(&B01000000 ) Return End Glcd_cls: Glcd_rs = 1 Glcd_rw = 0 X = 0 For A = 1 To 128 ' Bildschirm löschen For I = 1 To 60 Step 8 Call Glcd_framepos(a , I , X) Call Glcd_pixpos(a , I , X) Next I Next A Return End Glcd_mirror: Glcd_rs = 1 Glcd_rw = 0 For X = 1 To 63 For A = 1 To 128 Call Glcd_framepos(a , X , 1) Call Glcd_pixpos(a , X , 1) Next A Next X Return End Sub Glcd_line(byval Sx As Byte , Byval Sy As Byte , Byval Ex As Byte , Byval Ey As Byte) Local Dx As Integer Local Dy As Integer Local Yb As Byte Local Xb As Byte Local Xcount As Byte Xb = 0 Yb = 0 Dx = Sx - Ex Dy = Sy - Ey Ysingle = Dy / Dx For Xcount = Ex To Sx Step -1 Yb = Ysingle * Xb Yb = Yb + Sy Call Glcd_framepos(xcount , Yb , 1) Call Glcd_pixpos(xcount , Yb , 1) Incr Xb Yb = Ysingle * Xb Yb = Yb + Sy Call Glcd_framepos(xcount , Yb , 1) Call Glcd_pixpos(xcount , Yb , 1) Next A End Sub