PCF8574.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "PCF8574.h"
  2. /*
  3. * Warning Warning
  4. *
  5. * Il existe 2 type de PCF8474 les PCF8474A et les PCF8474.
  6. * les PCF8474A on des adresse en 0x4X, les PCF8474 ont des adresse en 0x7X.
  7. * La différence est faite dans partkeep dans ce projet j'ai utilisé des A
  8. *
  9. */
  10. uint8_t ADD_PCF8474[] = { 0x40, 0x42, 0x44, 0x46, 0x48, 0x4A, 0x4C, 0x4E };
  11. //uint8_t ADD_PCF8474[] = {0x70, 0x72, 0x74, 0x76, 0x78, 0x7A, 0x7C, 0x7E};
  12. /** Private Fonction*/
  13. HAL_StatusTypeDef read_I2C_PCF8574(uint8_t add, uint8_t *tab) {
  14. HAL_StatusTypeDef res;
  15. int nbTentative = 0;
  16. while ((res = HAL_I2C_Master_Receive(I2C_PCF8574, add, tab, 1, 1000))!= HAL_OK) {
  17. nbTentative++;
  18. if (nbTentative > 3)
  19. break;
  20. }
  21. return res;
  22. }
  23. HAL_StatusTypeDef write_I2C_PCF8574(uint8_t addresse, uint8_t *tab) {
  24. HAL_StatusTypeDef res;
  25. int nbTentative;
  26. while ((res = HAL_I2C_Master_Transmit(I2C_PCF8574, addresse, tab, 1, 1000))!= HAL_OK) {
  27. nbTentative++;
  28. if (nbTentative > 3)
  29. break;
  30. }
  31. return res;
  32. }
  33. /** Public Fonction*/
  34. void initPCF8574(I2C_HandleTypeDef *typ) {
  35. I2C_PCF8574 = typ;
  36. Write_PCF8574(0, 0);
  37. }
  38. HAL_StatusTypeDef Read_PCF8574(uint8_t addBit, uint8_t* val) {
  39. return read_I2C_PCF8574(ADD_PCF8474[addBit], val);
  40. }
  41. HAL_StatusTypeDef Write_PCF8574(uint8_t addBit, uint8_t val) {
  42. return write_I2C_PCF8574(ADD_PCF8474[addBit], &val);
  43. }
  44. HAL_StatusTypeDef Write_One_Bit_PCF8574(uint8_t addBit, uint8_t val, uint8_t Local) {
  45. uint8_t valInt;
  46. HAL_StatusTypeDef res;
  47. res = read_I2C_PCF8574(ADD_PCF8474[addBit], &valInt);
  48. if (res != HAL_OK)
  49. return res;
  50. if (val)
  51. val = valInt | (val << Local);
  52. else{
  53. uint8_t inte=1<<Local;
  54. inte = ~inte;
  55. val = valInt & inte;
  56. }
  57. return write_I2C_PCF8574(ADD_PCF8474[addBit], &val);
  58. }