ov2640.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "ov2640.h"
  3. #include "nrf_drv_gpiote.h"
  4. #include "app_scheduler.h"
  5. #include "user_twim.h"
  6. #include "app_timer.h"
  7. static custome_event ov2640_event;
  8. APP_TIMER_DEF(ov2640_timer_id);
  9. static uint32_t ov2640_timer_tick = 0;
  10. static bool ov2640_timer_end = true;
  11. static bool ov2640_timer_stat = false;
  12. void Set_Ov2640_Queue(OV2640_QUEUE_TYPE type);
  13. /** @addtogroup
  14. * @{
  15. */
  16. /** @addtogroup DCMI_Camera
  17. * @{
  18. */
  19. /* Exported types ------------------------------------------------------------*/
  20. /* Camera devices enumeration */
  21. /* Private typedef -----------------------------------------------------------*/
  22. /* Private define ------------------------------------------------------------*/
  23. /* Private macro -------------------------------------------------------------*/
  24. /* Private variables ---------------------------------------------------------*/
  25. const unsigned char OV2640_JPEG_INIT[][2]=
  26. {
  27. 0xff, 0x00,
  28. 0x2c, 0xff,
  29. 0x2e, 0xdf,
  30. 0xff, 0x01,
  31. 0x3c, 0x32,
  32. 0x11, 0x00,
  33. 0x09, 0x02,
  34. 0x04, 0x28,
  35. 0x13, 0xe5,
  36. 0x14, 0x48,
  37. 0x2c, 0x0c,
  38. 0x33, 0x78,
  39. 0x3a, 0x33,
  40. 0x3b, 0xfB,
  41. 0x3e, 0x00,
  42. 0x43, 0x11,
  43. 0x16, 0x10,
  44. 0x39, 0x92,
  45. 0x35, 0xda,
  46. 0x22, 0x1a,
  47. 0x37, 0xc3,
  48. 0x23, 0x00,
  49. 0x34, 0xc0,
  50. 0x36, 0x1a,
  51. 0x06, 0x88,
  52. 0x07, 0xc0,
  53. 0x0d, 0x87,
  54. 0x0e, 0x41,
  55. 0x4c, 0x00,
  56. 0x48, 0x00,
  57. 0x5B, 0x00,
  58. 0x42, 0x03,
  59. 0x4a, 0x81,
  60. 0x21, 0x99,
  61. 0x24, 0x40,
  62. 0x25, 0x38,
  63. 0x26, 0x82,
  64. 0x5c, 0x00,
  65. 0x63, 0x00,
  66. 0x61, 0x70,
  67. 0x62, 0x80,
  68. 0x7c, 0x05,
  69. 0x20, 0x80,
  70. 0x28, 0x30,
  71. 0x6c, 0x00,
  72. 0x6d, 0x80,
  73. 0x6e, 0x00,
  74. 0x70, 0x02,
  75. 0x71, 0x94,
  76. 0x73, 0xc1,
  77. 0x12, 0x40,//0x40
  78. 0x17, 0x11,
  79. 0x18, 0x43,
  80. 0x19, 0x00,
  81. 0x1a, 0x4b,
  82. 0x32, 0x09,
  83. 0x37, 0xc0,
  84. 0x4f, 0x60,
  85. 0x50, 0xa8,
  86. 0x6d, 0x00,
  87. 0x3d, 0x38,
  88. 0x46, 0x3f,
  89. 0x4f, 0x60,
  90. 0x0c, 0x3c,
  91. 0xff, 0x00,
  92. 0xe5, 0x7f,
  93. 0xf9, 0xc0,
  94. 0x41, 0x24,
  95. 0xe0, 0x14,
  96. 0x76, 0xff,
  97. 0x33, 0xa0,
  98. 0x42, 0x20,
  99. 0x43, 0x18,
  100. 0x4c, 0x00,
  101. 0x87, 0xd5,
  102. 0x88, 0x3f,
  103. 0xd7, 0x03,
  104. 0xd9, 0x10,
  105. 0xd3, 0x82,
  106. 0xc8, 0x08,
  107. 0xc9, 0x80,
  108. 0x7c, 0x00,
  109. 0x7d, 0x00,
  110. 0x7c, 0x03,
  111. 0x7d, 0x48,
  112. 0x7d, 0x48,
  113. 0x7c, 0x08,
  114. 0x7d, 0x20,
  115. 0x7d, 0x10,
  116. 0x7d, 0x0e,
  117. 0x90, 0x00,
  118. 0x91, 0x0e,
  119. 0x91, 0x1a,
  120. 0x91, 0x31,
  121. 0x91, 0x5a,
  122. 0x91, 0x69,
  123. 0x91, 0x75,
  124. 0x91, 0x7e,
  125. 0x91, 0x88,
  126. 0x91, 0x8f,
  127. 0x91, 0x96,
  128. 0x91, 0xa3,
  129. 0x91, 0xaf,
  130. 0x91, 0xc4,
  131. 0x91, 0xd7,
  132. 0x91, 0xe8,
  133. 0x91, 0x20,
  134. 0x92, 0x00,
  135. 0x93, 0x06,
  136. 0x93, 0xe3,
  137. 0x93, 0x05,
  138. 0x93, 0x05,
  139. 0x93, 0x00,
  140. 0x93, 0x04,
  141. 0x93, 0x00,
  142. 0x93, 0x00,
  143. 0x93, 0x00,
  144. 0x93, 0x00,
  145. 0x93, 0x00,
  146. 0x93, 0x00,
  147. 0x93, 0x00,
  148. 0x96, 0x00,
  149. 0x97, 0x08,
  150. 0x97, 0x19,
  151. 0x97, 0x02,
  152. 0x97, 0x0c,
  153. 0x97, 0x24,
  154. 0x97, 0x30,
  155. 0x97, 0x28,
  156. 0x97, 0x26,
  157. 0x97, 0x02,
  158. 0x97, 0x98,
  159. 0x97, 0x80,
  160. 0x97, 0x00,
  161. 0x97, 0x00,
  162. 0xc3, 0xed,
  163. 0xa4, 0x00,
  164. 0xa8, 0x00,
  165. 0xc5, 0x11,
  166. 0xc6, 0x51,
  167. 0xbf, 0x80,
  168. 0xc7, 0x10,
  169. 0xb6, 0x66,
  170. 0xb8, 0xA5,
  171. 0xb7, 0x64,
  172. 0xb9, 0x7C,
  173. 0xb3, 0xaf,
  174. 0xb4, 0x97,
  175. 0xb5, 0xFF,
  176. 0xb0, 0xC5,
  177. 0xb1, 0x94,
  178. 0xb2, 0x0f,
  179. 0xc4, 0x5c,
  180. 0xc0, 0x64,
  181. 0xc1, 0x4B,
  182. 0x8c, 0x00,
  183. 0x86, 0x3D,
  184. 0x50, 0x00,
  185. 0x51, 0xC8,
  186. 0x52, 0x96,
  187. 0x53, 0x00,
  188. 0x54, 0x00,
  189. 0x55, 0x00,
  190. 0x5a, 0xC8,
  191. 0x5b, 0x96,
  192. 0x5c, 0x00,
  193. 0xd3, 0x7f,
  194. 0xc3, 0xed,
  195. 0x7f, 0x00,
  196. 0xda, 0x00,
  197. 0xe5, 0x1f,
  198. 0xe1, 0x67,
  199. 0xe0, 0x00,
  200. 0xdd, 0x7f,
  201. 0x05, 0x00,
  202. 0x12, 0x40,//0x40
  203. 0xd3, 0x7f,
  204. 0xc0, 0x16,
  205. 0xC1, 0x12,
  206. 0x8c, 0x00,
  207. 0x86, 0x3d,
  208. 0x50, 0x00,
  209. 0x51, 0x2C,
  210. 0x52, 0x24,
  211. 0x53, 0x00,
  212. 0x54, 0x00,
  213. 0x55, 0x00,
  214. 0x5A, 0x2c,
  215. 0x5b, 0x24,
  216. 0x5c, 0x00,
  217. };
  218. const unsigned char OV2640_YUV422[][2]=
  219. {
  220. 0xFF, 0x00,
  221. 0x05, 0x00,
  222. 0xDA, 0x10,
  223. 0xD7, 0x03,
  224. 0xDF, 0x00,
  225. 0x33, 0x80,
  226. 0x3C, 0x40,
  227. 0xe1, 0x77,
  228. 0x00, 0x00,
  229. };
  230. const unsigned char OV2640_JPEG[][2]=
  231. {
  232. 0xe0, 0x14,
  233. 0xe1, 0x77,
  234. 0xe5, 0x1f,
  235. 0xd7, 0x03,
  236. 0xda, 0x10,
  237. 0xe0, 0x00,
  238. 0xFF, 0x01,
  239. 0x04, 0x08,
  240. };
  241. /* JPG 320x240 */
  242. const unsigned char OV2640_800x600_JPEG[][2]=
  243. {
  244. {0xff, 0x01},
  245. {0x11, 0x01},
  246. {0x12, 0x00}, // Bit[6:4]: Resolution selection//0x02为彩条
  247. {0x17, 0x11}, // HREFST[10:3]
  248. {0x18, 0x75}, // HREFEND[10:3]
  249. {0x32, 0x36}, // Bit[5:3]: HREFEND[2:0]; Bit[2:0]: HREFST[2:0]
  250. {0x19, 0x01}, // VSTRT[9:2]
  251. {0x1a, 0x97}, // VEND[9:2]
  252. {0x03, 0x0f}, // Bit[3:2]: VEND[1:0]; Bit[1:0]: VSTRT[1:0]
  253. {0x37, 0x40},
  254. {0x4f, 0xbb},
  255. {0x50, 0x9c},
  256. {0x5a, 0x57},
  257. {0x6d, 0x80},
  258. {0x3d, 0x34},
  259. {0x39, 0x02},
  260. {0x35, 0x88},
  261. {0x22, 0x0a},
  262. {0x37, 0x40},
  263. {0x34, 0xa0},
  264. {0x06, 0x02},
  265. {0x0d, 0xb7},
  266. {0x0e, 0x01},
  267. ////////////////
  268. /*
  269. //176*144
  270. 0xff, 0x00,
  271. 0xc0, 0xC8,
  272. 0xc1, 0x96,
  273. 0x8c, 0x00,
  274. 0x86, 0x3D,
  275. 0x50, 0x9B,
  276. 0x51, 0x90,
  277. 0x52, 0x2C,
  278. 0x53, 0x00,
  279. 0x54, 0x00,
  280. 0x55, 0x88,
  281. 0x5a, 0x2C,
  282. 0x5b, 0x24,
  283. 0x5c, 0x00,
  284. 0xd3, 0x7F,
  285. ////////////
  286. */
  287. /*
  288. ////////////////
  289. //320*240
  290. 0xff, 0x00,
  291. 0xe0, 0x04,
  292. 0xc0, 0xc8,
  293. 0xc1, 0x96,
  294. 0x86, 0x3d,
  295. 0x50, 0x92,
  296. 0x51, 0x90,
  297. 0x52, 0x2c,
  298. 0x53, 0x00,
  299. 0x54, 0x00,
  300. 0x55, 0x88,
  301. 0x57, 0x00,
  302. 0x5a, 0x50,
  303. 0x5b, 0x3c,
  304. 0x5c, 0x00,
  305. 0xd3, 0x7F,
  306. 0xe0, 0x00,
  307. ///////////////////
  308. */
  309. /*
  310. 0xff, 0x00,
  311. 0xe0, 0x04,
  312. 0xc0, 0xc8,
  313. 0xc1, 0x96,
  314. 0x86, 0x35,
  315. 0x50, 0x92,
  316. 0x51, 0x90,
  317. 0x52, 0x2c,
  318. 0x53, 0x00,
  319. 0x54, 0x00,
  320. 0x55, 0x88,
  321. 0x57, 0x00,
  322. 0x5a, 0x58,
  323. 0x5b, 0x48,
  324. 0x5c, 0x00,
  325. 0xd3, 0x08,
  326. 0xe0, 0x00
  327. */
  328. /*
  329. //640*480
  330. 0xff, 0x00,
  331. 0xe0, 0x04,
  332. 0xc0, 0xc8,
  333. 0xc1, 0x96,
  334. 0x86, 0x3d,
  335. 0x50, 0x89,
  336. 0x51, 0x90,
  337. 0x52, 0x2c,
  338. 0x53, 0x00,
  339. 0x54, 0x00,
  340. 0x55, 0x88,
  341. 0x57, 0x00,
  342. 0x5a, 0xa0,
  343. 0x5b, 0x78,
  344. 0x5c, 0x00,
  345. 0xd3, 0x04,
  346. 0xe0, 0x00
  347. */
  348. /////////////////////
  349. //800*600
  350. 0xff, 0x00,
  351. 0xe0, 0x04,
  352. 0xc0, 0xc8,
  353. 0xc1, 0x96,
  354. 0x86, 0x35,
  355. 0x50, 0x89,
  356. 0x51, 0x90,
  357. 0x52, 0x2c,
  358. 0x53, 0x00,
  359. 0x54, 0x00,
  360. 0x55, 0x88,
  361. 0x57, 0x00,
  362. 0x5a, 0xc8,
  363. 0x5b, 0x96,
  364. 0x5c, 0x00,
  365. 0xd3, 0x02,
  366. 0xe0, 0x00
  367. /*
  368. //1280*1024
  369. 0xff, 0x00,
  370. 0xe0, 0x04,
  371. 0xc0, 0xc8,
  372. 0xc1, 0x96,
  373. 0x86, 0x3d,
  374. 0x50, 0x00,
  375. 0x51, 0x90,
  376. 0x52, 0x2c,
  377. 0x53, 0x00,
  378. 0x54, 0x00,
  379. 0x55, 0x88,
  380. 0x57, 0x00,
  381. 0x5a, 0x40,
  382. 0x5b, 0xf0,
  383. 0x5c, 0x01,
  384. 0xd3, 0x02,
  385. 0xe0, 0x00
  386. */
  387. /*
  388. /////////////////////
  389. //1600*1200
  390. 0xff, 0x00,
  391. 0xe0, 0x04,
  392. 0xc0, 0xc8,
  393. 0xc1, 0x96,
  394. 0x86, 0x3d,
  395. 0x50, 0x00,
  396. 0x51, 0x90,
  397. 0x52, 0x2c,
  398. 0x53, 0x00,
  399. 0x54, 0x00,
  400. 0x55, 0x88,
  401. 0x57, 0x00,
  402. 0x5a, 0x90,
  403. 0x5b, 0x2C,
  404. 0x5c, 0x05,//bit2->1;bit[1:0]->1
  405. 0xd3, 0x02,
  406. 0xe0, 0x00
  407. /////////////////////
  408. */
  409. /*
  410. //1024*768
  411. 0xff, 0x00,
  412. 0xc0, 0xC8,
  413. 0xc1, 0x96,
  414. 0x8c, 0x00,
  415. 0x86, 0x3D,
  416. 0x50, 0x00,
  417. 0x51, 0x90,
  418. 0x52, 0x2C,
  419. 0x53, 0x00,
  420. 0x54, 0x00,
  421. 0x55, 0x88,
  422. 0x5a, 0x00,
  423. 0x5b, 0xC0,
  424. 0x5c, 0x01,
  425. 0xd3, 0x02
  426. */
  427. };
  428. /* Private function prototypes -----------------------------------------------*/
  429. /* Private functions ---------------------------------------------------------*/
  430. //启动毫秒计时应用定时器。启动后,即开始产生毫秒计时,也就是提供了一个以毫秒为单位的计时基准
  431. static void ov2640_timers_stop(void)
  432. {
  433. if(ov2640_timer_stat==true)
  434. {
  435. ret_code_t err_code = app_timer_stop(ov2640_timer_id);
  436. APP_ERROR_CHECK(err_code);
  437. ov2640_timer_stat=false;
  438. }
  439. }
  440. //启动毫秒计时应用定时器。启动后,即开始产生毫秒计时,也就是提供了一个以毫秒为单位的计时基准
  441. static void ov2640_timers_start(uint32_t ms)
  442. {
  443. ret_code_t err_code = app_timer_start(ov2640_timer_id, APP_TIMER_TICKS(ms), NULL);
  444. APP_ERROR_CHECK(err_code);
  445. ov2640_timer_stat=true;
  446. }
  447. static void ov2640_timers_work(uint32_t ms)
  448. {
  449. ov2640_timers_stop();
  450. if(ov2640_timer_stat==false)
  451. {
  452. if(ms>APP_TIMER_SAFE_WINDOW_MS)
  453. {
  454. ov2640_timers_start(APP_TIMER_SAFE_WINDOW_MS);
  455. ov2640_timer_end=false;
  456. ov2640_timer_tick=ms-APP_TIMER_SAFE_WINDOW_MS;
  457. }
  458. else{
  459. ov2640_timers_start(ms);
  460. ov2640_timer_end=true;
  461. ov2640_timer_tick=0;
  462. }
  463. }
  464. }
  465. //ov2640定时器回调
  466. static void ov2640_timers_handler(void * p_context)
  467. {
  468. UNUSED_PARAMETER(p_context);
  469. ov2640_timer_stat=false;
  470. if(ov2640_timer_end==true)
  471. {
  472. Set_Ov2640_Queue(OV2640_QUEUE_TIME);
  473. }
  474. else{
  475. ov2640_timers_work(ov2640_timer_tick);
  476. }
  477. }
  478. /**@brief Function for the Timer initialization.
  479. *
  480. * @details Initializes the timer module.
  481. */
  482. void ov2640_timers_init(void)
  483. {
  484. // Initialize timer module, making it use the scheduler
  485. // ret_code_t err_code = app_timer_init();
  486. // APP_ERROR_CHECK(err_code);
  487. //创建一个应用定时器,产生秒计时。模式:周期性应用定时器,注册应用定时器事件句柄
  488. ret_code_t err_code = app_timer_create(&ov2640_timer_id,
  489. APP_TIMER_MODE_SINGLE_SHOT,
  490. ov2640_timers_handler);
  491. APP_ERROR_CHECK(err_code);
  492. }
  493. /**
  494. * @brief Initializes the hardware resources (I2C and GPIO) used to configure
  495. * the OV2640 camera.
  496. * @param None
  497. * @retval None
  498. */
  499. void OV2640_Gpio_Init(void)
  500. {
  501. nrf_gpio_cfg_input(CAM_D0_PIN,NRF_GPIO_PIN_NOPULL);
  502. nrf_gpio_cfg_input(CAM_D1_PIN,NRF_GPIO_PIN_NOPULL);
  503. nrf_gpio_cfg_input(CAM_D2_PIN,NRF_GPIO_PIN_NOPULL);
  504. nrf_gpio_cfg_input(CAM_D3_PIN,NRF_GPIO_PIN_NOPULL);
  505. nrf_gpio_cfg_input(CAM_D4_PIN,NRF_GPIO_PIN_NOPULL);
  506. nrf_gpio_cfg_input(CAM_D5_PIN,NRF_GPIO_PIN_NOPULL);
  507. nrf_gpio_cfg_input(CAM_D6_PIN,NRF_GPIO_PIN_NOPULL);
  508. nrf_gpio_cfg_input(CAM_D7_PIN,NRF_GPIO_PIN_NOPULL);
  509. }
  510. void OV2640_CaptureGpioInit(nrf_drv_gpiote_in_config_t *config,nrfx_gpiote_evt_handler_t evt_handler)
  511. {
  512. ret_code_t err_code;
  513. //配置引脚为GPIOTE输入
  514. config->pull=NRF_GPIO_PIN_PULLUP;
  515. err_code = nrf_drv_gpiote_in_init(CAM_PCLK_PIN, config, evt_handler);
  516. APP_ERROR_CHECK(err_code);
  517. err_code = nrf_drv_gpiote_in_init(CAM_VSYNC_PIN, config, evt_handler);
  518. APP_ERROR_CHECK(err_code);
  519. err_code = nrf_drv_gpiote_in_init(CAM_HREF_PIN, config, evt_handler);
  520. APP_ERROR_CHECK(err_code);
  521. //使能事件模式
  522. nrf_drv_gpiote_in_event_enable(CAM_PCLK_PIN,true);
  523. nrf_drv_gpiote_in_event_enable(CAM_VSYNC_PIN,true);
  524. nrf_drv_gpiote_in_event_enable(CAM_HREF_PIN,true);
  525. }
  526. static void ov2640_i2s_done(void)
  527. {
  528. }
  529. static void ov2640_i2s_timeout(void)
  530. {
  531. }
  532. void OV2640_i2s_Init(void)
  533. {
  534. twim0_init(ov2640_i2s_done, ov2640_i2s_timeout);
  535. }
  536. /**
  537. * @brief Resets the OV2640 camera.
  538. * @param None
  539. * @retval None
  540. */
  541. void OV2640_Reset(void)
  542. {
  543. OV2640_WriteReg(OV2640_DSP_RA_DLMT, 0x01);
  544. OV2640_WriteReg(OV2640_SENSOR_COM7, 0x80);
  545. }
  546. /**
  547. * @brief Reads the OV2640 Manufacturer identifier.
  548. * @param OV2640ID: Pointer to the OV2640 Manufacturer identifier
  549. * @retval None
  550. */
  551. void OV2640_ReadID(OV2640_IDTypeDef *OV2640ID)
  552. {
  553. OV2640_WriteReg(OV2640_DSP_RA_DLMT, 0x01);
  554. OV2640ID->Manufacturer_ID1 = OV2640_ReadReg(OV2640_SENSOR_MIDH);
  555. OV2640ID->Manufacturer_ID2 = OV2640_ReadReg(OV2640_SENSOR_MIDL);
  556. OV2640ID->PIDH = OV2640_ReadReg(OV2640_SENSOR_PIDH);
  557. OV2640ID->PIDL = OV2640_ReadReg(OV2640_SENSOR_PIDL);
  558. }
  559. /**
  560. * @brief Configures the OV2640 camera in JPEG mode.
  561. * @param JPEGImageSize: JPEG image size
  562. * @retval None
  563. */
  564. void OV2640_JPEGConfig(void)
  565. {
  566. uint32_t i;
  567. OV2640_Reset();
  568. Delay_nMS(200);
  569. /* Initialize OV2640 */
  570. for(i=0; i<(sizeof(OV2640_JPEG_INIT)/2); i++)
  571. {
  572. OV2640_WriteReg(OV2640_JPEG_INIT[i][0], OV2640_JPEG_INIT[i][1]);
  573. Delay_nMS(1);
  574. }
  575. /* Set to output YUV422 */
  576. for(i=0; i<(sizeof(OV2640_YUV422)/2); i++)
  577. {
  578. OV2640_WriteReg(OV2640_YUV422[i][0], OV2640_YUV422[i][1]);
  579. Delay_nMS(1);
  580. }
  581. OV2640_WriteReg(0xff, 0x01);
  582. OV2640_WriteReg(0x15, 0x00);
  583. /* Set to output JPEG */
  584. for(i=0; i<(sizeof(OV2640_JPEG)/2); i++)
  585. {
  586. OV2640_WriteReg(OV2640_JPEG[i][0], OV2640_JPEG[i][1]);
  587. Delay_nMS(1);
  588. }
  589. Delay_nMS(100);
  590. for(i=0; i<(sizeof(OV2640_800x600_JPEG)/2); i++)
  591. {
  592. OV2640_WriteReg(OV2640_800x600_JPEG[i][0], OV2640_800x600_JPEG[i][1]);
  593. Delay_nMS(1);
  594. }
  595. }
  596. /**
  597. * @brief Configures the OV2640 camera brightness.
  598. * @param Brightness: Brightness value, where Brightness can be:
  599. * 0x40 for Brightness +2,
  600. * 0x30 for Brightness +1,
  601. * 0x20 for Brightness 0,
  602. * 0x10 for Brightness -1,
  603. * 0x00 for Brightness -2,
  604. * @retval None
  605. */
  606. void OV2640_BrightnessConfig(uint8_t Brightness)
  607. {
  608. OV2640_WriteReg(0xff, 0x00);
  609. OV2640_WriteReg(0x7c, 0x00);
  610. OV2640_WriteReg(0x7d, 0x04);
  611. OV2640_WriteReg(0x7c, 0x09);
  612. OV2640_WriteReg(0x7d, Brightness);
  613. OV2640_WriteReg(0x7d, 0x00);
  614. }
  615. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  616. const static uint8_t OV2640_AUTOEXPOSURE_LEVEL0[]=
  617. {
  618. 0xFF, 0x01, 0xff,
  619. 0x24, 0x20, 0xff,
  620. 0x25, 0x18, 0xff,
  621. 0x26, 0x60, 0xff,
  622. 0x00, 0x00, 0x00
  623. };
  624. const static uint8_t OV2640_AUTOEXPOSURE_LEVEL1[]=
  625. {
  626. 0xFF, 0x01, 0xff,
  627. 0x24, 0x34, 0xff,
  628. 0x25, 0x1c, 0xff,
  629. 0x26, 0x70, 0xff,
  630. 0x00, 0x00, 0x00
  631. };
  632. const static uint8_t OV2640_AUTOEXPOSURE_LEVEL2[]=
  633. {
  634. 0xFF, 0x01, 0xff,
  635. 0x24, 0x3e, 0xff,
  636. 0x25, 0x38, 0xff,
  637. 0x26, 0x81, 0xff,
  638. 0x00, 0x00, 0x00
  639. };
  640. const static uint8_t OV2640_AUTOEXPOSURE_LEVEL3[]=
  641. {
  642. 0xFF, 0x01, 0xff,
  643. 0x24, 0x48, 0xff,
  644. 0x25, 0x40, 0xff,
  645. 0x26, 0x81, 0xff,
  646. 0x00, 0x00, 0x00
  647. };
  648. const static uint8_t OV2640_AUTOEXPOSURE_LEVEL4[]=
  649. {
  650. 0xFF, 0x01, 0xff,
  651. 0x24, 0x58, 0xff,
  652. 0x25, 0x50, 0xff,
  653. 0x26, 0x92, 0xff,
  654. 0x00, 0x00, 0x00
  655. };
  656. void SCCB_WriteRegs(const uint8_t* pbuf)
  657. {
  658. while(1)
  659. {
  660. if((*pbuf == 0) && (*(pbuf + 1) == 0))
  661. {
  662. break;
  663. }
  664. else
  665. {
  666. OV2640_WriteReg(*pbuf++, *pbuf++);
  667. }
  668. }
  669. }
  670. void OV2640_AutoExposure(uint8_t level)
  671. {
  672. switch(level)
  673. {
  674. case 0:
  675. SCCB_WriteRegs(OV2640_AUTOEXPOSURE_LEVEL0);
  676. break;
  677. case 1:
  678. SCCB_WriteRegs(OV2640_AUTOEXPOSURE_LEVEL1);
  679. break;
  680. case 2:
  681. SCCB_WriteRegs(OV2640_AUTOEXPOSURE_LEVEL2);
  682. break;
  683. case 3:
  684. SCCB_WriteRegs(OV2640_AUTOEXPOSURE_LEVEL3);
  685. break;
  686. case 4:
  687. SCCB_WriteRegs(OV2640_AUTOEXPOSURE_LEVEL4);
  688. break;
  689. default:
  690. SCCB_WriteRegs(OV2640_AUTOEXPOSURE_LEVEL0);
  691. break;
  692. }
  693. }
  694. /**
  695. * @brief Configures the OV2640 camera Black and white mode.
  696. * @param BlackWhite: BlackWhite value, where BlackWhite can be:
  697. * 0x18 for B&W,
  698. * 0x40 for Negative,
  699. * 0x58 for B&W negative,
  700. * 0x00 for Normal,
  701. * @retval None
  702. */
  703. void OV2640_BandWConfig(uint8_t BlackWhite)
  704. {
  705. OV2640_WriteReg(0xff, 0x00);
  706. OV2640_WriteReg(0x7c, 0x00);
  707. OV2640_WriteReg(0x7d, BlackWhite);
  708. OV2640_WriteReg(0x7c, 0x05);
  709. OV2640_WriteReg(0x7d, 0x80);
  710. OV2640_WriteReg(0x7d, 0x80);
  711. }
  712. /**
  713. * @brief Configures the OV2640 camera color effects.
  714. * @param value1: Color effects value1
  715. * @param value2: Color effects value2
  716. * where value1 and value2 can be:
  717. * value1 = 0x40, value2 = 0xa6 for Antique,
  718. * value1 = 0xa0, value2 = 0x40 for Bluish,
  719. * value1 = 0x40, value2 = 0x40 for Greenish,
  720. * value1 = 0x40, value2 = 0xc0 for Reddish,
  721. * @retval None
  722. */
  723. void OV2640_ColorEffectsConfig(uint8_t value1, uint8_t value2)
  724. {
  725. OV2640_WriteReg(0xff, 0x00);
  726. OV2640_WriteReg(0x7c, 0x00);
  727. OV2640_WriteReg(0x7d, 0x18);
  728. OV2640_WriteReg(0x7c, 0x05);
  729. OV2640_WriteReg(0x7d, value1);
  730. OV2640_WriteReg(0x7d, value2);
  731. }
  732. /**
  733. * @brief Configures the OV2640 camera contrast.
  734. * @param value1: Contrast value1
  735. * @param value2: Contrast value2
  736. * where value1 and value2 can be:
  737. * value1 = 0x28, value2 = 0x0c for Contrast +2,
  738. * value1 = 0x24, value2 = 0x16 for Contrast +1,
  739. * value1 = 0x20, value2 = 0x20 for Contrast 0,
  740. * value1 = 0x1c, value2 = 0x2a for Contrast -1,
  741. * value1 = 0x18, value2 = 0x34 for Contrast -2,
  742. * @retval None
  743. */
  744. void OV2640_ContrastConfig(uint8_t value1, uint8_t value2)
  745. {
  746. OV2640_WriteReg(0xff, 0x00);
  747. OV2640_WriteReg(0x7c, 0x00);
  748. OV2640_WriteReg(0x7d, 0x04);
  749. OV2640_WriteReg(0x7c, 0x07);
  750. OV2640_WriteReg(0x7d, 0x20);
  751. OV2640_WriteReg(0x7d, value1);
  752. OV2640_WriteReg(0x7d, value2);
  753. OV2640_WriteReg(0x7d, 0x06);
  754. }
  755. /**
  756. * @brief Writes a byte at a specific Camera register
  757. * @param Addr: OV2640 register address.
  758. * @param Data: Data to be written to the specific register
  759. * @retval 0x00 if write operation is OK.
  760. * 0xFF if timeout condition occured (device not connected or bus error).
  761. */
  762. uint8_t OV2640_WriteReg(uint16_t Addr, uint8_t Data)
  763. {
  764. uint8_t sdata[3] = {0};
  765. memcpy(sdata, &Addr, 2);
  766. sdata[2] = Data;
  767. nrf_drv_twim0_tx(OV2640_DEVICE_WRITE_ADDRESS, sdata, 3, true);
  768. /* If operation is OK, return 0 */
  769. return 0;
  770. }
  771. /**
  772. * @brief Reads a byte from a specific Camera register
  773. * @param Addr: OV2640 register address.
  774. * @retval data read from the specific register or 0xFF if timeout condition
  775. * occured.
  776. */
  777. uint8_t OV2640_ReadReg(uint16_t Addr)
  778. {
  779. uint8_t recv_data = 0;
  780. nrf_drv_twim0_rx(OV2640_DEVICE_WRITE_ADDRESS, recv_data, 1);
  781. /* return the read data */
  782. return Data;
  783. }
  784. static uint8_t get_ov2640_data(void)
  785. {
  786. uint8_t data = 0;
  787. uint8_t temp = nrf_gpio_pin_read(CAM_D0_PIN);
  788. data <<= 1;
  789. data |= temp;
  790. temp = nrf_gpio_pin_read(CAM_D1_PIN);
  791. data <<= 1;
  792. data |= temp;
  793. temp = nrf_gpio_pin_read(CAM_D2_PIN);
  794. data <<= 1;
  795. data |= temp;
  796. temp = nrf_gpio_pin_read(CAM_D3_PIN);
  797. data <<= 1;
  798. data |= temp;
  799. temp = nrf_gpio_pin_read(CAM_D4_PIN);
  800. data <<= 1;
  801. data |= temp;
  802. temp = nrf_gpio_pin_read(CAM_D5_PIN);
  803. data <<= 1;
  804. data |= temp;
  805. temp = nrf_gpio_pin_read(CAM_D6_PIN);
  806. data <<= 1;
  807. data |= temp;
  808. temp = nrf_gpio_pin_read(CAM_D7_PIN);
  809. data <<= 1;
  810. data |= temp;
  811. return data;
  812. }
  813. void ov2640_init(void)
  814. {
  815. OV2640_HW_Init(); //IIC初始化
  816. OV2640_ReadID(&OV2640_Camera_ID); //读取OV2640ID,测试硬件,依次为:0x7F,0xA2,0x26,0x42
  817. OV2640_JPEGConfig(JPEG_320x240); //配置OV2640输出320*240像素的JPG图片
  818. //设置自动曝光和白平衡
  819. OV2640_BrightnessConfig(0x20);
  820. OV2640_AutoExposure(2);
  821. }
  822. /**
  823. * @}
  824. */
  825. /**
  826. * @}
  827. */
  828. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/