کد:
#include "Camera.h"
#include "includes.h"
#include "ov7670.h"
#include "Data_Exchange.h"
u8 buf1 [ 32 ];
u32 m = 0;
u32 rows = 0;
u32 col = 0;
u16 Camera_Data = 0;
u16 temp1 = 0;
extern u8 VsyncCnt;
U32 pixel = 0;
int pic_num = 0;
char Capture_mode = 0;
u8 Cam_Counter = 0;
char photo_name [ 50 ];
//*******************************************************************
char Capture_BMP ( int width , int height , int bpp )
{
U32 reg_tmp = 0;
unsigned char Header [ 70 ];
int offset , comp;
u8 r = 0 , g = 0, b = 0;
U32 p = 0;
int rows = 0;
int col = 0;
unsigned char High_Byte = 0;
unsigned char Low_Byte = 0;
FILE *file_tmp;
char pic_name [ 50 ];
PC_SendString ( "\r Capture_BMP \r" );
Picture_name ( pic_name );
PC_SendString ( pic_name );
file_tmp = fopen ( pic_name , "wb" );
//file_tmp = fopen ( pic_name , "ab" );
if ( file_tmp != NULL )
{
for ( p = 0; p < 54; p ++ )
{
Header [ p ] = 0;
}
// offset = ( Header [ 13 ] << 24 ) | ( Header [ 12 ] << 16 ) | ( Header [ 11 ] << 8 ) | ( Header [ 10 ] );
// width = ( Header [ 21 ] << 24 ) | ( Header [ 20 ] << 16 ) | ( Header [ 19 ] << 8 ) | ( Header [ 18 ] );
// height = ( Header [ 25 ] << 24 ) | ( Header [ 24 ] << 16 ) | ( Header [ 23 ] << 8 ) | ( Header [ 22 ] );
// bpp = ( Header [ 29 ] << 8 ) | ( Header [ 28 ] );
// comp = ( Header [ 33 ] << 24 ) | ( Header [ 32 ] << 16 ) | ( Header [ 31 ] << 8 ) | ( Header [ 30 ] );
offset = 54;
comp = 0;
Header [ 0 ] = 0x42;
Header [ 1 ] = 0x4D;
//2h->5h : Size of the BMP file
//320 * 240 : 36 84 03 00
reg_tmp = width * height;
reg_tmp *= ( bpp / 8 );
reg_tmp += 54; //header size
Header [ 2 ] = ( reg_tmp >> 0 ) & 0x0FF;
Header [ 3 ] = ( reg_tmp >> 8 ) & 0x0FF;
Header [ 4 ] = ( reg_tmp >> 16 ) & 0x0FF;
Header [ 5 ] = ( reg_tmp >> 24 ) & 0x0FF;
//6h->7h : Application Specific
Header [ 6 ] = 0x00;
Header [ 7 ] = 0x00;
//8h->9h : Application Specific
Header [ 8 ] = 0x00;
Header [ 9 ] = 0x00;
//Ah->Dh : The offset where the bitmap data (pixels) can be found.
Header [ 10 ] = 0x36;
Header [ 11 ] = 0x00;
Header [ 12 ] = 0x00;
Header [ 13 ] = 0x00;
//Eh->11h : The number of bytes in the header (from this point).
Header [ 14 ] = 0x28;
Header [ 15 ] = 0x00;
Header [ 16 ] = 0x00;
Header [ 17 ] = 0x00;
//12h->15h : The width of the bitmap in pixels
Header [ 18 ] = ( width >> 0 ) & 0x0FF;
Header [ 19 ] = ( width >> 8 ) & 0x0FF;
Header [ 20 ] = ( width >> 16 ) & 0x0FF;
Header [ 21 ] = ( width >> 24 ) & 0x0FF;
//16->19h : The height of the bitmap in pixels
Header [ 22 ] = ( height >> 0 ) & 0x0FF;
Header [ 23 ] = ( height >> 8 ) & 0x0FF;
Header [ 24 ] = ( height >> 16 ) & 0x0FF;
Header [ 25 ] = ( height >> 24 ) & 0x0FF;
//1Ah->1Bh : Number of color planes being used.
Header [ 26 ] = 0x01;
Header [ 27 ] = 0x00;
//1Ch->1Dh : The number of bits/pixel.
Header [ 28 ] = ( bpp >> 0 ) & 0x0FF;
Header [ 29 ] = ( bpp >> 8 ) & 0x0FF;
//Header [ 28 ] = 0x18;
//Header [ 29 ] = 0x00;
//1Eh->21h : BI_RGB, No compression used
Header [ 30 ] = 0x00;
Header [ 31 ] = 0x00;
Header [ 32 ] = 0x00;
Header [ 33 ] = 0x00;
//22h->25h : The size of the raw BMP data (after this header)
//the image size. This is the size of the raw bitmap data; a dummy 0 can be given for BI_RGB bitmaps
Header [ 34 ] = 0x00;
Header [ 35 ] = 0x00;
Header [ 36 ] = 0x00;
Header [ 37 ] = 0x00;
//26h->29h : The horizontal resolution of the image
Header [ 38 ] = 0xC4;
Header [ 39 ] = 0x0E;
Header [ 40 ] = 0x00;
Header [ 41 ] = 0x00;
//2Ah->2Dh : The vertical resolution of the image
Header [ 42 ] = 0xC4;
Header [ 43 ] = 0x0E;
Header [ 44 ] = 0x00;
Header [ 45 ] = 0x00;
//2Eh->31h : Number of colors in the palette
Header [ 46 ] = 0x00;
Header [ 47 ] = 0x00;
Header [ 48 ] = 0x00;
Header [ 49 ] = 0x00;
//32h->35h : Means all colors are important
Header [ 50 ] = 0x00;
Header [ 51 ] = 0x00;
Header [ 52 ] = 0x00;
Header [ 53 ] = 0x00;
// 36h = 54 : Start of Bitmap Data
sprintf ( str, "\r offset : %d \r width : %d \r height : %d \r bpp : %d \r comp : %d \r" , offset , width , height , bpp , comp );
PC_SendString ( str );
fwrite ( &Header , 1 , offset , file_tmp );
PC_SendString ( "\r break point #1 \r" );
//fseek ( file_tmp , offset , SEEK_SET );
p = 0;
for ( rows = 0; rows < 240; rows++ )
{
for ( col = 0; col < 320; col++ )
{
/*
if ( rows < ( height * 0.3 ) )
{
b = 0;
g = 0;
r = 255;
}
else if ( ( rows < ( height * 0.6 ) ) && ( rows > ( height * 0.3 ) ) )
{
b = 255;
g = 0;
r = 0;
}
else
{
b = 0;
g = 255;
r = 0;
}
*/
CAMERA_FIFO_ReadCLK_LOW
__nop();
__nop();
__nop();
__nop();
__nop();
CAMERA_FIFO_ReadCLK_HIGH
__nop();
__nop();
__nop();
__nop();
__nop();
High_Byte = CAMERA_FIFO_DATA_READ;
Camera_Data = ( High_Byte << 8 );
CAMERA_FIFO_ReadCLK_LOW
__nop();
__nop();
__nop();
__nop();
__nop();
CAMERA_FIFO_ReadCLK_HIGH
__nop();
__nop();
__nop();
__nop();
__nop();
Low_Byte = CAMERA_FIFO_DATA_READ;
Camera_Data |= Low_Byte;
//LCD_X_Write01_16 ( Camera_Data );
/*
LCD : RGB
Camera : RGB
BMP Format : BGR
*/
//Low_Byte = Camera_Data >> 8;
//High_Byte = Camera_Data & 0xff;
// //16bit
// r = ( High_Byte >> 3 ) & 0x1F;
//
// g = ( Camera_Data >> 5 ) & 0x3F;
//
// b = Low_Byte & 0x1F;
// //24bit_1
// r = High_Byte & 0xF8;
//
// g = ( ( Camera_Data & 0x07E0 ) >> 3 ) & 0xFF;
//
// b = ( Low_Byte & 0x1F ) << 3;
//24bit_2
r = ( High_Byte & 0xF8 ) | 0x07;
g = ( ( ( Camera_Data & 0x07E0 ) >> 3 ) & 0xFF ) | 0x03;
b = ( ( Low_Byte & 0x1F ) << 3 ) | 0x07;
// r = ( 0x3f ) & 0x3f;
//
// g = ( 0x00 ) & 0x1F;
//
// b = 0x00 & 0x1F;
//OK -> BGR16_TO_RGB16
//High_Byte = ( b << 3 ) | ( g >> 3 );
//Low_Byte = ( ( g << 5 ) & ( 0xE0 ) ) | r;
//BRG16
//High_Byte = ( b << 3 ) | ( r >> 4 );
//Low_Byte = ( ( r << 4 ) & ( 0xF0 ) ) | ( g >> 1 );
// sprintf ( str, "\r High_Byte : 0x%x \r Low_Byte : 0x%x \r Color16 : 0x%x \r" , High_Byte , Low_Byte , ( uint16_t ) ( (High_Byte <<8 ) | Low_Byte ) );
//
// PC_SendString ( str );
//LCD_Clear ( ( uint16_t ) ( (High_Byte <<8 ) | Low_Byte ));
//LCD_Clear ( Red );
//while ( 100 );
switch ( bpp )
{
case 16 :
{
fprintf ( file_tmp , "%c%c" , High_Byte , Low_Byte );
} break;
case 24 :
{
fprintf ( file_tmp , "%c%c%c" , b , g , r );
} break;
default :
{
} break;
}
p++;
}
// fputc ( b , file_tmp );
// fputc ( g , file_tmp );
// fputc ( r , file_tmp );
}
//TFTLCD_CS_HIGH;
fclose ( file_tmp );
sprintf ( str, "\r max pixel = %d" , p );
PC_SendString ( str );
}
else
{
sprintf ( str, "\r Can not Create File. switch /wb fname = %s" , pic_name );
PC_SendString ( str );
return 0;
}
PC_SendString ( "\r Capture_BMP END \r" );
return 1;
}
/*----------------------------------------------------------------------*/
__inline void Camera_Display ( void )
{
if ( Capture_mode == 0 )
{
PC_SendString ( "\r Capture_mode == 0 END \r" );
//
// //sprintf ( pic_name , "pic_%d.bmp" , pic_num );
//
//
// Picture_name ( pic_name );
//
// //if ( Capture_BMP ( pic_name , 240 , 320 , 16 ) )
//
// if ( Capture_BMP ( pic_name , 240 , 320 , 24 ) )
// {
// lcd_show_bmp ( 0 , 0 , pic_name );
//
// pic_num++;
// }
Capture_mode = 0;
}
else if ( Capture_mode == 2 )
{
PC_SendString ( "\r Capture_mode == 2 \r");
//sprintf ( pic_name , "pic_%d.bmp" , pic_num );
//Picture_name ( pic_name );
//if ( Capture_BMP ( pic_name , 240 , 320 , 16 ) )
// if ( Capture_BMP ( pic_name , 240 , 320 , 24 ) )
// {
// lcd_show_bmp ( 0 , 0 , pic_name );
// pic_num++;
// }
//sprintf ( photo_name , "%s\\%8s" , Photo_Folder_Name , TODAY_DATE );
//Create_a_Folder ( photo_name );
if ( Capture_BMP ( 240 , 320 , 24 ) )
{
lcd_show_bmp ( 0 , 0 , " " );
pic_num++;
}
Capture_mode = 0;
}
else if ( Capture_mode == 1 )
{
// TFTLCD_CS_LOW;
// LCD_SetCursor ( 0 , 0 );
// LCD_X_Write00_16 ( 0x0022 );
// TFTLCD_CS_LOW;
TFTLCD_CS_LOW;
//LCD_SetCursor ( 0 , 0 );
LCD_WriteReg ( 0x0020 , 0 ); // GRAM horizontal Address /* Line */
LCD_WriteReg ( 0x0021 , 0 ); // GRAM Vertical Address
LCD_X_Write00_16 ( 0x0022 );
TFTLCD_CS_LOW;
//for ( m = 0; m < 76800; m++ ) //320*240
for ( rows = 0; rows < 320; rows++ )
{
for ( col = 0; col < 240; col++ )
{
CAMERA_FIFO_ReadCLK_LOW
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
CAMERA_FIFO_ReadCLK_HIGH
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
Camera_Data = ( CAMERA_FIFO_DATA_READ << 8 );
//Delay_NS(1000);
CAMERA_FIFO_ReadCLK_LOW
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
CAMERA_FIFO_ReadCLK_HIGH
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
Camera_Data |= CAMERA_FIFO_DATA_READ;
//Delay_NS(1000);
//pixel++;
// if ( ( rows < 30 ) || ( rows > 290 ) || ( col < 30 ) || ( col > 210 ) )
// {
// LCD_X_Write01_16 ( Yellow );
// }
// else
{
LCD_X_Write01_16 ( Camera_Data );
}
}
}
TFTLCD_CS_HIGH;
}
__nop();
CAMERA_FIFO_nReadRST_LOW
__nop();
for ( Cam_Counter = 0; Cam_Counter < 128; Cam_Counter++ )
{
CAMERA_FIFO_ReadCLK_LOW
__nop();
CAMERA_FIFO_ReadCLK_HIGH
__nop();
}
CAMERA_FIFO_nReadRST_HIGH
__nop ();
CAMERA_FIFO_nWriteRST_LOW
__nop();
Delay_ns ( 2 );
CAMERA_FIFO_nWriteRST_HIGH
__nop ();
VsyncCnt = 0;
__nop();
}
/*----------------------------------------------------------------------*/
void CAMERA_FIFO_VSYNC_IRQ_ROUTIN ( void )
{
if ( VsyncCnt < 2 )
{
VsyncCnt ++;
}
if ( VsyncCnt == 1 )
{
CAMERA_FIFO_nWriteEnable_HIGH
__nop();
CAMERA_FIFO_nReadEnable_HIGH
__nop();
}
else if ( VsyncCnt == 2 )
{
CAMERA_FIFO_nWriteEnable_LOW
__nop();
CAMERA_FIFO_nReadEnable_LOW
__nop();
}
}
/*----------------------------------------------------------------------*/
void Camera_RUN ( void )
{
PC_SendString ( "\r Camera_RUN " );
while ( 1 )
{
if ( OV7660_init () )
{
PC_SendString ( "\r OV7660_init " );
break; //OV7660_init()==1
}
Delay_ms ( 500 );
}
OV7660_config_window ( 270, 16, 480, 320 );
//OV7660_config_window(272,0,480,640);
//OV7660_config_window (140,16,640,480);// is good for VGA
//OV7660_config_window(272,16,320,240); //is good for QVGA
CAMERA_FLASH_ON
KEYBOARD_LED_ON
Capture_mode = 1;
do
{
KBD_Check_OS();
if ( ( key == 'Y' ) || ( key == 'm' ) )
{
Capture_mode = 2;
}
else if ( key == 'C' )
{
Capture_mode = 0;
}
if ( VsyncCnt == 2 )
{
Camera_Display();
}
} while ( Capture_mode );
VsyncCnt = 2;
CAMERA_FLASH_OFF;
Camera_RESET ();
}