'********************************************************************************** ' READING OUT THE LDU VIA FOUR PCF 8591 '********************************************************************************** ' ' SCL (To Atmega 8 / Portd.4 + 10K pullup to +5V) ' SDA (To Atmega 8 / Portd.5 + 10K pullup to +5V) ' ' Beeper: Portb.0 ' ' Paste code, generated by blacklevel and whitelevel program into this program ' '********************************************************************************** $regfile = "m8def.dat" 'Atmega8-declarations $crystal = 3686400 $baud = 38400 Ddrb = &B00000001 Config Sda = Portd.5 Config Scl = Portd.4 Config I2cdelay = 10 Const Contrast = 35 'adjust contrast between surface and line (ca.20...50 20=no contrast 50=high contrast -- this contrastlevel is needed to find out, whether there's a line or not -- if there's no line, an alert will occur) Dim I As Byte 'i...k = general purpose variables Dim J As Byte Dim K As Byte Dim L As Byte Dim Ldr(16) As Integer 'ldr contains the value of the light dependant resistors Dim Maxbrightval As Byte 'maxbright=value of brightest ldr Dim Maxbrightsen As Byte 'maxbrightsensor=number of brightest ldr Dim Difbright As Word 'difference between the brightest sensor and the average brightnes of the other sensors Dim Ool As Byte 'outofline alert Do '********************************************************************************** ' linedetection ' read the values of the 16 sensors via i²c '********************************************************************************** I2cstart 'i2c start condition I2cwbyte &B10010000 'send pcf address (write) I2cwbyte &B01000101 'send control byte (auto-increment on, internal oscillator on) I2cstart 'repeated start condition I2cwbyte &B10010001 'send pcf addressieren (read) I2crbyte Ldr(1) , Ack 'read value of adc0 & acknowledge I2crbyte Ldr(2) , Ack '... I2crbyte Ldr(3) , Ack '... I2crbyte Ldr(4) , Ack 'read value of adc3 / don't acknowledge I2crbyte Ldr(1) , Ack 'read value of adc0 & acknowledge I2crbyte Ldr(2) , Ack '... I2crbyte Ldr(3) , Ack '... I2crbyte Ldr(4) , Nack 'read value of adc3 / don't acknowledge I2cstop I2cstart 'i2c start condition I2cwbyte &B10010010 'send pcf address (write) I2cwbyte &B01000101 'send control byte (auto-increment on, internal oscillator on) I2cstart 'repeated start condition I2cwbyte &B10010011 'send pcf addressieren (read) I2crbyte Ldr(5) , Ack 'read value of adc0 & acknowledge I2crbyte Ldr(6) , Ack '... I2crbyte Ldr(7) , Ack '... I2crbyte Ldr(8) , Ack 'read value of adc3 / don't acknowledge I2crbyte Ldr(5) , Ack 'read value of adc0 & acknowledge I2crbyte Ldr(6) , Ack '... I2crbyte Ldr(7) , Ack '... I2crbyte Ldr(8) , Nack 'read value of adc3 / don't acknowledge I2cstop I2cstart 'i2c start condition I2cwbyte &B10010100 'send pcf address (write) I2cwbyte &B01000101 'send control byte (auto-increment on, internal oscillator on) I2cstart 'repeated start condition I2cwbyte &B10010101 'send pcf addressieren (read) I2crbyte Ldr(9) , Ack 'read value of adc0 & acknowledge I2crbyte Ldr(10) , Ack '... I2crbyte Ldr(11) , Ack '... I2crbyte Ldr(12) , Ack 'read value of adc3 / don't acknowledge I2crbyte Ldr(9) , Ack 'read value of adc0 & acknowledge I2crbyte Ldr(10) , Ack '... I2crbyte Ldr(11) , Ack '... I2crbyte Ldr(12) , Nack 'read value of adc3 / don't acknowledge I2cstop I2cstart 'i2c start condition I2cwbyte &B10010110 'send pcf address (write) I2cwbyte &B01000101 'send control byte (auto-increment on, internal oscillator on) I2cstart 'repeated start condition I2cwbyte &B10010111 'send pcf addressieren (read) I2crbyte Ldr(13) , Ack 'read value of adc0 & acknowledge I2crbyte Ldr(14) , Ack '... I2crbyte Ldr(15) , Ack '... I2crbyte Ldr(16) , Ack 'read value of adc3 / don't acknowledge I2crbyte Ldr(13) , Ack 'read value of adc0 & acknowledge I2crbyte Ldr(14) , Ack '... I2crbyte Ldr(15) , Ack '... I2crbyte Ldr(16) , Nack 'read value of adc3 / don't acknowledge I2cstop 'i2c stop condition ' paste code from LDU_adjust_black here: Ldr(1) = Ldr(1) - 38 Ldr(2) = Ldr(2) - 37 Ldr(3) = Ldr(3) - 37 Ldr(4) = Ldr(4) - 43 Ldr(5) = Ldr(5) - 40 Ldr(6) = Ldr(6) - 40 Ldr(7) = Ldr(7) - 48 Ldr(8) = Ldr(8) - 36 Ldr(9) = Ldr(9) - 42 Ldr(10) = Ldr(10) - 40 Ldr(11) = Ldr(11) - 39 Ldr(12) = Ldr(12) - 41 Ldr(13) = Ldr(13) - 33 Ldr(14) = Ldr(14) - 33 Ldr(15) = Ldr(15) - 28 Ldr(16) = Ldr(16) - 22 ' end code from LDU_adjust_black For I = 1 To 16 Ldr(i) = Ldr(i) * 200 Next I ' paste code from LDU_adjust_white here: Ldr(1) = Ldr(1) / 139 Ldr(2) = Ldr(2) / 139 Ldr(3) = Ldr(3) / 142 Ldr(4) = Ldr(4) / 138 Ldr(5) = Ldr(5) / 138 Ldr(6) = Ldr(6) / 142 Ldr(7) = Ldr(7) / 140 Ldr(8) = Ldr(8) / 142 Ldr(9) = Ldr(9) / 142 Ldr(10) = Ldr(10) / 139 Ldr(11) = Ldr(11) / 141 Ldr(12) = Ldr(12) / 138 Ldr(13) = Ldr(13) / 149 Ldr(14) = Ldr(14) / 148 Ldr(15) = Ldr(15) / 150 Ldr(16) = Ldr(16) / 142 ' end code from LDU_adjust_white ' invert values (black line) For I = 1 To 16 Ldr(i) = 200 - Ldr(i) Next I ' print out bargraph Waitms 100 Print Chr(12) For I = 1 To 16 'clear screen If I > 9 Then Print I ; " = " ; Ldr(i); Else Print I ; " = " ; Ldr(i); End If If Ldr(i) < 10 Then Print " "; If Ldr(i) > 9 And Ldr(i) < 100 Then Print " "; Print " "; K = Ldr(i) / 4 For L = 1 To K Print "-"; Next L Print "" J = I Mod 4 If J = 0 Then Print "" Next I Loop End