mat4.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 "ohos_init.h"
  11. // #include <stdio.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <unistd.h>
  15. // #include <cmsis_os2.h>
  16. #include <stdbool.h>
  17. // #ifdef __cplusplus
  18. // #if __cplusplus
  19. // extern "C" {
  20. // #endif /* __cplusplus */
  21. // #endif /* __cplusplus */
  22. #define IIC_SDA 0
  23. #define IIC_SCL 1
  24. #define ADDR 0x27 // 0100111
  25. #define IIC_IDX 1
  26. /*
  27. Command Register
  28. 0 Input port 0
  29. 1 Input port 1
  30. 2 Output port 0
  31. 3 Output port 1
  32. 4 Polarity Inversion port 0
  33. 5 Polarity Inversion port 1
  34. 6 Configuration port 0
  35. 7 Configuration port 1
  36. */
  37. #define CMD_CFG0 6
  38. #define CMD_CFG1 7
  39. #define CMD_OUT0 2
  40. #define CMD_OUT1 3
  41. UINT32 g_timerCount1 = 0;
  42. UINT32 g_timerCount2 = 0;
  43. // VOID Timer1_CallBack(UINT32 arg)
  44. // {
  45. // UINT64 lastTick;
  46. // g_timerCount1++;
  47. // lastTick=(UINT32)LOS_TickCountGet();
  48. // // dprintf("g_timerCount1=%d\n", g_timerCount1);
  49. // // dprintf("tick_last1=%d\n", lastTick);
  50. // }
  51. // VOID Timer2_CallBack(UINT32 arg)
  52. // {
  53. // UINT64 lastTick;
  54. // lastTick=(UINT32)LOS_TickCountGet();
  55. // g_timerCount2++;
  56. // // dprintf("g_timerCount2=%d\n", g_timerCount2);
  57. // // dprintf("tick_last2=%d\n", lastTick);
  58. // }
  59. int write_iic(uint8_t* data){
  60. int ret = IoTI2cWrite(IIC_IDX, (ADDR << 1) | 0x00, data, 3);
  61. //printf("***@@@###$$$ ret = %d\n",ret);
  62. return ret;
  63. }
  64. //start
  65. uint8_t CFG0[] = {CMD_CFG0,0x0,0x0}; //配置为输出
  66. uint8_t CFG1[] = {CMD_CFG1,0x0,0x0}; //配置为输出
  67. uint8_t OUT0[] = {CMD_OUT0,0x00,0x00}; // 输出
  68. uint8_t OUT1[] = {CMD_OUT1,0x00,0x00}; // 输出
  69. char alpha[8][9] = {
  70. "11111111",
  71. "11100000",
  72. "11100000",
  73. "11111111",
  74. "11111111",
  75. "11100000",
  76. "11100000",
  77. "11111111"
  78. };
  79. // char alpha[8][9] = {
  80. // "00011111",
  81. // "11100000",
  82. // "11100000",
  83. // "00011111",
  84. // "00011111",
  85. // "11100000",
  86. // "11100000",
  87. // "00011111"
  88. // };
  89. void write_data(char byte1,char byte2){
  90. //printf("write %02x ,%02x\n",byte1,byte2);
  91. uint8_t data[3] = {CMD_OUT0,0x00,0x00};
  92. data[1] = byte1;
  93. data[2] = byte2;
  94. write_iic(data);
  95. data[0] = CMD_OUT1;
  96. write_iic(data);
  97. }
  98. void iic(void* args ){
  99. int i,j;
  100. UINT16 id1; // Timer1 id
  101. UINT16 id2; // Timer2 id
  102. UINT32 tick;
  103. // LOS_SwtmrCreate(1000, LOS_SWTMR_MODE_ONCE, Timer1_CallBack, &id1, 1);
  104. // LOS_SwtmrCreate(100, LOS_SWTMR_MODE_PERIOD, Timer2_CallBack, &id2, 1);
  105. // LOS_SwtmrCreate(200, LOS_SWTMR_MODE_PERIOD, Timer1_CallBack, &id1, NULL); //时间的单位是ms
  106. // LOS_SwtmrCreate(80, LOS_SWTMR_MODE_PERIOD, Timer2_CallBack, &id2, NULL);
  107. // dprintf("create Timer1 success\n");
  108. printf("iic thread running...\n");
  109. IoTGpioInit(IIC_SDA);
  110. IoTGpioInit(IIC_SCL);
  111. IoTGpioSetFunc(IIC_SDA, IOT_GPIO_FUNC_GPIO_0_I2C1_SDA);
  112. IoTGpioSetFunc(IIC_SCL, IOT_GPIO_FUNC_GPIO_1_I2C1_SCL);
  113. IoTI2cInit(IIC_IDX, 400000);
  114. write_iic(CFG0);
  115. write_iic(CFG1);
  116. usleep(20);
  117. //usleep(200*1000);
  118. // LOS_SwtmrStart(id1);
  119. // dprintf("start Timer1 sucess\n");
  120. // //LOS_TaskDelay(200);
  121. // LOS_TaskDelay(200);
  122. // LOS_SwtmrTimeGet(id1, &tick);
  123. // dprintf("tick = %d\n", tick);
  124. // LOS_SwtmrStop(id1);
  125. // dprintf("stop Timer1 sucess\n");
  126. // // LOS_SwtmrStart(id1);
  127. // // LOS_TaskDelay(1000);
  128. // LOS_SwtmrDelete(id1);
  129. // dprintf("delete Timer1 sucess\n");
  130. write_iic(OUT0);
  131. write_iic(OUT1);
  132. //usleep(1000*1000);
  133. usleep(100);
  134. int a=0;
  135. // LOS_SwtmrStart(id2);
  136. // dprintf("start Timer2 sucess\n");
  137. // //LOS_TaskDelay(1000);
  138. // LOS_TaskDelay(200);
  139. // LOS_SwtmrTimeGet(id2, &tick);
  140. // dprintf("tick = %d\n", tick);
  141. // LOS_SwtmrStop(id2);
  142. // dprintf("stop Timer2 sucess\n");
  143. // LOS_SwtmrDelete(id2);
  144. // dprintf("delete Timer2 sucess\n");
  145. // LOS_SwtmrStop(id2);
  146. // LOS_SwtmrDelete(id2);
  147. // write_data(0b11111110,0b11111111);
  148. // LOS_TaskDelay(1000);
  149. // write_data(0b11111101,0b11111000);
  150. // LOS_TaskDelay(1000);
  151. // write_data(0b00100000,0b00000000);
  152. // LOS_TaskDelay(1000);
  153. // write_data(0b00100000,0b00000000);
  154. // LOS_TaskDelay(1000);
  155. // write_data(0b00010000,0b11111111);
  156. // LOS_TaskDelay(1000);
  157. while(1){
  158. for(int i=0;i<8;i++){
  159. unsigned char hex = 0;
  160. for(int j=0;j<8;j++){
  161. hex = hex <<1;
  162. if(alpha[i][j] == '1'){
  163. hex = hex | 0x1;
  164. }
  165. }
  166. // OUT1[2] = hex;
  167. // OUT0[1] = ~(1 << i);
  168. write_data(~(1 << i),hex);
  169. for(int a=0;a<3;a++){printf("+++a:%d\r\n",a);}
  170. }
  171. printf("set count :%d\r\n",a=a+1);
  172. }
  173. }
  174. void iic_entry(){
  175. printf("iic_entry called \n");
  176. osThreadAttr_t attr;
  177. attr.name = "thread_iic";
  178. attr.attr_bits = 0U; // 如果为1 ,则可以使用osThreadJoin函数
  179. attr.cb_mem = NULL; //控制快的指针
  180. attr.cb_size = 0U;
  181. attr.stack_mem = NULL; //栈内存指针
  182. attr.stack_size = 1024 * 4; //栈大小
  183. attr.priority = 25; //优先级
  184. if (osThreadNew((osThreadFunc_t)iic, NULL, &attr) == NULL)
  185. {
  186. printf("Fail to create thread!\n");
  187. }
  188. }
  189. APP_FEATURE_INIT(iic_entry);
  190. // /***** 定时器创建 *****/
  191. // static void Timer_example(void)
  192. // {
  193. // UINT16 id1; // Timer1 id
  194. // UINT16 id2; // Timer2 id
  195. // UINT32 tick;
  196. // LOS_SwtmrCreate(1000, LOS_SWTMR_MODE_ONCE, Timer1_CallBack, &id1, 1);
  197. // LOS_SwtmrCreate(100, LOS_SWTMR_MODE_PERIOD, Timer2_CallBack, &id2, 1);
  198. // dprintf("create Timer1 success\n");
  199. // LOS_SwtmrStart(id1);
  200. // dprintf("start Timer1 sucess\n");
  201. // LOS_TaskDelay(200);
  202. // LOS_SwtmrTimeGet(id1, &tick);
  203. // dprintf("tick =%d\n", tick);
  204. // LOS_SwtmrStop(id1);
  205. // dprintf("stop Timer1 sucess\n");
  206. // LOS_SwtmrStart(id1);
  207. // LOS_TaskDelay(1000);
  208. // LOS_SwtmrDelete(id1);
  209. // dprintf("delete Timer1 sucess\n");
  210. // LOS_SwtmrStart(id2);
  211. // dprintf("start Timer2\n");
  212. // LOS_TaskDelay(1000);
  213. // LOS_SwtmrStop(id2);
  214. // LOS_SwtmrDelete(id2);
  215. // }
  216. // APP_FEATURE_INIT(Timer_example);