OV7740 STM32 source code

Publisher:WhisperingRainLatest update time:2017-07-13 Source: eefocusKeywords:OV7740  STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
  1. /* Includes ------------------------------------------------------------------*/  

  2. #include "stm32f10x.h"  

  3. #include "stm32f10x_exti.h"  

  4. #include "OV7740.h"  

  5. #include "OV7740Reg.h"  

  6. #include "sccb.h"  

  7. #include "misc.h"  

  8. #include "delay.h"  

  9. /* Private define ------------------------------------------------------------*/  

  10. #define OV7740_HREF_BIT     GPIO_Pin_2  

  11. #define OV7740_VSYNC_BIT    GPIO_Pin_4  

  12. #define GPIO_PIN_PCLK       GPIO_Pin_1  

  13. #define GPIO_PIN_REST       GPIO_Pin_5  

  14. #define GPIO_PIN_XCLK       GPIO_Pin_8  

  15. #define GPIO_PCLK_PORT      GPIOB  

  16.   

  17. #define GPIO_VSYNC_CMOS                    GPIOB  

  18. #define RCC_APB2Periph_GPIO_VSYNC_CMOS     RCC_APB2Periph_GPIOB  

  19. #define GPIO_PIN_VSYNC_CMOS                GPIO_Pin_4//GPIO_Pin_4  

  20. #define EXTI_LINE_VSYNC_CMOS EXTI_Line4//EXTI_Line4  

  21. #define GPIO_PORT_SOURCE_VSYNC_CMOS        GPIO_PortSourceGPIOB  

  22. #define GPIO_PIN_SOURCE_VSYNC_CMOS         GPIO_PinSource4//GPIO_PinSource14  

  23.   

  24. #define HREF_H GPIOB->BSRR = GPIO_Pin_2  //  

  25. #define HREF_L GPIOB->BRR = GPIO_Pin_2  //  

  26.   

  27. #define XCLK_H GPIOA->BSRR = GPIO_Pin_8  

  28. #define XCLK_L GPIOA->BRR =  GPIO_Pin_8  

  29.   

  30. #define CAMRST_H  GPIOB->BSRR  = GPIO_Pin_5  

  31. #define CAMRST_L  GPIOB->BRR  = GPIO_Pin_5  

  32.   

  33. /* Private variables ---------------------------------------------------------*/  

  34. /* Private functions ---------------------------------------------------------*/  

  35.   

  36. void OV7740_XCLK_ON(void)  

  37. {  

  38.     GPIO_InitTypeDef GPIO_InitStructure;  

  39.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;   

  40.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   

  41.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP ;   

  42.     GPIO_Init(GPIOA, &GPIO_InitStructure);  

  43.   

  44.     RCC_MCOConfig(RCC_MCO_HSE); //hsi //Output clock source on the specified pin PA8  

  45. }  

  46.   

  47. void OV7740_GPIO_Init(void)  

  48. {    

  49. GPIO_InitTypeDef GPIO_InitStructure;  

  50.   

  51. GPIO_InitStructure.GPIO_Pin =  OV7740_VSYNC_BIT;  

  52. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  

  53. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  

  54. GPIO_Init(GPIOB, &GPIO_InitStructure);   

  55.   

  56. GPIO_InitStructure.GPIO_Pin =  OV7740_HREF_BIT|GPIO_PIN_REST;  

  57. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  

  58. GPIO_Init(GPIOB, &GPIO_InitStructure);  

  59. }  

  60.   

  61.   

  62. void OV7740_Interrupts_Config(void)  

  63. {  

  64. NVIC_InitTypeDef NVIC_InitStructure;  

  65. NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;  

  66. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //Sub-priority  

  67. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  

  68. NVIC_Init(&NVIC_InitStructure);  

  69. }  

  70.   

  71.   

  72. void OV7740_EXTI_Config(void)     //  

  73. {  

  74. EXTI_InitTypeDef EXTI_InitStructure;  

  75. GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource4);  

  76. EXTI_InitStructure.EXTI_Line = EXTI_Line4;  

  77. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;  

  78. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;  

  79. EXTI_InitStructure.EXTI_LineCmd = ENABLE;  

  80. EXTI_Init(&EXTI_InitStructure);  

  81. EXTI_GenerateSWInterrupt(EXTI_Line4);  

  82. }  

  83. u8 OV7740_writeReg(u8 regID, u8 regDat)  

  84. {  

  85. while(SCCB_Start() ==0);  

  86. if(0==SCCB_writeByte(SCCB_7740_WRADDR))  

  87. {  

  88. SCCB_Stop();  

  89. return(0);  

  90. }  

  91.   if(0==SCCB_writeByte(regID))  

  92. {  

  93. SCCB_Stop();  

  94. return(0);  

  95. }  

  96.   if(0 == SCCB_writeByte(regDat))  

  97. {  

  98. SCCB_Stop();  

  99. return(0);  

  100. }  

  101.   SCCB_Stop();  

  102.   

  103.   return(1);   

  104. }  

  105.   

  106. u8 OV7740_readReg(u8 regID, u8 *regDat)  

  107. {  

  108. //Set the register address through write operation  

  109. while(SCCB_Start() ==0);  

  110. if(0==SCCB_writeByte(SCCB_7740_WRADDR))  

  111. {  

  112. SCCB_Stop();  

  113. return(0);  

  114. }  

  115.   if(0 == SCCB_writeByte(regID))  

  116. {  

  117. SCCB_Stop();  

  118. return(0);  

  119. }  

  120. SCCB_Stop();  

  121. //After setting the register address, read  

  122. while(SCCB_Start() == 0);  

  123. if(0==SCCB_writeByte(SCCB_7740_RDADDR))  

  124. {  

  125. SCCB_Stop();  

  126. return(0);  

  127. }  

  128.   *regDat = SCCB_readByte();  

  129.   SCCB_Stop();  

  130.   return(1);  

  131. }  

  132.   

  133.   

  134. u8 OV7740_init(void)  

  135. {  

  136. int i,n;  

  137. u8 val;  

  138. SCCB_GPIO_Init();  

  139. OV7740_GPIO_Init();  

  140. OV7740_XCLK_ON();  

  141.   CAMRST_L;  

  142.   delay_us(100);  

  143. CAMRST_H;  

  144. delay_us(100);  

  145. if(0 == OV7740_writeReg(SCCB_REG12,0x80)) return 0;  

  146. Delay_ms(10);    

  147. if(0 == OV7740_writeReg(SCCB_REG12,0x51)) return 0;  

  148.     delay_us(10);  

  149. OV7740_readReg(0x0A,&val);  

  150.   

  151. n = sizeof(RegConf0)/sizeof(UINT8);   

  152. for(i=0;i

  153. {   

  154. if(0 == OV7740_writeReg(RegConf0[i][0],RegConf0[i][1]))  

  155. {   

  156. return 0;  

  157. }  

  158. delay_us(10);  

  159. }  

  160. OV7740_setExposure(15);  

  161. OV7740_setGainVal(0);  

  162. OV7740_setVWindow(230);  

  163. OV7740_Interrupts_Config();  

  164. OV7740_EXTI_Config();  

  165. return 1;  

  166. }  

  167. void OV7740_setGainVal(int x)  

  168. {  

  169. //OV7740_readReg(0x15,&val);  

  170. OV7740_writeReg(0x00,Testgain[x][0]);  

  171. OV7740_writeReg(0x15,Testgain[x][1]);  

  172. }  

  173. void OV7740_setExposure(int exposure)  

  174. {  

  175. OV7740_writeReg(0x10,exposure&0xff);  

  176. OV7740_writeReg(0x0f,(exposure>>8)&0xff);  

  177. }  

  178. u8 setGain(int macro,int micro)  

  179. {  

  180.     u8 getGain[8]={7,0,1,3,7,15,31,63};  

  181.     u8 r00,r15;  

  182. /*if(macro ==0) {OV7740_writeReg(0x13,0x87);return 0;}*/  

  183.     if (macro>7) return 0;  

  184.     if (micro>15) return 0;   

  185.     r15 = getGain[macro]>>4;  

  186.     r00 = (getGain[macro]<<4)+micro;  

  187. /*OV7740_writeReg(0x13,0x00);manual modification*/  

  188.     OV7740_writeReg(0x00,r00);  

  189.     if (r15) OV7740_writeReg(0x15,r15);  

  190.     return 1;  

  191. }  

  192.   

  193.   

  194. //even for startline  

  195. void OV7740_setVWindow(int startline)  

  196. {  

  197.     OV7740_writeReg(0x19,startline>>1);  

  198.     OV7740_writeReg(0x1a,(20>>1)); //20lines, >>1  

  199. }  

  200. void OV7740_setLines(int lines)  

  201. {  

  202. OV7740_writeReg(0x1a,(lines>>1)); //20lines, >>1  

  203. }  

  204. u8 OV7740_test(void)  

  205. {  

  206. SCCB_GPIO_Init();  

  207. OV7740_GPIO_Init();  

  208. OV7740_XCLK_ON();  

  209. CAMRST_L;  

  210. delay_us(100);  

  211. CAMRST_H;  

  212. delay_us(100);  

  213. if(0 == OV7740_writeReg(0x12,0x80)) return 0;  

  214. delay_us(10);  

  215. if(0 == OV7740_writeReg(0x12,0x40)) return 0;  

  216. delay_us(10);  

  217.   OV7740_setVWindow(230);  

  218. if(0 == OV7740_writeReg(SCCB_REG38,0x07) )return 0;  

  219. delay_us(10);  

  220. if(0 == OV7740_writeReg(0x84,0x02) )return 0;  

  221. delay_us(10);  

  222. if(0 == OV7740_writeReg(SCCB_REG38,0x08)) return 0;  

  223. delay_us(10);  

  224. if(0 == OV7740_writeReg(0x84,0x07)) return 0;  

  225. delay_us(10);  

  226. OV7740_Interrupts_Config();  

  227. OV7740_EXTI_Config();  

  228. return 1;  

  229. }


Keywords:OV7740  STM32 Reference address:OV7740 STM32 source code

Previous article:Use STM32CubeMx to create a USB disk reading and writing program
Next article:STM32 IWDG settings

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号