matwo.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. LOS_SwtmrStart(id2);
  135. dprintf("start Timer2 sucess\n");
  136. //LOS_TaskDelay(1000);
  137. LOS_TaskDelay(200);
  138. LOS_SwtmrTimeGet(id2, &tick);
  139. dprintf("tick = %d\n", tick);
  140. LOS_SwtmrStop(id2);
  141. dprintf("stop Timer2 sucess\n");
  142. LOS_SwtmrDelete(id2);
  143. dprintf("delete Timer2 sucess\n");
  144. // LOS_SwtmrStop(id2);
  145. // LOS_SwtmrDelete(id2);
  146. while(1){
  147. for(int i=0;i<8;i++){
  148. unsigned char hex = 0;
  149. for(int j=0;j<8;j++){
  150. hex = hex <<1;
  151. if(alpha[i][j] == '1'){
  152. hex = hex | 0x1;
  153. }
  154. }
  155. OUT1[2] = hex;
  156. OUT0[1] = ~(1 << i);
  157. write_data(~(1 << i),hex);
  158. //usleep(20); //延时,肉眼无法观察的速度
  159. //osDelay(100);
  160. LOS_SwtmrStart(id2);
  161. //dprintf("***### start Timer2 sucess\n");
  162. //LOS_TaskDelay(1000);
  163. LOS_TaskDelay(1); //时间数值越小,数码管一行一行闪烁亮灯的速度越快
  164. LOS_SwtmrTimeGet(id2, &tick);
  165. //dprintf("***### tick = %d\n", tick);
  166. LOS_SwtmrStop(id2);
  167. //dprintf("***### stop Timer2 sucess\n");
  168. LOS_SwtmrDelete(id2);
  169. //dprintf("***### delete Timer2 sucess\n");
  170. // usleep(10000); //延时,肉眼无法观察的速度
  171. // //alpha[i][j] = 0xff; //消隐,防止给下次扫描带来影响
  172. alpha[i][j] == '0';
  173. }
  174. // LOS_SwtmrStart(id2);
  175. // dprintf("***### start Timer2 sucess\n");
  176. // //LOS_TaskDelay(1000);
  177. // LOS_TaskDelay(300);
  178. // LOS_SwtmrTimeGet(id2, &tick);
  179. // dprintf("***### tick = %d\n", tick);
  180. // LOS_SwtmrStop(id2);
  181. // dprintf("***### stop Timer2 sucess\n");
  182. // LOS_SwtmrDelete(id2);
  183. // dprintf("***### delete Timer2 sucess\n");
  184. // // usleep(10000); //延时,肉眼无法观察的速度
  185. // // //alpha[i][j] = 0xff; //消隐,防止给下次扫描带来影响
  186. alpha[i][j] == '0';
  187. }
  188. //usleep(300); //延时,肉眼无法观察的速度
  189. //alpha[i][j] = 0xff; //消隐,防止给下次扫描带来影响
  190. //alpha[i][j] = '0';
  191. // char alpha[8][9] = {
  192. // "00000000",
  193. // "00000000",
  194. // "00000000",
  195. // "00000000",
  196. // "00000000",
  197. // "00000000",
  198. // "00000000",
  199. // "00000000"
  200. // };
  201. //printf("set finish!\n");
  202. }
  203. void iic_entry(){
  204. printf("iic_entry called \n");
  205. osThreadAttr_t attr;
  206. attr.name = "thread_iic";
  207. attr.attr_bits = 0U; // 如果为1 ,则可以使用osThreadJoin函数
  208. attr.cb_mem = NULL; //控制快的指针
  209. attr.cb_size = 0U;
  210. attr.stack_mem = NULL; //栈内存指针
  211. attr.stack_size = 1024 * 4; //栈大小
  212. attr.priority = 25; //优先级
  213. if (osThreadNew((osThreadFunc_t)iic, NULL, &attr) == NULL)
  214. {
  215. printf("Fail to create thread!\n");
  216. }
  217. }
  218. APP_FEATURE_INIT(iic_entry);
  219. // /***** 定时器创建 *****/
  220. // static void Timer_example(void)
  221. // {
  222. // UINT16 id1; // Timer1 id
  223. // UINT16 id2; // Timer2 id
  224. // UINT32 tick;
  225. // LOS_SwtmrCreate(1000, LOS_SWTMR_MODE_ONCE, Timer1_CallBack, &id1, 1);
  226. // LOS_SwtmrCreate(100, LOS_SWTMR_MODE_PERIOD, Timer2_CallBack, &id2, 1);
  227. // dprintf("create Timer1 success\n");
  228. // LOS_SwtmrStart(id1);
  229. // dprintf("start Timer1 sucess\n");
  230. // LOS_TaskDelay(200);
  231. // LOS_SwtmrTimeGet(id1, &tick);
  232. // dprintf("tick =%d\n", tick);
  233. // LOS_SwtmrStop(id1);
  234. // dprintf("stop Timer1 sucess\n");
  235. // LOS_SwtmrStart(id1);
  236. // LOS_TaskDelay(1000);
  237. // LOS_SwtmrDelete(id1);
  238. // dprintf("delete Timer1 sucess\n");
  239. // LOS_SwtmrStart(id2);
  240. // dprintf("start Timer2\n");
  241. // LOS_TaskDelay(1000);
  242. // LOS_SwtmrStop(id2);
  243. // LOS_SwtmrDelete(id2);
  244. // }
  245. // APP_FEATURE_INIT(Timer_example);