matrix6.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. #include <stdio.h>
  2. #include "ohos_init.h"
  3. #include "iot_gpio.h"
  4. #include "iot_gpio_ex.h"
  5. #include "iot_i2c.h"
  6. #include "cmsis_os2.h"
  7. // #include "ohos_init.h"
  8. // #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <unistd.h>
  12. // #include <cmsis_os2.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_t exec1, exec2, exec3;
  34. /***** 定时器1 回调函数 *****/
  35. void Timer1_Callback(void *arg)
  36. {
  37. (void)arg;
  38. //printf("This is Timer1_Callback!\r\n");
  39. }
  40. /***** 定时器2 回调函数 *****/
  41. void Timer2_Callback(void *arg)
  42. {
  43. (void)arg;
  44. //printf("This is Timer2_Callback!\r\n");
  45. }
  46. /***** 定时器3 回调函数 *****/
  47. void Timer3_Callback(void *arg)
  48. {
  49. (void)arg;
  50. //printf("This is Timer3_Callback!\r\n");
  51. }
  52. int write_iic(uint8_t* data){
  53. int ret = IoTI2cWrite(IIC_IDX, (ADDR << 1) | 0x00, data, 3);
  54. //printf("***@@@###$$$ ret = %d\n",ret);
  55. return ret;
  56. }
  57. //start
  58. uint8_t CFG0[] = {CMD_CFG0,0x0,0x0}; //配置为输出
  59. uint8_t CFG1[] = {CMD_CFG1,0x0,0x0}; //配置为输出
  60. uint8_t OUT0[] = {CMD_OUT0,0x00,0x00}; // 输出
  61. uint8_t OUT1[] = {CMD_OUT1,0x00,0x00}; // 输出
  62. char alpha[8][9] = {
  63. "11111111",
  64. "11100000",
  65. "11100000",
  66. "11111111",
  67. "11111111",
  68. "11100000",
  69. "11100000",
  70. "11111111"
  71. };
  72. // char alpha[8][9] = {
  73. // "00011111",
  74. // "11100000",
  75. // "11100000",
  76. // "00011111",
  77. // "00011111",
  78. // "11100000",
  79. // "11100000",
  80. // "00011111"
  81. // };
  82. void write_data(char byte1,char byte2){
  83. //printf("write %02x ,%02x\n",byte1,byte2);
  84. uint8_t data[3] = {CMD_OUT0,0x00,0x00};
  85. data[1] = byte1;
  86. data[2] = byte2;
  87. write_iic(data);
  88. data[0] = CMD_OUT1;
  89. write_iic(data);
  90. //printf("write %02x ,%02x\n",byte1,byte2);
  91. // printf("data[3] = %c\n",data[3]);
  92. // printf("data[2] = %c\n",data[2]);
  93. // printf("data[1] = %c\n",data[1]);
  94. // printf("data[0] = %c\n",data[0]);
  95. }
  96. void iic(void* args ){
  97. int i,j;
  98. osTimerId_t id1, id2, id3;
  99. uint32_t timerDelay;
  100. osStatus_t status;
  101. // exec1 = 1U;
  102. // id1 = osTimerNew(Timer1_Callback, osTimerPeriodic, &exec1, NULL);
  103. // if (id1 != NULL)
  104. // {
  105. // printf(" id1 != NULL\r\n");
  106. // // Hi3861 1U=10ms,100U=1S
  107. // timerDelay = 100U;
  108. // printf(" start status1\r\n ");
  109. // status = osTimerStart(id1, timerDelay);
  110. // printf(" ******end status1\r\n ");
  111. // if (status != osOK)
  112. // {
  113. // // Timer could not be started
  114. // printf(" Timer could not be started\r\n ");
  115. // }else{
  116. // printf(" Timer could be started\r\n "); //have
  117. // }
  118. // printf(" end status1\r\n "); //have
  119. // }else{
  120. // printf(" id1 = NULL\r\n ");
  121. // }
  122. // exec2 = 1U;
  123. // id2 = osTimerNew(Timer2_Callback, osTimerPeriodic, &exec2, NULL);
  124. // if (id2 != NULL)
  125. // {
  126. // printf(" id2 != NULL\r\n ");
  127. // // Hi3861 1U=10ms,300U=3S
  128. // timerDelay = 300U;
  129. // printf(" start status2\r\n ");
  130. // status = osTimerStart(id2, timerDelay);
  131. // printf(" *******end status2\r\n ");
  132. // if (status != osOK)
  133. // {
  134. // // Timer could not be started
  135. // printf(" Timer could not be started \r\n");
  136. // }else{
  137. // printf(" Timer could be started\r\n ");
  138. // }
  139. // printf(" end status2\r\n ");
  140. // }else{
  141. // printf(" id2 = NULL\r\n ");
  142. // }
  143. printf("iic thread running...");
  144. IoTGpioInit(IIC_SDA);
  145. IoTGpioInit(IIC_SCL);
  146. IoTGpioSetFunc(IIC_SDA, IOT_GPIO_FUNC_GPIO_0_I2C1_SDA);
  147. IoTGpioSetFunc(IIC_SCL, IOT_GPIO_FUNC_GPIO_1_I2C1_SCL);
  148. IoTI2cInit(IIC_IDX, 400000);
  149. write_iic(CFG0);
  150. write_iic(CFG1);
  151. //usleep(20);
  152. //usleep(200*1000);
  153. exec1 = 1U;
  154. id1 = osTimerNew(Timer1_Callback, osTimerPeriodic, &exec1, NULL);
  155. if (id1 != NULL)
  156. {
  157. printf(" id1 != NULL\r\n");
  158. // Hi3861 1U=10ms,100U=1S
  159. timerDelay = 100U;
  160. printf(" start status1\r\n ");
  161. status = osTimerStart(id1, timerDelay);
  162. printf(" ******end status1\r\n ");
  163. if (status != osOK)
  164. {
  165. // Timer could not be started
  166. printf(" Timer could not be started\r\n ");
  167. }else{
  168. printf(" Timer could be started\r\n "); //have
  169. }
  170. printf(" end status1\r\n "); //have
  171. }else{
  172. printf(" id1 = NULL\r\n ");
  173. }
  174. write_iic(OUT0);
  175. write_iic(OUT1);
  176. //usleep(1000*1000);
  177. //usleep(100);
  178. exec2 = 1U;
  179. id2 = osTimerNew(Timer2_Callback, osTimerPeriodic, &exec2, NULL);
  180. if (id2 != NULL)
  181. {
  182. printf(" id2 != NULL\r\n ");
  183. // Hi3861 1U=10ms,300U=3S
  184. timerDelay = 300U;
  185. printf(" start status2\r\n ");
  186. status = osTimerStart(id2, timerDelay);
  187. printf(" *******end status2\r\n ");
  188. if (status != osOK)
  189. {
  190. // Timer could not be started
  191. printf(" Timer could not be started \r\n");
  192. }else{
  193. printf(" Timer could be started\r\n ");
  194. }
  195. printf(" end status2\r\n ");
  196. }else{
  197. printf(" id2 = NULL\r\n ");
  198. }
  199. while(1){
  200. for(int i=0;i<8;i++){
  201. unsigned char hex = 0;
  202. for(int j=0;j<8;j++){
  203. hex = hex <<1;
  204. if(alpha[i][j] == '1'){
  205. hex = hex | 0x1;
  206. }
  207. }
  208. OUT1[2] = hex;
  209. OUT0[1] = ~(1 << i);
  210. write_data(~(1 << i),hex);
  211. //usleep(100); //延时,肉眼无法观察的速度
  212. //alpha[i][j] = 0xff; //消隐,防止给下次扫描带来影响
  213. //alpha[i][j] == '0';
  214. // char alpha[8][9] = {
  215. // "00000000",
  216. // "00000000",
  217. // "00000000",
  218. // "00000000",
  219. // "00000000",
  220. // "00000000",
  221. // "00000000",
  222. // "00000000"
  223. // };
  224. //hex = 0; //消隐,防止给下次扫描带来影响
  225. //usleep(100);
  226. // printf("OUT1[2] = %c\n",OUT1[2]);
  227. // printf("OUT1[1] = %c\n",OUT1[1]);
  228. }
  229. //usleep(10000); //延时,肉眼无法观察的速度
  230. exec3 = 1U;
  231. id3 = osTimerNew(Timer3_Callback, osTimerPeriodic, &exec3, NULL);
  232. if (id3 != NULL)
  233. {
  234. printf(" id3 != NULL\r\n ");
  235. // Hi3861 1U=10ms,300U=3S
  236. timerDelay = 300U;
  237. printf(" start status3\r\n ");
  238. status = osTimerStart(id3, timerDelay);
  239. printf(" *******end status3\r\n ");
  240. if (status != osOK)
  241. {
  242. // Timer could not be started
  243. printf(" Timer could not be started \r\n");
  244. }else{
  245. printf(" Timer could be started\r\n ");
  246. }
  247. printf(" end status3\r\n ");
  248. }
  249. // else
  250. // {
  251. // printf(" id3 = NULL\r\n ");
  252. // }
  253. // //alpha[i][j] = 0xff; //消隐,防止给下次扫描带来影响
  254. //osDelay(100);
  255. alpha[i][j] = '0';
  256. }
  257. }
  258. void iic_entry(){
  259. printf("iic_entry called \n");
  260. osThreadAttr_t attr;
  261. attr.name = "thread_iic";
  262. attr.attr_bits = 0U; // 如果为1 ,则可以使用osThreadJoin函数
  263. attr.cb_mem = NULL; //控制快的指针
  264. attr.cb_size = 0U;
  265. attr.stack_mem = NULL; //栈内存指针
  266. attr.stack_size = 1024 * 4; //栈大小
  267. attr.priority = 25; //优先级
  268. if (osThreadNew((osThreadFunc_t)iic, NULL, &attr) == NULL)
  269. {
  270. printf("Fail to create thread!\n");
  271. }
  272. }
  273. APP_FEATURE_INIT(iic_entry);
  274. // #include <stdio.h>
  275. // #include <string.h>
  276. // #include <unistd.h>
  277. // #include "ohos_init.h"
  278. // #include "cmsis_os2.h"
  279. // uint32_t exec1, exec2;
  280. // /***** 定时器1 回调函数 *****/
  281. // void Timer1_Callback(void *arg)
  282. // {
  283. // (void)arg;
  284. // printf("This is BearPi Harmony Timer1_Callback!\r\n");
  285. // }
  286. // /***** 定时器2 回调函数 *****/
  287. // void Timer2_Callback(void *arg)
  288. // {
  289. // (void)arg;
  290. // printf("This is BearPi Harmony Timer2_Callback!\r\n");
  291. // }
  292. // /***** 定时器创建 *****/
  293. // static void Timer_example(void)
  294. // {
  295. // osTimerId_t id1, id2;
  296. // uint32_t timerDelay;
  297. // osStatus_t status;
  298. // exec1 = 1U;
  299. // id1 = osTimerNew(Timer1_Callback, osTimerPeriodic, &exec1, NULL);
  300. // if (id1 != NULL)
  301. // {
  302. // printf(" id1 != NULL\r\n");
  303. // // Hi3861 1U=10ms,100U=1S
  304. // timerDelay = 100U;
  305. // printf(" start status1\r\n ");
  306. // status = osTimerStart(id1, timerDelay);
  307. // printf(" ******end status1\r\n ");
  308. // if (status != osOK)
  309. // {
  310. // // Timer could not be started
  311. // printf(" Timer could not be started\r\n ");
  312. // }else{
  313. // printf(" Timer could be started\r\n "); //have
  314. // }
  315. // printf(" end status1\r\n "); //have
  316. // }else{
  317. // printf(" id1 = NULL\r\n ");
  318. // }
  319. // exec2 = 1U;
  320. // id2 = osTimerNew(Timer2_Callback, osTimerPeriodic, &exec2, NULL);
  321. // if (id2 != NULL)
  322. // {
  323. // printf(" id2 != NULL\r\n ");
  324. // // Hi3861 1U=10ms,300U=3S
  325. // timerDelay = 300U;
  326. // printf(" start status2\r\n ");
  327. // status = osTimerStart(id2, timerDelay);
  328. // printf(" *******end status2\r\n ");
  329. // if (status != osOK)
  330. // {
  331. // // Timer could not be started
  332. // printf(" Timer could not be started \r\n");
  333. // }else{
  334. // printf(" Timer could be started\r\n ");
  335. // }
  336. // printf(" end status2\r\n ");
  337. // }else{
  338. // printf(" id2 = NULL\r\n ");
  339. // }
  340. // }
  341. // APP_FEATURE_INIT(Timer_example);
  342. //原文链接:https://blog.csdn.net/qq_36347513/article/details/117650058