al422b

Dependencies:   BufferedSerial

Files at this revision

API Documentation at this revision

Comitter:
kangmingyo
Date:
Thu Sep 05 13:04:01 2019 +0000
Parent:
6:fe8b32cb9357
Child:
8:221b2fc093e4
Commit message:
0905;

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
ov7670.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Aug 13 07:51:37 2019 +0000
+++ b/main.cpp	Thu Sep 05 13:04:01 2019 +0000
@@ -12,22 +12,24 @@
 ros::Publisher img_pub("image_data", &img);
 
 
+
 OV7670 camera(
     D14,D15,       // SDA,SCL(I2C / SCCB)
     D9,D8,D11,   // VSYNC,HREF,WEN(FIFO)
     //p18,p17,p16,p15,p11,p12,p14,p13, // D7-D0
-    PortB,0xFF,
-    D13,D12,D6) ; // RRST,OE,RCLK
+    PortB,0x00FF,
+    D13,D12,D6,D2) ; // RRST,OE,RCLK,WRST
 
 //Serial pc(USBTX,USBRX,115200) ;
-Timer t;
+
 
 //#undef QQVGA
 #define QQVGA
 
 #ifdef QQVGA
-# define SIZEX (12)
-# define SIZEY (8)
+# define SIZEX (160)
+# define SIZEY (120)
+# define NUMBYTE (2)
 #else
 # define SIZEX (320)
 # define SIZEY (240)
@@ -35,11 +37,15 @@
 
 
 uint8_t fdata[SIZEX*SIZEY];
+uint8_t fdata_ros[480];
+
+uint8_t dummy;
 
 size_t size = SIZEX*SIZEY;
 
-int main() {
-    int last ;
+int main()
+{
+    wait_ms(1);
 //    pc.baud(115200) ;
     camera.Reset() ;
     nh.advertise(img_pub);
@@ -48,43 +54,42 @@
 //    camera.InitQQVGA565(true,false) ;
     camera.InitQQVGAYUV(false,false);
 
-#else 
+#else
     //camera.InitQVGA565(true,false) ;
 #endif
-    
-    
+
+
     // CAPTURE and SEND LOOP
-    t.start();
-    last = t.read_ms() ;
-    
-    
-    while(1)
-    {
-        
-        img.data_length = size;
-        
+
+
+    while(1) {
+
+            
         camera.CaptureNext() ;
         while(camera.CaptureDone() == false) ;
 
-        last = t.read_ms() ;
         camera.ReadStart() ;
-        
-        
+
         for (int y = 0; y < SIZEX*SIZEY ; y++) {
-                
             camera.ReadOneByte();
             fdata[y]=camera.ReadOneByte();
-
         }
         
-        camera.ReadStop() ; 
-        last = t.read_ms() ;
+        camera.ReadStop() ;
+        
+       for(int x=0; x<40; x++) {
+            for(int y=0; y<480; y++) {
+                fdata_ros[y]=fdata[(480*(x))+y];
+            }
         
-        img.data = &fdata[0]; 
-        img_pub.publish(&img);
-        
+            img.data_length = 480;
+            img.data = &fdata_ros[0];
+            img_pub.publish(&img);
+            nh.spinOnce();
+            wait_ms(600); //max...
+       }
 
-        nh.spinOnce();
-        wait_ms(1); //max...
+
+       
     }
 }
--- a/ov7670.h	Tue Aug 13 07:51:37 2019 +0000
+++ b/ov7670.h	Thu Sep 05 13:04:01 2019 +0000
@@ -6,11 +6,11 @@
 #define OV7670_WRITEWAIT (20)
 #define OV7670_NOACK (0)
 #define OV7670_REGMAX (201)
-#define OV7670_I2CFREQ (40000)
+#define OV7670_I2CFREQ (10000)
 
 //
 // OV7670 + FIFO AL422B camera board test
-//
+//wen =1일 때 read시작 wen = 0일때 안읽음
 class OV7670
 {
 public:
@@ -18,7 +18,7 @@
     InterruptIn vsync,href;
     DigitalOut wen ;
     PortIn data ;
-    DigitalOut rrst,oe,rclk ;
+    DigitalOut rrst,oe,rclk,wrst ;
     volatile int LineCounter ;
     volatile int LastLines ;
     volatile bool CaptureReq ;
@@ -35,18 +35,20 @@
         int mask, // 0b0000_0M65_4000_0321_L000_0000_0000_0000 = 0x07878000
         PinName rt, // /RRST
         PinName o,  // /OE
-        PinName rc  // RCLK
-    ) : _i2c(sda,scl),vsync(vs),href(hr),wen(we),data(port,mask),rrst(rt),oe(o),rclk(rc)
+        PinName rc,
+        PinName wt  // RCLK
+    ) : _i2c(sda,scl),vsync(vs),href(hr),wen(we),data(port,mask),rrst(rt),oe(o),rclk(rc),wrst(wt)
     {
         _i2c.stop() ;
         _i2c.frequency(OV7670_I2CFREQ) ;
         vsync.fall(this,&OV7670::VsyncHandler) ;
-        href.rise(this,&OV7670::HrefHandler) ;
+//        vsync.rise(this,&OV7670::VsyncHandler) ;
         CaptureReq = false ;
         Busy = false ;
         Done = false ;
         LineCounter = 0 ;
         rrst = 1 ;
+ 
         oe = 1 ;
         rclk = 1 ;
         wen = 0 ;
@@ -75,30 +77,31 @@
     // write to camera
     void WriteReg(int addr,int data)
     {
-     int status;
-     char data_write[2];
-    data_write[0]= addr;
-    data_write[1]= data;
-    status = _i2c.write(OV7670_WRITE,data_write,2,0);
-    if(status!=0){
-//        printf("I2C configuration error(adr: %x)!\r\n",addr);
-        while(1){
+        wait_us(10);
+        int status;
+        char data_write[2];
+        data_write[0]= addr;
+        data_write[1]= data;
+        status = _i2c.write(OV7670_WRITE,data_write,2,0);
+        if(status!=0) {
+            printf("I2C configuration error(adr: %x)!\r\n",addr);
+            while(1) {
+            }
         }
     }
-    }
 
     // read from camera
     int ReadReg(int addr)
     {
-       char data_write[2];
-    char data_read[2];
-    int data;
-    int status;
-   
-    data_write[0]=addr;
-    _i2c.write(OV7670_WRITE, data_write, 1, 0);
-    status =_i2c.read(OV7670_READ, data_read, 1, 0);
-    data = (int)data_read[0];
+        char data_write[2];
+        char data_read[2];
+        int data;
+        int status;
+
+        data_write[0]=addr;
+        _i2c.write(OV7670_WRITE, data_write, 1, 0);
+        status =_i2c.read(OV7670_READ, data_read, 1, 0);
+        data = (int)data_read[0];
 //    printf("Data is %x\r\n",data);
 //    printf("Status is %d\r\n",status);
         return data ;
@@ -106,15 +109,18 @@
 
     void Reset(void)
     {
+
         WriteReg(0x12,0x80) ; // RESET CAMERA
         wait_ms(200) ;
+        wait_us(1) ;
+
     }
 
     void InitQQVGA565(bool flipv,bool fliph)
     {
         // QQVGA RGB565
         WriteReg(REG_CLKRC,0x80);
-        WriteReg(REG_COM11,0x0A) ;          
+        WriteReg(REG_COM11,0x0A) ;
         WriteReg(REG_TSLB,0x04);
         WriteReg(REG_COM7,0x04) ;
         WriteReg(REG_RGB444, 0x00);
@@ -206,37 +212,35 @@
         WriteReg(0xb0,0x84);
     }
     void  InitQQVGAYUV(bool flipv,bool fliph)
-    { 
-        WriteReg(0x11,0x01);
+    {   
+        WriteReg(REG_COM10,0x02) ;
+//        WriteReg(0x11,0x81);
+        WriteReg(REG_HSTART,0x16) ;
+        WriteReg(REG_HSTOP,0x04) ;
+        WriteReg(REG_HREF,0x24) ;
+        WriteReg(REG_VSTART,0x02) ;
+        WriteReg(REG_VSTOP,0x7a) ;
+        WriteReg(REG_VREF,0x0a) ;
+        WriteReg(REG_COM14, 0x1a);
+        WriteReg(0x72, 0x22);
+        WriteReg(0x73, 0xf2);
+        WriteReg(0x70,0x3A);
+        WriteReg(0x71,0x35);
+        WriteReg(REG_TSLB,0x0C);
+        WriteReg(0xA2,0x02);
+        
+
         WriteReg(REG_COM7, 0x00);           // YUV
         WriteReg(REG_COM17, 0x00);          // color bar disable
         WriteReg(REG_COM3, 0x04);
-        WriteReg(0x12, 0x00);//COM7
-       
+        WriteReg(0x8C, 0x00);//RGB444
+        WriteReg(0x04, 0x00);//COM1
+        WriteReg(0x40, 0xC0);//COM15
         WriteReg(0x14, 0x1A);//COM9
         WriteReg(0x3D, 0x40);//COM13
-        WriteReg(REG_COM15, 0xC0);
-        WriteReg(REG_TSLB,0x0C);
-        WriteReg(REG_COM14, 0x1a);          // divide by 4
-        WriteReg(0x72, 0x22);               // downsample by 4
-        WriteReg(0x73, 0xf2);               // divide by 4
-//        WriteReg(REG_HREF, 0xa4);
-//        WriteReg(REG_HSTART, 0x16);
-//        WriteReg(REG_HSTOP, 0x04);
-//        WriteReg(REG_VREF, 0x0a);   
-//        WriteReg(REG_VSTART, 0x02);
-//        WriteReg(REG_VSTOP, 0x7a); 
-        WriteReg(0x70,0x3A);
-        WriteReg(0x71,0x35);
+    
 
-        WriteReg(0xA2,0x02);
-                  
-       
-       
-       
-       
-       
-         /* END MARKER */    
+        /* END MARKER */
     }
 
 
@@ -245,12 +249,15 @@
     {
         // Capture Enable
         if (CaptureReq) {
+            wrst=0;
+            wait_us(1);
             wen = 1 ;
+            wrst=1;
             Done = false ;
             CaptureReq = false ;
         } else {
             wen = 0 ;
-            if (Busy) {
+            if (Busy) { //next exit
                 Busy = false ;
                 Done = true ;
             }
@@ -272,8 +279,8 @@
     {
         int result ;
         rclk = 1 ;
-//        wait_us(1) ;
-        result = data ;
+        wait_us(1);
+        result = data.read() ;
         rclk = 0 ;
         return result ;
     }
@@ -299,29 +306,36 @@
 
         int r,r1,r2;
         rclk=1;
-        r = data;
+        wait_us(1);
+        r = data.read();
         rclk=0;
-        r1 = r<<8;
+//        wait_us(50);
+        r1 = r;
+        wait_us(1);
         rclk=1;
-        r= data;
+        wait_us(1);
+        r= data.read();
         rclk=0;
         r2 = r;
 
-
-        return r2+r1 ;
+//        wait_us(50);
+      return r2 ;
     }
 
     // Data Start
     void ReadStart(void)
     {
-        rrst = 0 ;
+        rrst=0;
         oe = 0 ;
-        wait_us(1) ;
         rclk = 0 ;
+        wait_us(1); 
+        rclk = 1 ; 
         wait_us(1) ;
-        rclk = 1 ;
-        wait_us(1) ;
-        rrst = 1 ;
+        rclk = 0 ; 
+        rrst=1;
+
+//        wait_us(1);
+        
     }
 
     // Data Stop
@@ -332,3 +346,4 @@
         rclk = 1 ;
     }
 };
+ 
\ No newline at end of file