![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Communication between TSL3301 and F7 board
main.cpp@0:96ad87d71144, 2018-03-06 (annotated)
- Committer:
- codebreaker7
- Date:
- Tue Mar 06 12:50:47 2018 +0000
- Revision:
- 0:96ad87d71144
- Child:
- 1:979d4820e32a
TSL3301 project for F7 Discovery
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
codebreaker7 | 0:96ad87d71144 | 1 | #include "mbed.h" |
codebreaker7 | 0:96ad87d71144 | 2 | |
codebreaker7 | 0:96ad87d71144 | 3 | DigitalIn miso(D8); |
codebreaker7 | 0:96ad87d71144 | 4 | DigitalOut mosi(D7); |
codebreaker7 | 0:96ad87d71144 | 5 | DigitalOut clk(D6); |
codebreaker7 | 0:96ad87d71144 | 6 | |
codebreaker7 | 0:96ad87d71144 | 7 | unsigned char lcam_buffer[102]; |
codebreaker7 | 0:96ad87d71144 | 8 | |
codebreaker7 | 0:96ad87d71144 | 9 | void lcam_pulse(void) { |
codebreaker7 | 0:96ad87d71144 | 10 | clk = 1; |
codebreaker7 | 0:96ad87d71144 | 11 | clk = 0; |
codebreaker7 | 0:96ad87d71144 | 12 | } |
codebreaker7 | 0:96ad87d71144 | 13 | |
codebreaker7 | 0:96ad87d71144 | 14 | void lcam_pulse_clock(uint8_t times){ |
codebreaker7 | 0:96ad87d71144 | 15 | for(unsigned char i = 0; i < times; i++) |
codebreaker7 | 0:96ad87d71144 | 16 | { |
codebreaker7 | 0:96ad87d71144 | 17 | lcam_pulse(); |
codebreaker7 | 0:96ad87d71144 | 18 | } |
codebreaker7 | 0:96ad87d71144 | 19 | } |
codebreaker7 | 0:96ad87d71144 | 20 | |
codebreaker7 | 0:96ad87d71144 | 21 | void lcam_send(uint8_t value){ |
codebreaker7 | 0:96ad87d71144 | 22 | // Start bit |
codebreaker7 | 0:96ad87d71144 | 23 | mosi = 0; |
codebreaker7 | 0:96ad87d71144 | 24 | lcam_pulse(); |
codebreaker7 | 0:96ad87d71144 | 25 | // Send 8 bits to camera |
codebreaker7 | 0:96ad87d71144 | 26 | for(unsigned char i = 0; i < 8; i++) |
codebreaker7 | 0:96ad87d71144 | 27 | { |
codebreaker7 | 0:96ad87d71144 | 28 | mosi = ((value >> i) & 1); |
codebreaker7 | 0:96ad87d71144 | 29 | lcam_pulse(); |
codebreaker7 | 0:96ad87d71144 | 30 | } |
codebreaker7 | 0:96ad87d71144 | 31 | // Stop bit |
codebreaker7 | 0:96ad87d71144 | 32 | mosi = 1; |
codebreaker7 | 0:96ad87d71144 | 33 | lcam_pulse(); |
codebreaker7 | 0:96ad87d71144 | 34 | } |
codebreaker7 | 0:96ad87d71144 | 35 | |
codebreaker7 | 0:96ad87d71144 | 36 | void lcam_reset(void){ |
codebreaker7 | 0:96ad87d71144 | 37 | wait_ms(1); |
codebreaker7 | 0:96ad87d71144 | 38 | |
codebreaker7 | 0:96ad87d71144 | 39 | // 10 clock impulsions with SDIN held high to clear the receiver logic |
codebreaker7 | 0:96ad87d71144 | 40 | mosi = 1; |
codebreaker7 | 0:96ad87d71144 | 41 | lcam_pulse_clock(10); |
codebreaker7 | 0:96ad87d71144 | 42 | // 3 reset instructions to clear the control logic |
codebreaker7 | 0:96ad87d71144 | 43 | lcam_send(0x1b); |
codebreaker7 | 0:96ad87d71144 | 44 | lcam_send(0x1b); |
codebreaker7 | 0:96ad87d71144 | 45 | lcam_send(0x1b); |
codebreaker7 | 0:96ad87d71144 | 46 | // 30 clock impulsions to assure the state of SDOUT |
codebreaker7 | 0:96ad87d71144 | 47 | lcam_pulse_clock(30); |
codebreaker7 | 0:96ad87d71144 | 48 | |
codebreaker7 | 0:96ad87d71144 | 49 | // register write mode |
codebreaker7 | 0:96ad87d71144 | 50 | lcam_send(0X5F); |
codebreaker7 | 0:96ad87d71144 | 51 | // Clear mode register (single chip, not sleep) |
codebreaker7 | 0:96ad87d71144 | 52 | lcam_send(0x00); |
codebreaker7 | 0:96ad87d71144 | 53 | } |
codebreaker7 | 0:96ad87d71144 | 54 | |
codebreaker7 | 0:96ad87d71144 | 55 | void lcam_setup(void){ |
codebreaker7 | 0:96ad87d71144 | 56 | lcam_reset(); |
codebreaker7 | 0:96ad87d71144 | 57 | |
codebreaker7 | 0:96ad87d71144 | 58 | // set inputs and outputs |
codebreaker7 | 0:96ad87d71144 | 59 | //clk = 1; |
codebreaker7 | 0:96ad87d71144 | 60 | //pin_mode(LCAM_PORT, LCAM_SDIN, 1); |
codebreaker7 | 0:96ad87d71144 | 61 | //pin_mode(LCAM_PORT, LCAM_SDOUT, 0); |
codebreaker7 | 0:96ad87d71144 | 62 | // make sure the clock is cleared |
codebreaker7 | 0:96ad87d71144 | 63 | clk = 0; |
codebreaker7 | 0:96ad87d71144 | 64 | |
codebreaker7 | 0:96ad87d71144 | 65 | // Left offset |
codebreaker7 | 0:96ad87d71144 | 66 | lcam_send(0x40); |
codebreaker7 | 0:96ad87d71144 | 67 | lcam_send(0); |
codebreaker7 | 0:96ad87d71144 | 68 | // Left gain |
codebreaker7 | 0:96ad87d71144 | 69 | lcam_send(0x41); |
codebreaker7 | 0:96ad87d71144 | 70 | lcam_send(15); |
codebreaker7 | 0:96ad87d71144 | 71 | // Middle offset |
codebreaker7 | 0:96ad87d71144 | 72 | lcam_send(0x42); |
codebreaker7 | 0:96ad87d71144 | 73 | lcam_send(0); |
codebreaker7 | 0:96ad87d71144 | 74 | // Middle gain |
codebreaker7 | 0:96ad87d71144 | 75 | lcam_send(0x43); |
codebreaker7 | 0:96ad87d71144 | 76 | lcam_send(15); |
codebreaker7 | 0:96ad87d71144 | 77 | // Right offset |
codebreaker7 | 0:96ad87d71144 | 78 | lcam_send(0x44); |
codebreaker7 | 0:96ad87d71144 | 79 | lcam_send(0); |
codebreaker7 | 0:96ad87d71144 | 80 | // Right gain |
codebreaker7 | 0:96ad87d71144 | 81 | lcam_send(0x45); |
codebreaker7 | 0:96ad87d71144 | 82 | lcam_send(15); |
codebreaker7 | 0:96ad87d71144 | 83 | } |
codebreaker7 | 0:96ad87d71144 | 84 | |
codebreaker7 | 0:96ad87d71144 | 85 | void lcam_startintegration(void){ |
codebreaker7 | 0:96ad87d71144 | 86 | // Send start integration command |
codebreaker7 | 0:96ad87d71144 | 87 | lcam_send(0x08); |
codebreaker7 | 0:96ad87d71144 | 88 | // delayed until the pixel reset cycle has been completed (22-clock delay) |
codebreaker7 | 0:96ad87d71144 | 89 | lcam_pulse_clock(22); |
codebreaker7 | 0:96ad87d71144 | 90 | } |
codebreaker7 | 0:96ad87d71144 | 91 | |
codebreaker7 | 0:96ad87d71144 | 92 | void lcam_endintegration(void){ |
codebreaker7 | 0:96ad87d71144 | 93 | // Sample int command |
codebreaker7 | 0:96ad87d71144 | 94 | lcam_send(0x10); |
codebreaker7 | 0:96ad87d71144 | 95 | // pixel reset sequence is initiated, requires 22 clocks |
codebreaker7 | 0:96ad87d71144 | 96 | lcam_pulse_clock(22); |
codebreaker7 | 0:96ad87d71144 | 97 | } |
codebreaker7 | 0:96ad87d71144 | 98 | |
codebreaker7 | 0:96ad87d71144 | 99 | void lcam_integrate(unsigned int microseconds){ |
codebreaker7 | 0:96ad87d71144 | 100 | lcam_startintegration(); |
codebreaker7 | 0:96ad87d71144 | 101 | wait_us(microseconds); |
codebreaker7 | 0:96ad87d71144 | 102 | lcam_endintegration(); |
codebreaker7 | 0:96ad87d71144 | 103 | } |
codebreaker7 | 0:96ad87d71144 | 104 | |
codebreaker7 | 0:96ad87d71144 | 105 | unsigned char* lcam_getdata(void){ |
codebreaker7 | 0:96ad87d71144 | 106 | return lcam_buffer; |
codebreaker7 | 0:96ad87d71144 | 107 | } |
codebreaker7 | 0:96ad87d71144 | 108 | |
codebreaker7 | 0:96ad87d71144 | 109 | void lcam_read(void){ |
codebreaker7 | 0:96ad87d71144 | 110 | unsigned char i, pixel_bit, pixel; |
codebreaker7 | 0:96ad87d71144 | 111 | // Read pixel command |
codebreaker7 | 0:96ad87d71144 | 112 | lcam_send(0x02); |
codebreaker7 | 0:96ad87d71144 | 113 | // 44-clock cycle delay until the first pixel data is output |
codebreaker7 | 0:96ad87d71144 | 114 | lcam_pulse_clock(44); |
codebreaker7 | 0:96ad87d71144 | 115 | // Read the 102 pixels from the camera |
codebreaker7 | 0:96ad87d71144 | 116 | for(i = 0; i < 102; i++) |
codebreaker7 | 0:96ad87d71144 | 117 | { |
codebreaker7 | 0:96ad87d71144 | 118 | pixel = 0; |
codebreaker7 | 0:96ad87d71144 | 119 | // pulse the pixel start bit (SDOUT = 0) |
codebreaker7 | 0:96ad87d71144 | 120 | lcam_pulse(); |
codebreaker7 | 0:96ad87d71144 | 121 | // read a byte, bit by bit |
codebreaker7 | 0:96ad87d71144 | 122 | for(pixel_bit = 0; pixel_bit < 8; pixel_bit++) |
codebreaker7 | 0:96ad87d71144 | 123 | { |
codebreaker7 | 0:96ad87d71144 | 124 | clk = 1; |
codebreaker7 | 0:96ad87d71144 | 125 | // read pin while clock is at 1 |
codebreaker7 | 0:96ad87d71144 | 126 | pixel |= (((miso) & 1) << pixel_bit); |
codebreaker7 | 0:96ad87d71144 | 127 | clk = 0; |
codebreaker7 | 0:96ad87d71144 | 128 | } |
codebreaker7 | 0:96ad87d71144 | 129 | // store byte to buffer |
codebreaker7 | 0:96ad87d71144 | 130 | lcam_buffer[i] = pixel; |
codebreaker7 | 0:96ad87d71144 | 131 | // pulse the pixel stop bit (SDOUT = 1) |
codebreaker7 | 0:96ad87d71144 | 132 | lcam_pulse(); |
codebreaker7 | 0:96ad87d71144 | 133 | } |
codebreaker7 | 0:96ad87d71144 | 134 | } |
codebreaker7 | 0:96ad87d71144 | 135 | |
codebreaker7 | 0:96ad87d71144 | 136 | unsigned char lcam_getpic(void){ |
codebreaker7 | 0:96ad87d71144 | 137 | |
codebreaker7 | 0:96ad87d71144 | 138 | } |
codebreaker7 | 0:96ad87d71144 | 139 | |
codebreaker7 | 0:96ad87d71144 | 140 | int main() { |
codebreaker7 | 0:96ad87d71144 | 141 | while(1) { |
codebreaker7 | 0:96ad87d71144 | 142 | |
codebreaker7 | 0:96ad87d71144 | 143 | } |
codebreaker7 | 0:96ad87d71144 | 144 | } |