AS7341.LST 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. C251 COMPILER V5.60.0, AS7341 03/12/24 17:22:31 PAGE 1
  2. C251 COMPILER V5.60.0, COMPILATION OF MODULE AS7341
  3. OBJECT MODULE PLACED IN AS7341.OBJ
  4. COMPILER INVOKED BY: C:\Keil_v5\C251\BIN\C251.EXE AS7341.c XSMALL INTR2 BROWSE DEBUG
  5. stmt level source
  6. 1 #include "AS7341.h"
  7. 2 #include "STC32G.h"
  8. 3 #include <intrins.h>
  9. 4 #include "delay.h"
  10. 5
  11. 6 sbit sda=P0^6; //数据线
  12. 7 sbit scl=P0^5; //时钟线
  13. 8
  14. 9 eMode_t measureMode;
  15. 10 void AS7341_IIC_Init(void){
  16. 11 1
  17. 12 1 sda=1;
  18. 13 1 scl=1;
  19. 14 1 delay2us();
  20. 15 1 delay2us();
  21. 16 1 }
  22. 17
  23. 18
  24. 19 void AS7341_IIC_Start(void){
  25. 20 1 sda=1;
  26. 21 1 delay2us();
  27. 22 1 scl=1;
  28. 23 1 delay2us();
  29. 24 1 sda=0;
  30. 25 1 delay2us();
  31. 26 1 scl=0;
  32. 27 1 delay2us();
  33. 28 1 }
  34. 29
  35. 30 void AS7341_IIC_Stop(void){
  36. 31 1 sda=0;
  37. 32 1 delay2us();
  38. 33 1 scl=1;
  39. 34 1 delay2us();
  40. 35 1 sda=1;
  41. 36 1 delay2us();
  42. 37 1 scl=0;
  43. 38 1 delay2us();
  44. 39 1 }
  45. 40
  46. 41 unsigned char AS7341_IIC_Wait_Ack(void){
  47. 42 1 bit ack1=0;
  48. 43 1
  49. 44 1 sda=1;
  50. 45 1 delay2us();
  51. 46 1
  52. 47 1 scl=1;
  53. 48 1 delay2us();
  54. 49 1
  55. 50 1 ack1=sda;
  56. 51 1
  57. 52 1 scl=0;
  58. 53 1 delay2us();
  59. 54 1
  60. 55 1 return ack1;
  61. 56 1 }
  62. 57
  63. 58 void AS7341_IIC_Ack(void){
  64. 59 1 sda=0;
  65. C251 COMPILER V5.60.0, AS7341 03/12/24 17:22:31 PAGE 2
  66. 60 1 delay2us();
  67. 61 1
  68. 62 1 scl=1;
  69. 63 1 delay2us();
  70. 64 1 scl=0;
  71. 65 1 delay2us();
  72. 66 1 }
  73. 67
  74. 68 void AS7341_IIC_NAck(void){
  75. 69 1 scl=1;
  76. 70 1 delay2us();
  77. 71 1 sda=0;
  78. 72 1 scl=0;
  79. 73 1 delay2us();
  80. 74 1 }
  81. 75
  82. 76 void AS7341_IIC_Send_Byte(unsigned char DATA){
  83. 77 1
  84. 78 1 unsigned char Bit_Counter=8;
  85. 79 1 while(Bit_Counter>0)
  86. 80 1 {
  87. 81 2
  88. 82 2 sda=(bit)(DATA&0x80);
  89. 83 2 DATA<<=1;
  90. 84 2
  91. 85 2 scl=1;
  92. 86 2 delay2us();
  93. 87 2 scl=0;
  94. 88 2 delay2us();
  95. 89 2
  96. 90 2 Bit_Counter--;
  97. 91 2 }
  98. 92 1 }
  99. 93
  100. 94 unsigned char AS7341_IIC_Read_Byte(unsigned char ack){
  101. 95 1 unsigned char x=0,y=8;
  102. 96 1
  103. 97 1 sda=1;
  104. 98 1
  105. 99 1 while(y>0)
  106. 100 1 {
  107. 101 2 scl=1;
  108. 102 2 delay2us();
  109. 103 2
  110. 104 2
  111. 105 2 if(sda)
  112. 106 2 x=x|0x01;
  113. 107 2 else
  114. 108 2 x=x&0xfe;
  115. 109 2
  116. 110 2
  117. 111 2 scl=0;
  118. 112 2 delay2us();
  119. 113 2
  120. 114 2 if(y>1)
  121. 115 2 x=x<<1;
  122. 116 2
  123. 117 2 y--;
  124. 118 2 }
  125. 119 1 if(!ack)
  126. 120 1 AS7341_IIC_NAck();
  127. 121 1 else
  128. 122 1 AS7341_IIC_Ack();
  129. 123 1 return x;
  130. 124 1 }
  131. 125
  132. C251 COMPILER V5.60.0, AS7341 03/12/24 17:22:31 PAGE 3
  133. 126 /******************************************************************************
  134. 127 function: Read one byte of data to AS7341 via I2C
  135. 128 parameter:
  136. 129 Addr: Register address
  137. 130 Info:
  138. 131 ******************************************************************************/
  139. 132 UBYTE AS7341_Read_Byte(UBYTE Addr)
  140. 133 {
  141. 134 1 UBYTE reg_data=0;
  142. 135 1
  143. 136 1 AS7341_IIC_Start();
  144. 137 1 AS7341_IIC_Send_Byte(AS7341_ADDRESS << 1 | Write_BIT);
  145. 138 1 if(AS7341_IIC_Wait_Ack())return 0;
  146. 139 1 AS7341_IIC_Send_Byte(Addr);
  147. 140 1 if(AS7341_IIC_Wait_Ack())return 0;
  148. 141 1
  149. 142 1 AS7341_IIC_Start();
  150. 143 1 AS7341_IIC_Send_Byte(AS7341_ADDRESS << 1 | Read_BIT);
  151. 144 1 if(AS7341_IIC_Wait_Ack())return 0;
  152. 145 1 reg_data= AS7341_IIC_Read_Byte(1);
  153. 146 1
  154. 147 1 AS7341_IIC_Stop();
  155. 148 1
  156. 149 1 return reg_data;
  157. 150 1 }
  158. 151
  159. 152 ///******************************************************************************
  160. 153 //function: Send one byte of data to AS7341 via I2C
  161. 154 //parameter:
  162. 155 // Addr: Register address
  163. 156 // Value: Write to the value of the register
  164. 157 //Info:
  165. 158 //******************************************************************************/
  166. 159 ///**
  167. 160 // * @brief AS7341写数据
  168. 161 // */
  169. 162 UBYTE AS7341_Write_Byte(UBYTE Addr, UBYTE Value)
  170. 163 {
  171. 164 1
  172. 165 1
  173. 166 1 AS7341_IIC_Start();
  174. 167 1
  175. 168 1 AS7341_IIC_Send_Byte(AS7341_ADDRESS << 1 | Write_BIT);
  176. 169 1 if(AS7341_IIC_Wait_Ack())return 0;
  177. 170 1 AS7341_IIC_Send_Byte(Addr);
  178. 171 1 if(AS7341_IIC_Wait_Ack())return 0;
  179. 172 1 AS7341_IIC_Send_Byte(Value);
  180. 173 1 if(AS7341_IIC_Wait_Ack())return 0;
  181. 174 1
  182. 175 1 AS7341_IIC_Stop();
  183. 176 1 delay2ms();
  184. 177 1 return 1;
  185. 178 1 }
  186. 179
  187. 180 ///******************************************************************************
  188. 181 //function: AS7341 Initialization
  189. 182 //parameter:
  190. 183 //Info:
  191. 184 //******************************************************************************/
  192. 185 UBYTE AS7341_Init(eMode_t mode)
  193. 186 {
  194. 187 1 // DEV_I2C_Init(AS7341_ADDRESS<<1);//8-bit address
  195. 188 1 // printf("ID = 0x%x\r\n",AS7341_Read_Byte(AS7341_ID));//get AS7341 id
  196. 189 1 AS7341_Enable(true);
  197. 190 1 /////////////////////////////measureMode = mode;
  198. 191 1 return 0;
  199. C251 COMPILER V5.60.0, AS7341 03/12/24 17:22:31 PAGE 4
  200. 192 1 }
  201. *** WARNING C47 IN LINE 185 OF AS7341.c: 'mode': unreferenced parameter
  202. 193
  203. 194 ///******************************************************************************
  204. 195 //function: enable PON
  205. 196 //info: power on
  206. 197 //******************************************************************************/
  207. 198 void AS7341_Enable(int flag)
  208. 199 {
  209. 200 1 UBYTE data0, data1,aa;
  210. 201 1 data0 = AS7341_Read_Byte(AS7341_ENABLE);
  211. 202 1 if (flag == true)
  212. 203 1 {
  213. 204 2 data0 = data0 | (1 << 0);
  214. 205 2 }
  215. 206 1 else
  216. 207 1 {
  217. 208 2 data0 = data0 & (~1);
  218. 209 2 }
  219. 210 1 aa=AS7341_Write_Byte(AS7341_ENABLE, data0);
  220. 211 1 // DEV_Delay_ms(500);
  221. 212 1 ////////////////////////////////vTaskDelay(500 / portTICK_PERIOD_MS);
  222. 213 1 data1 = AS7341_Read_Byte(AS7341_ENABLE);
  223. 214 1 if (data1 == data0){
  224. 215 2 //printf("Initialization is complete !\r\n");
  225. 216 2 }else{
  226. 217 2 //printf("initialization is failure !\r\n");
  227. 218 2 //printf("Please check whether the IIC pin is connected correctly and whether the IIC function is
  228. - normal \r\n");
  229. 219 2 //printf("Please press the reset button or run the program again after checking \r\n");
  230. 220 2 while (1)
  231. 221 2 ;
  232. 222 2 }
  233. 223 1 aa=AS7341_Write_Byte(0x00, 0x30);
  234. 224 1 }
  235. 225
  236. 226 ///******************************************************************************
  237. 227 //function: enable Spectral measure
  238. 228 //info:
  239. 229 //******************************************************************************/
  240. 230 void AS7341_EnableSpectralMeasure(int flag) // Enable spectral measurement
  241. 231 {
  242. 232 1 UBYTE data0,aa;
  243. 233 1 data0 = AS7341_Read_Byte(AS7341_ENABLE);
  244. 234 1 if (flag == true)
  245. 235 1 {
  246. 236 2 data0 = data0 | (1 << 1);
  247. 237 2 }
  248. 238 1 else
  249. 239 1 {
  250. 240 2 data0 = data0 & (~(1 << 1));
  251. 241 2 }
  252. 242 1 aa=AS7341_Write_Byte(AS7341_ENABLE, data0);
  253. 243 1 }
  254. 244
  255. 245 ///******************************************************************************
  256. 246 //function: enable SMUX
  257. 247 //info:
  258. 248 //******************************************************************************/
  259. 249 void AS7341_EnableSMUX(int flag) // Enable multiplexer
  260. 250 /*The other available channels can be accessed by amultiplexer (SMUX) connecting them to one of the inter
  261. -nal ADCs.*/
  262. 251 {
  263. 252 1 UBYTE data0,aa;
  264. 253 1 data0 = AS7341_Read_Byte(AS7341_ENABLE);
  265. 254 1 if (flag == true)
  266. C251 COMPILER V5.60.0, AS7341 03/12/24 17:22:31 PAGE 5
  267. 255 1 {
  268. 256 2 data0 = data0 | (1 << 4);
  269. 257 2 }
  270. 258 1 else
  271. 259 1 {
  272. 260 2 data0 = data0 & (~(1 << 4));
  273. 261 2 }
  274. 262 1 aa=AS7341_Write_Byte(AS7341_ENABLE, data0);
  275. 263 1 }
  276. 264
  277. 265 ///******************************************************************************
  278. 266 //function: enable flicker detection
  279. 267 //info:
  280. 268 //******************************************************************************/
  281. 269 void AS7341_EnableFlickerDetection(int flag)
  282. 270 {
  283. 271 1
  284. 272 1 UBYTE data0,aa;
  285. 273 1 data0 = AS7341_Read_Byte(AS7341_ENABLE);
  286. 274 1 if (flag == true)
  287. 275 1 {
  288. 276 2 data0 = data0 | (1 << 6);
  289. 277 2 }
  290. 278 1 else
  291. 279 1 {
  292. 280 2 data0 = data0 & (~(1 << 6));
  293. 281 2 }
  294. 282 1 aa=AS7341_Write_Byte(AS7341_ENABLE, data0);
  295. 283 1 }
  296. 284
  297. 285 ///******************************************************************************
  298. 286 //function: choose model for spectral measurement
  299. 287 //info:
  300. 288 //******************************************************************************/
  301. 289 void AS7341_Config(eMode_t mode)
  302. 290 {
  303. 291 1 UBYTE data0,aa;
  304. 292 1 AS7341_SetBank(1);
  305. 293 1 data0 = AS7341_Read_Byte(AS7341_CONFIG);
  306. 294 1 switch (mode)
  307. 295 1 {
  308. 296 2 case eSpm:
  309. 297 2 {
  310. 298 3 data0 = (data0 & (~3)) | eSpm;
  311. 299 3 };
  312. 300 2 break;
  313. 301 2 case eSyns:
  314. 302 2 {
  315. 303 3 data0 = (data0 & (~3)) | eSyns;
  316. 304 3 };
  317. 305 2 break;
  318. 306 2 case eSynd:
  319. 307 2 {
  320. 308 3 data0 = (data0 & (~3)) | eSynd;
  321. 309 3 };
  322. 310 2 break;
  323. 311 2 default:
  324. 312 2 break;
  325. 313 2 }
  326. 314 1 aa=AS7341_Write_Byte(AS7341_CONFIG, data0);
  327. 315 1 AS7341_SetBank(0);
  328. 316 1 }
  329. 317
  330. 318 ///******************************************************************************
  331. 319 //function: Configure SMUX for sensors F1-4, Clear and NIR
  332. 320 //info:
  333. C251 COMPILER V5.60.0, AS7341 03/12/24 17:22:31 PAGE 6
  334. 321 //******************************************************************************/
  335. 322 void F1F4_Clear_NIR()
  336. 323 {
  337. 324 1 UBYTE aa;
  338. 325 1 aa=AS7341_Write_Byte(0x00, 0x30);
  339. 326 1 aa=AS7341_Write_Byte(0x01, 0x01);
  340. 327 1 aa=AS7341_Write_Byte(0x02, 0x00);
  341. 328 1 aa=AS7341_Write_Byte(0x03, 0x00);
  342. 329 1 aa=AS7341_Write_Byte(0x04, 0x00);
  343. 330 1 aa=AS7341_Write_Byte(0x05, 0x42);
  344. 331 1 aa=AS7341_Write_Byte(0x06, 0x00);
  345. 332 1 aa=AS7341_Write_Byte(0x07, 0x00);
  346. 333 1 aa=AS7341_Write_Byte(0x08, 0x50);
  347. 334 1 aa=AS7341_Write_Byte(0x09, 0x00);
  348. 335 1 aa=AS7341_Write_Byte(0x0A, 0x00);
  349. 336 1 aa=AS7341_Write_Byte(0x0B, 0x00);
  350. 337 1 aa=AS7341_Write_Byte(0x0C, 0x20);
  351. 338 1 aa=AS7341_Write_Byte(0x0D, 0x04);
  352. 339 1 aa=AS7341_Write_Byte(0x0E, 0x00);
  353. 340 1 aa=AS7341_Write_Byte(0x0F, 0x30);
  354. 341 1 aa=AS7341_Write_Byte(0x10, 0x01);
  355. 342 1 aa=AS7341_Write_Byte(0x11, 0x50);
  356. 343 1 aa=AS7341_Write_Byte(0x12, 0x00);
  357. 344 1 aa=AS7341_Write_Byte(0x13, 0x06);
  358. 345 1 }
  359. 346 ///******************************************************************************
  360. 347 //function: Configure SMUX for sensors F5-8, Clear and NIR
  361. 348 //info:
  362. 349 //******************************************************************************/
  363. 350 void F5F8_Clear_NIR()
  364. 351 {
  365. 352 1 UBYTE aa;
  366. 353 1 aa=AS7341_Write_Byte(0x00, 0x00);
  367. 354 1 aa=AS7341_Write_Byte(0x01, 0x00);
  368. 355 1 aa=AS7341_Write_Byte(0x02, 0x00);
  369. 356 1 aa=AS7341_Write_Byte(0x03, 0x40);
  370. 357 1 aa=AS7341_Write_Byte(0x04, 0x02);
  371. 358 1 aa=AS7341_Write_Byte(0x05, 0x00);
  372. 359 1 aa=AS7341_Write_Byte(0x06, 0x10);
  373. 360 1 aa=AS7341_Write_Byte(0x07, 0x03);
  374. 361 1 aa=AS7341_Write_Byte(0x08, 0x50);
  375. 362 1 aa=AS7341_Write_Byte(0x09, 0x10);
  376. 363 1 aa=AS7341_Write_Byte(0x0A, 0x03);
  377. 364 1 aa=AS7341_Write_Byte(0x0B, 0x00);
  378. 365 1 aa=AS7341_Write_Byte(0x0C, 0x00);
  379. 366 1 aa=AS7341_Write_Byte(0x0D, 0x00);
  380. 367 1 aa=AS7341_Write_Byte(0x0E, 0x24);
  381. 368 1 aa=AS7341_Write_Byte(0x0F, 0x00);
  382. 369 1 aa=AS7341_Write_Byte(0x10, 0x00);
  383. 370 1 aa=AS7341_Write_Byte(0x11, 0x50);
  384. 371 1 aa=AS7341_Write_Byte(0x12, 0x00);
  385. 372 1 aa=AS7341_Write_Byte(0x13, 0x06);
  386. 373 1 }
  387. 374 ///******************************************************************************
  388. 375 //function: Configure SMUX for flicker detection
  389. 376 //info:
  390. 377 //******************************************************************************/
  391. 378 void FDConfig()
  392. 379 {
  393. 380 1 UBYTE aa;
  394. 381 1 aa=AS7341_Write_Byte(0x00, 0x00);
  395. 382 1 aa=AS7341_Write_Byte(0x01, 0x00);
  396. 383 1 aa=AS7341_Write_Byte(0x02, 0x00);
  397. 384 1 aa=AS7341_Write_Byte(0x03, 0x00);
  398. 385 1 aa=AS7341_Write_Byte(0x04, 0x00);
  399. 386 1 aa=AS7341_Write_Byte(0x05, 0x00);
  400. C251 COMPILER V5.60.0, AS7341 03/12/24 17:22:31 PAGE 7
  401. 387 1 aa=AS7341_Write_Byte(0x06, 0x00);
  402. 388 1 aa=AS7341_Write_Byte(0x07, 0x00);
  403. 389 1 aa=AS7341_Write_Byte(0x08, 0x00);
  404. 390 1 aa=AS7341_Write_Byte(0x09, 0x00);
  405. 391 1 aa=AS7341_Write_Byte(0x0A, 0x00);
  406. 392 1 aa=AS7341_Write_Byte(0x0B, 0x00);
  407. 393 1 aa=AS7341_Write_Byte(0x0C, 0x00);
  408. 394 1 aa=AS7341_Write_Byte(0x0D, 0x00);
  409. 395 1 aa=AS7341_Write_Byte(0x0E, 0x00);
  410. 396 1 aa=AS7341_Write_Byte(0x0F, 0x00);
  411. 397 1 aa=AS7341_Write_Byte(0x10, 0x00);
  412. 398 1 aa=AS7341_Write_Byte(0x11, 0x00);
  413. 399 1 aa=AS7341_Write_Byte(0x12, 0x00);
  414. 400 1 aa=AS7341_Write_Byte(0x13, 0x60);
  415. 401 1 }
  416. 402
  417. 403 ///******************************************************************************
  418. 404 //function: Start the measurement
  419. 405 //info: This function only handles SPM and SYNS modes.
  420. 406 //******************************************************************************/
  421. 407 void AS7341_startMeasure(eChChoose_t mode)
  422. 408 {
  423. 409 1 UBYTE aa,data0 = 0;
  424. 410 1 data0 = AS7341_Read_Byte(AS7341_CFG_0);
  425. 411 1 data0 = data0 & (~(1 << 4));
  426. 412 1
  427. 413 1 aa=AS7341_Write_Byte(AS7341_CFG_0, data0);
  428. 414 1
  429. 415 1 AS7341_EnableSpectralMeasure(false);
  430. 416 1 aa=AS7341_Write_Byte(0xAF, 0x10); // SMUX Command config
  431. 417 1
  432. 418 1 if (mode == eF1F4ClearNIR)
  433. 419 1 F1F4_Clear_NIR();
  434. 420 1 else if (mode == eF5F8ClearNIR)
  435. 421 1 F5F8_Clear_NIR();
  436. 422 1 AS7341_EnableSMUX(true);
  437. 423 1 // if (measureMode == eSyns)
  438. 424 1 // {
  439. 425 1 // AS7341_SetGpioMode(INPUT);
  440. 426 1 // AS7341_Config(eSyns);
  441. 427 1 // }
  442. 428 1 // else if (measureMode == eSpm)
  443. 429 1 // {
  444. 430 1 // AS7341_Config(eSpm);
  445. 431 1 // }
  446. 432 1 // AS7341_EnableSpectralMeasure(true);
  447. 433 1 // if (measureMode == eSpm)
  448. 434 1 // {
  449. 435 1 // while (!AS7341_MeasureComplete())
  450. 436 1 // {
  451. 437 1 // // DEV_Delay_ms(1);
  452. 438 1 // //////////////////////////vTaskDelay(1 / portTICK_PERIOD_MS);
  453. 439 1 // }
  454. 440 1 // }
  455. 441 1 }
  456. 442 ///******************************************************************************
  457. 443 //function: read flicker data
  458. 444 //info:
  459. 445 //******************************************************************************/
  460. 446 UBYTE AS7341_ReadFlickerData()
  461. 447 {
  462. 448 1 UBYTE flicker;
  463. 449 1 UBYTE aa,data0 = 0;
  464. 450 1 UBYTE retry = 100;
  465. 451 1
  466. 452 1 data0 = AS7341_Read_Byte(AS7341_CFG_0);
  467. C251 COMPILER V5.60.0, AS7341 03/12/24 17:22:31 PAGE 8
  468. 453 1 data0 = data0 & (~(1 << 4));
  469. 454 1 aa=AS7341_Write_Byte(AS7341_CFG_0, data0);
  470. 455 1 AS7341_EnableSpectralMeasure(false);
  471. 456 1 aa=AS7341_Write_Byte(0xAF, 0x10);
  472. 457 1 FDConfig();
  473. 458 1 AS7341_EnableSMUX(true);
  474. 459 1 AS7341_EnableSpectralMeasure(true);
  475. 460 1 // AS7341_Write_Byte(0xDA,0x00);
  476. 461 1
  477. 462 1 if (retry == 0)
  478. 463 1 //printf(" data access error");
  479. 464 1 AS7341_EnableFlickerDetection(true);
  480. 465 1 // DEV_Delay_ms(600);
  481. 466 1 //////////////////////////vTaskDelay(600 / portTICK_PERIOD_MS);
  482. 467 1 flicker = AS7341_Read_Byte(AS7341_STATUS);
  483. 468 1 //printf("flicker: %d \r\n", flicker);
  484. 469 1 AS7341_EnableFlickerDetection(false);
  485. 470 1 switch (flicker)
  486. 471 1 {
  487. 472 2 case 37:
  488. 473 2 flicker = 100;
  489. 474 2 break;
  490. 475 2 case 40:
  491. 476 2 flicker = 0;
  492. 477 2 break;
  493. 478 2 case 42:
  494. 479 2 flicker = 120;
  495. 480 2 break;
  496. 481 2 case 44:
  497. 482 2 flicker = 1;
  498. 483 2 break;
  499. 484 2 case 45:
  500. 485 2 flicker = 2;
  501. 486 2 break;
  502. 487 2 default:
  503. 488 2 flicker = 2;
  504. 489 2 }
  505. 490 1 return flicker;
  506. 491 1 }
  507. 492
  508. 493 ///******************************************************************************
  509. 494 //function: Determine whether the measurement is complete
  510. 495 //info:
  511. 496 //******************************************************************************/
  512. 497
  513. 498 int AS7341_MeasureComplete()
  514. 499 {
  515. 500 1 UBYTE status;
  516. 501 1 status = AS7341_Read_Byte(AS7341_STATUS_2);
  517. 502 1 if ((status & (1 << 6)))
  518. 503 1 {
  519. 504 2 return true;
  520. 505 2 }
  521. 506 1 else
  522. 507 1 {
  523. 508 2 return false;
  524. 509 2 }
  525. 510 1 }
  526. 511
  527. 512 ///******************************************************************************
  528. 513 //function: Gets data for all channels
  529. 514 //info:
  530. 515 //******************************************************************************/
  531. 516 UWORD AS7341_GetChannelData(UBYTE channel)
  532. 517 {
  533. 518 1 UWORD data0[2];
  534. C251 COMPILER V5.60.0, AS7341 03/12/24 17:22:31 PAGE 9
  535. 519 1 UWORD channelData = 0x0000;
  536. 520 1 data0[0] = AS7341_Read_Byte(AS7341_CH0_DATA_L + channel * 2);
  537. *** WARNING C188 IN LINE 520 OF AS7341.c: 'parameter 1': value truncated
  538. 521 1 data0[1] = AS7341_Read_Byte(AS7341_CH0_DATA_H + channel * 2);
  539. *** WARNING C188 IN LINE 521 OF AS7341.c: 'parameter 1': value truncated
  540. 522 1 channelData = data0[1];
  541. 523 1 channelData = (channelData << 8) | data0[0];
  542. 524 1 // DEV_Delay_ms(50);
  543. 525 1 /////////////////////////////vTaskDelay(50 / portTICK_PERIOD_MS);
  544. 526 1 return channelData;
  545. 527 1 }
  546. 528
  547. 529 ///******************************************************************************
  548. 530 //function: Use SMUX to read data from the low channel
  549. 531 //info:
  550. 532 //******************************************************************************/
  551. 533
  552. 534 //sModeOneData_t AS7341_ReadSpectralDataOne()
  553. 535 //{
  554. 536 // sModeOneData_t data;
  555. 537 // data.channel1 = AS7341_GetChannelData(0);
  556. 538 // data.channel2 = AS7341_GetChannelData(1);
  557. 539 // data.channel3 = AS7341_GetChannelData(2);
  558. 540 // data.channel4 = AS7341_GetChannelData(3);
  559. 541 // data.CLEAR = AS7341_GetChannelData(4);
  560. 542 // data.NIR = AS7341_GetChannelData(5);
  561. 543 // return data;
  562. 544 //}
  563. 545
  564. 546 ///******************************************************************************
  565. 547 //function: Use SMUX to read data from the high channel
  566. 548 //info:
  567. 549 //******************************************************************************/
  568. 550
  569. 551 //sModeTwoData_t AS7341_ReadSpectralDataTwo()
  570. 552 //{
  571. 553 // sModeTwoData_t data0;
  572. 554 // data0.channel5 = AS7341_GetChannelData(0);
  573. 555 // data0.channel6 = AS7341_GetChannelData(1);
  574. 556 // data0.channel7 = AS7341_GetChannelData(2);
  575. 557 // data0.channel8 = AS7341_GetChannelData(3);
  576. 558 // data0.CLEAR = AS7341_GetChannelData(4);
  577. 559 // data0.NIR = AS7341_GetChannelData(5);
  578. 560 // return data0;
  579. 561 //}
  580. 562
  581. 563 ///******************************************************************************
  582. 564 //function: Set GPIO to input or output mode
  583. 565 //info:
  584. 566 //******************************************************************************/
  585. 567 void AS7341_SetGpioMode(UBYTE mode)
  586. 568 {
  587. 569 1 UBYTE data0,aa;
  588. 570 1
  589. 571 1 data0 = AS7341_Read_Byte(AS7341_GPIO_2);
  590. 572 1 if (mode == INPUT)
  591. 573 1 {
  592. 574 2 data0 = data0 | (1 << 2);
  593. 575 2 }
  594. 576 1
  595. 577 1 if (mode == OUTPUT)
  596. 578 1 {
  597. 579 2 data0 = data0 & (~(1 << 2));
  598. 580 2 }
  599. 581 1 aa=AS7341_Write_Byte(AS7341_GPIO_2, data0);
  600. 582 1 }
  601. C251 COMPILER V5.60.0, AS7341 03/12/24 17:22:31 PAGE 10
  602. 583
  603. 584 ///******************************************************************************
  604. 585 //function: Configure the ATIME register
  605. 586 //info:
  606. 587 //******************************************************************************/
  607. 588 void AS7341_ATIME_config(UBYTE value)
  608. 589 {
  609. 590 1 UBYTE aa;
  610. 591 1 aa=AS7341_Write_Byte(AS7341_ATIME, value);
  611. 592 1 }
  612. 593
  613. 594 ///******************************************************************************
  614. 595 //function: Configure the ASTEP register
  615. 596 //info:
  616. 597 //******************************************************************************/
  617. 598 void AS7341_ASTEP_config(UWORD value)
  618. 599 {
  619. 600 1 UBYTE highValue, lowValue,aa;
  620. 601 1 lowValue = value & 0x00ff;
  621. 602 1 highValue = value >> 8;
  622. 603 1 aa=AS7341_Write_Byte(AS7341_ASTEP_L, lowValue);
  623. 604 1 aa=AS7341_Write_Byte(AS7341_ASTEP_H, highValue);
  624. 605 1 }
  625. 606
  626. 607 ///******************************************************************************
  627. 608 //function: Configure the AGAIN register
  628. 609 //value: 0 1 2 3 4 5 6 7 8 9 10
  629. 610 //gain: X0.5 | X1 | X2 | X4 | X8 | X16 | X32 | X64 | X128 | X256 | X512
  630. 611 //******************************************************************************/
  631. 612 void AS7341_AGAIN_config(UBYTE value)
  632. 613 {
  633. 614 1 UBYTE aa;
  634. 615 1 if (value > 10)
  635. 616 1 value = 10;
  636. 617 1 aa=AS7341_Write_Byte(AS7341_CFG_1, value);
  637. 618 1 }
  638. 619
  639. 620 ///******************************************************************************
  640. 621 //function: enable led
  641. 622 //info:true : Enable
  642. 623 // false : Anergy
  643. 624 //******************************************************************************/
  644. 625 void AS7341_EnableLED(int flag)
  645. 626 {
  646. 627 1 UBYTE aa,data0 = 0;
  647. 628 1 UBYTE data1 = 0;
  648. 629 1 AS7341_SetBank(1);
  649. 630 1 data0 = AS7341_Read_Byte(AS7341_CONFIG);
  650. 631 1 data1 = AS7341_Read_Byte(AS7341_LED);
  651. 632 1 if (flag == true)
  652. 633 1 {
  653. 634 2 data0 = data0 | 0x08;
  654. 635 2 }
  655. 636 1 else
  656. 637 1 {
  657. 638 2 data0 = data0 & 0xf7;
  658. 639 2 data1 = data1 & 0x7f;
  659. 640 2 aa=AS7341_Write_Byte(AS7341_LED, data1);
  660. 641 2 }
  661. 642 1 aa=AS7341_Write_Byte(AS7341_CONFIG, data0);
  662. 643 1 AS7341_SetBank(0);
  663. 644 1 }
  664. 645 ///******************************************************************************
  665. 646 //function: set REG_BANK
  666. 647 //info: 0: Register access to register 0x80 and above
  667. 648 // 1: Register access to register 0x60 to 0x74
  668. C251 COMPILER V5.60.0, AS7341 03/12/24 17:22:31 PAGE 11
  669. 649 //******************************************************************************/
  670. 650 void AS7341_SetBank(UBYTE addr)
  671. 651 {
  672. 652 1 UBYTE aa,data0 = 0;
  673. 653 1 data0 = AS7341_Read_Byte(AS7341_CFG_0);
  674. 654 1 if (addr == 1)
  675. 655 1 {
  676. 656 2
  677. 657 2 data0 = data0 | (1 << 4);
  678. 658 2 }
  679. 659 1
  680. 660 1 if (addr == 0)
  681. 661 1 {
  682. 662 2
  683. 663 2 data0 = data0 & (~(1 << 4));
  684. 664 2 }
  685. 665 1 aa=AS7341_Write_Byte(AS7341_CFG_0, data0);
  686. 666 1 }
  687. 667 ///******************************************************************************
  688. 668 //function: Control the brightness of the LED
  689. 669 //info: LED : true : LED ON
  690. 670 // false : LED OFF
  691. 671 // current : intensity control
  692. 672 //******************************************************************************/
  693. 673 void AS7341_ControlLed(UBYTE LED, UBYTE current)
  694. 674 {
  695. 675 1 UBYTE aa,data0 = 0;
  696. 676 1 if (current < 1)
  697. 677 1 current = 1;
  698. 678 1 current--;
  699. 679 1 if (current > 19)
  700. 680 1 current = 19;
  701. 681 1 AS7341_SetBank(1);
  702. 682 1 if (LED == true)
  703. 683 1 data0 = 0x80 | current;
  704. 684 1 else
  705. 685 1 data0 = current;
  706. 686 1 aa=AS7341_Write_Byte(AS7341_LED, data0);
  707. 687 1 // DEV_Delay_ms(100);
  708. 688 1 /////////////////////////vTaskDelay(100 / portTICK_PERIOD_MS);
  709. 689 1 AS7341_SetBank(0);
  710. 690 1 }
  711. 691
  712. 692 ///******************************************************************************
  713. 693 //function: Determine whether the threshold setting is exceeded
  714. 694 //info:Spectral interruptions occur when the set threshold is exceeded
  715. 695 //******************************************************************************/
  716. 696 void AS7341_INTerrupt()
  717. 697 {
  718. 698 1 UBYTE data0 = AS7341_Read_Byte(AS7341_STATUS_1);
  719. 699 1 if (data0 & 0x80)
  720. 700 1 {
  721. 701 2 //printf("Spectral interrupt generation !\r\n");
  722. 702 2 }
  723. 703 1 else
  724. 704 1 {
  725. 705 2 return;
  726. 706 2 }
  727. 707 1 }
  728. 708
  729. 709 ///******************************************************************************
  730. 710 //function: clear interrupt
  731. 711 //info: This register is self-clearing, meaning that writing a "1" to any bit in the
  732. 712 // register clears that status bit.
  733. 713 //******************************************************************************/
  734. 714 void AS7341_ClearInterrupt()
  735. C251 COMPILER V5.60.0, AS7341 03/12/24 17:22:31 PAGE 12
  736. 715 {
  737. 716 1 UBYTE aa;
  738. 717 1 aa=AS7341_Write_Byte(AS7341_STATUS_1, 0xff);
  739. 718 1 }
  740. 719 ///******************************************************************************
  741. 720 //function: enable spectral interrupt
  742. 721 //info:
  743. 722 //******************************************************************************/
  744. 723 void AS7341_EnableSpectralInterrupt(int flag)
  745. 724 {
  746. 725 1 UBYTE data0,aa;
  747. 726 1 data0 = AS7341_Read_Byte(AS7341_INTENAB);
  748. 727 1 if (flag == true)
  749. 728 1 {
  750. 729 2 data0 = data0 | (1 << 3);
  751. 730 2 aa=AS7341_Write_Byte(AS7341_INTENAB, data0);
  752. 731 2 }
  753. 732 1 else
  754. 733 1 {
  755. 734 2 data0 = data0 & (~(1 << 3));
  756. 735 2 aa=AS7341_Write_Byte(AS7341_INTENAB, data0);
  757. 736 2 }
  758. 737 1 }
  759. 738 ///******************************************************************************
  760. 739 //function:Spectral Interrupt Persistence
  761. 740 // value: CHANNEL:
  762. 741 // 0 Every spectral cycle generates aninterrupt
  763. 742 // 1 1
  764. 743 // 2 2
  765. 744 // 3 3
  766. 745 // 4 5
  767. 746 // 5 10
  768. 747 // ... 5*(value-3)
  769. 748 // 14 55
  770. 749 // 15 60
  771. 750 //******************************************************************************/
  772. 751 void AS7341_SetInterruptPersistence(UBYTE value)
  773. 752 {
  774. 753 1 UBYTE data0,aa;
  775. 754 1 data0 = value;
  776. 755 1 aa=AS7341_Write_Byte(AS7341_PERS, data0);
  777. 756 1 data0 = AS7341_Read_Byte(AS7341_PERS);
  778. 757 1 }
  779. 758 ///******************************************************************************
  780. 759 //function: Set the interrupt threshold up and
  781. 760 //info:
  782. 761 //******************************************************************************/
  783. 762 void AS7341_SetThreshold(UWORD lowThre, UWORD highThre)
  784. 763 {
  785. 764 1 UBYTE aa;
  786. 765 1
  787. 766 1 if (lowThre >= highThre)
  788. 767 1 return;
  789. 768 1 else
  790. 769 1
  791. 770 1 aa=AS7341_Write_Byte(AS7341_SP_TH_L_LSB, lowThre);
  792. *** WARNING C188 IN LINE 770 OF AS7341.c: 'parameter 2': value truncated
  793. 771 1
  794. 772 1 aa=AS7341_Write_Byte(AS7341_SP_TH_L_MSB, lowThre >> 8);
  795. *** WARNING C188 IN LINE 772 OF AS7341.c: 'parameter 2': value truncated
  796. 773 1
  797. 774 1 aa=AS7341_Write_Byte(AS7341_SP_TH_H_LSB, highThre);
  798. *** WARNING C188 IN LINE 774 OF AS7341.c: 'parameter 2': value truncated
  799. 775 1 aa=AS7341_Write_Byte(AS7341_SP_TH_H_MSB, highThre >> 8);
  800. *** WARNING C188 IN LINE 775 OF AS7341.c: 'parameter 2': value truncated
  801. 776 1
  802. C251 COMPILER V5.60.0, AS7341 03/12/24 17:22:31 PAGE 13
  803. 777 1 // DEV_Delay_ms(20);
  804. 778 1 //////////////////////////////vTaskDelay(20 / portTICK_PERIOD_MS);
  805. 779 1 }
  806. 780 ///******************************************************************************
  807. 781 //function: Set the Spectral Threshold Channel
  808. 782 // VALUE CHANNEL
  809. 783 // 0 CH0
  810. 784 // 1 CH1
  811. 785 // 2 CH2
  812. 786 // 3 CH3
  813. 787 // 4 CH4
  814. 788 //******************************************************************************/
  815. 789 void AS7341_SetSpectralThresholdChannel(UBYTE value)
  816. 790 {
  817. 791 1 UBYTE aa;
  818. 792 1 aa=AS7341_Write_Byte(AS7341_CFG_12, value);
  819. 793 1 }
  820. 794
  821. 795 ///******************************************************************************
  822. 796 //function: get low threshold
  823. 797 //info:
  824. 798 //******************************************************************************/
  825. 799
  826. 800 UWORD AS7341_GetLowThreshold()
  827. 801 {
  828. 802 1 UWORD data0;
  829. 803 1 data0 = AS7341_Read_Byte(AS7341_SP_TH_L_LSB);
  830. 804 1 data0 = (AS7341_Read_Byte(AS7341_SP_TH_L_MSB) << 8) | data0;
  831. 805 1 return data0;
  832. 806 1 }
  833. 807
  834. 808 ///******************************************************************************
  835. 809 //function: get high threshold
  836. 810 //info:
  837. 811 //******************************************************************************/
  838. 812 UWORD AS7341_GetHighThreshold()
  839. 813 {
  840. 814 1 UWORD data0;
  841. 815 1 data0 = AS7341_Read_Byte(AS7341_SP_TH_H_LSB);
  842. 816 1 data0 = (AS7341_Read_Byte(AS7341_SP_TH_H_MSB) << 8) | data0;
  843. 817 1 return data0;
  844. 818 1 }
  845. 819
  846. 820 ///******************************************************************************
  847. 821 //function: syns interrupt set
  848. 822 //info:
  849. 823 //******************************************************************************/
  850. 824 void AS7341_SynsINT_sel()
  851. 825 {
  852. 826 1 UBYTE aa;
  853. 827 1 aa=AS7341_Write_Byte(AS7341_CONFIG, 0x05);
  854. 828 1 }
  855. 829
  856. 830 ///******************************************************************************
  857. 831 //function: disable power,spectral reading, flicker detection
  858. 832 //info:
  859. 833 //******************************************************************************/
  860. 834 void AS7341_disableALL()
  861. 835 {
  862. 836 1 UBYTE aa;
  863. 837 1 aa=AS7341_Write_Byte(AS7341_ENABLE, 0x02);
  864. 838 1 }
  865. 839
  866. Module Information Static Overlayable
  867. ------------------------------------------------
  868. C251 COMPILER V5.60.0, AS7341 03/12/24 17:22:31 PAGE 14
  869. code size = 1637 ------
  870. ecode size = ------ ------
  871. data size = ------ ------
  872. idata size = ------ ------
  873. pdata size = ------ ------
  874. xdata size = ------ ------
  875. xdata-const size = ------ ------
  876. edata size = 2 4
  877. bit size = ------ 1
  878. ebit size = ------ ------
  879. bitaddressable size = ------ ------
  880. ebitaddressable size = ------ ------
  881. far data size = ------ ------
  882. huge data size = ------ ------
  883. const size = ------ ------
  884. hconst size = ------ ------
  885. End of Module Information.
  886. C251 COMPILATION COMPLETE. 7 WARNING(S), 0 ERROR(S)