I2C.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /********************************************************************************
  2. **** Copyright (C), 2019, Fortior Technology Co., Ltd. ****
  3. ********************************************************************************
  4. File Name : I2C.c
  5. Author : Bruce HW&RD
  6. Date : 2019-03-27
  7. Description : .C file function description
  8. Version : 1.0
  9. Function List :
  10. Record :
  11. 1.Date : 2019-03-27
  12. Author : Bruce HW&RD
  13. Modification: Created file
  14. ********************************************************************************/
  15. #include "I2C.h"
  16. /*
  17. void I2C_Init_Master(void)
  18. {
  19. SetBit(P0_PU , P00);
  20. SetBit(P0_PU , P01);
  21. // I2C initial
  22. SetBit(I2C_CR , I2CMS); //主从模式选择 0-->Slave Mode 1-->Host Mode
  23. ClrBit(I2C_CR , I2CSPD1); //00-->100kHz 01-->400kHz
  24. SetBit(I2C_CR , I2CSPD0); //10-->1MHz 11-->RSV
  25. ClrBit(I2C_CR , I2CIE); //中断使能 0-->Disable 1-->Enable
  26. I2C_ID = 0xD0;
  27. SetBit(I2C_ID , GC); //广播呼叫使能 0-->Disable 1-->Enable
  28. SetBit(I2C_CR , I2CEN); //I2C使能 0-->Disable 1-->Enable
  29. }
  30. void I2C_ID_Start(bool rw)
  31. {
  32. if(rw)
  33. SetBit(I2C_SR , DMOD);
  34. else
  35. ClrBit(I2C_SR , DMOD);
  36. SetBit(I2C_SR , I2CSTA); //1-->发送START或RESTART和地址字节
  37. while(!ReadBit(I2C_SR , STR));
  38. ClrBit(I2C_SR , STR);
  39. }
  40. void I2C_Addr_Write(unsigned char addr,unsigned char wdata)
  41. {
  42. I2C_ID_Start(0); // ID write
  43. I2C_DR = addr; // Slave reg addr.
  44. while(!ReadBit(I2C_SR , STR));
  45. ClrBit(I2C_SR , STR);
  46. I2C_DR = wdata; // Slave reg data.
  47. while(!ReadBit(I2C_SR , STR));
  48. ClrBit(I2C_SR , STR);
  49. SetBit(I2C_SR , I2CSTP); //1-->发送STOP
  50. }
  51. unsigned char I2C_Addr_Read(char addr)
  52. {
  53. unsigned char rd_data;
  54. //write process
  55. I2C_ID_Start(0); // write+ID
  56. I2C_DR = addr; // write reg. addr
  57. while(!ReadBit(I2C_SR , STR));
  58. ClrBit(I2C_SR , STR);
  59. // SetBit(I2C_SR , I2CSTP); //1-->发送STOP
  60. //read process
  61. I2C_ID_Start(1); // read+ID
  62. while(!ReadBit(I2C_SR , STR));
  63. ClrBit(I2C_SR , STR);
  64. rd_data = (unsigned char)(I2C_DR); // read data
  65. SetBit(I2C_SR , NACK); //第9位发送NACK
  66. SetBit(I2C_SR , I2CSTP); //1-->发送STOP
  67. return(rd_data);
  68. }*/