CatPot 2015-2016
/
Pr034_S11059
S11059-02DT
Diff: main.cpp
- Revision:
- 0:e2155c63b32b
- Child:
- 1:eabfa27c9374
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Apr 10 14:17:59 2015 +0000 @@ -0,0 +1,139 @@ +#include "mbed.h" + +DigitalOut Led(LED1); +DigitalOut Val(LED2); +Serial pc(USBTX,USBRX); +I2C Sensor(p9,p10); + +#define DEVICE_ADDRESS 0x2A +#define CONTROL_REG 0x00 +#define TIMING_REG_H 0x01 +#define TIMING_REG_L 0x02 + +uint16_t red = 0; +uint16_t green = 0; +uint16_t blue = 0; +uint16_t IR = 0; + +char address=DEVICE_ADDRESS; +char cmd[3]={ + CONTROL_REG + ,TIMING_REG_H + ,TIMING_REG_L + }; +char ord[2]={}; +char data[2]={}; +char data_h[1]={}; +char data_l[1]={}; + +void led_blink(){ + Led=1; + wait_ms(10); + Led=0; +} + +void setManualtGain(uint16_t ex_time) +{ + char reg[1]={0}; + + reg[0] =(ex_time >>= 8); + Sensor.write(address, reg, 1); + reg[0] |= ex_time; + Sensor.write(address, reg, 1); +} + +void getRGB() +{ + + /* //manual exposure mode + + setManualtGain(100); + + ord[0]=cmd[0]; + Sensor.write(address, ord, 1); + + ord[0]=0x84;// ADC reset LowGain wakeup + Sensor.write(address, ord, 1); + + ord[0]=cmd[0]; + Sensor.write(address, ord, 1); + + ord[0]=0x04;//start + Sensor.write(address, ord, 1); + + */ + + //preset gain mode exposure time (each color ch) + // 00 = 87.5us + // 01= 1.4ms + // 10= 22.4ms + // 11= 179.2ms + + + Sensor.start(); + + ord[0]=0x00; + ord[1]=0x84; + Val = !Sensor.write(address&0xFE, ord, 2, true); + ord[0]=0x00; + ord[1]=0x04; + Val = !Sensor.write(address&0xFE, ord, 2, false); + + + wait_ms(180*4); //total exposure time + + led_blink(); + + + Sensor.start(); + + ord[0]=0x03; + Val = !Sensor.write(address&0xFE, ord, 1, true); + + Val = !Sensor.write(address|0x01); + + //read data + + data_h[0] = Sensor.read(false); + data_l[0] = Sensor.read(false); + + red = (uint8_t)(data_h[0])<<8 | (uint8_t)(data_l[0]); + + data_h[0] = Sensor.read(false); + data_l[0] = Sensor.read(false); + + green = (uint8_t)(data_h[0])<<8 | (uint8_t)(data_l[0]); + + data_h[0] = Sensor.read(false); + data_l[0] = Sensor.read(false); + + blue = (uint8_t)(data_h[0])<<8 | (uint8_t)(data_l[0]); + + data_h[0] = Sensor.read(false); + data_l[0] = Sensor.read(true); + + IR = (uint8_t)(data_h[0])<<8 | (uint8_t)(data_l[0]); + + Sensor.stop(); + + return; +} + +int main() +{ + Sensor.frequency(10000); + Led=0; + led_blink(); + + while(1) + { + getRGB(); + + pc.printf("Red = %4d, Green = %4d, Blue = %4d\n" + ,red + ,green + ,blue + ); + wait_ms(500); + } +}