matnm5.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. typedef unsigned int u16; //对数据类型进行声明定义
  42. typedef unsigned char u8;
  43. UINT32 g_timerCount1 = 0;
  44. UINT32 g_timerCount2 = 0;
  45. // VOID Timer1_CallBack(UINT32 arg)
  46. // {
  47. // UINT64 lastTick;
  48. // g_timerCount1++;
  49. // lastTick=(UINT32)LOS_TickCountGet();
  50. // // dprintf("g_timerCount1=%d\n", g_timerCount1);
  51. // // dprintf("tick_last1=%d\n", lastTick);
  52. // }
  53. // VOID Timer2_CallBack(UINT32 arg)
  54. // {
  55. // UINT64 lastTick;
  56. // lastTick=(UINT32)LOS_TickCountGet();
  57. // g_timerCount2++;
  58. // // dprintf("g_timerCount2=%d\n", g_timerCount2);
  59. // // dprintf("tick_last2=%d\n", lastTick);
  60. // }
  61. /*延时函数*/
  62. void delayab(u16 i)
  63. {
  64. while(i--);
  65. }
  66. void delaynm(unsigned int z)
  67. {
  68. unsigned int x,y;
  69. for(x=z;x>0;x--)
  70. for(y=120;y>0;y--);
  71. }
  72. void delay1(void)
  73. {
  74. unsigned char i,j;
  75. for(i=10;i>0;i--)
  76. for(j=248;j>0;j--);
  77. }
  78. void delay2(void)
  79. {
  80. unsigned char i,j,k;
  81. for(k=10;k>0;k--)
  82. for(i=20;i>0;i--)
  83. for(j=248;j>0;j--);
  84. }
  85. int write_iic(uint8_t* data){
  86. int ret = IoTI2cWrite(IIC_IDX, (ADDR << 1) | 0x00, data, 3);
  87. //printf("***@@@###$$$ ret = %d\n",ret);
  88. return ret;
  89. }
  90. //start
  91. uint8_t CFG0[] = {CMD_CFG0,0x0,0x0}; //配置为输出
  92. uint8_t CFG1[] = {CMD_CFG1,0x0,0x0}; //配置为输出
  93. uint8_t OUT0[] = {CMD_OUT0,0x00,0x00}; // 输出
  94. uint8_t OUT1[] = {CMD_OUT1,0x00,0x00}; // 输出
  95. char alpha[8][9] = {
  96. "11111111",
  97. "11100000",
  98. "11100000",
  99. "11111111",
  100. "11111111",
  101. "11100000",
  102. "11100000",
  103. "11111111"
  104. };
  105. // char alpha[8][9] = {
  106. // "00011111",
  107. // "11100000",
  108. // "11100000",
  109. // "00011111",
  110. // "00011111",
  111. // "11100000",
  112. // "11100000",
  113. // "00011111"
  114. // };
  115. void write_data(char byte1,char byte2){
  116. //printf("write %02x ,%02x\n",byte1,byte2);
  117. uint8_t data[3] = {CMD_OUT0,0x00,0x00};
  118. data[1] = byte1;
  119. data[2] = byte2;
  120. write_iic(data);
  121. data[0] = CMD_OUT1;
  122. write_iic(data);
  123. }
  124. void iic(void* args ){
  125. int i,j;
  126. UINT16 id1; // Timer1 id
  127. UINT16 id2; // Timer2 id
  128. UINT32 tick;
  129. // LOS_SwtmrCreate(1000, LOS_SWTMR_MODE_ONCE, Timer1_CallBack, &id1, 1);
  130. // LOS_SwtmrCreate(100, LOS_SWTMR_MODE_PERIOD, Timer2_CallBack, &id2, 1);
  131. // LOS_SwtmrCreate(200, LOS_SWTMR_MODE_PERIOD, Timer1_CallBack, &id1, NULL); //时间的单位是ms
  132. // LOS_SwtmrCreate(80, LOS_SWTMR_MODE_PERIOD, Timer2_CallBack, &id2, NULL);
  133. // dprintf("create Timer1 success\n");
  134. printf("iic thread running...\n");
  135. IoTGpioInit(IIC_SDA);
  136. IoTGpioInit(IIC_SCL);
  137. IoTGpioSetFunc(IIC_SDA, IOT_GPIO_FUNC_GPIO_0_I2C1_SDA);
  138. IoTGpioSetFunc(IIC_SCL, IOT_GPIO_FUNC_GPIO_1_I2C1_SCL);
  139. IoTI2cInit(IIC_IDX, 400000);
  140. write_iic(CFG0);
  141. write_iic(CFG1);
  142. usleep(20);
  143. //usleep(200*1000);
  144. // LOS_SwtmrStart(id1);
  145. // dprintf("start Timer1 sucess\n");
  146. // //LOS_TaskDelay(200);
  147. // LOS_TaskDelay(200);
  148. // LOS_SwtmrTimeGet(id1, &tick);
  149. // dprintf("tick = %d\n", tick);
  150. // LOS_SwtmrStop(id1);
  151. // dprintf("stop Timer1 sucess\n");
  152. // // LOS_SwtmrStart(id1);
  153. // // LOS_TaskDelay(1000);
  154. // LOS_SwtmrDelete(id1);
  155. // dprintf("delete Timer1 sucess\n");
  156. write_iic(OUT0);
  157. write_iic(OUT1);
  158. //usleep(1000*1000);
  159. usleep(100);
  160. int a=0;
  161. // LOS_SwtmrStart(id2);
  162. // dprintf("start Timer2 sucess\n");
  163. // //LOS_TaskDelay(1000);
  164. // LOS_TaskDelay(200);
  165. // LOS_SwtmrTimeGet(id2, &tick);
  166. // dprintf("tick = %d\n", tick);
  167. // LOS_SwtmrStop(id2);
  168. // dprintf("stop Timer2 sucess\n");
  169. // LOS_SwtmrDelete(id2);
  170. // dprintf("delete Timer2 sucess\n");
  171. // LOS_SwtmrStop(id2);
  172. // LOS_SwtmrDelete(id2);
  173. // write_data(0b11111110,0b11111111);
  174. // LOS_TaskDelay(1000);
  175. // write_data(0b11111101,0b11111000);
  176. // LOS_TaskDelay(1000);
  177. // write_data(0b00100000,0b00000000);
  178. // LOS_TaskDelay(1000);
  179. // write_data(0b00100000,0b00000000);
  180. // LOS_TaskDelay(1000);
  181. // write_data(0b00010000,0b11111111);
  182. // LOS_TaskDelay(1000);
  183. while(1){
  184. for(int i=0;i<8;i++){
  185. unsigned char hex = 0;
  186. for(int j=0;j<8;j++){
  187. hex = hex <<1;
  188. if(alpha[i][j] == '1'){
  189. hex = hex | 0x1;
  190. }
  191. }
  192. // OUT1[2] = hex;
  193. // OUT0[1] = ~(1 << i);
  194. write_data(~(1 << i),hex);
  195. for(int a=0;a<3;a++){printf("+++a:%d\r\n",a);}
  196. //delay2();
  197. }
  198. printf("set count :%d\r\n",a=a+1);
  199. //alpha[i][j] == '0';
  200. }
  201. }
  202. void iic_entry(){
  203. printf("iic_entry called \n");
  204. osThreadAttr_t attr;
  205. attr.name = "thread_iic";
  206. attr.attr_bits = 0U; // 如果为1 ,则可以使用osThreadJoin函数
  207. attr.cb_mem = NULL; //控制快的指针
  208. attr.cb_size = 0U;
  209. attr.stack_mem = NULL; //栈内存指针
  210. attr.stack_size = 1024 * 4; //栈大小
  211. attr.priority = 25; //优先级
  212. if (osThreadNew((osThreadFunc_t)iic, NULL, &attr) == NULL)
  213. {
  214. printf("Fail to create thread!\n");
  215. }
  216. }
  217. APP_FEATURE_INIT(iic_entry);
  218. // /***** 定时器创建 *****/
  219. // static void Timer_example(void)
  220. // {
  221. // UINT16 id1; // Timer1 id
  222. // UINT16 id2; // Timer2 id
  223. // UINT32 tick;
  224. // LOS_SwtmrCreate(1000, LOS_SWTMR_MODE_ONCE, Timer1_CallBack, &id1, 1);
  225. // LOS_SwtmrCreate(100, LOS_SWTMR_MODE_PERIOD, Timer2_CallBack, &id2, 1);
  226. // dprintf("create Timer1 success\n");
  227. // LOS_SwtmrStart(id1);
  228. // dprintf("start Timer1 sucess\n");
  229. // LOS_TaskDelay(200);
  230. // LOS_SwtmrTimeGet(id1, &tick);
  231. // dprintf("tick =%d\n", tick);
  232. // LOS_SwtmrStop(id1);
  233. // dprintf("stop Timer1 sucess\n");
  234. // LOS_SwtmrStart(id1);
  235. // LOS_TaskDelay(1000);
  236. // LOS_SwtmrDelete(id1);
  237. // dprintf("delete Timer1 sucess\n");
  238. // LOS_SwtmrStart(id2);
  239. // dprintf("start Timer2\n");
  240. // LOS_TaskDelay(1000);
  241. // LOS_SwtmrStop(id2);
  242. // LOS_SwtmrDelete(id2);
  243. // }
  244. // APP_FEATURE_INIT(Timer_example);