Hallo,
nach einiger Überlegungszeit möchte ich nun mit meiner FA anfangen. Dafür benötige ich ein I2C Netzwerk, da es versch. Module geben wird. Mein Hauptcontroller (M328P) ist bereits fertig aufgebaut auf einer kleinen Platine, als Slave dient derzeit noch ein RN-Control (M32). Diese möchte ich nun miteinander kommunizieren lassen, aber irgendwie habe ich einen Fehler in meinen Programmen:
Master:
Slave:Code:$regfile = "m328pdef.dat" $crystal = 16000000 $framesize = 80 $hwstack = 80 $swstack = 80 $lib "i2c_twi.lbx" Config Scl = Portc.5 Config Sda = Portc.4 Config Twi = 400000 I2cinit Config Timer0 = Pwm , Compare A Pwm = Clear Down , Compare B Pwm = Clear Down , Prescale = 1 Pwm0a = 0 Pwm0b = 0 Config Portd.5 = Output Config Portd.6 = Output Const Slave_write = &H70 Const Slave_read = &H71 Const Gen_call = &H00 Dim B1 As Byte , B2 As Byte Dim Ar(3) As Byte Ar(1) = Asc( "h") Ar(2) = Asc( "a") Enable Interrupts Do I2csend Slave_write , Ar(1) , 2 Waitms 500 I2cstart I2cwbyte Slave_read I2crbyte B1 , Ack I2crbyte B2 , Nack I2cstop Pwm0a = B1 Pwm0b = B2 Waitms 500 Loop End
Die Verkabelung sollte stimmen, da ich mit dem gleichen Setup schon mal eine Kommunikation hinbekommen habe, nur leider habe ich jetzt einen neuen PC und habe die Programme nicht mehr... Das Problem ist, eigentlich sollte bei JEDEM Zugriff des Masters auf den Slave die LED an Pc.6 getoggelt werden, aber nichteinmal das funktioniert.. Erkennt jemand einen Fehler?Code:$regfile = "m32def.dat" $crystal = 16000000 $framesize = 80 $hwstack = 80 $swstack = 80 $lib "i2c_twi-slave.lbx" Config Twislave = &H70 , Btr = 2 , Bitrate = 400000 , Save = Save , Gencall = 1 Config Sda = Portc.1 Config Scl = Portc.0 I2cinit Config Lcd = 16 * 2 Config Lcdpin = Pin , Db4 = Portb.0 , Db5 = Portb.1 , Db6 = Portb.2 , Db7 = Portb.3 , E = Portb.4 , Rs = Portb.5 Config Lcdbus = 4 Initlcd Cls Cursor Off Noblink Config Portc.6 = Output Portc.6 = 0 Dim Flag As Bit Dim I As Byte Locate 1 , 1 Lcd "TEST" Enable Interrupts Do !nop Loop Twi_slave_error: Return Twi_write_addressed: Toggle Portc.6 Return Twi_read_addressed: Toggle Portc.6 Return Twi_stop_rstart_received: Return 'this label is called when the master sends data and the slave has received the byte 'the variable TWI holds the received value Twi_gotdata: Return 'this label is called when the master receives data and needs a byte 'the variable twi_btr is a byte variable that holds the index of the needed byte 'so when sending multiple bytes from an array, twi_btr can be used for the index Twi_master_needs_more_byte: I = Rnd(255) Twi = I Locate 2 , 1 Lcd Twi ; " " Return 'when the mast has all bytes received this label will be called Twi_master_need_nomore_byte: Return End
Vielen Dank & Gruß
Chris







Zitieren
Lesezeichen