STM32F103ZET6+OV7670串口成功输出图像数据,但显示出问题了!
具体为:我通过串口将OV7670拍摄的QVGA格式的图像(RGB565)发送到串口助手,然后以txt格式保存下来。再通过matlab将txt文件中每个像素的RGB565格式,转换为RGB格式,并显示。但结果如下图所示,请大家指点下错在哪里了呢?上面的图片为手机拍摄,下面的图像为OV7670拍摄并用matlab转换后的结果,比较可以发现图像错位并且发生了信号的反折。错位肯定不是摄像头抖动造成的,因通过多幅图像实现发现都是这样的错误显示!
下面为matlab,转化为正常RGB值的程序:
length=240;width=320;
in_name='0113.txt';%0213 1253.txt
out_name='0113.bmp';%0213
pic=uint8(zeros(length,width,3));
in=textread(in_name,'%s');
dec_data=hex2dec(in);
% dlmwrite('dec.txt',dec_data,'','');
% dec_test=dec_data(1:320);
k=1;
for i=1:length
for j=1:width
H=dec_data(k,1);
if(k+1>width*length*2)
k=k-1;
else
k=k;
end
L=dec_data(k+1,1);
k=k+2;
pic(i,j,1)=(H/8)*8;
pic(i,j,2)=(mod(H,8)*8+L/32)*4;
pic(i,j,3)=(mod(L,32))*8;
end
end
figure,imshow(pic);
imwrite(pic,out_name);
---------------------------------
原子哥我只是想单纯的想把USB读卡器实验移植到照相机实验上去,就像把手机插在电脑上,电脑可以识别手机中SD的内容,这时手机拍下照片,电脑刷新一下就会看见拍下的照片已经在SD的目录里了,我认为理论上是可行的,结果出现好多问题,至今还未解决....
我的TFT屏上显示的内容是 战舰 STM32开发板
照相机实验
WK_UP:拍照
正点原子@ALIENTEK
2012年9月19日
SD卡正常!
USB Connecting
OV7670 正常
然而OV7670并不工作,让我纠结。屏幕一直是显示这些内容,没有显示摄像头拍到的内容,照相机功能也没有,USB也无法读出SD卡,实在是想不出是什么原因。
请原子哥给指条明路,我这两个实验结合是理论上就无法实现还是我的程序改的不对,应该怎么改呢。
main函数如下:
extern u8 ov_sta;
extern u8 ov_frame;
void camera_refresh(void)
{
u32 j;
u16 color;
if(ov_sta==2)
{
LCD_Scan_Dir(U2D_L2R);
LCD_SetCursor(0x00,0x0000);
LCD_WriteRAM_Prepare();
OV7670_RRST=0;
OV7670_RCK=0;
OV7670_RCK=1;
OV7670_RCK=0;
OV7670_RRST=1;
OV7670_RCK=1;
for(j=0;j<76800;j++)
{
OV7670_RCK=0;
color=GPIOC->IDR&0XFF;
OV7670_RCK=1;
color<<=8;
OV7670_RCK=0;
color|=GPIOC->IDR&0XFF;
OV7670_RCK=1;
LCD->LCD_RAM=color;
}
EXTI_ClearITPendingBit(EXTI_Line8);
ov_sta=0;
ov_frame++;
LCD_Scan_Dir(DFT_SCAN_DIR);
}
}
void usb_port_set(u8 enable)//把这句定义USB电源供电的语句放在这应该没问题吧?
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
if(enable)_SetCNTR(_GetCNTR()&(~(1<<1)));
else
{
_SetCNTR(_GetCNTR()|(1<<1));
GPIOA->CRH&=0XFFF00FFF;
GPIOA->CRH|=0X00033000;
PAout(12)=0;
}
}
void camera_new_pathname(u8 *pname)
{
u8 res;
u16 index=0;
while(index<0XFFFF)
{
sprintf((char*)pname,"0HOTO/PIC%05d.bmp",index);
res=f_open(ftemp,(const TCHAR*)pname,FA_READ);
if(res==FR_NO_FILE)break;
index++;
}
}
int main(void)
{
u8 res;
u8 *pname;
u8 i;
u8 sd_ok=1;
u8 key;
u8 offline_cnt=0;//USB要用到的四个变量
u8 tct=0;
u8 USB_STA;
u8 Divece_STA;
delay_init();
NVIC_Configuration();
uart_init(9600);
LED_Init();
LCD_Init();
BEEP_Init();
KEY_Init();
mem_init(SRAMIN);
usmart_dev.init(72);
exfuns_init();
f_mount(0,fs[0]);
f_mount(1,fs[1]);
piclib_init();
POINT_COLOR=RED;
SPI_Flash_Init();
if(SD_Initialize())LCD_ShowString(60,190,200,16,16,"SD Card Error!");
else
{
LCD_ShowString(60,190,200,16,16,"SD Card Size: MB");
Mass_Memory_Size[0]=(long long)SD_GetSectorCount()*512;
Mass_Block_Size[0] =512;
Mass_Block_Count[0]=Mass_Memory_Size[0]/Mass_Block_Size[0];
LCD_ShowNum(164,130,Mass_Memory_Size[0]>>20,5,16);
}
if(SPI_FLASH_TYPE!=W25Q64)LCD_ShowString(60,190,200,16,16,"W25Q64 Error!");
else //SPI FLASH ????
{
Mass_Memory_Size[1]=1024*1024*6;
Mass_Block_Size[1] =512;
Mass_Block_Count[1]=Mass_Memory_Size[1]/Mass_Block_Size[1];
LCD_ShowString(60,210,200,16,16,"SPI FLASH Size:6144KB");
}
while(font_init())
{
LCD_ShowString(60,50,200,16,16,"Font Error!");
delay_ms(200);
LCD_Fill(60,50,240,66,WHITE);
}
Show_Str(60,50,200,16,"战舰STM32开发板",16,0);
Show_Str(60,70,200,16,"照相机实验",16,0);
Show_Str(60,90,200,16,"WK_UP:拍照",16,0);
Show_Str(60,110,200,16,"正点原子@ALIENTEK",16,0);
Show_Str(60,130,200,16,"2012年9月19日",16,0);
res=f_mkdir("0:/PHOTO");
if(res!=FR_EXIST&&res!=FR_OK)
{
Show_Str(60,150,240,16,"SD?¨?í?ó!",16,0);
delay_ms(200);
Show_Str(60,170,240,16,"????????????????!",16,0);
sd_ok=0;
}else
{
Show_Str(60,150,240,16,"SD?¨????!",16,0);
delay_ms(200);
Show_Str(60,170,240,16,"KEY_UP:????",16,0);
sd_ok=1;
}
pname=mymalloc(SRAMIN,30);
while(pname==NULL)
{
Show_Str(60,190,240,16,"????·????§°?!",16,0);
delay_ms(200);
LCD_Fill(60,190,240,146,WHITE);
delay_ms(200);
}
while(OV7670_Init())//按说有这句初始化ov7670的话,屏幕显示ov7670正常以后,LCD就会有图像的,然而.......
{
Show_Str(60,190,240,16,"OV7670 ?í?ó!",16,0);
delay_ms(200);
LCD_Fill(60,190,239,186,WHITE);
delay_ms(200);
}
Show_Str(60,190,200,16,"OV7670 ????",16,0);
delay_ms(1500);
TIM6_Int_Init(10000,7199);
EXTI8_Init();
OV7670_Window_Set(10,174,240,320);
OV7670_CS=0;
delay_ms(1800);
usb_port_set(0);
delay_ms(300);
usb_port_set(1);
LCD_ShowString(60,170,200,16,16,"USB Connecting...");
//USB????
USB_Interrupts_Config();
Set_USBClock();
USB_Init();
delay_ms(1800);
while(1)//死循环的逻辑不知道对不对。
{
key=KEY_Scan(0);?
if(key==KEY_UP)
{
LED1=0;
//BEEP=1;
LED0=0;
if(sd_ok)
{
LED1=0;
camera_new_pathname(pname);
if(bmp_encode(pname,0,0,240,320,0))
{
Show_Str(40,130,240,12,"?????????í?ó!",12,0);
}else
{
Show_Str(40,130,240,12,"????????!",12,0);
Show_Str(40,150,240,12,"±?????:",12,0);
Show_Str(40+42,150,240,12,pname,12,0);
BEEP=1;
delay_ms(100);
}
}
else
{
Show_Str(40,130,240,12,"SD?¨?í?ó!",12,0);
Show_Str(40,150,240,12,"??????????????!",12,0);
}
//BEEP=0;
//LED1=1;
delay_ms(1000);
if(USB_STA!=USB_STATUS_REG)
{
LCD_Fill(60,190,240,190+16,WHITE);
if(USB_STATUS_REG&0x01)
{
LCD_ShowString(60,250,200,16,16,"USB Writing...");
}
if(USB_STATUS_REG&0x02)//??????
{
LCD_ShowString(60,250,200,16,16,"USB Reading...");
}
if(USB_STATUS_REG&0x04)LCD_ShowString(60,270,200,16,16,"USB Write Err ");
else// LCD_Fill(60,210,240,210+16,WHITE);
if(USB_STATUS_REG&0x08)LCD_ShowString(60,270,200,16,16,"USB Read Err ");
else //LCD_Fill(60,230,240,230+16,WHITE);
USB_STA=USB_STATUS_REG;
}
}else
LED1=1;
//BEEP=0;
LED0=1;
delay_ms(10);
camera_refresh();
i++;
if(i==20)
{
i=0;
LED0=!LED0;
}
if(Divece_STA!=bDeviceState)
{
if(bDeviceState==CONFIGURED)LCD_ShowString(60,170,200,16,16,"USB Connected ");
else LCD_ShowString(60,170,200,16,16,"USB DisConnected ");
Divece_STA=bDeviceState;
}
tct++;
if(tct==200)
{
tct=0;
LED0=!LED0;
if(USB_STATUS_REG&0x10)
{
offline_cnt=0;
bDeviceState=CONFIGURED;
}else
{
offline_cnt++;
if(offline_cnt>10)bDeviceState=UNCONNECTED;
}
USB_STATUS_REG=0;
}
}
}
因为要做毕设所以第一次接触stm32,以前51学的都不是很扎实,希望原子哥多多指点,最近自己瞎琢磨的已经有点醉了。
一周热门 更多>