OV7620支持的像素是:VGA(640*480)、QVGA(320*240),我用的是QVGA模式(配合液晶屏显示)。使用OV7620关键还是正确配置其内部寄存器,还有就是SCCB数据传输协议的实现。
首先有必要说下OV7620所支持的图像格式,因为,我对图像这块不熟悉,这里只说明OV支持的几种输出格式,以及相关寄存器的配置。
RGB16/8 bit ,这些格式都不是真正的RGB数据流,而是Bayer矩阵,所以在终端显示时,必须将Bayer矩阵转换为RGB。转换的核心思想是通过插值算法将Bayer矩阵中的每个像素值(一个字节)转换为RGB三个字节(我不懂图像,只能说出方法,具体算法后面我会贴出)。
本次采用的是RGB raw16 bit 模式,由于OV7620只引出了Y通道,所以配置为one line mode(这里出现了问题,明明RGB三个分量都有,但是通过插值后显示出来的尽然还是灰度图像,后面再分析原因)。下面贴出OV7620相关寄存器的配置:
unsigned char reg_12_w_reset[2]={0x12,0x80};//先清零
unsigned char reg_11_w[2]={0x11,0x00}; //配置PCLK速率 PCLK=(CLK_input/(CLK<5:0>+1)*2)),本次为1/2时钟频率
unsigned char reg_12_w[2]={0x12,0x2c}; //使能AGC,选择raw data outtput,并且使能自动白平衡
unsigned char reg_14_w[2]={0x14,0x24};//QVGA digital output format ,RGB gamma on
unsigned char reg_28_w[2]={0x28,0xa0}; //one-line RGB data format ,select Progressive scan mode
下面是Bayer转RGB24的算法(在液晶屏上显示时还要转为RGB16):
void bayer2rgb24(unsigned char *dst, unsigned char *src, long WIDTH, long HEIGHT)
{
long int i;
unsigned char *rawpt, *scanpt;
long int size;
rawpt = src;
scanpt = dst;
size = WIDTH*HEIGHT;
for ( i = 0; i < size; i++ )
{
if ( (i/WIDTH) % 2 == 0 ) //奇数行
{
if ( (i % 2) == 0 ) //奇数列
{
/* B */
if ( (i > WIDTH) && ((i % WIDTH) > 0) )
{
*scanpt++ = ((long int)*(rawpt-WIDTH-1)+(long int)*(rawpt-WIDTH+1)+
(long int)*(rawpt+WIDTH-1)+(long int)*(rawpt+WIDTH+1))/4; /* R */
*scanpt++ = ((long int)*(rawpt-1)+(long int)*(rawpt+1)+
(long int)*(rawpt+WIDTH)+(long int)*(rawpt-WIDTH))/4; /* G */
*scanpt++ = *rawpt; /* B */
}
else /* first line or left column */
{
*scanpt++ = *(rawpt+WIDTH+1); /* R */
*scanpt++ = ((long int)*(rawpt+1)+(long int)*(rawpt+WIDTH))/2; /* G */
*scanpt++ = *rawpt; /* B */
}
}
else
{
/* (B)G */
if ( (i > WIDTH) && ((i % WIDTH) < (WIDTH-1)) )
{
*scanpt++ = ((long int)*(rawpt+WIDTH)+(long int)*(rawpt-WIDTH))/2; /* R */
*scanpt++ = *rawpt; /* G */
*scanpt++ = ((long int)*(rawpt-1)+(long int)*(rawpt+1))/2; /* B */
}
else /* first line or right column */
{
*scanpt++ = *(rawpt+WIDTH); /* R */
*scanpt++ = *rawpt; /* G */
*scanpt++ = *(rawpt-1); /* B */
}
}
}
else //偶数行
{
if ( (i % 2) == 0 ) //奇数列
{
/* G(R) */
if ( (i < (WIDTH*(HEIGHT-1))) && ((i % WIDTH) > 0) )
{
*scanpt++ = ((long int)*(rawpt-1)+(long int)*(rawpt+1))/2; /* R */
*scanpt++ = *rawpt; /* G */
*scanpt++ = ((long int)*(rawpt+WIDTH)+(long int)*(rawpt-WIDTH))/2; /* B */
}
else
{
/* bottom line or left column */
*scanpt++ = *(rawpt+1); /* R */
*scanpt++ = *rawpt; /* G */
*scanpt++ = *(rawpt-WIDTH); /* B */
}
}
else
{
/* R */
if ( i < (WIDTH*(HEIGHT-1)) && ((i % WIDTH) < (WIDTH-1)) )
{
*scanpt++ = *rawpt; /* R */
*scanpt++ = ((long int)*(rawpt-1)+(long int)*(rawpt+1)+
(long int)*(rawpt-WIDTH)+(long int)*(rawpt+WIDTH))/4; /* G */
*scanpt++ = ((long int)*(rawpt-WIDTH-1)+(long int)*(rawpt-WIDTH+1)+
(long int)*(rawpt+WIDTH-1)+(long int)*(rawpt+WIDTH+1))/4; /* B */
} else {
/* bottom line or right column */
*scanpt++ = *rawpt; /* R */
*scanpt++ = ((long int)*(rawpt-1)+(long int)*(rawpt-WIDTH))/2; /* G */
*scanpt++ = *(rawpt-WIDTH-1); /* B */
}
}
}
rawpt++;
}
}