main_entry.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include <stdio.h>
  2. #include "time.h"
  3. #include "los_swtmr.h"
  4. #include "los_sys.h"
  5. #include "ohos_init.h"
  6. #include "iot_gpio.h"
  7. #include "iot_gpio_ex.h"
  8. #include "iot_i2c.h"
  9. #include "cmsis_os2.h"
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <stdbool.h>
  14. #define IIC_SDA 0
  15. #define IIC_SCL 1
  16. #define ADDR 0x27 // 0100111
  17. #define IIC_IDX 1
  18. /*
  19. Command Register
  20. 0 Input port 0
  21. 1 Input port 1
  22. 2 Output port 0
  23. 3 Output port 1
  24. 4 Polarity Inversion port 0
  25. 5 Polarity Inversion port 1
  26. 6 Configuration port 0
  27. 7 Configuration port 1
  28. */
  29. #define CMD_CFG0 6
  30. #define CMD_CFG1 7
  31. #define CMD_OUT0 2
  32. #define CMD_OUT1 3
  33. UINT32 g_timerCount1 = 0;
  34. UINT32 g_timerCount2 = 0;
  35. int write_iic(uint8_t* data){
  36. int ret = IoTI2cWrite(IIC_IDX, (ADDR << 1) | 0x00, data, 3);
  37. //printf("***@@@###$$$ ret = %d\n",ret);
  38. return ret;
  39. }
  40. //start
  41. uint8_t CFG0[] = {CMD_CFG0,0x0,0x0}; //配置为输出
  42. uint8_t CFG1[] = {CMD_CFG1,0x0,0x0}; //配置为输出
  43. uint8_t OUT0[] = {CMD_OUT0,0x00,0x00}; // 输出
  44. uint8_t OUT1[] = {CMD_OUT1,0x00,0x00}; // 输出
  45. char alpha[8][9] = {
  46. "11111111",
  47. "11100000",
  48. "11100000",
  49. "11111111",
  50. "11111111",
  51. "11100000",
  52. "11100000",
  53. "11111111"
  54. };
  55. void write_data(char byte1,char byte2){
  56. uint8_t data[3] = {CMD_OUT0,0x00,0x00};
  57. data[1] = byte1;
  58. data[2] = byte2;
  59. write_iic(data);
  60. data[0] = CMD_OUT1;
  61. write_iic(data);
  62. }
  63. void iic(void* args )
  64. {
  65. int i,j;
  66. UINT16 id1; // Timer1 id
  67. UINT16 id2; // Timer2 id
  68. UINT32 tick;
  69. printf("iic thread running...\n");
  70. IoTGpioInit(IIC_SDA);
  71. IoTGpioInit(IIC_SCL);
  72. IoTGpioSetFunc(IIC_SDA, IOT_GPIO_FUNC_GPIO_0_I2C1_SDA);
  73. IoTGpioSetFunc(IIC_SCL, IOT_GPIO_FUNC_GPIO_1_I2C1_SCL);
  74. IoTI2cInit(IIC_IDX, 400000);
  75. write_iic(CFG0);
  76. write_iic(CFG1);
  77. usleep(20);
  78. write_iic(OUT0);
  79. write_iic(OUT1);
  80. //usleep(1000*1000);
  81. usleep(100);
  82. while(1)
  83. {
  84. for(int i=0;i<8;i++){
  85. unsigned char hex = 0;
  86. for(int j=0;j<8;j++){
  87. hex = hex <<1;
  88. if(alpha[i][j] == '1'){
  89. hex = hex | 0x1;
  90. }
  91. }
  92. write_data(~(1 << i),hex);
  93. for(int a=0;a<3;a++){printf("delay:%d\r\n",a);}
  94. }
  95. // printf("set count :%d\r\n",a=a+1);
  96. }
  97. }
  98. void iic_entry()
  99. {
  100. printf("iic_entry called \n");
  101. osThreadAttr_t attr;
  102. attr.name = "thread_iic";
  103. attr.attr_bits = 0U; // 如果为1 ,则可以使用osThreadJoin函数
  104. attr.cb_mem = NULL; //控制快的指针
  105. attr.cb_size = 0U;
  106. attr.stack_mem = NULL; //栈内存指针
  107. attr.stack_size = 1024 * 4; //栈大小
  108. attr.priority = 25; //优先级
  109. if (osThreadNew((osThreadFunc_t)iic, NULL, &attr) == NULL)
  110. {
  111. printf("Fail to create thread!\n");
  112. }
  113. }
  114. APP_FEATURE_INIT(iic_entry);