PCF8574.c 1.7 KB

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