' Program obsługi wbudowanego w mikrokontroler '
'10-bitowego przetwornika A/C
$regfile = "m8def.dat" 'informuje kompilator o pliku
'dyrektyw mikrokontrolera
$crystal = 8000000 'informuje kompilator
$baud = 9600
'o częstotliwości oscylatora
'taktującego mikrokontroler
Config Lcd = 20 * 2
Config Lcdbus = 4
Config Portd = &B11110000
Config Pinb.0 = Output
Config Pinb.1 = Output
Config Int0 = Low Level ' rs232 wyjście
Config Adc = Single , Prescaler = Auto , Reference = Avcc 'konfiguracja wewnętrznego przetwornika A/C
Config Sda = Portc.3
Config Scl = Portc.4
Declare Sub Write_eeprom(byval Adres As Byte , Byval Value As Byte)
Declare Sub Read_eeprom(byval Adres As Byte , Value As Byte)
Dim Addressw As Byte 'slave write address
Dim Addressr As Byte 'slave read address
'Dim Wyraz As String * 10
Dim B1 As Byte , Adres As Byte , Value As Byte , T As Byte 'dim byte
Config Rc5 = Pind.3 [timer2]
Set Portd.3
Config Serialout = Buffered , Size = 40
'$baud = 2400
'$lib "mcsbyte.lbx"
'Config Int0 = Low Level
Enable Interrupts
Dim A As Byte
Dim Wart_ac0 As Word 'zmienna na wartość odczytaną
Dim Wart_ac1 As Integer
Dim Wart_ac2 As Integer 'Word
Dim Wart_ac3 As Integer
Dim Wart_ac4 As Integer
Dim Wart_ac5 As Integer
'Dim V0 As Single
Dim V1 As Single
Dim V2 As Single
Dim V3 As Single ' Integer Double '
Dim V4 As Single
Dim V5 As Single
Dim Wart_temp0 As Integer
Dim Wart_temp1 As String * 3
Dim Wart_temp2 As String * 3
Dim Wart_temp3 As String * 3 ' Integer
Dim Wart_temp4 As String * 3
Dim Wart_temp5 As String * 3
Dim Address As Byte , Command As Byte
Dim Meni As Byte
Dim Vol As Byte
Dim M As Byte
Dim Ms As Byte
Ms = 5
M = 1
Cls
Lcd "<=PINIO=> POKOJ.M8"
Wait 1
Cls
Lcd "Szukam urzadzen ";
Lowerline
Lcd "na szynie I2C ";
Set Portd.2
For Addressw = 2 To 254 Step 2
I2cstart
I2cwbyte Addressw
I2cstop
If Err = 0 Then Exit For
Next
Cls
If Addressw < 255 Then
Lcd "Adres &H" ; Hex(addressw);
Lowerline
Lcd "odpowiedzial ACK";
Addressr = Addressw + 1
Wait 2
Cls
Lcd "Addressr=" ; Addressr
Lowerline
Lcd "Addressw=" ; Addressw
Else
Lcd "Urzadzenia nie";
Lowerline
Lcd "odpowiadaja :(";
Stop
End If
Print " super "
Waitms 2
Sound Pinb.1 , 100 , 10
Reset Portd.2
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Goto Witaj
Portb.0 = 0
Do
Start Adc
Wart_ac0 = Getadc(0) 'sztuczna masa temperatura bateri
Wart_ac1 = Getadc(1) 'foto dioda
Wart_ac2 = Getadc(2) 'lm35 out
Wart_ac3 = Getadc(3) 'lm35 in room
Wart_ac4 = Getadc(4) '???
Wart_ac5 = Getadc(5) 'batery
Stop Adc
''''''''''''''''''''''OBLICZANIE''''''''''''''''''''''''''''''''''''''''''''''''
Wart_temp0 = 277 - Wart_ac0
Wart_ac1 = Wart_ac1 - Wart_ac0 'foto dioda
V5 = Wart_ac5 * 0.05 'napięcie baterii'
If Wart_ac2 < Wart_ac0 Then V2 = V2 * -1 'czujnik 1 najpierw sprawdzam czy adc1 jest mniejsze od masy(adc0)
Wart_ac2 = Wart_ac2 - Wart_ac0
V2 = Wart_ac2 * 0.5
' Wart_temp2 =
If Wart_ac3 < Wart_ac0 Then V3 = V3 * -1 'czujnik 2 najpierw sprawdzam czy adc1 jest mniejsze od masy(adc0)
Wart_ac3 = Wart_ac3 - Wart_ac0
V3 = Wart_ac3 * 0.5
' Wart_temp3 = Fusing(v3 , "#.&&")
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Enable Interrupts
Getrc5(address , Command)
Command = Command And &B01111111
If Address <> 255 Then
Waitms 20
Sound Pinb.1 , 100 , 20
Sound Pinb.1 , 100 , 30
Cls
Lcd Command
Select Case Command
Case 32 : Meni = Meni + 1
Case 33 : Meni = Meni - 1
Case 15 : Gosub Odczyttemp
Case 13 : Gosub Bateria
Case 38 : Gosub Odczyttemp
Case 12 : Toggle Portb.0
Case 16 : Vol = Vol + 1
Case 17 : Vol = Vol - 1
End Select
End If
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Select Case Meni
Case Is < 1 : Meni = 0
Case 0 : Gosub Offf
Case 1 : Gosub Bateria
Case 2 : Gosub Przejscie
Case 3 : Gosub Czerwony
Case 4 : Gosub Niebieski
Case 5 : Gosub Zielony
Case 6 : Gosub Bialy
Case 7 : Gosub Roleta
Case 8 : Gosub Odczyttemp
Case 9 : Gosub Witaj
Case Is > 9 : Meni = 9
End Select
Loop
End
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Odczyttemp:
Print "jasnosc=" ; Wart_ac1
Waitms 50 'opóźnienie 100 ms
Print "t1=" ; Fusing(v2 , " #.&") ; "`C"
Print "t2=" ; Fusing(v3 , " #.&") ; "`C"
Print "tempB" ; Wart_temp0 ; "`C"
Cls
Home Upper
Lcd "jasnosc=" ; Wart_ac1 ; "tempB" ; Wart_temp0 ; "`C"
Lowerline
Lcd "t1=" ; Fusing(v2 , " #.&") ; "`C" ; " t2=" ; Fusing(v3 , " #.&") ; "`C" ' Wart_ac2 ; Wart_temp2 Wart_ac1
'
Call Write_eeprom(2 , 11)
Call Write_eeprom(5 , 25)
Call Write_eeprom(9 , 111)
Call Read_eeprom(t , B1)
Wait 1
Cls
Lcd "ADRES" ; T ; "WYNIK" ; B1
' If B1 = &HFF Or B1 = 0 Or T > 10 Then Exit Do
' Wyraz = Wyraz + Chr(b1)
Incr T
' Waitms 2
'Waitms 2
Return
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Witaj:
'Dim A As Byte
'Do
Cls 'kasujemy LCD
Lcd "Witaj swiecie" 'wydrukujemy tekst w górnej linii
Print " witaj "
Waitms 20
Lowerline 'wybieramy drugą linię
Waitms 20
Lcd " Pinio " 'wydrukujemy to w drugiej linii
Waitms 20
For A = 1 To 10
Shiftlcd Right 'przesuniemy tekst w prawo
Waitms 20 'czekamy aż wykona
Next
For A = 1 To 10
Shiftlcd Left 'przesuniemy tekst w lewo
Waitms 20 'czekamy aż wykona
Next
Cursor Off Noblink 'chowamy kursor
Waitms 20 'czekamy
'Cursor On Blink 'pokazujemy kursor
'Waitms 20 'czekamy
'Display Off 'wyłączamy wyświetlacz
'Waitms 20 'czekamy
'Display On
Cls 'wybieramy Data RAM
Return
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Bateria:
If V5 < 12 Then Portb.0 = 1
If V5 > 14 Then Portb.0 = 0
If Wart_temp0 > 35 Then Portb.0 = 0
'Cls
'Lcd "bat=" ; Fusing(v5 , " #.&&") ; " temp=" ; Wart_temp0 ; " =" ; Portd.2
Waitms 100
Print "bat =" ; Fusing(v5 , "#.&&")
Print "temp =" ; Wart_temp0
Print "ładowanie =" ; Portd.2
Return
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Offf:
Call Write_eeprom(2 , 10)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Przejscie:
Cls
Lcd "PRZEJŚCIE ="
Call Read_eeprom(12 , B1)
If B1 > 8 Then B1 = 8
Call Write_eeprom(1 , 12)
If Command = 16 Then
Call Write_eeprom(2 , 12)
Call Write_eeprom(12 , Vol)
End If
If Command = 17 Then
Call Write_eeprom(2 , 12)
Call Write_eeprom(12 , Vol)
End If
Vol = B1
Lcd B1
Return
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Czerwony:
Cls
Lcd "CZERWONY ="
Call Read_eeprom(13 , B1)
Call Read_eeprom(2 , T)
Call Write_eeprom(1 , 13)
If Command = 16 Then
Call Write_eeprom(2 , 13)
Call Write_eeprom(13 , Vol)
End If
'End If
If Command = 17 Then
Call Write_eeprom(2 , 13)
Call Write_eeprom(13 , Vol)
End If
Vol = B1
Lcd B1 ; " " ; T
Return
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Niebieski:
Cls
Lcd "NIEBIESKI"
Call Read_eeprom(14 , B1)
Call Write_eeprom(1 , 14)
If Command = 16 Then
Call Write_eeprom(2 , 14)
Call Write_eeprom(14 , Vol)
End If
'End If
If Command = 17 Then
Call Write_eeprom(2 , 14)
Call Write_eeprom(14 , Vol)
End If
Vol = B1
Lcd B1
Return
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Zielony:
Cls
Lcd "ZIELONY"
Call Read_eeprom(15 , B1)
Call Write_eeprom(1 , 15)
If Command = 16 Then
Call Write_eeprom(2 , 15)
Call Write_eeprom(15 , Vol)
End If
'End If
If Command = 17 Then
Call Write_eeprom(2 , 15)
Call Write_eeprom(15 , Vol)
End If
Vol = B1
Lcd B1
Return
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Bialy:
Cls
Lcd "BIAŁY"
Call Read_eeprom(16 , B1)
Call Write_eeprom(1 , 16)
If Command = 16 Then
Call Write_eeprom(2 , 16)
Call Write_eeprom(16 , Vol)
End If
'End If
If Command = 17 Then
Call Write_eeprom(2 , 16)
Call Write_eeprom(16 , Vol)
End If
Vol = B1
Lcd B1
Return
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Roleta:
Portb.0 = 1
Waitms 20
Cls
Lcd "ROLETA"
If Command = 17 Then
Do
Toggle Portb.0
Portd = &B00010000
Waitms Ms
Portd = &B00110010
Waitms Ms
Portd = &B00100000
Waitms Ms
Portd = &B01100000
Waitms Ms
Portd = &B01000000
Waitms Ms
Portd = &B11000010
Waitms Ms
Portd = &B10000000
Waitms Ms
Portd = &B10010000
Waitms Ms
If M = 1 Then
'Cls
Lcd " TRYB NOCNY "
Lowerline
Lcd " PROSZĘ CZEKAĆ " 'ROLETA
Waitms 50
Print "ROLETA NA DÓŁ PROSZĘ CZEKAĆ"
End If
M = M + 1
Ms = Ms - 1
If Ms <= 1 Then Ms = 1
Loop Until M = 100 'Until M <> 60
Portd = &B00000000
Ms = 5
Waitms 50
Print "ZAKOŃCZONO"
Cls
Lcd "ZAKOŃCZONO"
End If
If Command = 16 And M = 100 Then
Do
Toggle Portb.0
Portd = &B00010000
Waitms Ms
Portd = &B10010000
Waitms Ms
Portd = &B10000000
Waitms Ms
Portd = &B11000000
Waitms Ms
Portd = &B01000000
Waitms Ms
Portd = &B01100000
Waitms Ms
Portd = &B00100000
Waitms Ms
Portd = &B00110000
Waitms Ms
If M = 100 Then
'Cls
Lcd " TRYB DZIENNY "
Lowerline
Lcd " PROSZĘ CZEKAĆ " ' ROLETA W GURE
'Waitms 50
Print "ROLETA W GURE PROSZĘ CZEKAĆ"
End If
M = M - 1
Ms = Ms - 1
If Ms <= 1 Then Ms = 1
Loop Until M = 1
Portd = &B00000000
Ms = 5
Waitms 50
Print "ZAKOŃCZONO"
Cls
Lcd "ZAKOŃCZONO"
End If
Return
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Program wykonujący odczyt z pamięci EEPROM
Sub Read_eeprom(byval Adres As Byte , Value As Byte)
If Pind.2 = 1 Then
Set Portd.2
I2cstart
I2cwbyte Addressw
I2cwbyte Adres
I2cstart
I2cwbyte Addressr
I2crbyte Value , Nack
I2cstop
Waitms 5
Reset Portd.2
End Sub
End If
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub Write_eeprom(adres As Byte , Value As Byte)
If Pind.2 = 1 Then
Set Portd.2
I2cstart
I2cwbyte Addressw
I2cwbyte Adres
I2cwbyte Value
I2cstop
Waitms 10
Reset Portd.2
End Sub
End If