将OV2640拍到的图像通过串口2发到电脑上

2019-07-20 12:58发布

    求助!!!!!!我现在在调试OV2640摄像头模块,可是我发现我怎么也进不了中断,大神看看void DCMI_IRQHandler(void)     //dcmi.c{
if(DCMI_GetITStatus(DCMI_IT_FRAME)==SET)//
{
        jpeg_data_process();  //main.c
        DCMI_ClearITPendingBit(DCMI_IT_FRAME);
        LED1=!LED1;   //test
        ov_frame++;  
}
}
通过串口显示图片,LCD用来辅助调试
下面是main.c和dcmi.c,其他的文件都是配置,就不列了
main.c:
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "timer.h"
#include "ov2640.h"
#include "dcmi.h"
#include "key.h"
#include "lcd.h"
#include "usart2.h"

u8 ov2640_mode=0;                                                //1,JPEG模式

#define jpeg_buf_size 31*1024                          //定义jepg_buf数据缓存大小(*4字节)
__align(4) u32 jpeg_buf[jpeg_buf_size];        //JPEG数据缓存buf
volatile u32 jpeg_data_len=0;                         //buf中的JPEG有效数据长度
volatile u8  jpeg_data_ok=0;                                //JPEG数据采集完成标准
                                                                                   //0,没采集完
                                                                                   //1,采集完,没处理
                                                                                   //2,处理完,开始下一帧采集

//JPEG3ß′çÖ§3ÖáD±í
const u16 jpeg_img_size_tbl[][2]=
{
176,144,          //QCIF
160,120,          //QQVGA
352,288,          //CIF
320,240,   //QVGA
640,480,          //VGA
800,600,          //SVGA
1024,768,        //XGA
1280,1024,        //SXGA
1600,1200,        //UXGA
};

const u8*EFFECTS_TBL[7]={"Normal","Negative","B&W","Redish","Greenish","Bluish","Antique"};        //7中特效
const u8*JPEG_SIZE_TBL[9]={"QCIF","QQVGA","CIF","QVGA","VGA","SVGA","XGA","SXGA","UXGA"};        //9中尺寸

//处理JPEG数据
//采集完一帧数据后用此函数.
void jpeg_data_process(void)
{
if(ov2640_mode)
{
        if(jpeg_data_ok==0)        //jpegêy¾Y»1Î′2é¼ˉíê?
        {       
         DMA_Cmd(DMA2_Stream1,DISABLE);//í£Ö1μ±Ç°′«êä
         while (DMA_GetCmdStatus(DMA2_Stream1)!=DISABLE){}//μè′yDMA2_Stream1¿éÅäÖà 
         jpeg_data_len=jpeg_buf_size-DMA_GetCurrDataCounter(DMA2_Stream1);//μÃμ½′Ë′Îêy¾Y′«êäμÄ3¤¶è
         jpeg_data_ok=1;                                 //±ê¼ÇJPEGêy¾Y2é¼ˉíê°′3é,μè′yÆäËûoˉêy′|àí
        }
        if(jpeg_data_ok==2)        //éÏò»′ÎμÄjpegêy¾YòѾ-±»′|àíáË
        {
         DMA2_Stream1->NDTR=jpeg_buf_size;       
         DMA_SetCurrDataCounter(DMA2_Stream1,jpeg_buf_size);//′«êä3¤¶èÎajpeg_buf_size*4×Ö½ú
         DMA_Cmd(DMA2_Stream1,ENABLE);                        //ÖØDÂ′«êä
         jpeg_data_ok=0;                                                //±ê¼Çêy¾YÎ′2é¼ˉ
        }
}         
}

//JPEG test
//JPEG,通过串口2发送电脑
void jpeg_test(void)
{
u32 i;
u8 *p;
//u8 key;
//u8 effect=0,saturation=2,contrast=2;    //ìØD§  ±¥oí¶è  ¶Ô±è¶è       
u8 size=3;                //默认QVGA ,320*240尺寸
u8 msgbuf[15];        //消息缓存区
usart2_init(42,115200);          
//LCD_Clear(WHITE);
POINT_COLOR=BLUE;
LCD_ShowString(30,130,200,16,16,"OV2640 Mode:JPEG");
sprintf((char*)msgbuf,"JPEG Size:%s",JPEG_SIZE_TBL[size]);
LCD_ShowString(30,180,200,16,16,msgbuf);                                        //ÏÔê¾μ±Ç°JPEG·Ö±æÂê
       
OV2640_JPEG_Mode();                //JPEGÄ£ê½
My_DCMI_Init();                          //DCMIÅäÖÃ
DCMI_DMA_Init((u32)&jpeg_buf,jpeg_buf_size,DMA_MemoryDataSize_Word,DMA_MemoryInc_Enable);//DCMI DMA配置
OV2640_OutSize_Set(jpeg_img_size_tbl[size][0],jpeg_img_size_tbl[size][1]);//设置输出尺寸
DCMI_Start();                 //dcmi.c    开始传输                                    
while(1)
{
  if(jpeg_data_ok==1)   //已经采集完一帧图像
        {               
         p=(u8*)jpeg_buf;
         LCD_ShowString(30,210,210,16,16,"Sending JPEG data..."); //ìáê¾ÕyÔú′«êäêy¾Y
         for(i=0;i<jpeg_data_len*4;i++)                //dma′&#171;ê&#228;1′&#206;μèóú4×&#214;&#189;ú,&#203;ùò&#212;3&#203;ò&#212;4.
         {
                USART_SendData(USART2,p[i]);        
    while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);        //循环发送,直到发送完毕                       
         }       
   jpeg_data_ok=2;        //处理完,采集下一帧
  }         
}
}


int main(void)
{
//u8 key;       
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//éè&#214;&#195;&#207;μí3&#214;D&#182;&#207;ó&#197;&#207;è&#188;&#182;·&#214;×é2
delay_init(168);  //3&#245;ê&#188;&#187;ˉ&#209;óê±oˉêy
uart_init(115200);                //3&#245;ê&#188;&#187;ˉ′&#174;&#191;ú2¨ì&#216;&#194;ê&#206;a115200
usart2_init(42,115200);                //3&#245;ê&#188;&#187;ˉ′&#174;&#191;ú22¨ì&#216;&#194;ê&#206;a115200       
LED_Init();                                  //3&#245;ê&#188;&#187;ˉLED&#182;&#203;&#191;ú

LCD_Init();       
        POINT_COLOR=RED;
TIM3_Int_Init(10000-1,8400-1);//10Khz&#188;&#198;êy,1&#195;&#235;&#214;ó&#214;D&#182;&#207;ò&#187;′&#206;

while(OV2640_Init())//3&#245;ê&#188;&#187;ˉOV2640                     //u8 OV2640_Init()   return:0 3é1|//
{
        LCD_ShowString(30,130,240,16,16,"OV2640 Init ERR");
  delay_ms(200);
        LCD_Fill(30,130,239,170,WHITE);
        delay_ms(200);
}
LCD_ShowString(30,50,200,16,16,"OV2640 Init OK");  

GPIO_ResetBits(GPIOF,GPIO_Pin_9);
//while(1)
//{
        //key=KEY_Scan(0);

        //if(key==KEY0_PRES)
        //{       
         ov2640_mode=1;
         //break;       
  //}       
  //delay_ms(5);       
// jpeg_test();          
//}       
if(ov2640_mode==1)
{
jpeg_test();
}         
}


dcmi.c:

#include "sys.h"
#include "dcmi.h"
#include "lcd.h"
#include "led.h"
#include "ov2640.h"

u8 ov_frame=0;                                                  //&#214;&#161;&#194;ê
extern void jpeg_data_process(void);        //JPEGêy&#190;Y′|àíoˉêy

DCMI_InitTypeDef DCMI_InitStructure;

//DCMI&#214;D&#182;&#207;·t&#206;&#241;oˉêy//
void DCMI_IRQHandler(void)
{
if(DCMI_GetITStatus(DCMI_IT_FRAME)==SET)//2&#182;&#187;&#241;μ&#189;ò&#187;&#214;&#161;í&#188;&#207;&#241;
{
        jpeg_data_process();         //jpegêy&#190;Y′|àí       
        DCMI_ClearITPendingBit(DCMI_IT_FRAME);//&#199;&#229;3y&#214;&#161;&#214;D&#182;&#207;
        LED1=!LED1;
        ov_frame++;  
}
}

//DCMI DMA&#197;&#228;&#214;&#195;
//DMA_Memory0BaseAddr:′&#230;′¢&#198;÷μ&#216;&#214;·    &#189;&#171;òa′&#230;′¢é&#227;&#207;&#241;í·êy&#190;Yμ&#196;&#196;ú′&#230;μ&#216;&#214;·(ò2&#191;éò&#212;ê&#199;íaéèμ&#216;&#214;·)
//DMA_BufferSize:′&#230;′¢&#198;÷3¤&#182;è    0~65535  
//DMA_MemoryDataSize:′&#230;′¢&#198;÷&#206;&#187;&#191;í    @defgroup DMA_memory_data_size MA_MemoryDataSize_Byte/DMA_MemoryDataSize_HalfWord/DMA_MemoryDataSize_Word
//DMA_MemoryInc:′&#230;′¢&#198;÷&#212;&#246;3¤·&#189;ê&#189;  @defgroup DMA_memory_incremented_mode  /** @defgroup DMA_memory_incremented_mode : DMA_MemoryInc_Enable/DMA_MemoryInc_Disable
void DCMI_DMA_Init(u32 DMA_Memory0BaseAddr,u16 DMA_BufferSize,u32 DMA_MemoryDataSize,u32 DMA_MemoryInc)
{
DMA_InitTypeDef  DMA_InitStructure;
       
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);//DMA2ê±&#214;óê1&#196;ü
DMA_DeInit(DMA2_Stream1);       
       
while(DMA_GetCmdStatus(DMA2_Stream1)!=DISABLE){}//μè′yDMA2_Stream1&#191;é&#197;&#228;&#214;&#195;

/* &#197;&#228;&#214;&#195; DMA Stream */
DMA_InitStructure.DMA_Channel=DMA_Channel_1;  //
DMA_InitStructure.DMA_PeripheralBaseAddr=(u32)&DCMI->DR;//íaéèμ&#216;&#214;·&#206;aCMI->DR
DMA_InitStructure.DMA_Memory0BaseAddr=DMA_Memory0BaseAddr;//DMA ′&#230;′¢&#198;÷0μ&#216;&#214;·
DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralToMemory;//íaéèμ&#189;′&#230;′¢&#198;÷&#196;£ê&#189;
DMA_InitStructure.DMA_BufferSize =DMA_BufferSize;//êy&#190;Y′&#171;ê&#228;á&#191;
DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;//íaéè·&#199;&#212;&#246;á&#191;&#196;£ê&#189;
DMA_InitStructure.DMA_MemoryInc =DMA_MemoryInc;//′&#230;′¢&#198;÷&#212;&#246;á&#191;&#196;£ê&#189;
DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_Word;//íaéèêy&#190;Y3¤&#182;è:32&#206;&#187;
DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize;//′&#230;′¢&#198;÷êy&#190;Y3¤&#182;è
DMA_InitStructure.DMA_Mode=DMA_Mode_Circular;// ê1ó&#195;&#209;-&#187;·&#196;£ê&#189;
DMA_InitStructure.DMA_Priority=DMA_Priority_High;//&#184;&#223;ó&#197;&#207;è&#188;&#182;
DMA_InitStructure.DMA_FIFOMode=DMA_FIFOMode_Enable; //FIFO&#196;£ê&#189;        
DMA_InitStructure.DMA_FIFOThreshold=DMA_FIFOThreshold_Full;//ê1ó&#195;è&#171;FIFO
DMA_InitStructure.DMA_MemoryBurst=DMA_MemoryBurst_Single;//íaéèí&#187;·¢μ¥′&#206;′&#171;ê&#228;
DMA_InitStructure.DMA_PeripheralBurst=DMA_PeripheralBurst_Single;//′&#230;′¢&#198;÷í&#187;·¢μ¥′&#206;′&#171;ê&#228;
DMA_Init(DMA2_Stream1,&DMA_InitStructure);//3&#245;ê&#188;&#187;ˉDMA Stream         
}

void My_DCMI_Init()
{
GPIO_InitTypeDef  GPIO_InitStructure;       
NVIC_InitTypeDef NVIC_InitStructure;       
       
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOE, ENABLE);//ê1&#196;üGPIOA B C E ê±&#214;ó
RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_DCMI,ENABLE);//ê1&#196;üDCMIê±&#214;ó

//PA4/63&#245;ê&#188;&#187;ˉéè&#214;&#195;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_6;//PA4/6   &#184;′ó&#195;1|&#196;üê&#228;3&#246;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF; //&#184;′ó&#195;1|&#196;üê&#228;3&#246;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;//í&#198;íìê&#228;3&#246;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;//é&#207;à-
GPIO_Init(GPIOA,&GPIO_InitStructure);//3&#245;ê&#188;&#187;ˉ       

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7|GPIO_Pin_6;// PB6/7   &#184;′ó&#195;1|&#196;üê&#228;3&#246;
GPIO_Init(GPIOB,&GPIO_InitStructure);//3&#245;ê&#188;&#187;ˉ
       
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_11;//PC6/7/8/9/11 &#184;′ó&#195;1|&#196;üê&#228;3&#246;
GPIO_Init(GPIOC,&GPIO_InitStructure);//3&#245;ê&#188;&#187;ˉ       

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5|GPIO_Pin_6;//PE5/6  &#184;′ó&#195;1|&#196;üê&#228;3&#246;
GPIO_Init(GPIOE,&GPIO_InitStructure);//3&#245;ê&#188;&#187;ˉ
                                                ////////&#184;′ó&#195;&#206;a&#207;àó|1|&#196;ü
GPIO_PinAFConfig(GPIOA,GPIO_PinSource4,GPIO_AF_DCMI); //PA4,AF13  DCMI_HSYNC  
GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_DCMI); //PA6,AF13  DCMI_PCLK  
GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_DCMI); //PB7,AF13  DCMI_VSYNC
GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_DCMI); //PC6,AF13  DCMI_D0  
GPIO_PinAFConfig(GPIOC,GPIO_PinSource7,GPIO_AF_DCMI); //PC7,AF13  DCMI_D1
GPIO_PinAFConfig(GPIOC,GPIO_PinSource8,GPIO_AF_DCMI); //PC8,AF13  DCMI_D2
GPIO_PinAFConfig(GPIOC,GPIO_PinSource9,GPIO_AF_DCMI); //PC9,AF13  DCMI_D3
GPIO_PinAFConfig(GPIOC,GPIO_PinSource11,GPIO_AF_DCMI);//PC11,AF13 DCMI_D4
GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_DCMI); //PB6,AF13  DCMI_D5
GPIO_PinAFConfig(GPIOE,GPIO_PinSource5,GPIO_AF_DCMI); //PE5,AF13  DCMI_D6
GPIO_PinAFConfig(GPIOE,GPIO_PinSource6,GPIO_AF_DCMI); //PE6,AF13  DCMI_D7

DCMI_DeInit();//&#199;&#229;3y&#212;-à′μ&#196;éè&#214;&#195;

DCMI_InitStructure.DCMI_CaptureMode=DCMI_CaptureMode_Continuous;//á&#172;D&#248;&#196;£ê&#189;
DCMI_InitStructure.DCMI_CaptureRate=DCMI_CaptureRate_All_Frame;//è&#171;&#214;&#161;2&#182;&#187;&#241;
DCMI_InitStructure.DCMI_ExtendedDataMode= DCMI_ExtendedDataMode_8b;//8&#206;&#187;êy&#190;Y&#184;&#241;ê&#189;  
DCMI_InitStructure.DCMI_HSPolarity= DCMI_HSPolarity_Low;//HSYNC μíμ&#231;&#198;&#189;óDD§
DCMI_InitStructure.DCMI_PCKPolarity= DCMI_PCKPolarity_Rising;//PCLK é&#207;éy&#209;&#216;óDD§
DCMI_InitStructure.DCMI_SynchroMode= DCMI_SynchroMode_Hardware;//ó2&#188;tí&#172;2&#189;HSYNC,VSYNC
DCMI_InitStructure.DCMI_VSPolarity=DCMI_VSPolarity_Low;//VSYNC μíμ&#231;&#198;&#189;óDD§
DCMI_Init(&DCMI_InitStructure);

DCMI_Cmd(ENABLE);        //DCMIê1&#196;ü

NVIC_InitStructure.NVIC_IRQChannel=DCMI_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//&#199;à&#213;&#188;ó&#197;&#207;è&#188;&#182;1
NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;                //×óó&#197;&#207;è&#188;&#182;3
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;                        //IRQí¨μàê1&#196;ü
NVIC_Init(&NVIC_InitStructure);        //&#184;ù&#190;Y&#214;&#184;&#182;¨μ&#196;2&#206;êy3&#245;ê&#188;&#187;ˉVIC&#188;&#196;′&#230;&#198;÷
}

//DCMI,启动传输
void DCMI_Start(void)
{  
LCD_SetCursor(0,0);  
LCD_WriteRAM_Prepare();                   //&#191;aê&#188;D′è&#235;GRAM
DMA_Cmd(DMA2_Stream1,ENABLE);//&#191;a&#198;&#244;DMA2,Stream1
DCMI_CaptureCmd(ENABLE);     //DCMI2&#182;&#187;&#241;ê1&#196;ü  
}

//DCMI,关闭传输
void DCMI_Stop(void)
{
DCMI_CaptureCmd(DISABLE);//DCMI2&#182;&#187;&#241;ê11&#216;±&#213;       
while(DCMI->CR&0X01);                //μè′y′&#171;ê&#228;&#189;áê&#248;
DMA_Cmd(DMA2_Stream1,DISABLE);//1&#216;±&#213;DMA2,Stream1
}





友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。