'######################################################################### '### W48-Handy ### '### (C) jr2008 (Joachim Fossie Bär Reiter, fossie@fakedomain.de) ### '### Begin : 24.01.2008 ### '### Last modified: 24.01.2008 ### '### ### '### ### '######################################################################### $regfile = "m16def.dat" ' specify the used micro Const Quarz = 8000000 Const Use_lcd = 0 $crystal = Quarz ' used crystal frequency $baud = 9600 ' use baud rate $hwstack = 64 $swstack = 64 $framesize = 64 #if Use_lcd = 1 ' LCD-Definition $lib "lcd4busy.lib" 'define the used constants 'I used portA for testing Const _lcdport = Porta Const _lcdddr = Ddra Const _lcdin = Pina Const _lcd_e = 3 Const _lcd_rw = 1 Const _lcd_rs = 2 Config Lcd = 16 * 2 #endif Declare Function Waitfor(byval Waitstring As String , Byref Waittime As Integer) As String Const Ticker = Quarz / 65536 'so viele Ticker/Sekunde Const Zehntelticker = Int(quarz / 655360) 'so viele Ticker pro Zehntel Sekunde ' Timer initialisieren Const Norm_timeout = 30 ' 3 Sekunden Const Long_timeout = 100 ' 10 Sekunden Const Aus = 1 Const Ein = 0 Const Aufgelegt = 1 Const Abgehoben = 0 ' Belegung des Atmel ' Port A LCD-Display ' Port C ' Bit 7 Eingang Gabelumschalter ' Bit 6 Eingang NSA-Kontakt ' Bit 5 Eingang NSI-Kontakt ' Bit 4 Eingang Drucktaster ' Bit 3 Ausgang Statusled ' Port B ' Bit 0 Ausgang Klingel Power ' Bit 1 Ausgang Klingel EIN/AUS (später A-Leitung) ' Bit 2 Ausgang Klingel_b Enable (Ansteuerung L293 direkt) ' Bit 3 Ausgang Handy_power ' Bit 4 Ausgang Handy_on (Einschaltkontakt) ' Bit 5 Ausgang Waehlton EIN/AUS (später A-Leitung) ' Bit 6 Ausgang Waehlton_b (Ansteuerung Hörer direkt) Gabel Alias Pinc.7 Nsa Alias Pinc.6 Nsi Alias Pinc.5 Dialbutton Alias Pinc.4 Status_led Alias Portc.3 Klingel_power Alias Portb.0 Klingel Alias Portb.1 'Klingel_b Alias Portb.2 Handy_power Alias Portb.3 Handy_on Alias Portb.4 Waehlton Alias Portb.5 'Waehlton_b Alias Portb.6 Config Portb = &B11110000 Config Portc = &B11110000 Dim Timevar As Long Dim Rec_string As String * 100 DIM Pin_nr as String * 8 at Rec_string Overlay Dim Rec_array(100) As Byte At Rec_string Overlay Dim Rec_char As Byte Dim Rec_count As Byte Dim Dialnumber(32) As Byte Dim Lastnumber(32) As Byte Dim Dialnumber_count As Byte Dim Nsi_count As Byte Dim Ring As Byte Dim Count As Integer Dim Timeout As Integer Dim Prgstep As Byte Dim Blinker As Byte Config Serialin = Buffered , Size = 40 ' 40Byte Puffer für serielle '###### Init Timer ' Timer initialisieren Config Timer1 = Timer , Prescale = 1 ' und starten Enable Timer1 On Timer1 Timerint Enable Interrupts Init: #if Use_lcd = 1 Cls Lcd "INIT" #endif Wait 1 Status_led = Ein #if Use_lcd = 1 Cls Lcd "Power ON Handy" #endif Handy_on=EIN Wait 1 Handy_on = AUS Wait 10 Status_led = Ein Prgstep = 0 Rec_string = "" Gosub Clearbuf #if Use_lcd = 1 Locate 1 , 1 Lcd "Reset Handy (atz)" #endif Print "ATZ" Timeout = Norm_timeout ' etwa 3 Sekunden als Wartezeit Rec_string = Waitfor( "OK" , Timeout) #if Use_lcd = 1 Locate 2 , 1 Lcd "Antwort: " ; Rec_string Wait 3 #endif Incr Prgstep #if Use_lcd = 1 Cls Locate 1 , 1 Lcd "Checke PIN? #endif Gosub Clearbuf Print "AT+CPIN?" Timeout = Norm_timeout ' etwa 3 Sekunden als Wartezeit Rec_string = Waitfor( "OK" , Timeout) #if Use_lcd = 1 Locate 2 , 1 Lcd "Antwort: " ; Rec_string Wait 3 #endif If Rec_string = "" Then Goto Errorhandling If Instr(rec_string , "READY") = 0 Then Incr Prgstep #if Use_lcd = 1 Cls Locate 1 , 1 Lcd "gebe PIN ein" #endif Gosub Clearbuf ' READEEPROM PIN_nr ' Print "AT+CPIN={034}";PIN_nr;"{034}" Timeout = Long_timeout ' etwa 10 Sekunden zum Einloggen Rec_string = Waitfor( "OK" , Timeout) #if Use_lcd = 1 Locate 2 , 1 Lcd "Antwort: " ; Rec_string Wait 3 #endif If Rec_string = "" Then Goto Errorhandling End If Incr Prgstep #if Use_lcd = 1 Cls Locate 1 , 1 Lcd "schalte CLIP ein" #endif Gosub Clearbuf Print "AT+CLIP=1" Timeout = Norm_timeout ' etwa 3 Sekunden als Wartezeit Rec_string = Waitfor( "OK" , Timeout) #if Use_lcd = 1 Locate 2 , 1 Lcd "Antwort: " ; Rec_string Wait 1 #endif If Rec_string = "" Then Goto Errorhandling Rec_string = "" Gosub Clearbuf Main: #if Use_lcd = 1 CLS LCD "W48-Handy V0.3" lcd "(C) fossie 2008" #endif Do Status_led = Aus While Gabel = Aufgelegt If Ring = 1 Then Gosub In_call Klingel = Aus Wend If Gabel = Abgehoben Then Gosub Out_call Loop End 'end program ' ######################################################################## ' ###### Outgoing call ###### ' ######################################################################## Out_call: #if Use_lcd = 1 cls lcd "ABGEHOBEN" #end if Dialnumber_count = 0 Waehlton = Ein For Count = 1 To 32 Dialnumber(count) = 0 Next Count Waitofnsa: Nsi_count = 0 If Gabel = Aufgelegt Then Goto Hangup Debounce Dialbutton , Ein , Dial If Nsa = Aus Then Goto Waitofnsa Waitms 20 ' NSA-Debounce Waehlton = Aus While Nsa = Ein While Nsi = Ein If Nsa = Aus Then Exit While If Gabel = Aufgelegt Then Exit While Wend Waitms 50 Status_led = Ein While Nsi = Aus If Nsa = Aus Then Exit While If Gabel = Aufgelegt Then Exit While Wend Waitms 50 Status_led = Aus Incr Nsi_count Wend If Gabel = Aufgelegt Then Goto Hangup Incr Dialnumber_count If Nsi_count > 0 And Nsi_count < 11 Then If Nsi_count = 10 Then Nsi_count = 0 #if Use_lcd = 1 locate 2,dialnumer_count lcd Nsi_count #endif Dialnumber(dialnumber_count) = Nsi_count End If Goto Waitofnsa Dial: #if Use_lcd = 1 locate 1,1 lcd "Waehle Nummer" #endif Print : Print "atd"; For Count = 1 To Dialnumber_count Print Dialnumber(dialnumber_count); Next Count Print Do Loop Until Gabel = Aufgelegt Hangup: Print : Print "ath" Waitms 500 Return ' ######################################################################## ' ###### Incoming call ###### ' ######################################################################## In_call: #if Use_lcd = 1 CLS Lcd "Anruf von" Locate 2,1 lcd dialnuber #endif ' 1 Sekunde klingeln, 4 Sekunden warten Timevar = 10 ' Eine Sekunde setzen Klingel = Ein ' Klingel an Status_led = Ein ' led an Do Loop Until Timevar = 0 ' eine Sekunde warten Klingel = Aus ' Klingel Aus Status_led = Aus ' led aus Timevar = 40 ' vier Sekunden setzen Do If Gabel = Abgehoben Then Exit Do If Ring = 0 Then Exit Do Loop Until Timevar = 0 ' vier Sekunden warten If Ring = 0 Then Return ' Anruf zu Ende, dann zurück If Gabel = Aufgelegt Then Goto In_call ' von vorne Print "ata" ' abheben Do Loop Until Gabel = Aufgelegt Print "ath" Wait 1 Gosub Clearbuf #if Use_lcd = 1 cls #endif Return ' ######################################################################## ' ### löscht Combuffer ' ######################################################################## Clearbuf: Waitms 5 ' ganz kurz warten Do Rec_char = Inkey() ' und einlesen bis nix mehr da ist Loop Until Rec_char = 0 Return Return Function Waitfor(byval Waitstring As String , Waittime As Integer) As String * 30 Local Wait_string As String * 30 Local Waitchar As Byte Local Ende As Byte Wait_string = "" Ende = 0 While Ende = 0 If Ischarwaiting() = 1 Then Waitchar = Waitkey() Timeout = Waittime Select Case Waitchar ' Case 13 ' ignorieren Case 10 ' auch nix machen Case Else Wait_string = Wait_string + Chr(waitchar) If Instr(wait_string , Waitstring) > 0 Then Ende = 1 End If End Select End If If Timeout = 0 Then Ende = 1 Wait_string = "" End If Wend Waitfor = Wait_string End Function Errorhandling: Locate 4 , 1 Lcd "Error Step: " ; Prgstep End 'end program ' ######################################################################## ' ### Interruptroutinen ' ######################################################################## ' etwa jede Microsekunde ein Interrupt Timerint: Decr Timevar Decr Blinker If Blinker > Zehntelticker Then ' jetzt jede Zehntelsekunde If Timevar > 0 Then Decr Timevar If Timeout > 0 Then Decr Timeout Blinker = 0 End If Return Return ' ###### Serieller Empfang ###### Rs232_receive: Rec_char = Udr ' Zeichen in Variable einlesen If Usr.or = 1 Then Return ' Overrun Error, abbrechen If Usr.fe = 1 Then Return ' Framing Error, abbrechen Incr Rec_count ' Empfangszähler erhöhen Return