找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
楼主: eagler8
打印 上一主题 下一主题
收起左侧

【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)

  [复制链接]
1561#
ID:513258 发表于 2019-8-4 09:26 | 只看该作者
本帖最后由 eagler8 于 2019-8-4 14:27 编辑



3V3-------输入电源电压(推荐使用3.3,5V也可,但不推荐)
GDN------接地点
SIO_C-----SCCB接口的控制时钟(注意:部分低级单片机需要上拉控制,和I2C接口类似)
SIO_D-----SCCB接口的串行数据输入(出)端(注意:部分低级单片机需要上拉控制,和I2C接口类似)
VSYNC----帧同步信号(输出信号)
HREF------行同步信号(输出信号)
PCLK------像素时钟(输出信号)
D0-D7-----数据端口(输出信号)
RESTE-----复位端口(正常使用拉高)
PWDN----功耗选择模式(正常使用拉低)

回复

使用道具 举报

1562#
ID:513258 发表于 2019-8-4 11:34 | 只看该作者
  1. /*
  2.   【Arduino】66种传感器模块系列实验(80)
  3.   实验八十: OV7670摄像头模块30W采集拍照模组(替OV7725)
  4.   程序通过验证,上传成功,结果是13脚LED一直闪烁,不知那根线接错了,
  5.   已经检查二遍了,还是不对,准备重新再连接一次,再不行只好放弃
  6.   
  7.   备注:程序烧录进arduino后,除了power灯之外,如果led灯(13脚)亮,说明出错,
  8.   连接有问题,请检查插线,正常会显示tx灯闪烁,隔一段闪一次,不是狂闪,闪一次说
  9.   明采集完成了一张照片,也就是读取了一帧,并传输完成,使用windows系统的采集软件
  10.   ReadSerialPortWin打开图像并保存。
  11. */

  12. //wukongxuetang teacher zhang
  13. // Source code for application to transmit image from ov7670 to PC via USB
  14. // By Siarhei Charkes in 2015
  15. // http://privateblog.info

  16. #include <stdint.h>
  17. #include <avr/io.h>
  18. #include <util/twi.h>
  19. #include <util/delay.h>
  20. #include <avr/pgmspace.h>

  21. #define F_CPU 16000000UL
  22. #define vga   0
  23. #define qvga  1
  24. #define qqvga   2
  25. #define yuv422  0
  26. #define rgb565  1
  27. #define bayerRGB  2
  28. #define camAddr_WR  0x42
  29. #define camAddr_RD  0x43

  30. /* Registers */
  31. #define REG_GAIN    0x00  /* Gain lower 8 bits (rest in vref) */
  32. #define REG_BLUE    0x01  /* blue gain */
  33. #define REG_RED       0x02  /* red gain */
  34. #define REG_VREF    0x03  /* Pieces of GAIN, VSTART, VSTOP */
  35. #define REG_COM1    0x04  /* Control 1 */
  36. #define COM1_CCIR656  0x40    /* CCIR656 enable */

  37. #define REG_BAVE    0x05  /* U/B Average level */
  38. #define REG_GbAVE   0x06  /* Y/Gb Average level */
  39. #define REG_AECHH   0x07  /* AEC MS 5 bits */
  40. #define REG_RAVE    0x08  /* V/R Average level */
  41. #define REG_COM2    0x09  /* Control 2 */
  42. #define COM2_SSLEEP         0x10  /* Soft sleep mode */
  43. #define REG_PID           0x0a  /* Product ID MSB */
  44. #define REG_VER           0x0b  /* Product ID LSB */
  45. #define REG_COM3    0x0c  /* Control 3 */
  46. #define COM3_SWAP         0x40  /* Byte swap */
  47. #define COM3_SCALEEN          0x08  /* Enable scaling */
  48. #define COM3_DCWEN          0x04  /* Enable downsamp/crop/window */
  49. #define REG_COM4    0x0d  /* Control 4 */
  50. #define REG_COM5    0x0e  /* All "reserved" */
  51. #define REG_COM6    0x0f  /* Control 6 */
  52. #define REG_AECH    0x10  /* More bits of AEC value */
  53. #define REG_CLKRC   0x11  /* Clocl control */
  54. #define CLK_EXT           0x40  /* Use external clock directly */
  55. #define CLK_SCALE   0x3f  /* Mask for internal clock scale */
  56. #define REG_COM7    0x12  /* Control 7 */ //REG mean address.
  57. #define COM7_RESET          0x80  /* Register reset */
  58. #define COM7_FMT_MASK         0x38
  59. #define COM7_FMT_VGA          0x00
  60. #define COM7_FMT_CIF          0x20  /* CIF format */
  61. #define COM7_FMT_QVGA         0x10  /* QVGA format */
  62. #define COM7_FMT_QCIF         0x08  /* QCIF format */
  63. #define COM7_RGB          0x04  /* bits 0 and 2 - RGB format */
  64. #define COM7_YUV          0x00  /* YUV */
  65. #define COM7_BAYER          0x01  /* Bayer format */
  66. #define COM7_PBAYER         0x05  /* "rocessed bayer" */
  67. #define REG_COM8    0x13  /* Control 8 */
  68. #define COM8_FASTAEC          0x80  /* Enable fast AGC/AEC */
  69. #define COM8_AECSTEP          0x40  /* Unlimited AEC step size */
  70. #define COM8_BFILT    0x20  /* Band filter enable */
  71. #define COM8_AGC    0x04  /* Auto gain enable */
  72. #define COM8_AWB    0x02  /* White balance enable */
  73. #define COM8_AEC    0x01  /* Auto exposure enable */
  74. #define REG_COM9    0x14  /* Control 9- gain ceiling */
  75. #define REG_COM10   0x15  /* Control 10 */
  76. #define COM10_HSYNC         0x40  /* HSYNC instead of HREF */
  77. #define COM10_PCLK_HB         0x20  /* Suppress PCLK on horiz blank */
  78. #define COM10_HREF_REV          0x08  /* Reverse HREF */
  79. #define COM10_VS_LEAD         0x04  /* VSYNC on clock leading edge */
  80. #define COM10_VS_NEG          0x02  /* VSYNC negative */
  81. #define COM10_HS_NEG          0x01  /* HSYNC negative */
  82. #define REG_HSTART    0x17  /* Horiz start high bits */
  83. #define REG_HSTOP   0x18  /* Horiz stop high bits */
  84. #define REG_VSTART    0x19  /* Vert start high bits */
  85. #define REG_VSTOP   0x1a  /* Vert stop high bits */
  86. #define REG_PSHFT   0x1b  /* Pixel delay after HREF */
  87. #define REG_MIDH    0x1c  /* Manuf. ID high */
  88. #define REG_MIDL    0x1d  /* Manuf. ID low */
  89. #define REG_MVFP    0x1e  /* Mirror / vflip */
  90. #define MVFP_MIRROR         0x20  /* Mirror image */
  91. #define MVFP_FLIP   0x10  /* Vertical flip */

  92. #define REG_AEW           0x24  /* AGC upper limit */
  93. #define REG_AEB           0x25    /* AGC lower limit */
  94. #define REG_VPT           0x26  /* AGC/AEC fast mode op region */
  95. #define REG_HSYST   0x30  /* HSYNC rising edge delay */
  96. #define REG_HSYEN   0x31  /* HSYNC falling edge delay */
  97. #define REG_HREF    0x32  /* HREF pieces */
  98. #define REG_TSLB    0x3a  /* lots of stuff */
  99. #define TSLB_YLAST    0x04  /* UYVY or VYUY - see com13 */
  100. #define REG_COM11   0x3b  /* Control 11 */
  101. #define COM11_NIGHT         0x80  /* NIght mode enable */
  102. #define COM11_NMFR          0x60  /* Two bit NM frame rate */
  103. #define COM11_HZAUTO          0x10  /* Auto detect 50/60 Hz */
  104. #define COM11_50HZ          0x08  /* Manual 50Hz select */
  105. #define COM11_EXP   0x02
  106. #define REG_COM12   0x3c  /* Control 12 */
  107. #define COM12_HREF          0x80  /* HREF always */
  108. #define REG_COM13   0x3d  /* Control 13 */
  109. #define COM13_GAMMA         0x80  /* Gamma enable */
  110. #define COM13_UVSAT         0x40  /* UV saturation auto adjustment */
  111. #define COM13_UVSWAP          0x01  /* V before U - w/TSLB */
  112. #define REG_COM14   0x3e  /* Control 14 */
  113. #define COM14_DCWEN         0x10  /* DCW/PCLK-scale enable */
  114. #define REG_EDGE    0x3f  /* Edge enhancement factor */
  115. #define REG_COM15   0x40  /* Control 15 */
  116. #define COM15_R10F0         0x00  /* Data range 10 to F0 */
  117. #define COM15_R01FE         0x80  /*      01 to FE */
  118. #define COM15_R00FF         0xc0  /*      00 to FF */
  119. #define COM15_RGB565          0x10  /* RGB565 output */
  120. #define COM15_RGB555          0x30  /* RGB555 output */
  121. #define REG_COM16   0x41  /* Control 16 */
  122. #define COM16_AWBGAIN         0x08  /* AWB gain enable */
  123. #define REG_COM17   0x42  /* Control 17 */
  124. #define COM17_AECWIN          0xc0  /* AEC window - must match COM4 */
  125. #define COM17_CBAR          0x08  /* DSP Color bar */
  126. /*
  127. * This matrix defines how the colors are generated, must be
  128. * tweaked to adjust hue and saturation.
  129. *
  130. * Order: v-red, v-green, v-blue, u-red, u-green, u-blue
  131. * They are nine-bit signed quantities, with the sign bit
  132. * stored in0x58.Sign for v-red is bit 0, and up from there.
  133. */
  134. #define REG_CMATRIX_BASE  0x4f
  135. #define CMATRIX_LEN           6
  136. #define REG_CMATRIX_SIGN  0x58
  137. #define REG_BRIGHT    0x55  /* Brightness */
  138. #define REG_CONTRAS         0x56  /* Contrast control */
  139. #define REG_GFIX    0x69  /* Fix gain control */
  140. #define REG_REG76   0x76  /* OV's name */
  141. #define R76_BLKPCOR         0x80  /* Black pixel correction enable */
  142. #define R76_WHTPCOR         0x40  /* White pixel correction enable */
  143. #define REG_RGB444          0x8c  /* RGB 444 control */
  144. #define R444_ENABLE         0x02  /* Turn on RGB444, overrides 5x5 */
  145. #define R444_RGBX   0x01  /* Empty nibble at end */
  146. #define REG_HAECC1    0x9f  /* Hist AEC/AGC control 1 */
  147. #define REG_HAECC2    0xa0  /* Hist AEC/AGC control 2 */
  148. #define REG_BD50MAX         0xa5  /* 50hz banding step limit */
  149. #define REG_HAECC3    0xa6  /* Hist AEC/AGC control 3 */
  150. #define REG_HAECC4    0xa7  /* Hist AEC/AGC control 4 */
  151. #define REG_HAECC5    0xa8  /* Hist AEC/AGC control 5 */
  152. #define REG_HAECC6    0xa9  /* Hist AEC/AGC control 6 */
  153. #define REG_HAECC7    0xaa  /* Hist AEC/AGC control 7 */
  154. #define REG_BD60MAX         0xab  /* 60hz banding step limit */
  155. #define REG_GAIN    0x00  /* Gain lower 8 bits (rest in vref) */
  156. #define REG_BLUE    0x01  /* blue gain */
  157. #define REG_RED           0x02  /* red gain */
  158. #define REG_VREF    0x03  /* Pieces of GAIN, VSTART, VSTOP */
  159. #define REG_COM1    0x04  /* Control 1 */
  160. #define COM1_CCIR656          0x40  /* CCIR656 enable */
  161. #define REG_BAVE    0x05  /* U/B Average level */
  162. #define REG_GbAVE   0x06  /* Y/Gb Average level */
  163. #define REG_AECHH   0x07  /* AEC MS 5 bits */
  164. #define REG_RAVE    0x08  /* V/R Average level */
  165. #define REG_COM2    0x09  /* Control 2 */
  166. #define COM2_SSLEEP         0x10  /* Soft sleep mode */
  167. #define REG_PID           0x0a  /* Product ID MSB */
  168. #define REG_VER           0x0b  /* Product ID LSB */
  169. #define REG_COM3    0x0c  /* Control 3 */
  170. #define COM3_SWAP         0x40  /* Byte swap */
  171. #define COM3_SCALEEN          0x08  /* Enable scaling */
  172. #define COM3_DCWEN          0x04  /* Enable downsamp/crop/window */
  173. #define REG_COM4    0x0d  /* Control 4 */
  174. #define REG_COM5    0x0e  /* All "reserved" */
  175. #define REG_COM6    0x0f  /* Control 6 */
  176. #define REG_AECH    0x10  /* More bits of AEC value */
  177. #define REG_CLKRC   0x11  /* Clocl control */
  178. #define CLK_EXT           0x40  /* Use external clock directly */
  179. #define CLK_SCALE   0x3f  /* Mask for internal clock scale */
  180. #define REG_COM7    0x12  /* Control 7 */
  181. #define COM7_RESET          0x80  /* Register reset */
  182. #define COM7_FMT_MASK         0x38
  183. #define COM7_FMT_VGA          0x00
  184. #define COM7_FMT_CIF          0x20  /* CIF format */
  185. #define COM7_FMT_QVGA         0x10  /* QVGA format */
  186. #define COM7_FMT_QCIF         0x08  /* QCIF format */
  187. #define COM7_RGB    0x04  /* bits 0 and 2 - RGB format */
  188. #define COM7_YUV    0x00  /* YUV */
  189. #define COM7_BAYER          0x01  /* Bayer format */
  190. #define COM7_PBAYER         0x05  /* "rocessed bayer" */
  191. #define REG_COM8    0x13  /* Control 8 */
  192. #define COM8_FASTAEC          0x80  /* Enable fast AGC/AEC */
  193. #define COM8_AECSTEP          0x40  /* Unlimited AEC step size */
  194. #define COM8_BFILT    0x20  /* Band filter enable */
  195. #define COM8_AGC    0x04  /* Auto gain enable */
  196. #define COM8_AWB    0x02  /* White balance enable */
  197. #define COM8_AEC    0x01  /* Auto exposure enable */
  198. #define REG_COM9    0x14  /* Control 9- gain ceiling */
  199. #define REG_COM10   0x15  /* Control 10 */
  200. #define COM10_HSYNC         0x40  /* HSYNC instead of HREF */
  201. #define COM10_PCLK_HB         0x20  /* Suppress PCLK on horiz blank */
  202. #define COM10_HREF_REV          0x08  /* Reverse HREF */
  203. #define COM10_VS_LEAD           0x04  /* VSYNC on clock leading edge */
  204. #define COM10_VS_NEG          0x02  /* VSYNC negative */
  205. #define COM10_HS_NEG          0x01  /* HSYNC negative */
  206. #define REG_HSTART    0x17  /* Horiz start high bits */
  207. #define REG_HSTOP   0x18  /* Horiz stop high bits */
  208. #define REG_VSTART    0x19  /* Vert start high bits */
  209. #define REG_VSTOP   0x1a  /* Vert stop high bits */
  210. #define REG_PSHFT   0x1b  /* Pixel delay after HREF */
  211. #define REG_MIDH    0x1c  /* Manuf. ID high */
  212. #define REG_MIDL    0x1d  /* Manuf. ID low */
  213. #define REG_MVFP    0x1e  /* Mirror / vflip */
  214. #define MVFP_MIRROR         0x20  /* Mirror image */
  215. #define MVFP_FLIP   0x10  /* Vertical flip */
  216. #define REG_AEW           0x24  /* AGC upper limit */
  217. #define REG_AEB           0x25  /* AGC lower limit */
  218. #define REG_VPT           0x26  /* AGC/AEC fast mode op region */
  219. #define REG_HSYST   0x30  /* HSYNC rising edge delay */
  220. #define REG_HSYEN   0x31  /* HSYNC falling edge delay */
  221. #define REG_HREF    0x32  /* HREF pieces */
  222. #define REG_TSLB    0x3a  /* lots of stuff */
  223. #define TSLB_YLAST    0x04  /* UYVY or VYUY - see com13 */
  224. #define REG_COM11   0x3b  /* Control 11 */
  225. #define COM11_NIGHT         0x80  /* NIght mode enable */
  226. #define COM11_NMFR          0x60  /* Two bit NM frame rate */
  227. #define COM11_HZAUTO          0x10  /* Auto detect 50/60 Hz */
  228. #define COM11_50HZ          0x08  /* Manual 50Hz select */
  229. #define COM11_EXP   0x02
  230. #define REG_COM12   0x3c  /* Control 12 */
  231. #define COM12_HREF          0x80  /* HREF always */
  232. #define REG_COM13   0x3d  /* Control 13 */
  233. #define COM13_GAMMA         0x80  /* Gamma enable */
  234. #define COM13_UVSAT         0x40  /* UV saturation auto adjustment */
  235. #define COM13_UVSWAP          0x01  /* V before U - w/TSLB */
  236. #define REG_COM14   0x3e  /* Control 14 */
  237. #define COM14_DCWEN         0x10  /* DCW/PCLK-scale enable */
  238. #define REG_EDGE    0x3f  /* Edge enhancement factor */
  239. #define REG_COM15   0x40  /* Control 15 */
  240. #define COM15_R10F0         0x00  /* Data range 10 to F0 */
  241. #define COM15_R01FE         0x80  /*      01 to FE */
  242. #define COM15_R00FF         0xc0  /*      00 to FF */
  243. #define COM15_RGB565          0x10  /* RGB565 output */
  244. #define COM15_RGB555          0x30  /* RGB555 output */
  245. #define REG_COM16   0x41  /* Control 16 */
  246. #define COM16_AWBGAIN         0x08  /* AWB gain enable */
  247. #define REG_COM17   0x42  /* Control 17 */
  248. #define COM17_AECWIN          0xc0  /* AEC window - must match COM4 */
  249. #define COM17_CBAR          0x08  /* DSP Color bar */

  250. #define CMATRIX_LEN             6
  251. #define REG_BRIGHT    0x55  /* Brightness */
  252. #define REG_REG76   0x76  /* OV's name */
  253. #define R76_BLKPCOR         0x80  /* Black pixel correction enable */
  254. #define R76_WHTPCOR         0x40  /* White pixel correction enable */
  255. #define REG_RGB444          0x8c  /* RGB 444 control */
  256. #define R444_ENABLE         0x02  /* Turn on RGB444, overrides 5x5 */
  257. #define R444_RGBX   0x01  /* Empty nibble at end */
  258. #define REG_HAECC1    0x9f  /* Hist AEC/AGC control 1 */
  259. #define REG_HAECC2    0xa0  /* Hist AEC/AGC control 2 */
  260. #define REG_BD50MAX         0xa5  /* 50hz banding step limit */
  261. #define REG_HAECC3    0xa6  /* Hist AEC/AGC control 3 */
  262. #define REG_HAECC4    0xa7  /* Hist AEC/AGC control 4 */
  263. #define REG_HAECC5    0xa8  /* Hist AEC/AGC control 5 */
  264. #define REG_HAECC6    0xa9  /* Hist AEC/AGC control 6 */
  265. #define REG_HAECC7    0xaa  /* Hist AEC/AGC control 7 */
  266. #define REG_BD60MAX         0xab  /* 60hz banding step limit */
  267. #define MTX1            0x4f  /* Matrix Coefficient 1 */
  268. #define MTX2            0x50  /* Matrix Coefficient 2 */
  269. #define MTX3            0x51  /* Matrix Coefficient 3 */
  270. #define MTX4            0x52  /* Matrix Coefficient 4 */
  271. #define MTX5            0x53  /* Matrix Coefficient 5 */
  272. #define MTX6            0x54  /* Matrix Coefficient 6 */
  273. #define REG_CONTRAS         0x56  /* Contrast control */
  274. #define MTXS            0x58  /* Matrix Coefficient Sign */
  275. #define AWBC7           0x59  /* AWB Control 7 */
  276. #define AWBC8           0x5a  /* AWB Control 8 */
  277. #define AWBC9           0x5b  /* AWB Control 9 */
  278. #define AWBC10            0x5c  /* AWB Control 10 */
  279. #define AWBC11            0x5d  /* AWB Control 11 */
  280. #define AWBC12            0x5e  /* AWB Control 12 */
  281. #define REG_GFI           0x69  /* Fix gain control */
  282. #define GGAIN           0x6a  /* G Channel AWB Gain */
  283. #define DBLV            0x6b  
  284. #define AWBCTR3           0x6c  /* AWB Control 3 */
  285. #define AWBCTR2           0x6d  /* AWB Control 2 */
  286. #define AWBCTR1           0x6e  /* AWB Control 1 */
  287. #define AWBCTR0           0x6f  /* AWB Control 0 */

  288. struct regval_list{
  289.   uint8_t reg_num;
  290.   uint16_t value;
  291. };

  292. const struct regval_list qvga_ov7670[] PROGMEM = {
  293.   { REG_COM14, 0x19 },
  294.   { 0x72, 0x11 },
  295.   { 0x73, 0xf1 },

  296.   { REG_HSTART, 0x16 },
  297.   { REG_HSTOP, 0x04 },
  298.   { REG_HREF, 0xa4 },
  299.   { REG_VSTART, 0x02 },
  300.   { REG_VSTOP, 0x7a },
  301.   { REG_VREF, 0x0a },


  302. /*  { REG_HSTART, 0x16 },
  303.   { REG_HSTOP, 0x04 },
  304.   { REG_HREF, 0x24 },
  305.   { REG_VSTART, 0x02 },
  306.   { REG_VSTOP, 0x7a },
  307.   { REG_VREF, 0x0a },*/
  308.   { 0xff, 0xff }, /* END MARKER */
  309. };

  310. const struct regval_list yuv422_ov7670[] PROGMEM = {
  311.   { REG_COM7, 0x0 },  /* Selects YUV mode */
  312.   { REG_RGB444, 0 },  /* No RGB444 please */
  313.   { REG_COM1, 0 },
  314.   { REG_COM15, COM15_R00FF },
  315.   { REG_COM9, 0x6A }, /* 128x gain ceiling; 0x8 is reserved bit */
  316.   { 0x4f, 0x80 },   /* "matrix coefficient 1" */
  317.   { 0x50, 0x80 },   /* "matrix coefficient 2" */
  318.   { 0x51, 0 },    /* vb */
  319.   { 0x52, 0x22 },   /* "matrix coefficient 4" */
  320.   { 0x53, 0x5e },   /* "matrix coefficient 5" */
  321.   { 0x54, 0x80 },   /* "matrix coefficient 6" */
  322.   { REG_COM13, COM13_UVSAT },
  323.   { 0xff, 0xff },   /* END MARKER */
  324. };

  325. const struct regval_list ov7670_default_regs[] PROGMEM = {//from the linux driver
  326.   { REG_COM7, COM7_RESET },
  327.   { REG_TSLB, 0x04 }, /* OV */
  328.   { REG_COM7, 0 },  /* VGA */
  329.   /*
  330.   * Set the hardware window.  These values from OV don't entirely
  331.   * make sense - hstop is less than hstart.  But they work...
  332.   */
  333.   { REG_HSTART, 0x13 }, { REG_HSTOP, 0x01 },
  334.   { REG_HREF, 0xb6 }, { REG_VSTART, 0x02 },
  335.   { REG_VSTOP, 0x7a }, { REG_VREF, 0x0a },

  336.   { REG_COM3, 0 }, { REG_COM14, 0 },
  337.   /* Mystery scaling numbers */
  338.   { 0x70, 0x3a }, { 0x71, 0x35 },
  339.   { 0x72, 0x11 }, { 0x73, 0xf0 },
  340.   { 0xa2,/* 0x02 changed to 1*/1 }, { REG_COM10, 0x0 },
  341.   /* Gamma curve values */
  342.   { 0x7a, 0x20 }, { 0x7b, 0x10 },
  343.   { 0x7c, 0x1e }, { 0x7d, 0x35 },
  344.   { 0x7e, 0x5a }, { 0x7f, 0x69 },
  345.   { 0x80, 0x76 }, { 0x81, 0x80 },
  346.   { 0x82, 0x88 }, { 0x83, 0x8f },
  347.   { 0x84, 0x96 }, { 0x85, 0xa3 },
  348.   { 0x86, 0xaf }, { 0x87, 0xc4 },
  349.   { 0x88, 0xd7 }, { 0x89, 0xe8 },
  350.   /* AGC and AEC parameters.  Note we start by disabling those features,
  351.   then turn them only after tweaking the values. */
  352.   { REG_COM8, COM8_FASTAEC | COM8_AECSTEP },
  353.   { REG_GAIN, 0 }, { REG_AECH, 0 },
  354.   { REG_COM4, 0x40 }, /* magic reserved bit */
  355.   { REG_COM9, 0x18 }, /* 4x gain + magic rsvd bit */
  356.   { REG_BD50MAX, 0x05 }, { REG_BD60MAX, 0x07 },
  357.   { REG_AEW, 0x95 }, { REG_AEB, 0x33 },
  358.   { REG_VPT, 0xe3 }, { REG_HAECC1, 0x78 },
  359.   { REG_HAECC2, 0x68 }, { 0xa1, 0x03 }, /* magic */
  360.   { REG_HAECC3, 0xd8 }, { REG_HAECC4, 0xd8 },
  361.   { REG_HAECC5, 0xf0 }, { REG_HAECC6, 0x90 },
  362.   { REG_HAECC7, 0x94 },
  363.   { REG_COM8, COM8_FASTAEC | COM8_AECSTEP | COM8_AGC | COM8_AEC },
  364.   { 0x30, 0 }, { 0x31, 0 },//disable some delays
  365.   /* Almost all of these are magic "reserved" values.  */
  366.   { REG_COM5, 0x61 }, { REG_COM6, 0x4b },
  367.   { 0x16, 0x02 }, { REG_MVFP, 0x07 },
  368.   { 0x21, 0x02 }, { 0x22, 0x91 },
  369.   { 0x29, 0x07 }, { 0x33, 0x0b },
  370.   { 0x35, 0x0b }, { 0x37, 0x1d },
  371.   { 0x38, 0x71 }, { 0x39, 0x2a },
  372.   { REG_COM12, 0x78 }, { 0x4d, 0x40 },
  373.   { 0x4e, 0x20 }, { REG_GFIX, 0 },
  374.   /*{0x6b, 0x4a},*/{ 0x74, 0x10 },
  375.   { 0x8d, 0x4f }, { 0x8e, 0 },
  376.   { 0x8f, 0 }, { 0x90, 0 },
  377.   { 0x91, 0 }, { 0x96, 0 },
  378.   { 0x9a, 0 }, { 0xb0, 0x84 },
  379.   { 0xb1, 0x0c }, { 0xb2, 0x0e },
  380.   { 0xb3, 0x82 }, { 0xb8, 0x0a },

  381.   /* More reserved magic, some of which tweaks white balance */
  382.   { 0x43, 0x0a }, { 0x44, 0xf0 },
  383.   { 0x45, 0x34 }, { 0x46, 0x58 },
  384.   { 0x47, 0x28 }, { 0x48, 0x3a },
  385.   { 0x59, 0x88 }, { 0x5a, 0x88 },
  386.   { 0x5b, 0x44 }, { 0x5c, 0x67 },
  387.   { 0x5d, 0x49 }, { 0x5e, 0x0e },
  388.   { 0x6c, 0x0a }, { 0x6d, 0x55 },
  389.   { 0x6e, 0x11 }, { 0x6f, 0x9e }, /* it was 0x9F "9e for advance AWB" */
  390.   { 0x6a, 0x40 }, { REG_BLUE, 0x40 },
  391.   { REG_RED, 0x60 },
  392.   { REG_COM8, COM8_FASTAEC | COM8_AECSTEP | COM8_AGC | COM8_AEC | COM8_AWB },

  393.   /* Matrix coefficients */
  394.   { 0x4f, 0x80 }, { 0x50, 0x80 },
  395.   { 0x51, 0 },    { 0x52, 0x22 },
  396.   { 0x53, 0x5e }, { 0x54, 0x80 },
  397.   { 0x58, 0x9e },

  398.   { REG_COM16, COM16_AWBGAIN }, { REG_EDGE, 0 },
  399.   { 0x75, 0x05 }, { REG_REG76, 0xe1 },
  400.   { 0x4c, 0 },     { 0x77, 0x01 },
  401.   { REG_COM13, /*0xc3*/0x48 }, { 0x4b, 0x09 },
  402.   { 0xc9, 0x60 },   /*{REG_COM16, 0x38},*/
  403.   { 0x56, 0x40 },

  404.   { 0x34, 0x11 }, { REG_COM11, COM11_EXP | COM11_HZAUTO },
  405.   { 0xa4, 0x82/*Was 0x88*/ }, { 0x96, 0 },
  406.   { 0x97, 0x30 }, { 0x98, 0x20 },
  407.   { 0x99, 0x30 }, { 0x9a, 0x84 },
  408.   { 0x9b, 0x29 }, { 0x9c, 0x03 },
  409.   { 0x9d, 0x4c }, { 0x9e, 0x3f },
  410.   { 0x78, 0x04 },

  411.   /* Extra-weird stuff.  Some sort of multiplexor register */
  412.   { 0x79, 0x01 }, { 0xc8, 0xf0 },
  413.   { 0x79, 0x0f }, { 0xc8, 0x00 },
  414.   { 0x79, 0x10 }, { 0xc8, 0x7e },
  415.   { 0x79, 0x0a }, { 0xc8, 0x80 },
  416.   { 0x79, 0x0b }, { 0xc8, 0x01 },
  417.   { 0x79, 0x0c }, { 0xc8, 0x0f },
  418.   { 0x79, 0x0d }, { 0xc8, 0x20 },
  419.   { 0x79, 0x09 }, { 0xc8, 0x80 },
  420.   { 0x79, 0x02 }, { 0xc8, 0xc0 },
  421.   { 0x79, 0x03 }, { 0xc8, 0x40 },
  422.   { 0x79, 0x05 }, { 0xc8, 0x30 },
  423.   { 0x79, 0x26 },
  424.   { 0xff, 0xff }, /* END MARKER */
  425. };


  426. void error_led(void){
  427.   DDRB |= 32;//make sure led is output
  428.   while (1){//wait for reset
  429.     PORTB ^= 32;// toggle led
  430.     _delay_ms(100);
  431.   }
  432. }

  433. void twiStart(void){
  434.   TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);//send start
  435.   while (!(TWCR & (1 << TWINT)));//wait for start to be transmitted
  436.   if ((TWSR & 0xF8) != TW_START)
  437.     error_led();
  438. }

  439. void twiWriteByte(uint8_t DATA, uint8_t type){
  440.   TWDR = DATA;
  441.   TWCR = _BV(TWINT) | _BV(TWEN);
  442.   while (!(TWCR & (1 << TWINT))) {}
  443.   if ((TWSR & 0xF8) != type)
  444.     error_led();
  445. }

  446. void twiAddr(uint8_t addr, uint8_t typeTWI){
  447.   TWDR = addr;//send address
  448.   TWCR = _BV(TWINT) | _BV(TWEN);    /* clear interrupt to start transmission */
  449.   while ((TWCR & _BV(TWINT)) == 0); /* wait for transmission */
  450.   if ((TWSR & 0xF8) != typeTWI)
  451.     error_led();
  452. }

  453. void wrReg(uint8_t reg, uint8_t dat){
  454.   //send start condition
  455.   twiStart();
  456.   twiAddr(camAddr_WR, TW_MT_SLA_ACK);
  457.   twiWriteByte(reg, TW_MT_DATA_ACK);
  458.   twiWriteByte(dat, TW_MT_DATA_ACK);
  459.   TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);//send stop
  460.   _delay_ms(1);
  461. }

  462. static uint8_t twiRd(uint8_t nack){
  463.   if (nack){
  464.     TWCR = _BV(TWINT) | _BV(TWEN);
  465.     while ((TWCR & _BV(TWINT)) == 0); /* wait for transmission */
  466.     if ((TWSR & 0xF8) != TW_MR_DATA_NACK)
  467.       error_led();
  468.     return TWDR;
  469.   }
  470.   else{
  471.     TWCR = _BV(TWINT) | _BV(TWEN) | _BV(TWEA);
  472.     while ((TWCR & _BV(TWINT)) == 0); /* wait for transmission */
  473.     if ((TWSR & 0xF8) != TW_MR_DATA_ACK)
  474.       error_led();
  475.     return TWDR;
  476.   }
  477. }

  478. uint8_t rdReg(uint8_t reg){
  479.   uint8_t dat;
  480.   twiStart();
  481.   twiAddr(camAddr_WR, TW_MT_SLA_ACK);
  482.   twiWriteByte(reg, TW_MT_DATA_ACK);
  483.   TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);//send stop
  484.   _delay_ms(1);
  485.   twiStart();
  486.   twiAddr(camAddr_RD, TW_MR_SLA_ACK);
  487.   dat = twiRd(1);
  488.   TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);//send stop
  489.   _delay_ms(1);
  490.   return dat;
  491. }

  492. void wrSensorRegs8_8(const struct regval_list reglist[]){
  493.   uint8_t reg_addr, reg_val;
  494.   const struct regval_list *next = reglist;
  495.   while ((reg_addr != 0xff) | (reg_val != 0xff)){
  496.     reg_addr = pgm_read_byte(&next->reg_num);
  497.     reg_val = pgm_read_byte(&next->value);
  498.     wrReg(reg_addr, reg_val);
  499.     next++;
  500.   }
  501. }

  502. void setColor(void){
  503.   wrSensorRegs8_8(yuv422_ov7670);
  504. }

  505. void setRes(void){
  506.   wrReg(REG_COM3, 4); // REG_COM3 enable scaling
  507.   wrSensorRegs8_8(qvga_ov7670);
  508. }

  509. void camInit(void){
  510.   wrReg(0x12, 0x80);
  511.   _delay_ms(100);
  512.   wrSensorRegs8_8(ov7670_default_regs);
  513.   wrReg(REG_COM10, 32);//PCLK does not toggle on HBLANK.
  514. }

  515. void arduinoUnoInut(void) {
  516.   cli();//disable interrupts

  517.     /* Setup the 8mhz PWM clock
  518.   * This will be on pin 11*/
  519.   DDRB |= (1 << 3);//pin 11
  520.   ASSR &= ~(_BV(EXCLK) | _BV(AS2));
  521.   TCCR2A = (1 << COM2A0) | (1 << WGM21) | (1 << WGM20);
  522.   TCCR2B = (1 << WGM22) | (1 << CS20);
  523.   OCR2A = 0;//(F_CPU)/(2*(X+1))
  524.   DDRC &= ~15;//low d0-d3 camera
  525.   DDRD &= ~252;//d7-d4 and interrupt pins
  526.   _delay_ms(3000);

  527.     //set up twi for 100khz
  528.   TWSR &= ~3;//disable prescaler for TWI
  529.   TWBR = 72;//set to 100khz

  530.     //enable serial
  531.   UBRR0H = 0;
  532.   UBRR0L = 1;//0 = 2M baud rate. 1 = 1M baud. 3 = 0.5M. 7 = 250k 207 is 9600 baud rate.
  533.   UCSR0A |= 2;//double speed aysnc
  534.   UCSR0B = (1 << RXEN0) | (1 << TXEN0);//Enable receiver and transmitter
  535.   UCSR0C = 6;//async 1 stop bit 8bit char no parity bits
  536. }


  537. void StringPgm(const char * str){
  538.   do{
  539.       while (!(UCSR0A & (1 << UDRE0)));//wait for byte to transmit
  540.       UDR0 = pgm_read_byte_near(str);
  541.       while (!(UCSR0A & (1 << UDRE0)));//wait for byte to transmit
  542.   } while (pgm_read_byte_near(++str));
  543. }

  544. static void captureImg(uint16_t wg, uint16_t hg){
  545.   uint16_t y, x;

  546.   StringPgm(PSTR("*RDY*"));

  547.   while (!(PIND & 8));//wait for high
  548.   while ((PIND & 8));//wait for low

  549.     y = hg;
  550.   while (y--){
  551.         x = wg;
  552.       //while (!(PIND & 256));//wait for high
  553.     while (x--){
  554.       while ((PIND & 4));//wait for low
  555.             UDR0 = (PINC & 15) | (PIND & 240);
  556.           while (!(UCSR0A & (1 << UDRE0)));//wait for byte to transmit
  557.       while (!(PIND & 4));//wait for high
  558.       while ((PIND & 4));//wait for low
  559.       while (!(PIND & 4));//wait for high
  560.     }
  561.     //  while ((PIND & 256));//wait for low
  562.   }
  563.    // _delay_ms(100);
  564. }

  565. void setup(){
  566.   arduinoUnoInut();
  567.   camInit();
  568.   setRes();
  569.   setColor();
  570.   wrReg(0x11, 10); //Earlier it had the value: wrReg(0x11, 12); New version works better for me  !!!!
  571. }


  572. void loop(){
  573.   captureImg(320, 240);
  574. }
复制代码


回复

使用道具 举报

1563#
ID:513258 发表于 2019-8-4 11:54 | 只看该作者
虽然能通过验证并上传成功,还是发现有红字的警告,不懂如何处理

ino:24:0: warning: "F_CPU" redefined


#define F_CPU 16000000UL


^


<command-line>:0:0: note: this is the location of the previous definition


回复

使用道具 举报

1564#
ID:513258 发表于 2019-8-4 12:12 | 只看该作者

回复

使用道具 举报

1565#
ID:513258 发表于 2019-8-4 14:26 | 只看该作者
  1. /*
  2.   【Arduino】66种传感器模块系列实验(80)
  3.   实验八十: OV7670摄像头模块30W采集拍照模组(替OV7725)
  4.   程序之二
  5.   直接用arduino的Wire库函数对ov7670 fifo寄存器进行操作需要注意的是:
  6. 1.要对官方给的芯片地址0x42向右移一位;
  7. 2.要先对其进行复位也就是将0x12寄存器写0x80;
  8. 3.这里读的是两个寄存器这里读的分别是两个标志寄存器PID和VER,地址分别是0x0a和0x0b。
  9. 然后运行的结果就像这样
  10. */

  11. #include <Wire.h>

  12. void setup() {
  13.   // put your setup code here, to run once:
  14.   Serial.begin(9600);
  15.   Wire.begin();
  16.    Wire.beginTransmission(0x42>>1);
  17.    Wire.write(0x12);
  18.    Wire.write(0x80);
  19.    Wire.endTransmission();
  20.    delay(10);
  21. }

  22. void loop() {
  23.   // put your main code here, to run repeatedly:
  24. Wire.beginTransmission(0x42>>1);
  25. Wire.write(0x0a);
  26. Wire.endTransmission();
  27. Wire.requestFrom((0x42>>1), 2);
  28. byte data1 = Wire.read();
  29. byte data2 = Wire.read();
  30. Serial.println(data1,HEX);
  31. Serial.println(data2,HEX);
  32. Serial.println("********");
  33. delay(1000);
  34. }
复制代码


回复

使用道具 举报

1566#
ID:513258 发表于 2019-8-4 14:30 | 只看该作者

回复

使用道具 举报

1567#
ID:513258 发表于 2019-8-4 14:52 | 只看该作者
  1. /*
  2.   【Arduino】66种传感器模块系列实验(80)
  3.   实验八十: OV7670摄像头模块30W采集拍照模组(替OV7725)

  4. 在github.com上的最新OV7670库文件
  5. OV7670.h
  6. */

  7. #pragma once
  8. #include <stdint.h>
  9. void wrReg(uint8_t reg,uint8_t dat);
  10. uint8_t rdReg(uint8_t reg);
  11. enum RESOLUTION{VGA,QVGA,QQVGA};
  12. enum COLORSPACE{YUV422,RGB565,BAYER_RGB};
  13. struct regval_list{
  14.         uint8_t reg_num;
  15.         uint8_t value;
  16. };
  17. void setColorSpace(enum COLORSPACE color);
  18. void setRes(enum RESOLUTION res);
  19. void camInit(void);
  20. #define OV7670_I2C_ADDRESS        0x21
  21. /* Registers */
  22. #define REG_GAIN        0x00        /* Gain lower 8 bits (rest in vref) */
  23. #define REG_BLUE        0x01        /* blue gain */
  24. #define REG_RED                0x02        /* red gain */
  25. #define REG_VREF        0x03        /* Pieces of GAIN, VSTART, VSTOP */
  26. #define REG_COM1        0x04        /* Control 1 */
  27. #define COM1_CCIR656        0x40        /* CCIR656 enable */
  28. #define REG_BAVE        0x05        /* U/B Average level */
  29. #define REG_GbAVE        0x06        /* Y/Gb Average level */
  30. #define REG_AECHH        0x07        /* AEC MS 5 bits */
  31. #define REG_RAVE        0x08        /* V/R Average level */
  32. #define REG_COM2        0x09        /* Control 2 */
  33. #define COM2_SSLEEP        0x10        /* Soft sleep mode */
  34. #define REG_PID                0x0a        /* Product ID MSB */
  35. #define REG_VER                0x0b        /* Product ID LSB */
  36. #define REG_COM3        0x0c        /* Control 3 */
  37. #define COM3_SWAP        0x40        /* Byte swap */
  38. #define COM3_SCALEEN        0x08        /* Enable scaling */
  39. #define COM3_DCWEN        0x04        /* Enable downsamp/crop/window */
  40. #define REG_COM4        0x0d        /* Control 4 */
  41. #define REG_COM5        0x0e        /* All "reserved" */
  42. #define REG_COM6        0x0f        /* Control 6 */
  43. #define REG_AECH        0x10        /* More bits of AEC value */
  44. #define REG_CLKRC        0x11        /* Clocl control */
  45. #define CLK_EXT                0x40        /* Use external clock directly */
  46. #define CLK_SCALE        0x3f        /* Mask for internal clock scale */
  47. #define REG_COM7        0x12        /* Control 7 */
  48. #define COM7_RESET        0x80        /* Register reset */
  49. #define COM7_FMT_MASK        0x38
  50. #define COM7_FMT_VGA        0x00
  51. #define        COM7_FMT_CIF        0x20        /* CIF format */
  52. #define COM7_FMT_QVGA        0x10        /* QVGA format */
  53. #define COM7_FMT_QCIF        0x08        /* QCIF format */
  54. #define        COM7_RGB        0x04        /* bits 0 and 2 - RGB format */
  55. #define        COM7_YUV        0x00        /* YUV */
  56. #define        COM7_BAYER        0x01        /* Bayer format */
  57. #define        COM7_PBAYER        0x05        /* "Processed bayer" */
  58. #define REG_COM8        0x13        /* Control 8 */
  59. #define COM8_FASTAEC        0x80        /* Enable fast AGC/AEC */
  60. #define COM8_AECSTEP        0x40        /* Unlimited AEC step size */
  61. #define COM8_BFILT        0x20        /* Band filter enable */
  62. #define COM8_AGC        0x04        /* Auto gain enable */
  63. #define COM8_AWB        0x02        /* White balance enable */
  64. #define COM8_AEC        0x01        /* Auto exposure enable */
  65. #define REG_COM9        0x14        /* Control 9- gain ceiling */
  66. #define REG_COM10        0x15        /* Control 10 */
  67. #define COM10_HSYNC        0x40        /* HSYNC instead of HREF */
  68. #define COM10_PCLK_HB        0x20        /* Suppress PCLK on horiz blank */
  69. #define COM10_HREF_REV        0x08        /* Reverse HREF */
  70. #define COM10_VS_LEAD        0x04        /* VSYNC on clock leading edge */
  71. #define COM10_VS_NEG        0x02        /* VSYNC negative */
  72. #define COM10_HS_NEG        0x01        /* HSYNC negative */
  73. #define REG_HSTART        0x17        /* Horiz start high bits */
  74. #define REG_HSTOP        0x18        /* Horiz stop high bits */
  75. #define REG_VSTART        0x19        /* Vert start high bits */
  76. #define REG_VSTOP        0x1a        /* Vert stop high bits */
  77. #define REG_PSHFT        0x1b        /* Pixel delay after HREF */
  78. #define REG_MIDH        0x1c        /* Manuf. ID high */
  79. #define REG_MIDL        0x1d        /* Manuf. ID low */
  80. #define REG_MVFP        0x1e        /* Mirror / vflip */
  81. #define MVFP_MIRROR        0x20        /* Mirror image */
  82. #define MVFP_FLIP        0x10        /* Vertical flip */

  83. #define REG_AEW                0x24        /* AGC upper limit */
  84. #define REG_AEB                0x25        /* AGC lower limit */
  85. #define REG_VPT                0x26        /* AGC/AEC fast mode op region */
  86. #define REG_HSYST        0x30        /* HSYNC rising edge delay */
  87. #define REG_HSYEN        0x31        /* HSYNC falling edge delay */
  88. #define REG_HREF        0x32        /* HREF pieces */
  89. #define REG_TSLB        0x3a        /* lots of stuff */
  90. #define TSLB_YLAST        0x04        /* UYVY or VYUY - see com13 */
  91. #define REG_COM11        0x3b        /* Control 11 */
  92. #define COM11_NIGHT        0x80        /* NIght mode enable */
  93. #define COM11_NMFR        0x60        /* Two bit NM frame rate */
  94. #define COM11_HZAUTO        0x10        /* Auto detect 50/60 Hz */
  95. #define        COM11_50HZ        0x08        /* Manual 50Hz select */
  96. #define COM11_EXP        0x02
  97. #define REG_COM12        0x3c        /* Control 12 */
  98. #define COM12_HREF        0x80        /* HREF always */
  99. #define REG_COM13        0x3d        /* Control 13 */
  100. #define COM13_GAMMA        0x80        /* Gamma enable */
  101. #define        COM13_UVSAT        0x40        /* UV saturation auto adjustment */
  102. #define COM13_UVSWAP        0x01        /* V before U - w/TSLB */
  103. #define REG_COM14        0x3e        /* Control 14 */
  104. #define COM14_DCWEN        0x10        /* DCW/PCLK-scale enable */
  105. #define REG_EDGE        0x3f        /* Edge enhancement factor */
  106. #define REG_COM15        0x40        /* Control 15 */
  107. #define COM15_R10F0        0x00        /* Data range 10 to F0 */
  108. #define        COM15_R01FE        0x80        /*                        01 to FE */
  109. #define COM15_R00FF        0xc0        /*                        00 to FF */
  110. #define COM15_RGB565        0x10        /* RGB565 output */
  111. #define COM15_RGB555        0x30        /* RGB555 output */
  112. #define REG_COM16        0x41        /* Control 16 */
  113. #define COM16_AWBGAIN        0x08        /* AWB gain enable */
  114. #define REG_COM17        0x42        /* Control 17 */
  115. #define COM17_AECWIN        0xc0        /* AEC window - must match COM4 */
  116. #define COM17_CBAR        0x08        /* DSP Color bar */
  117. /*
  118. * This matrix defines how the colors are generated, must be
  119. * tweaked to adjust hue and saturation.
  120. *
  121. * Order: v-red, v-green, v-blue, u-red, u-green, u-blue
  122. * They are nine-bit signed quantities, with the sign bit
  123. * stored in0x58.Sign for v-red is bit 0, and up from there.
  124. */
  125. #define        REG_CMATRIX_BASE0x4f
  126. #define CMATRIX_LEN 6
  127. #define REG_CMATRIX_SIGN0x58
  128. #define REG_BRIGHT        0x55        /* Brightness */
  129. #define REG_CONTRAS        0x56        /* Contrast control */
  130. #define REG_GFIX        0x69        /* Fix gain control */
  131. #define REG_REG76        0x76        /* OV's name */
  132. #define R76_BLKPCOR        0x80        /* Black pixel correction enable */
  133. #define R76_WHTPCOR        0x40        /* White pixel correction enable */
  134. #define REG_RGB444        0x8c        /* RGB 444 control */
  135. #define R444_ENABLE        0x02        /* Turn on RGB444, overrides 5x5 */
  136. #define R444_RGBX        0x01        /* Empty nibble at end */
  137. #define REG_HAECC1        0x9f        /* Hist AEC/AGC control 1 */
  138. #define REG_HAECC2        0xa0        /* Hist AEC/AGC control 2 */
  139. #define REG_BD50MAX        0xa5        /* 50hz banding step limit */
  140. #define REG_HAECC3        0xa6        /* Hist AEC/AGC control 3 */
  141. #define REG_HAECC4        0xa7        /* Hist AEC/AGC control 4 */
  142. #define REG_HAECC5        0xa8        /* Hist AEC/AGC control 5 */
  143. #define REG_HAECC6        0xa9        /* Hist AEC/AGC control 6 */
  144. #define REG_HAECC7        0xaa        /* Hist AEC/AGC control 7 */
  145. #define REG_BD60MAX        0xab        /* 60hz banding step limit */
  146. #define COM7_FMT_CIF        0x20        /* CIF format */
  147. #define COM7_RGB        0x04        /* bits 0 and 2 - RGB format */
  148. #define COM7_YUV        0x00        /* YUV */
  149. #define COM7_BAYER        0x01        /* Bayer format */
  150. #define COM7_PBAYER        0x05        /* "Processed bayer" */
  151. #define COM10_VS_LEAD         0x04        /* VSYNC on clock leading edge */
  152. #define COM11_50HZ        0x08        /* Manual 50Hz select */
  153. #define COM13_UVSAT        0x40        /* UV saturation auto adjustment */
  154. #define COM15_R01FE        0x80        /*                        01 to FE */
  155. #define MTX1                0x4f        /* Matrix Coefficient 1 */
  156. #define MTX2                0x50        /* Matrix Coefficient 2 */
  157. #define MTX3                0x51        /* Matrix Coefficient 3 */
  158. #define MTX4                0x52        /* Matrix Coefficient 4 */
  159. #define MTX5                0x53        /* Matrix Coefficient 5 */
  160. #define MTX6                0x54        /* Matrix Coefficient 6 */
  161. #define MTXS                0x58        /* Matrix Coefficient Sign */
  162. #define AWBC7                0x59        /* AWB Control 7 */
  163. #define AWBC8                0x5a        /* AWB Control 8 */
  164. #define AWBC9                0x5b        /* AWB Control 9 */
  165. #define AWBC10                0x5c        /* AWB Control 10 */
  166. #define AWBC11                0x5d        /* AWB Control 11 */
  167. #define AWBC12                0x5e        /* AWB Control 12 */
  168. #define REG_GFI                0x69        /* Fix gain control */
  169. #define GGAIN                0x6a        /* G Channel AWB Gain */
  170. #define DBLV                0x6b        
  171. #define AWBCTR3                0x6c        /* AWB Control 3 */
  172. #define AWBCTR2                0x6d        /* AWB Control 2 */
  173. #define AWBCTR1                0x6e        /* AWB Control 1 */
  174. #define AWBCTR0                0x6f        /* AWB Control 0 */
复制代码


回复

使用道具 举报

1568#
ID:513258 发表于 2019-8-4 14:55 | 只看该作者
  1. /*
  2.   【Arduino】66种传感器模块系列实验(80)
  3.   实验八十: OV7670摄像头模块30W采集拍照模组(替OV7725)

  4. 在github.com上的最新OV7670库文件之二
  5. OV7670.c
  6. */

  7. #define F_CPU 16000000UL
  8. #include <stdint.h>
  9. #include <avr/io.h>
  10. #include <util/twi.h>
  11. #include <util/delay.h>
  12. #include <avr/pgmspace.h>
  13. #include "ov7670.h"
  14. static const struct regval_list vga_ov7670[] PROGMEM = {
  15.         {REG_HREF,0xF6},        // was B6  
  16.         {0x17,0x13},                // HSTART
  17.         {0x18,0x01},                // HSTOP
  18.         {0x19,0x02},                // VSTART
  19.         {0x1a,0x7a},                // VSTOP
  20.         {REG_VREF,0x0a},        // VREF
  21.         {0xff, 0xff},                /* END MARKER */
  22. };
  23. static const struct regval_list qvga_ov7670[] PROGMEM = {
  24.         {REG_COM14, 0x19},
  25.         {0x72, 0x11},
  26.         {0x73, 0xf1},
  27.         {REG_HSTART,0x16},
  28.         {REG_HSTOP,0x04},
  29.         {REG_HREF,0x24},
  30.         {REG_VSTART,0x02},
  31.         {REG_VSTOP,0x7a},
  32.         {REG_VREF,0x0a},
  33.         {0xff, 0xff},        /* END MARKER */
  34. };
  35. static const struct regval_list qqvga_ov7670[] PROGMEM = {
  36.         {REG_COM14, 0x1a},        // divide by 4
  37.         {0x72, 0x22},                // downsample by 4
  38.         {0x73, 0xf2},                // divide by 4
  39.         {REG_HSTART,0x16},
  40.         {REG_HSTOP,0x04},
  41.         {REG_HREF,0xa4},                  
  42.         {REG_VSTART,0x02},
  43.         {REG_VSTOP,0x7a},
  44.         {REG_VREF,0x0a},
  45.         {0xff, 0xff},        /* END MARKER */
  46. };
  47. static const struct regval_list yuv422_ov7670[] PROGMEM = {
  48.         {REG_COM7, 0x0},        /* Selects YUV mode */
  49.         {REG_RGB444, 0},        /* No RGB444 please */
  50.         {REG_COM1, 0},
  51.         {REG_COM15, COM15_R00FF},
  52.         {REG_COM9, 0x6A},        /* 128x gain ceiling; 0x8 is reserved bit */
  53.         {0x4f, 0x80},                /* "matrix coefficient 1" */
  54.         {0x50, 0x80},                /* "matrix coefficient 2" */
  55.         {0x51, 0},                /* vb */
  56.         {0x52, 0x22},                /* "matrix coefficient 4" */
  57.         {0x53, 0x5e},                /* "matrix coefficient 5" */
  58.         {0x54, 0x80},                /* "matrix coefficient 6" */
  59.         {REG_COM13,/*COM13_GAMMA|*/COM13_UVSAT},
  60.         {0xff, 0xff},                /* END MARKER */
  61. };
  62. static const struct regval_list rgb565_ov7670[] PROGMEM = {
  63.         {REG_COM7, COM7_RGB}, /* Selects RGB mode */
  64.         {REG_RGB444, 0},          /* No RGB444 please */
  65.         {REG_COM1, 0x0},
  66.         {REG_COM15, COM15_RGB565|COM15_R00FF},
  67.         {REG_COM9, 0x6A},         /* 128x gain ceiling; 0x8 is reserved bit */
  68.         {0x4f, 0xb3},                 /* "matrix coefficient 1" */
  69.         {0x50, 0xb3},                 /* "matrix coefficient 2" */
  70.         {0x51, 0},                 /* vb */
  71.         {0x52, 0x3d},                 /* "matrix coefficient 4" */
  72.         {0x53, 0xa7},                 /* "matrix coefficient 5" */
  73.         {0x54, 0xe4},                 /* "matrix coefficient 6" */
  74.         {REG_COM13, /*COM13_GAMMA|*/COM13_UVSAT},
  75.         {0xff, 0xff},        /* END MARKER */
  76. };
  77. static const struct regval_list bayerRGB_ov7670[] PROGMEM = {
  78.         {REG_COM7, COM7_BAYER},
  79.         {REG_COM13, 0x08}, /* No gamma, magic rsvd bit */
  80.         {REG_COM16, 0x3d}, /* Edge enhancement, denoise */
  81.         {REG_REG76, 0xe1}, /* Pix correction, magic rsvd */
  82.         {0xff, 0xff},        /* END MARKER */
  83. };
  84. static const struct regval_list ov7670_default_regs[] PROGMEM = {//from the linux driver
  85.         {REG_COM7, COM7_RESET},
  86.         {REG_TSLB,  0x04},        /* OV */
  87.         {REG_COM7, 0},        /* VGA */
  88.         /*
  89.          * Set the hardware window.  These values from OV don't entirely
  90.          * make sense - hstop is less than hstart.  But they work...
  91.          */
  92.         {REG_HSTART, 0x13},        {REG_HSTOP, 0x01},
  93.         {REG_HREF, 0xb6},        {REG_VSTART, 0x02},
  94.         {REG_VSTOP, 0x7a},        {REG_VREF, 0x0a},

  95.         {REG_COM3, 0},        {REG_COM14, 0},
  96.         /* Mystery scaling numbers */
  97.         {0x70, 0x3a},                {0x71, 0x35},
  98.         {0x72, 0x11},                {0x73, 0xf0},
  99.         {0xa2,/* 0x02 changed to 1*/1},{REG_COM10, COM10_VS_NEG},
  100.         /* Gamma curve values */
  101.         {0x7a, 0x20},                {0x7b, 0x10},
  102.         {0x7c, 0x1e},                {0x7d, 0x35},
  103.         {0x7e, 0x5a},                {0x7f, 0x69},
  104.         {0x80, 0x76},                {0x81, 0x80},
  105.         {0x82, 0x88},                {0x83, 0x8f},
  106.         {0x84, 0x96},                {0x85, 0xa3},
  107.         {0x86, 0xaf},                {0x87, 0xc4},
  108.         {0x88, 0xd7},                {0x89, 0xe8},
  109.         /* AGC and AEC parameters.  Note we start by disabling those features,
  110.            then turn them only after tweaking the values. */
  111.         {REG_COM8, COM8_FASTAEC | COM8_AECSTEP},
  112.         {REG_GAIN, 0},        {REG_AECH, 0},
  113.         {REG_COM4, 0x40}, /* magic reserved bit */
  114.         {REG_COM9, 0x18}, /* 4x gain + magic rsvd bit */
  115.         {REG_BD50MAX, 0x05},        {REG_BD60MAX, 0x07},
  116.         {REG_AEW, 0x95},        {REG_AEB, 0x33},
  117.         {REG_VPT, 0xe3},        {REG_HAECC1, 0x78},
  118.         {REG_HAECC2, 0x68},        {0xa1, 0x03}, /* magic */
  119.         {REG_HAECC3, 0xd8},        {REG_HAECC4, 0xd8},
  120.         {REG_HAECC5, 0xf0},        {REG_HAECC6, 0x90},
  121.         {REG_HAECC7, 0x94},
  122.         {REG_COM8, COM8_FASTAEC|COM8_AECSTEP|COM8_AGC|COM8_AEC},
  123.         {0x30,0},{0x31,0},//disable some delays
  124.         /* Almost all of these are magic "reserved" values.  */
  125.         {REG_COM5, 0x61},        {REG_COM6, 0x4b},
  126.         {0x16, 0x02},                {REG_MVFP, 0x07},
  127.         {0x21, 0x02},                {0x22, 0x91},
  128.         {0x29, 0x07},                {0x33, 0x0b},
  129.         {0x35, 0x0b},                {0x37, 0x1d},
  130.         {0x38, 0x71},                {0x39, 0x2a},
  131.         {REG_COM12, 0x78},        {0x4d, 0x40},
  132.         {0x4e, 0x20},                {REG_GFIX, 0},
  133.         /*{0x6b, 0x4a},*/                {0x74,0x10},
  134.         {0x8d, 0x4f},                {0x8e, 0},
  135.         {0x8f, 0},                {0x90, 0},
  136.         {0x91, 0},                {0x96, 0},
  137.         {0x9a, 0},                {0xb0, 0x84},
  138.         {0xb1, 0x0c},                {0xb2, 0x0e},
  139.         {0xb3, 0x82},                {0xb8, 0x0a},

  140.         /* More reserved magic, some of which tweaks white balance */
  141.         {0x43, 0x0a},                {0x44, 0xf0},
  142.         {0x45, 0x34},                {0x46, 0x58},
  143.         {0x47, 0x28},                {0x48, 0x3a},
  144.         {0x59, 0x88},                {0x5a, 0x88},
  145.         {0x5b, 0x44},                {0x5c, 0x67},
  146.         {0x5d, 0x49},                {0x5e, 0x0e},
  147.         {0x6c, 0x0a},                {0x6d, 0x55},
  148.         {0x6e, 0x11},                {0x6f, 0x9e}, /* it was 0x9F "9e for advance AWB" */
  149.         {0x6a, 0x40},                {REG_BLUE, 0x40},
  150.         {REG_RED, 0x60},
  151.         {REG_COM8, COM8_FASTAEC|COM8_AECSTEP|COM8_AGC|COM8_AEC|COM8_AWB},

  152.         /* Matrix coefficients */
  153.         {0x4f, 0x80},                {0x50, 0x80},
  154.         {0x51, 0},                {0x52, 0x22},
  155.         {0x53, 0x5e},                {0x54, 0x80},
  156.         {0x58, 0x9e},

  157.         {REG_COM16, COM16_AWBGAIN},        {REG_EDGE, 0},
  158.         {0x75, 0x05},                {REG_REG76, 0xe1},
  159.         {0x4c, 0},                {0x77, 0x01},
  160.         {REG_COM13, /*0xc3*/0x48},        {0x4b, 0x09},
  161.         {0xc9, 0x60},                /*{REG_COM16, 0x38},*/
  162.         {0x56, 0x40},

  163.         {0x34, 0x11},                {REG_COM11, COM11_EXP|COM11_HZAUTO},
  164.         {0xa4, 0x82/*Was 0x88*/},                {0x96, 0},
  165.         {0x97, 0x30},                {0x98, 0x20},
  166.         {0x99, 0x30},                {0x9a, 0x84},
  167.         {0x9b, 0x29},                {0x9c, 0x03},
  168.         {0x9d, 0x4c},                {0x9e, 0x3f},
  169.         {0x78, 0x04},

  170.         /* Extra-weird stuff.  Some sort of multiplexor register */
  171.         {0x79, 0x01},                {0xc8, 0xf0},
  172.         {0x79, 0x0f},                {0xc8, 0x00},
  173.         {0x79, 0x10},                {0xc8, 0x7e},
  174.         {0x79, 0x0a},                {0xc8, 0x80},
  175.         {0x79, 0x0b},                {0xc8, 0x01},
  176.         {0x79, 0x0c},                {0xc8, 0x0f},
  177.         {0x79, 0x0d},                {0xc8, 0x20},
  178.         {0x79, 0x09},                {0xc8, 0x80},
  179.         {0x79, 0x02},                {0xc8, 0xc0},
  180.         {0x79, 0x03},                {0xc8, 0x40},
  181.         {0x79, 0x05},                {0xc8, 0x30},
  182.         {0x79, 0x26},

  183.         {0xff, 0xff},        /* END MARKER */
  184. };
  185. static void errorLed(void){
  186.         DDRB|=32;//make sure led is output
  187.         while(1){//wait for reset
  188.                 PORTB^=32;// toggle led
  189.                 _delay_ms(100);
  190.         }
  191. }
  192. static void twiStart(void){
  193.         TWCR=_BV(TWINT)| _BV(TWSTA)| _BV(TWEN);//send start
  194.         while(!(TWCR & (1<<TWINT)));//wait for start to be transmitted
  195.         if((TWSR & 0xF8)!=TW_START)
  196.                 errorLed();
  197. }
  198. static void twiWriteByte(uint8_t DATA,uint8_t type){
  199.         TWDR = DATA;
  200.         TWCR = _BV(TWINT) | _BV(TWEN);                /* clear interrupt to start transmission */
  201.         while (!(TWCR & (1<<TWINT)));                /* wait for transmission */
  202.         if ((TWSR & 0xF8) != type)
  203.                 errorLed();
  204. }
  205. void wrReg(uint8_t reg,uint8_t dat){
  206.         //send start condition
  207.         twiStart();
  208.         twiWriteByte(OV7670_I2C_ADDRESS<<1,TW_MT_SLA_ACK);
  209.         twiWriteByte(reg,TW_MT_DATA_ACK);
  210.         twiWriteByte(dat,TW_MT_DATA_ACK);
  211.         TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);//send stop
  212.         _delay_ms(1);
  213. }
  214. static uint8_t twiRd(uint8_t nack){
  215.         if (nack){
  216.                 TWCR=_BV(TWINT) | _BV(TWEN);
  217.                 while ((TWCR & _BV(TWINT)) == 0);        /* wait for transmission */
  218.                 if ((TWSR & 0xF8) != TW_MR_DATA_NACK)
  219.                         errorLed();
  220.         }else{
  221.                 TWCR=_BV(TWINT) | _BV(TWEN) | _BV(TWEA);
  222.                 while ((TWCR & _BV(TWINT)) == 0) ; /* wait for transmission */
  223.                 if ((TWSR & 0xF8) != TW_MR_DATA_ACK)
  224.                         errorLed();
  225.         }
  226.         return TWDR;
  227. }
  228. uint8_t rdReg(uint8_t reg){
  229.         uint8_t dat;
  230.         twiStart();
  231.         twiWriteByte(OV7670_I2C_ADDRESS<<1,TW_MT_SLA_ACK);
  232.         twiWriteByte(reg,TW_MT_DATA_ACK);
  233.         TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);//send stop
  234.         _delay_ms(1);
  235.         twiStart();
  236.         twiWriteByte((OV7670_I2C_ADDRESS<<1)|1,TW_MR_SLA_ACK);
  237.         dat=twiRd(1);
  238.         TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);//send stop
  239.         _delay_ms(1);
  240.         return dat;
  241. }
  242. static void wrSensorRegs8_8(const struct regval_list reglist[]){
  243.         const struct regval_list *next = reglist;
  244.         for(;;){
  245.                 uint8_t reg_addr = pgm_read_byte(&next->reg_num);
  246.                 uint8_t reg_val = pgm_read_byte(&next->value);
  247.                 if((reg_addr==255)&&(reg_val==255))
  248.                         break;
  249.                 wrReg(reg_addr, reg_val);
  250.                 next++;
  251.         }
  252. }
  253. void setColorSpace(enum COLORSPACE color){
  254.         switch(color){
  255.                 case YUV422:
  256.                         wrSensorRegs8_8(yuv422_ov7670);
  257.                 break;
  258.                 case RGB565:
  259.                         wrSensorRegs8_8(rgb565_ov7670);
  260.                         {uint8_t temp=rdReg(0x11);
  261.                         _delay_ms(1);
  262.                         wrReg(0x11,temp);}//according to the Linux kernel driver rgb565 PCLK needs rewriting
  263.                 break;
  264.                 case BAYER_RGB:
  265.                         wrSensorRegs8_8(bayerRGB_ov7670);
  266.                 break;
  267.         }
  268. }
  269. void setRes(enum RESOLUTION res){
  270.         switch(res){
  271.                 case VGA:
  272.                         wrReg(REG_COM3,0);        // REG_COM3
  273.                         wrSensorRegs8_8(vga_ov7670);
  274.                 break;
  275.                 case QVGA:
  276.                         wrReg(REG_COM3,4);        // REG_COM3 enable scaling
  277.                         wrSensorRegs8_8(qvga_ov7670);
  278.                 break;
  279.                 case QQVGA:
  280.                         wrReg(REG_COM3,4);        // REG_COM3 enable scaling
  281.                         wrSensorRegs8_8(qqvga_ov7670);
  282.                 break;
  283.         }
  284. }
  285. void camInit(void){
  286.         wrReg(0x12, 0x80);//Reset the camera.
  287.         _delay_ms(100);
  288.         wrSensorRegs8_8(ov7670_default_regs);
  289.         wrReg(REG_COM10,32);//PCLK does not toggle on HBLANK.
  290. }
复制代码


回复

使用道具 举报

1569#
ID:513258 发表于 2019-8-4 15:01 | 只看该作者
实验八十一:槽型光耦对射光电开关模块(红外计数测速传感器)

槽型光耦
以光为媒介传输电信号。它对输入、输出电信号有良好的隔离作用,所以,它在各种电路中得 1553b耦合器线缆接头到广泛的应用。目前它已成为种类最多、用途最广的光电器件之一。光耦合器一般由三部分组成:光的发射、光的接收及信号放大。输入的电信号驱动发光二极管(LED),使之发出一定波长的光,被光探测器接收而产生光电流,再经过进一步放大后输出。这就完成了电—光—电的转换,从而起到输入、输出、隔离的作用。由于光耦合器输入输出间互相隔离,电信号传输具有单向性等特点,因而具有良好的电绝缘能力和抗干扰能力。又由于光耦合器的输入端属于电流型工作的低阻元件,因而具有很强的共模抑制能力。所以,它在长线传输信息中作为终端隔离元件可以大大提高信噪比。在计算机数字通信及实时控制中作为信号隔离的接口器件,可以大大增加计算机工作的可靠性。槽型光耦作为一种非常常见的电子元件,在电子电路系统设计的过程中,其选型的正确与否是非常重要的。槽型光耦合器也常常被称为直射式光电传感器,其工作原理是通过对红外发射光的阻断和导通,在红外接收管感应出的电流变化来实现开和关的判断。光电耦合器具有体积小、使用寿命长、工作温度范围宽、抗干扰性能强。无触点且输入与输出在电气上完全隔离等特点,因而在各种电子设备上得到广泛的应用。光电耦合器可用于隔离电路、负载接口及各种家用电器等电路中。




回复

使用道具 举报

1570#
ID:513258 发表于 2019-8-4 15:18 | 只看该作者

回复

使用道具 举报

1571#
ID:513258 发表于 2019-8-4 16:06 | 只看该作者
光耦合器(opTIcalcoupler,英文缩写为OC)
亦称光电隔离器或光电耦合器,简称光耦。它是以光为媒介来传输电信号的器件,通常把发光器(红外线发光二极管LED)与受光器(光敏半导体管)封装在同一管壳内。以光为媒介把输入端信号耦合到输出端的光电耦合器,由于它具有体积小、寿命长、无触点,抗干扰能力强,输出和输入之间绝缘,单向传输信号等优点,在数字电路上获得广泛的应用。

槽型光耦对输入、输出电信号有良好的隔离作用,所以,它在各种电路中得到广泛的应用。目前槽型光耦已成为种类最多、用途最广的光电器件之一。槽型光耦一般由三部分组成:光的发射、光的接收及信号放大。输入的电信号驱动发光二极管(LED),使之发出一定波长的光,被光探测器接收而产生光电流,再经过进一步放大后输出。这就完成了电—光—电的转换,从而起到输入、输出、隔离的作用。由于槽型光耦输入输出间互相隔离,电信号传输具有单向性等特点,因而具有良好的电绝缘能力和抗干扰能力。所以,槽型光耦在长线传输信息中作为终端隔离元件可以大大提高信噪比。在计算机数字通信及实时控制中作为信号隔离的接口器件,可以大大提高计算机工作的可靠性。




回复

使用道具 举报

1572#
ID:513258 发表于 2019-8-4 16:24 | 只看该作者
槽型光耦的特点:
1、检测距离长。与接近开关等比较,光电开关的检测距离非常长,且是无接触式的,所以不会损伤检测物体,也不受检测物体的影响。
2、几乎不受检测物体的制约。由于是采用对检测对象的表面进行反射及光透过方式,不像接近开关只能对金属,还能对玻璃、塑料、木制物体、液体等各种物质进行检测。
3、响应速度快。与接近开关同样,由于无机械运动,所以能对高速运动的物体进行检测。镜头容易受有机尘土等的影响镜头免受污染后,光会散射或被遮光,所以在有活水蒸汽、尘土等较多的环境下使用的场合,需施加适当的保护装置。
4、不受环境强光的影响。几乎不受一般照明光的影响,但像太阳光那样的强光直接照射受光体时,会造成误动作或损坏。




回复

使用道具 举报

1573#
ID:513258 发表于 2019-8-4 16:42 | 只看该作者
光电耦合器原理
当电信号送入光电耦合器的输入端时,发光二极体通过电流而发光,光敏元件受到光照后产生电流,CE导通;当输入端无信号,发光二极体不亮,光敏三极管截止,CE不通。对于数位量,当输入为低电平“0”时,光敏三极管截止,输出为高电平“1”;当输入为高电平“1”时,光敏三极管饱和导通,输出为低电平“ 0”。若基极有引出线则可满足温度补偿、检测调制要求。这种光耦合器性能较好,价格便宜,因而应用广泛。光电耦合器之所以在传输信号的同时能有效地抑制尖脉冲和各种杂讯干扰,使通道上的信号杂讯比大为提高,主要有以下几方面的原因:(1)光电耦合器的输入阻抗很小,只有几百欧姆,而干扰源的阻抗较大,通常为105~106Ω。据分压原理可知,即使干扰电压的幅度较大,但馈送到光电耦合器输入端的杂讯电压会很小,只能形成很微弱的电流,由于没有足够的能量而不能使二极体发光,从而被抑制掉了。(2)光电耦合器的输入回路与输出回路之间没有电气联系,也没有共地;之间的分布电容极小,而绝缘电阻又很大,因此回路一边的各种干扰杂讯都很难通过光电耦合器馈送到另一边去,避免了共阻抗耦合的干扰信号的产生。(3)光电耦合器可起到很好的安全保障作用,即使当外部设备出现故障,甚至输入信号线短接时,也不会损坏仪表。因为光耦合器件的输入回路和输出回路之间可以承受几千伏的高压。(4)光电耦合器的回应速度极快,其回应延迟时间只有10μs左右,适于对回应速度要求很高的场合。




回复

使用道具 举报

1574#
ID:513258 发表于 2019-8-4 16:44 | 只看该作者
槽型光耦对射光电开关模块



回复

使用道具 举报

1575#
ID:513258 发表于 2019-8-4 17:15 | 只看该作者
模块特色
1、使用进口槽型光耦传感器
2、槽宽度5mm。
3、有输出状态指示灯,输出高电平灯灭,输出低电平灯亮。
4、有遮挡,输出高电平;无遮挡,输出低电平。
5、比较器输出,信号干净,波形好,驱动能力强,超过15mA。
6、工作电压3.3V-5V
7、输出形式 :数字开关量输出(0和1)
8、设有固定螺栓孔,方便安装
9、小板PCB尺寸:3.2cm x 1.4cm
10、使用宽电压LM393比较器



回复

使用道具 举报

1576#
ID:513258 发表于 2019-8-4 17:23 | 只看该作者
模块电原理图



回复

使用道具 举报

1577#
ID:513258 发表于 2019-8-4 18:37 | 只看该作者
模块4针的定义  
VCC 电源正,GND电源负,DO数字量输出,AO无效

模块使用说明:
1.模块槽中无遮挡时,接收管导通,模块DO输出低电平,遮挡时,DO输出高电平;
2.模块DO可与继电器相连,组成限位开关等功能,也可以与有源蜂鸣器模块相连,组成报警器。




回复

使用道具 举报

1578#
ID:513258 发表于 2019-8-4 19:02 | 只看该作者
  1. /*
  2.   【Arduino】66种传感器模块系列实验(81)
  3.   实验八十一:槽型光耦对射光电开关模块(红外计数测速传感器)
  4. 测量电机转速Speed ,单位 转/分

  5. G接GND、V接5V、S接数字引脚2或者3(使用中断,只能接这两个脚),
  6. 接好后,用一个遮挡物放在U型开关之间,模块上的LED点亮,无遮挡
  7. 物时,LED不亮;利用这一原理,当信号输出变化一次就计数一次,再
  8. 经过一些列的换算就可以得到转速了~~
  9. */

  10. int U_Pin = 2;   
  11. float Val = 0;      //设置变量Val,计数
  12. float time;  //设置变量time,计时
  13. float Speed;  //设置变量Speed,存储转速

  14. void setup(){

  15.   Serial.begin(9600);
  16.   attachInterrupt(0,count,CHANGE);    //引脚电平发生改变时触发
  17. }

  18. void loop(){

  19.   time = millis();
  20.   Speed =  (Val/40)/(time/60000) ;
  21.   Serial.println(Speed);
  22.   delay(1000);
  23. }

  24. void count(){
  25.   Val += 1;
  26. }
复制代码


回复

使用道具 举报

1579#
ID:513258 发表于 2019-8-4 19:04 | 只看该作者

回复

使用道具 举报

1580#
ID:513258 发表于 2019-8-4 19:49 | 只看该作者

回复

使用道具 举报

1581#
ID:513258 发表于 2019-8-4 19:53 | 只看该作者
  1. /*
  2.   【Arduino】66种传感器模块系列实验(81)
  3.   实验八十一:槽型光耦对射光电开关模块(红外计数测速传感器)
  4. 程序之二,遮断计数器
  5. VCC 5V
  6. GND GND
  7. OUT D2
  8. */

  9. int speedPin=2;//定义数字3接口
  10. int cntValue=0;
  11. void setup ()
  12. {
  13. pinMode(speedPin,INPUT);//3号数字口设置为输入状态
  14. Serial.begin(9600);
  15. Serial.println("Speed Count\n");
  16. }
  17. void loop()
  18. {
  19. //判断是否被遮挡
  20. if(digitalRead(speedPin)==0)
  21. {cntValue++;//计数增加
  22. Serial.println(cntValue);//串口输出计数值
  23. while(digitalRead(speedPin)==0);//等待遮挡结束
  24. }
  25. }
复制代码


回复

使用道具 举报

1582#
ID:513258 发表于 2019-8-4 19:55 | 只看该作者

回复

使用道具 举报

1583#
ID:513258 发表于 2019-8-4 20:12 | 只看该作者

回复

使用道具 举报

1584#
ID:513258 发表于 2019-8-4 20:15 | 只看该作者
实验八十二: MQ135空气质量检测传感器模块(有害物体 氨气 硫化物检测)

MQ135
气体传感器所使用的气敏材料是在清洁空气中电导率较低的二氧化锡(SnO2)。当传感器所处环境中存在污染气体时,传感器的电导率随空气中污染气体浓度的增加而增大。使用简单的电路即可将电导率的变化转换为与该气体浓度相对应的输出信号。MQ135气体传感器对氨气、硫化物、苯系蒸汽的灵敏度高,对烟雾和其它有害气体的监测也很理想。这种传感器可检测多种有害气体,是一款适合多种应用的低成本传感器。




回复

使用道具 举报

1585#
ID:513258 发表于 2019-8-4 20:45 | 只看该作者

回复

使用道具 举报

1586#
ID:513258 发表于 2019-8-4 20:54 | 只看该作者
探头型号        MQ135
产品类型        半导体气敏元件
标准封装        胶木(黑胶木)
检测气体        氨气、硫化物、苯系蒸汽
检测浓度        10-1000ppm(氨气、甲苯、氢气)
标准电路条件       
回路电压                 Vc        ≤24V  DC
加热电压                 VH        5.0V±0.2V ACorDC
负载电阻                 RL        可调
标准测试条件下气敏元件特性       
加热电阻                 RH        31Ω±3Ω(室温)
加热功耗                 PH        ≤900mW
敏感体表面电阻   Rs        2KΩ-20KΩ(in 100ppm NH3)
灵敏度                  S        Rs(in air)/Rs(100ppmNH3)≥5
浓度斜率                  α        ≤0.6(R100ppm/R50ppm NH3)
标准测试条件       
温度、湿度                20℃±2℃;65%±5%RH
标准测试电路        Vc:5.0V±0.1V;VH: 5.0V±0.1V
预热时间                        不少于48小时



回复

使用道具 举报

1587#
ID:513258 发表于 2019-8-4 20:56 | 只看该作者

回复

使用道具 举报

1588#
ID:513258 发表于 2019-8-4 21:33 | 只看该作者

回复

使用道具 举报

1589#
ID:513258 发表于 2019-8-4 21:34 | 只看该作者
MQ135应用电路



回复

使用道具 举报

1590#
ID:513258 发表于 2019-8-4 21:43 | 只看该作者
MQ135空气质量检测传感器模块






回复

使用道具 举报

1591#
ID:513258 发表于 2019-8-5 06:34 | 只看该作者
特点                                                                                                              
*在较宽的浓度范围内对有害气体有良好的灵敏度                                                
*对氨气、硫化物、苯系等气氛灵敏度较高                                       
*长寿命、低成本                                            
*简单的驱动电路即可

应用                        
*家庭用空气污染报警器
*工业用空气污染报警器
*便携式空气污染检测器



回复

使用道具 举报

1592#
ID:513258 发表于 2019-8-5 07:18 | 只看该作者

回复

使用道具 举报

1593#
ID:513258 发表于 2019-8-5 07:26 | 只看该作者
一、尺寸:32mm X22mm X30mm 长*宽*高
二、主要芯片:LM393、MQ135 气体感应探头
三、工作电压:直流 5V
四、模块特色
1、具有信号输出指示灯指示;
2、双路信号输出(模拟量输出及 TTL 电平输出);
3、TTL 输出有效信号为低电平;(输出低电平时信号灯亮,可接单片
机 IO 口)
4、模拟量输出随浓度增加而增加,浓度越高电压越高;
5、对硫化物、苯系蒸汽、烟雾等有害气体具有很高的灵敏度;
6、具有长期的使用寿命和可靠的稳定性;
7、快速的响应恢复特性;
8、带安装孔,方便固定安装;
9、探头可以插拔设计,方便试验。



回复

使用道具 举报

1594#
ID:513258 发表于 2019-8-5 07:45 | 只看该作者
模块使用注意事项——必须避免的情况
1.1 暴露于有机硅蒸气中
如果传感器的表面吸附了有机硅蒸气,传感器的敏感材料会被包裹住,抑制传感器的敏感性,并且不可恢复。传感器要避免暴露其在硅粘接剂、发胶、硅橡胶、腻子或其它含硅塑料添加剂可能存在的地方。
1.2 高腐蚀性的环境
传感器暴露在高浓度的腐蚀性气体(如 H2S,SOX,Cl2,HCl 等)中,不仅会引起加热材料及传感器引线的腐蚀或破坏,并会引起敏感材料性能发生不可逆的改变。
1.3 碱、碱金属盐、卤素的污染传感器被碱金属尤其是盐水喷雾污染后,及暴露在卤素如氟中也会引起性能劣变。
1.4 接触到水
溅上水或浸到水中会造成敏感特性下降。
1.5 结冰
水在敏感元件表面结冰会导致敏感材料碎裂而丧失敏感特性。
1.6 施加电压过高
如果给敏感元件或加热器施加的电压高于规定值,即使传感器没有受到物理损坏或破坏,也会造成引线和/或加热器损坏,并引起传感器敏感特性下降。




回复

使用道具 举报

1595#
ID:513258 发表于 2019-8-5 07:49 | 只看该作者

回复

使用道具 举报

1596#
ID:513258 发表于 2019-8-5 08:00 | 只看该作者

回复

使用道具 举报

1597#
ID:513258 发表于 2019-8-5 11:06 | 只看该作者

回复

使用道具 举报

1598#
ID:513258 发表于 2019-8-5 11:10 | 只看该作者
  1. /*
  2. 【Arduino】66种传感器模块系列实验(82)
  3. 实验八十二: MQ135空气质量检测传感器模块(有害物体 氨气 硫化物检测)
  4. 程序之一
  5. VCC 5V
  6. GND GND
  7. AO  A0
  8. DO  D3
  9. */

  10. const int gasSensor =0;
  11. void setup(){
  12.   Serial.begin(9600);      
  13. }
  14. void loop(){
  15.   float voltage;
  16.   voltage = getVoltage(gasSensor);
  17.   
  18.   Serial.println(voltage);
  19.   delay(1000);
  20. }

  21. float getVoltage(int pin){
  22.   return (analogRead(pin) * 0.004882814);
  23. //此公式将AnalogLead()的0值转换为1023值
  24. //返回0.0到5.0的值,即真正的电压
  25. //读取A0数值  
  26. }
复制代码


回复

使用道具 举报

1599#
ID:513258 发表于 2019-8-5 11:46 | 只看该作者

回复

使用道具 举报

1600#
ID:513258 发表于 2019-8-5 11:48 | 只看该作者
打火机丁烷测试,还是非常灵敏的




回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表