#include<lpc23xx.h>
#define uint unsigned int
#define uchar unsigned char
/****************************************************************************************************************************
| DATA_LCD_7 | DATA_LCD_6 | DATA_LCD_5 | DATA_LCD_4 | DATA_LCD_3 | DATA_LCD_2 | DATA_LCD_1 | DATA_LCD_0 |
| FIFO_WE | WRB_LCD | RES_CMOS | RESET_LCD | A0_LCD | CS_LCD | RD_LCD | VSYNC_INT |
| FIFO_WRST | OC_OUT | FIFO_RRST | FIFO_RCLK | DATA7 | DATA6 | DATA5 | DATA4 |
| DATA3 | DATA2 | DATA1 | DATA0 | SDA_CMOS | SCL_CMOS | RXD | TXD |
*****************************************************************************************************************************
| p4.7 I | p4.6 I | p4.5 I | p4.4 I | p4.3 I | p4.2 I | p4.1 I | p4.0 I |
| p4.13 I | p4.9 I | p1.22 I | p4.11 I | p4.10 I | p4.12 I | p4.8 I | P0.11 O |
| P4.15 I | OC_OUT | p3.2 I | p4.14 I | P2.7 O | P2.6 O | P2.5 O | P2.4 O |
| P2.3 O | P2.2 O | P2.1 O | P2.0 O | P1.29 I | P3.23 I | RXD | TXD |
*****************************************************************************************************************************/
/*液晶管脚*/
#define CS_LCD_1 FIO4SET = FIO4SET | (1<<12)
#define CS_LCD_0 FIO4CLR = FIO4CLR | (1<<12)
#define RES_LCD_1 FIO4SET = FIO4SET | (1<<11)
#define RES_LCD_0 FIO4CLR = FIO4CLR | (1<<11)
#define A0_LCD_1 FIO4SET = FIO4SET | (1<<10)
#define A0_LCD_0 FIO4CLR = FIO4CLR | (1<<10)
#define WRB_LCD_1 FIO4SET = FIO4SET | (1<<9)
#define WRB_LCD_0 FIO4CLR = FIO4CLR | (1<<9)
#define RD_LCD_1 FIO4SET = FIO4SET | (1<<8)
#define RD_LCD_0 FIO4CLR = FIO4CLR | (1<<8)
/*FIFO管脚*/
#define FIFO_WE_1 FIO4SET = FIO4SET | (1<<13)
#define FIFO_WE_0 FIO4CLR = FIO4CLR | (1<<13)
#define FIFO_WRST_1 FIO4SET = FIO4SET | (1<<15)
#define FIFO_WRST_0 FIO4CLR = FIO4CLR | (1<<15)
#define FIFO_RCLK_1 FIO4SET = FIO4SET | (1<<14)
#define FIFO_RCLK_0 FIO4CLR = FIO4CLR | (1<<14)
#define FIFO_RRST_1 FIO3SET = FIO3SET | (1<<2)
#define FIFO_RRST_0 FIO3CLR = FIO3DIR | (1<<2)
#define VSYNC 11
/*CMOS管脚*/
#define RES_CMOS_1 FIO1SET = FIO1SET | (1<<22)
#define RES_CMOS_0 FIO1CLR = FIO1CLR | (1<<22)
#define SDA_CMOS_1 FIO1SET = FIO1SET | (1<<28)
#define SDA_CMOS_0 FIO1CLR = FIO1CLR | (1<<28)
#define SCL_CMOS_1 FIO3SET = FIO3SET | (1<<23)
#define SCL_CMOS_0 FIO3CLR = FIO3CLR | (1<<23)
uchar tuxiang[160][128]={0};
uchar tuxiang_old[160][128]={0};
void GPIO_INT(void); //初始化所用的GPIO端口
void LCDWriteCOM(unsigned char a); //LCE写指令
void LCDWriteDAT(unsigned char b); //LCD写数据
void delayms(int i);
void ini_lcd(void); //LCD初始化
void I2C_START(void); //I2C总线开启
void I2C_INT(void); //CMOS初始化
void I2C_TX(uchar TX_DATA_H,uchar TX_DATA_L,uchar RS); //I2C发送数据,TX_DATA_H为数据高八位,TX_DATA_L为低八位,RS为0时写指令,RS为1写数据
void I2C_STOP(void); // I2C总线停止
void xianshi(void); //液晶显示初始值
void fuzhi(void); //采集摄像头信号
void chuli(void); //显示摄像头信号
void xunhuan(void); //将采集和显示集合到一个子函数里
void fuzhi1(void); //与fuzhi()一样
int key,can;
int main(void)
{
// uint q;
key=0;
can=1;
GPIO_INT();
RES_CMOS_0;
FIFO_WE_0;
delayms(20000);
RES_CMOS_1;
FIFO_WE_1;
SDA_CMOS_1;
SCL_CMOS_1;
FIFO_WRST_0;
FIFO_WRST_1;
FIFO_RCLK_0;
I2C_INT();
ini_lcd();
xianshi();
while(1)
{
while(((FIO0PIN>>VSYNC)&0x01))
{
// xunhuan();
// delayms(1000);
FIFO_WE_0;
FIFO_WRST_1;
FIFO_RRST_0;
FIFO_RCLK_0;
FIFO_RCLK_1;
FIFO_RCLK_0;
FIO1CLR = FIO1CLR | (1<<18); //开发板中这一位是个LED灯,所以在显示前后开关LED等便可知道实际帧数
// key=1;
chuli();
FIO1SET = FIO1SET | (1<<18);
FIFO_RRST_1;
FIFO_WRST_0;
// key=0;
if(can==0) //经过实际测验发现,采集的信号每隔一次就有一次不准,没找要什么毛病,所以就人为的屏蔽掉一次采集的信号。
{fuzhi();can=1;}
else {fuzhi1();can=0;}
FIFO_WE_1;
}
}
}
void xunhuan(void)
{
// int x,y;
if(key==0)
{
FIFO_WE_0;
FIFO_WRST_1;
FIFO_RRST_0;
FIFO_RCLK_0;
FIFO_RCLK_1;
FIFO_RCLK_0;
FIO1CLR = FIO1CLR | (1<<18);
key=1;
chuli();
FIO1SET = FIO1SET | (1<<18);
// for(y=0;y<126;y++)
// {
// for(x=0;x<175;x++)
// {
// FIFO_RCLK_1;
// FIFO_RCLK_0;
// } }
}
else
{
FIFO_RRST_1;
FIFO_WRST_0;
key=0;
if(can==0)
{fuzhi();can=1;}
else {fuzhi1();can=0;}
FIFO_WE_1;
}
}
void chuli(void)
{
int m,n;
for(m=0;m<160;m++)
{
for(n=0;n<128;n++)
{
LCDWriteDAT(tuxiang[m][n]);
LCDWriteDAT(0x00);
}
}
}
void fuzhi1(void)
{
uint m,n,k;
for(n=1;n<127;n++) //每一行多出15个像素点,所以每行先去掉15个再采集
{
for(k=0;k<15;k++)
{
FIFO_RCLK_1;
FIFO_RCLK_0;
}
for(m=0;m<160;m++)
{
tuxiang_old[m][n]=FIO2PIN0;
FIFO_RCLK_1;
FIFO_RCLK_0;
}
}
}
void fuzhi(void)
{
uint m,n,k;
for(n=1;n<127;n++)
{
for(k=0;k<15;k++)
{
FIFO_RCLK_1;
FIFO_RCLK_0;
}
for(m=0;m<160;m++)
{
tuxiang[m][n]=FIO2PIN0;
FIFO_RCLK_1;
FIFO_RCLK_0;
}
}
}
void GPIO_INT(void)
{
SCS = SCS | 1; //开启快速GPIO
PINSEL0 = 0x00000000; //P0.0~P0.15 将管脚都设置成GPIO端口模式
PINSEL3 = 0x00000000; //P1.16~P1.31
PINSEL4 = 0x00000000; //P2.0~P2.15
PINSEL6 = 0x00000000;
PINSEL7 = 0x00000000;
PINSEL8 = 0x00000000; //P4.0~p4.15
FIO0DIR1 = 0x00; //输出管脚都置一,输入都置零
FIO1DIR = FIO1DIR | (1<<22);
FIO1DIR = FIO1DIR | (1<<28);
FIO3DIR = FIO3DIR | (1<<2);
FIO2DIR0 = 0x00;
FIO3DIR = FIO3DIR | (1<<23);
FIO4DIRL = 0xffff;
FIO1DIR =FIO1DIR |(1<<18);
}
void I2C_START(void)
{
int j=0;
SDA_CMOS_1;
SCL_CMOS_1;
for(j=0;j<100;j++);
SDA_CMOS_0;
for(j=0;j<100;j++);
SCL_CMOS_0;
}
void I2C_INT(void)
{
uint j;
I2C_START();
I2C_TX(0x00,0x00,0); //电源、时钟分频器控制寄存器
I2C_STOP();
for(j=0;j<500;j++);
I2C_START();
I2C_TX(0x68,0x00,1); //开启自动曝光控制
I2C_STOP();
for(j=0;j<500;j++);
I2C_START();
I2C_TX(0x00,0x01,0); //传感器数据计数器控制寄存器
I2C_STOP();
for(j=0;j<500;j++);
I2C_START();
I2C_TX(0x01,0x80,1); //默认值
I2C_STOP();
for(j=0;j<500;j++);
I2C_START();
I2C_TX(0x00,0x02,0x00); //曝光下限和上限计数器控制寄存器
I2C_STOP();
for(j=0;j<500;j++);
I2C_START();
I2C_TX(0xb0,0x50,1); //上限为0xb0,下限为0x50
I2C_STOP();
for(j=0;j<500;j++);
I2C_START();
I2C_TX(0x00,0x03,0); //全局增益寄存器
I2C_STOP();
for(j=0;j<500;j++);
I2C_START();
I2C_TX(0x00,0x1f,1); //最高位0x1f,4.875倍
I2C_STOP();
for(j=0;j<500;j++);
I2C_START();
I2C_TX(0x00,0x05,0); //每行像素曝光寄存器
I2C_STOP();
for(j=0;j<500;j++);
I2C_START();
I2C_TX(0x00,0xb0,1); //每行像素最小值0xb0
I2C_STOP();
I2C_START();
I2C_TX(0x00,0x06,0); //曝光时间控制寄存器 由于自动曝光,此项可省,为默认
I2C_STOP();
for(j=0;j<500;j++);
I2C_START();
I2C_TX(0x00,0x30,1);
I2C_STOP();
for(j=0;j<500;j++);
}
void I2C_TX(uchar TX_DATA_H,uchar TX_DATA_L,uchar RS)
{
int i=0;
int j=0;
SDA_CMOS_0;
SCL_CMOS_0;
SDA_CMOS_1; //"1"
for(j=0;j<100;j++);
SCL_CMOS_1;
for(j=0;j<100;j++);
SCL_CMOS_0;
for(j=0;j<100;j++);
if(RS==1)
{
SDA_CMOS_1; //"1"
for(j=0;j<100;j++);
SCL_CMOS_1;
for(j=0;j<100;j++);
SCL_CMOS_0;
for(j=0;j<100;j++);
}
else
{
SDA_CMOS_0;