/*******************************************************************************
* Copyright (C) 2010 - All Rights Reserved
*
* 软件作者: 骑飞家族
* 版权所有: 骑飞电子
* 创建日期: 2010年6月10日
* 软件历史: 2010年11月2日修改
* Version: 1.0
* Demo 淘宝地址:http://store.taobao.com/shop/view_shop.htm?asker=wangwang&shop_nick=qifeidianzi
**********************************************************************************************************************************************
懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒
懒懒懒懒懒懒懒懒懒懒懒一懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒困一懒懒懒懒懒懒懒懒懒懒懒懒懒懒一一一一一一一懒懒懒懒懒懒懒
懒懒困一一一一懒一一一一一一二懒懒懒懒困一一一一一一一一懒懒懒懒懒懒懒懒懒四厲懒懒一一懒懒懒一懒懒懒懒懒懒懒懒懒一一一一四四一一一懒懒懒懒懒懒
懒懒懒懒懒懒一懒懒懒一二一懒懒懒懒懒懒一一一一四厲二一四懒一一懒懒懒懒懒四一一一一一一一一一一一懒懒懒懒懒懒懒懒四懒懒四一一一一厲懒懒懒懒懒懒
懒懒懒四厲厲一懒懒厲懒懒懒四懒懒懒懒懒懒懒懒懒懒懒困一懒一一懒懒懒懒懒懒懒一一厲厲一一厲厲厲一一懒懒懒懒懒懒厲厲厲厲厲厲一一厲厲厲懒懒懒懒懒懒
懒懒懒一懒二一一一一一一一一一一一懒懒懒懒懒懒懒懒二一一困懒懒懒懒懒懒懒懒一一一一一一一一一一四懒懒懒懒懒一一一一一一一一一一一一一一一懒懒懒
懒懒懒一一一一一厲二一一厲一懒厲懒懒懒懒懒懒懒懒懒一一一一一懒懒懒懒懒懒懒一一懒懒一一懒懒懒一困懒懒懒懒懒一一四懒懒懒懒一一懒懒困一一四懒懒懒
懒懒懒懒懒懒懒一困一懒一厲一懒懒懒懒懒懒懒懒懒懒懒困一懒懒一一懒懒懒懒懒懒一一一一一一一一一一厲懒懒懒懒懒懒懒懒懒懒懒懒一一厲懒懒懒懒懒懒懒懒
懒二一一一懒四困厲一一一厲一懒懒懒懒懒懒懒懒懒懒懒懒一一懒懒懒懒懒懒懒懒懒懒懒懒懒四一厲懒懒懒懒懒懒一懒懒懒懒懒懒懒懒懒一一厲懒懒懒懒懒懒懒懒
懒懒懒懒懒懒一懒懒懒懒懒一一懒懒懒懒懒懒懒懒懒懒懒懒厲一一一一一一困懒懒懒懒懒懒懒懒一一一一一一一一一懒懒懒懒懒困懒懒厲一一懒懒懒懒懒懒懒懒懒
懒懒懒懒一一一懒懒困一一一一懒懒懒懒懒懒懒懒懒懒懒懒懒懒一一一一一懒懒懒懒懒懒懒懒懒懒一一一一一一一懒懒懒懒懒懒懒一一一一四懒懒懒懒懒懒懒懒懒
懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒懒
**********************************************************************************************************************************************/
#include <c8051f330.h>
#include <english_16x8.h>
///////////////////////////////////////////////////////
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define AM 001
#define TIME 100
#define SDA_IN() {P0MDOUT= 0X00;}
#define SDA_OUT() {P0MDOUT= 0XFF;}
//Tft control lines
sbit Tft_RS_PIN=P0^6;
sbit Tft_WR_PIN=P0^7;
sbit Tft_RST_PIN=P0^4;
//camera control lines
sbit I2C_SCL=P0^1;
sbit I2C_SDA=P0^0;
sbit Cmos_VSYNC=P0^5;
sbit SDA_STATE=P0^0;
//sn74lvc245 control lines
sbit Cmos_CS_PIN=P0^2;
//SN74LVC1G125 CONTROL LINES
sbit WR_CONTROL_PIN=P0^3;
void OSCILLATOR_Init (void);
void Port_Init(void);
void Ext_Interrupt_Init (void);
void Tft_Init(void);
void TftClear(unsigned int Color);
void Tft_WR_DATA16(unsigned int inputdata);
void TftWrite16(unsigned int index);
void TftWrite(unsigned int index,unsigned int inputdata);
void TftWR_Str(unsigned int x,unsigned int y,unsigned char *s,unsigned int color,unsigned int xcolor);
void TftWR_SingleStr(unsigned char inputdata,unsigned int color,unsigned int xcolor);
void Tft_SetBackground(unsigned int StartX,unsigned int StartY,unsigned int LongX,unsigned int LongY);
void Tft_SetCursor(unsigned int Xpos, unsigned int Ypos);
unsigned char WrCmos7670(unsigned char regID, unsigned char regDat);
unsigned char Cmos7670_init(void);
void set_Cmos7670reg(void);
void delay_us(unsigned int time);
void delay_ms(unsigned int time);
void delay( unsigned long cnt);
///////////////////////////////////////////
void DelayI2C(void);
void InitI2C(void);
void StartI2C(void);
void StopI2C(void);
void NoAck(void);
unsigned char I2CWrite(unsigned char DData);
////////////////////////////////////////////
unsigned int flag;
///////////////////////////////////////////
void main(void)
{
PCA0MD &= ~0x40;
OSCILLATOR_Init ();
Port_Init();
Ext_Interrupt_Init( );
WR_CONTROL_PIN=1;
Tft_Init( );
TftClear(BLACK);
TftWR_Str(60,70,"C8051F330 Camera",RED,BLACK);
TftWR_Str(60,100,"QI FEI DIAN ZI VER1.0",RED,BLACK);
TftWR_Str(60,130,"OV7670 Initializing......",RED,BLACK);
delay_ms(3000);
while(1!=Cmos7670_init());
delay_ms(3000);
TftClear(BLACK);
TftWR_Str(60,110,"OV7670 Init SUCCESS! ",RED,BLACK);
delay_ms(5000);
TftClear(BLACK);
EA = 1;
while(1);
}
void OSCILLATOR_Init (void)
{
OSCICN = 0x83;
RSTSRC = 0x04;
}
void Port_Init(void)
{
P0MDIN = 0xFF;
P1MDIN = 0XFF;
P0MDOUT= 0XFF;
P1MDOUT= 0XFF;
P0SKIP = 0X00;
P1SKIP = 0X00;
XBR0 = 0x00;
XBR1 = 0x40;
}
void Ext_Interrupt_Init (void)
{
IT01CF = 0x0d;
IT0=1;
IE = 0x01;
}
void INT0_ISR (void) interrupt 0
{
EA=0;
IE0=0;
P0MDOUT= 0xff;
WR_CONTROL_PIN=1;
Cmos_CS_PIN=1;
TftWrite(0x0020,0x0000);
TftWrite(0x0021,0x013f);
TftWrite(0x0050,0x00);
TftWrite(0x0051,239);
TftWrite(0x0052,0x00);
TftWrite(0x0053,319);
TftWrite(0x0003,0x1018);
TftWrite16(0x0022);
Tft_RS_PIN=1;
Tft_WR_PIN=0;
WR_CONTROL_PIN=0;
Cmos_CS_PIN=0;
EA=1;
}
void Tft_Init(void)
{
Tft_RST_PIN=1;
delay_ms(10);;
Tft_RST_PIN=0;
delay_ms(10);
Tft_RST_PIN=1;
delay_ms(150);
TftWrite(0x00e5, 0x8000);
TftWrite(0x0000, 0x0001);
TftWrite(0x0001, 0x0100);
TftWrite(0x0002, 0x0700);
#if AM==000
TftWrite(0x0003,0x1000);
#elif AM==001
TftWrite(0x0003,0x1008);
#elif AM==010
TftWrite(0x0003,0x1010);
#elif AM==011
TftWrite(0x0003,0x1018);
#elif AM==100
TftWrite(0x0003,0x1020);
#elif AM==101
TftWrite(0x0003,0x1028);
#elif AM==110
TftWrite(0x0003,0x1030);
#elif AM==111
TftWrite(0x0003,0x1038);
#endif
TftWrite(0x0004, 0x0000);
TftWrite(0x0008, 0x0202);
TftWrite(0x0009, 0x0000);
TftWrite(0x000A, 0x0000);
TftWrite(0x000C, 0x0000);
TftWrite(0x000D, 0x0000);
TftWrite(0x000F, 0x0000);
TftWrite(0x0010, 0x0000);
TftWrite(0x0011, 0x0007);
TftWrite(0x0012, 0x0000);
TftWrite(0x0013, 0x0000);
delay_ms(1000);
TftWrite(0x0010, 0x17B0);
TftWrite(0x0011, 0x0007);
delay_ms(1000);
TftWrite(0x0012, 0x013A);
delay_ms(1000);
TftWrite(0x0013, 0x1A00);
TftWrite(0x0029, 0x000c);
delay_ms(1000);
TftWrite(0x0030, 0x0000);
TftWrite(0x0031, 0x0505);
TftWrite(0x0032, 0x0004);
TftWrite(0x0035, 0x0006);
TftWrite(0x0036, 0x0707);
TftWrite(0x0037, 0x0105);
TftWrite(0x0038, 0x0002);
TftWrite(0x0039, 0x0707);
TftWrite(0x003C, 0x0704);
TftWrite(0x003D, 0x0807);
TftWrite(0x0050, 0x0000);
TftWrite(0x0051, 0x00EF);
TftWrite(0x0052, 0x0000);
TftWrite(0x0053, 0x013F);
TftWrite(0x0060, 0x2700);
TftWrite(0x0061, 0x0001);
TftWrite(0x006A, 0x0000);
#if AM==000
TftWrite(0x0020,0x00ef);
TftWrite(0x0021,0x013f);
#elif AM==001
TftWrite(0x0020,0x00ef);
TftWrite(0x0021,0x013f);
#elif AM==010
TftWrite(0x0020,0x0000);
TftWrite(0x0021,0x013f);
#elif AM==011
TftWrite(0x0020,0x0000);
TftWrite(0x0021,0x013f);
#elif AM==100
TftWrite(0x0020,0x00ef);
TftWrite(0x0021,0x0000);
#elif AM==101
TftWrite(0x0020,0x00ef);
TftWrite(0x0021,0x0000);
#elif AM==110
TftWrite(0x0020,0x0000);
TftWrite(0x0021,0x0000);
#elif AM==111
TftWrite(0x0020,0x0000);