
|
INFO - FAQ - CC2-Forum - CCPro-Forum |
|
'----------------------------------------------------------------------------------------------------' '************************************************************************' '* WINDT SYSTEMS *' '* DATA TO PCF8574 VIA EMULATED I2C BUS v1.5 for CCIMainU1.1 with CCISB *' '* H.J. WINDT *' '* 2005 *' '************************************************************************' '----------------------------------------------------------------------------------------------------' 'This program will demonstrate sending data to a PCF8574A(P) via an emulated I2C bus.' 'I2C_address must contain the PCF8574P address/ &b0100(A2)(A1)(A0)0 or' 'I2C address must contain the PCF8574AP address/ &b0111(A2)(A1)(A0)0.' 'I2C_data must contain the byte to be sent.' 'The emulated SDA line is via port 1.' 'The emulated SCL line is via port 2.' 'Feel free to use and share this software!' '----------------------------------------------------------------------------------------------------' '*************** INS and OUTS ***************' define sda port[1] define scl port[2] '*******************************************' '**************** VARIABLES ****************' define i2c_error bit[7] define i2c_error_count byte[2] define i2c_byte byte[3] define i2c_ms_bit bit[24] define i2c_ls_bit bit[17] define loop byte[4] define pcf8574_i2c_address byte[5] define pcf8574_i2c_data byte[6] '*******************************************' '**************** CONSTANTS ****************' '*******************************************' '****************** SETUP ******************' i2c_error_count = 0 '*******************************************' '***************** PROGRAM *****************' #start print"Enter PCF8574P I2C address (64/66/68/70/72/74/76/78) or" print"Enter PCF8574AP I2C address (112/114/116/118/120/122/124)" input pcf8574_i2c_address print"Enter byte to be sent (0 to 255)" input pcf8574_i2c_data gosub send_data_via_i2c_to_pcf8574 goto start '*******************************************' '*************** SUBROUTINES ***************' #send_data_via_i2c_to_pcf8574 gosub start_i2c i2c_byte = pcf8574_i2c_address : gosub write_byte_i2c 'Transmit address (pcf8574_i2c_address) over the I2C bus' if i2c_error then goto pass_write i2c_byte = pcf8574_i2c_data : gosub write_byte_i2c 'Transmit byte (pcf8574_i2c_data) over the I2C bus' #pass_write gosub stop_i2c if i2c_error_count > 3 then goto no_ack_from_pcf8574 if i2c_error then goto send_data_via_i2c_to_pcf8574 'If I2C error then try again' i2c_error_count = 0 return #no_ack_from_pcf8574 print"No ACK from or no PCF8574A(P) device on I2C address ";pcf8574_i2c_address i2c_error_count = 0 return #start_i2c sda = 0 : scl = 0 : i2c_error = 0 return #stop_i2c deact sda : sda = 0 : scl = 1 : sda = 1 return #write_byte_i2c for loop = 1 to 8 sda = i2c_ms_bit #write_byte_i2c_clock_stretch scl = 1 : deact scl : if scl = 0 then goto write_byte_i2c_clock_stretch deact scl : scl = 0 i2c_byte = i2c_byte shl 1 next #write_byte_i2c_ack_clock_stretch scl = 1 : deact scl : if scl = 0 then goto write_byte_i2c_ack_clock_stretch deact sda : i2c_error = sda deact scl : scl = 0 deact sda if i2c_error then i2c_error_count = i2c_error_count + 1 return '*******************************************' '****************** DATA *******************' '*******************************************' |
| Antwort schreiben |