$regfile = "m32def.dat" 'Atmega32-Deklarationen $crystal = 16000000 'Quarz: 16.00 MHz Config Pina.0 = Output 'R/W Display Config Pinb.0 = Input 'Countereingang T0 Config Pinb.1 = Input 'Countereingang T1 Config Pind.4 = Output Config Lcd = 20 * 4 , Chipset = Ks077 Config Lcdpin = Pin , Db4 = Portb.4 , Db5 = Portb.3 , Db6 = Portb.2 , Db7 = Porta.2 , E = Porta.1 , Rs = Porta.0 'Initlcd ' -----[ Variables ]-------------------------------------------------- 'B0 = RS 'A2 = D7 'B2 = D6 'B3 = D5 'B4 = D4 Dim W(20) As Eram Word Dim T0 As Byte Dim T1 As Byte Dim H As Byte Dim M As Byte Dim S As Byte Dim P1 As Long Dim P2 As Long Dim P3 As Long Dim P4 As Long Dim P5 As Long Dim P6 As Long Dim P7 As Single Config I2cdelay = 5 ' default slow mode Config Sda = Portc.1 Config Scl = Portc.0 Portb.0 = 1 'PullUp von PB0/T0 aktivieren Portb.1 = 1 'PullUp von PB1/T1 aktivieren Portd.4 = 1 On Timer0 Ontimer0 'Interrupt-Routine für Timer0-Overflow Config Timer0 = Counter , Edge = Falling On Timer1 Ontimer1 'Interrupt-Routine für Timer1-Overflow Config Timer1 = Counter , Edge = Falling Enable Timer0 'Timer0-Overflow-Interrupt einschalten Enable Timer1 Enable Interrupts 'Interrupts global zulassen '--------------------------------------------------------- Declare Sub Lcdwrite(byval Zeichen As Byte) Declare Sub Set_counter() 'schreiben und lesen PCF8583 Declare Sub Get_counter() Declare Sub Clear_counter() '---Start Programm ------------------------------- Porta.0 = 0 'Display R/W auf Masse Call Lcdwrite(&B00101100) Waitms 5 Call Lcdwrite(&B00001001) Waitms 5 Call Lcdwrite(&B00101000) Waitms 5 Call Lcdwrite(&B00000110) Waitms 5 Call Lcdwrite(&B00001100) Waitms 5 Cursor Off Cls Waitms 100 T0 = 0 T1 = 0 M1 = 0 M2 = 0 M3 = 0 M4 = 0 '------------------------------------------- Locate 1 , 1 Lcd "Test Zähler" Locate 2 , 1 Lcd "PCF8583" Locate 3 , 1 Lcd "Timer0" Locate 4 , 1 Lcd "Timer1" Timer0 = 240 'Counter vorbesetzen Timer1 = 65525 Call Set_counter 'inizialisieren PCF8283 als Counter und auf 0 Waitms 10 Do 'Endlosschleife Portd.4 = 1 'Port als Testzählerinput Waitms 10 Portd.4 = 0 Call Get_counter() 'hole Werte aus pcf8583 Locate 2 , 9 Lcd P7 Locate 3 , 9 Lcd T0 Locate 4 , 9 Lcd T1 Wait 1 'Zähler pro Sec um 1 erhöhen Loop Sub Set_counter() 'PCF einstellen I2cstart 'generate start I2cwbyte &HA0 'write addres of PCF8583 I2cwbyte 0 'select Control register I2cwbyte &B00100000 'Als Zähler Bit 5=1, Bit 6=0 I2cstop End Sub Sub Get_counter() 'lese Zählerstand I2cstart 'generate start I2cwbyte &HA0 'write addres of PCF8583 I2cwbyte 1 'select second register I2cstart 'generate repeated start I2cwbyte &HA1 'read address for reading info I2crbyte S , Ack 'read D0 und D1 (einer und Zehner) I2crbyte M , Ack 'read D2 und D3 (100 und 1000) I2crbyte H , Nack 'read D4 und D5 (10000 und 100000 I2cstop 'Auflösen damit addiert werden kann P1 = S '1. Nibbl P2 = M P3 = H P4 = S '2.Nibbl P5 = M P6 = H P1.7 = 0 'lösche Bit 5-8 (D2) P1.6 = 0 P1.5 = 0 P1.4 = 0 Shift P4 , Right , 4 'Schiebe Bits nach rechts von D2 P4 = P4 * 10 'D2 * 10 P1 = P1 + P4 'addiere D1 und D2 P2.7 = 0 'das Gleiche mit D3 und D4 P2.6 = 0 P2.5 = 0 P2.4 = 0 Shift P5 , Right , 4 'schiebe bits nach rechts von D4 P5 = P5 * 1000 'D4 * 1000 P2 = P2 * 100 'D3 * 100 P2 = P2 + P5 'addiere D3 mit D4 P3.7 = 0 'das Gleiche mit D5 und D6 P3.6 = 0 P3.5 = 0 P3.4 = 0 Shift P6 , Right , 4 'schiebe bits nach rechts von D6 P6 = P6 * 100000 'D6 * 100000 P3 = P3 * 10000 'D5 * 10000 P3 = P3 + P6 'addiere D5+D6 P7 = P3 + P2 P7 = P7 + P1 'addiere alle 3 Bytes (D0-D6) End Sub Sub Clear_counter() 'lösche Zählerstände Reg. 1 bis 3 I2cstart 'generate start I2cwbyte &HA0 'write addres of PCF8583 I2cwbyte 1 'select second register I2cwbyte 0 'write 0 nach D0 und D1 (einer und Zehner) I2cwbyte 0 'write 0 nach D2 und D3 (100 und 1000) I2cwbyte 0 'write 0 nach D4 und D5 (10000 und 100000) I2cstop End Sub End