This is the device firmware for the imagingBoard on the DIY 3D Printable Raspberry Pi Raman Spectrometer. For more details please visit: http://hackaday.io/project/1279
main.cpp@1:a220fd937508, 2014-08-17 (annotated)
- Committer:
- flatcat
- Date:
- Sun Aug 17 16:33:40 2014 +0000
- Revision:
- 1:a220fd937508
- Parent:
- 0:984447b91a04
- Child:
- 2:7751080fb267
Total re-write.... Changed to triggered ICG ; Works much better..!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
flatcat | 0:984447b91a04 | 1 | #include "mbed.h" |
flatcat | 0:984447b91a04 | 2 | |
flatcat | 0:984447b91a04 | 3 | // *** BE SURE TO TEST BEFORE AND AFTER FOR 'IDLING'... CLEAR THE CCD EVERY OTHER FRAME TO TRY AND ELIMINATE NOISE BUILDUP |
flatcat | 0:984447b91a04 | 4 | //From http://www.ing.iac.es/~docs/ins/das/ins-das-29/integration.html |
flatcat | 0:984447b91a04 | 5 | //"Idling" vs. integrating |
flatcat | 0:984447b91a04 | 6 | //Charge from light leaks and thermal noise builds up on the detector between observations. If this charge is integrated by the detector, it may not be completely |
flatcat | 0:984447b91a04 | 7 | //cleared away by the clear cycle of the next observation. In that case, the observation will be contaminated by extra counts. (Often, this appears as a ramp in the |
flatcat | 0:984447b91a04 | 8 | //background leading up to a saturated region in the low-numbered rows.) |
flatcat | 0:984447b91a04 | 9 | //To avoid this problem, the detector is made to clear itself continuously between observations. This is called "idling", and is reported as such on the mimic. |
flatcat | 0:984447b91a04 | 10 | |
flatcat | 1:a220fd937508 | 11 | PwmOut masterClock(PB_4); |
flatcat | 0:984447b91a04 | 12 | PwmOut shiftGate(PB_8); |
flatcat | 1:a220fd937508 | 13 | InterruptIn shiftGate_int(PC_6); |
flatcat | 1:a220fd937508 | 14 | //PwmOut icg(PB_3); |
flatcat | 1:a220fd937508 | 15 | DigitalOut ICG(PB_3); |
flatcat | 1:a220fd937508 | 16 | //interruptIn icg_int(PB_3); |
flatcat | 0:984447b91a04 | 17 | AnalogIn imageIn(A0); |
flatcat | 1:a220fd937508 | 18 | //AnalogOut imageOut(A5); |
flatcat | 1:a220fd937508 | 19 | DigitalOut LED(LED1); |
flatcat | 0:984447b91a04 | 20 | Serial raspi(USBTX, USBRX); |
flatcat | 0:984447b91a04 | 21 | |
flatcat | 1:a220fd937508 | 22 | int masterFreq_period = 2; //microseconds |
flatcat | 1:a220fd937508 | 23 | int masterFreq_width = 1; //microseconds |
flatcat | 1:a220fd937508 | 24 | int shiftGate_period = 200; //microseconds |
flatcat | 1:a220fd937508 | 25 | int shiftGate_width = 100; //microseconds |
flatcat | 1:a220fd937508 | 26 | |
flatcat | 1:a220fd937508 | 27 | int veryLow = 1; |
flatcat | 1:a220fd937508 | 28 | int low = 100; |
flatcat | 1:a220fd937508 | 29 | int medium = 100000; |
flatcat | 1:a220fd937508 | 30 | int high = 1000000; |
flatcat | 1:a220fd937508 | 31 | int veryHigh = 10000000; |
flatcat | 1:a220fd937508 | 32 | |
flatcat | 1:a220fd937508 | 33 | int sensitivity = medium; |
flatcat | 1:a220fd937508 | 34 | int pixelTotal = 3694; |
flatcat | 1:a220fd937508 | 35 | int leadingDummyElements = 16; |
flatcat | 1:a220fd937508 | 36 | int leadShieldedElements = 13; |
flatcat | 1:a220fd937508 | 37 | int headerElements = 3; |
flatcat | 1:a220fd937508 | 38 | const int signalElements = 3648; |
flatcat | 1:a220fd937508 | 39 | int trailingDummyElements = 14; |
flatcat | 1:a220fd937508 | 40 | int pixelCount; |
flatcat | 1:a220fd937508 | 41 | int readOutTrigger; |
flatcat | 1:a220fd937508 | 42 | int state; |
flatcat | 1:a220fd937508 | 43 | |
flatcat | 1:a220fd937508 | 44 | #define readOut_Begin 1 |
flatcat | 1:a220fd937508 | 45 | #define readOut_ACTIVE 2 |
flatcat | 1:a220fd937508 | 46 | #define readOut_LeadingDummy 3 |
flatcat | 1:a220fd937508 | 47 | #define readOut_LeadingShielded 4 |
flatcat | 1:a220fd937508 | 48 | #define readOut_headerElements 5 |
flatcat | 1:a220fd937508 | 49 | #define readOut_signalElements 6 |
flatcat | 1:a220fd937508 | 50 | #define readOut_trailingDummy 7 |
flatcat | 1:a220fd937508 | 51 | #define readOut_integrationTime 8 |
flatcat | 1:a220fd937508 | 52 | #define readOut_IDLE 9 |
flatcat | 1:a220fd937508 | 53 | #define readOut_Finish 0 |
flatcat | 0:984447b91a04 | 54 | |
flatcat | 0:984447b91a04 | 55 | #define MV(x) ((0xFFF*x)/3300) |
flatcat | 0:984447b91a04 | 56 | |
flatcat | 1:a220fd937508 | 57 | float pixelValue[signalElements]; |
flatcat | 1:a220fd937508 | 58 | |
flatcat | 1:a220fd937508 | 59 | void error() |
flatcat | 1:a220fd937508 | 60 | { |
flatcat | 1:a220fd937508 | 61 | while(1) { |
flatcat | 1:a220fd937508 | 62 | LED = !LED; |
flatcat | 1:a220fd937508 | 63 | wait(0.5); |
flatcat | 1:a220fd937508 | 64 | } |
flatcat | 1:a220fd937508 | 65 | } |
flatcat | 1:a220fd937508 | 66 | |
flatcat | 1:a220fd937508 | 67 | void checkState() |
flatcat | 1:a220fd937508 | 68 | { |
flatcat | 1:a220fd937508 | 69 | if (readOutTrigger == 1) { |
flatcat | 1:a220fd937508 | 70 | // state = readOut_LeadingDummy; |
flatcat | 1:a220fd937508 | 71 | } |
flatcat | 1:a220fd937508 | 72 | switch (state) { |
flatcat | 1:a220fd937508 | 73 | case readOut_Begin: |
flatcat | 1:a220fd937508 | 74 | readOutTrigger = 1; |
flatcat | 1:a220fd937508 | 75 | state = readOut_ACTIVE; |
flatcat | 1:a220fd937508 | 76 | // ICG = 1; |
flatcat | 1:a220fd937508 | 77 | LED = 1; |
flatcat | 1:a220fd937508 | 78 | break; |
flatcat | 1:a220fd937508 | 79 | case readOut_ACTIVE: |
flatcat | 1:a220fd937508 | 80 | ICG = 1; |
flatcat | 1:a220fd937508 | 81 | state = readOut_LeadingDummy; |
flatcat | 1:a220fd937508 | 82 | break; |
flatcat | 1:a220fd937508 | 83 | case readOut_LeadingDummy: |
flatcat | 1:a220fd937508 | 84 | pixelCount++; |
flatcat | 1:a220fd937508 | 85 | if (pixelCount == leadingDummyElements) { |
flatcat | 1:a220fd937508 | 86 | pixelCount = 0; |
flatcat | 1:a220fd937508 | 87 | state = readOut_LeadingShielded; |
flatcat | 1:a220fd937508 | 88 | } |
flatcat | 1:a220fd937508 | 89 | break; |
flatcat | 1:a220fd937508 | 90 | case readOut_LeadingShielded: |
flatcat | 1:a220fd937508 | 91 | pixelCount++; |
flatcat | 1:a220fd937508 | 92 | if (pixelCount == leadShieldedElements) { |
flatcat | 1:a220fd937508 | 93 | pixelCount = 0; |
flatcat | 1:a220fd937508 | 94 | state = readOut_headerElements; |
flatcat | 1:a220fd937508 | 95 | } |
flatcat | 1:a220fd937508 | 96 | break; |
flatcat | 1:a220fd937508 | 97 | case readOut_headerElements: |
flatcat | 1:a220fd937508 | 98 | pixelCount++; |
flatcat | 1:a220fd937508 | 99 | if (pixelCount == headerElements) { |
flatcat | 1:a220fd937508 | 100 | pixelCount = 0; |
flatcat | 1:a220fd937508 | 101 | state = readOut_signalElements; |
flatcat | 1:a220fd937508 | 102 | } |
flatcat | 1:a220fd937508 | 103 | break; |
flatcat | 1:a220fd937508 | 104 | case readOut_signalElements: |
flatcat | 1:a220fd937508 | 105 | pixelCount++; |
flatcat | 1:a220fd937508 | 106 | // pixelValue[pixelCount] = imageIn.read(); |
flatcat | 1:a220fd937508 | 107 | LED = !LED; |
flatcat | 1:a220fd937508 | 108 | if (pixelCount == signalElements) { |
flatcat | 1:a220fd937508 | 109 | pixelCount = 0; |
flatcat | 1:a220fd937508 | 110 | state = readOut_trailingDummy; |
flatcat | 1:a220fd937508 | 111 | } |
flatcat | 1:a220fd937508 | 112 | break; |
flatcat | 1:a220fd937508 | 113 | case readOut_trailingDummy: |
flatcat | 1:a220fd937508 | 114 | pixelCount++; |
flatcat | 1:a220fd937508 | 115 | if (pixelCount == trailingDummyElements) { |
flatcat | 1:a220fd937508 | 116 | pixelCount = 0; |
flatcat | 1:a220fd937508 | 117 | state = readOut_integrationTime; |
flatcat | 1:a220fd937508 | 118 | } |
flatcat | 1:a220fd937508 | 119 | break; |
flatcat | 1:a220fd937508 | 120 | case readOut_integrationTime: |
flatcat | 1:a220fd937508 | 121 | if (ICG == 1) { |
flatcat | 1:a220fd937508 | 122 | ICG = 0; |
flatcat | 1:a220fd937508 | 123 | wait_us(sensitivity); |
flatcat | 1:a220fd937508 | 124 | state = readOut_Finish; |
flatcat | 1:a220fd937508 | 125 | } |
flatcat | 1:a220fd937508 | 126 | break; |
flatcat | 1:a220fd937508 | 127 | case readOut_Finish: |
flatcat | 1:a220fd937508 | 128 | state = readOut_IDLE; |
flatcat | 1:a220fd937508 | 129 | LED = 0; |
flatcat | 1:a220fd937508 | 130 | ICG = 1; |
flatcat | 1:a220fd937508 | 131 | break; |
flatcat | 1:a220fd937508 | 132 | case readOut_IDLE: |
flatcat | 1:a220fd937508 | 133 | if (ICG == 1) { |
flatcat | 1:a220fd937508 | 134 | ICG = 0; |
flatcat | 1:a220fd937508 | 135 | state = readOut_Begin; |
flatcat | 1:a220fd937508 | 136 | } |
flatcat | 1:a220fd937508 | 137 | break; |
flatcat | 1:a220fd937508 | 138 | default: |
flatcat | 1:a220fd937508 | 139 | break; |
flatcat | 1:a220fd937508 | 140 | } |
flatcat | 1:a220fd937508 | 141 | } |
flatcat | 1:a220fd937508 | 142 | |
flatcat | 0:984447b91a04 | 143 | int main() |
flatcat | 0:984447b91a04 | 144 | { |
flatcat | 1:a220fd937508 | 145 | ICG = 1; |
flatcat | 1:a220fd937508 | 146 | LED = 0; |
flatcat | 1:a220fd937508 | 147 | pixelCount = 0; |
flatcat | 1:a220fd937508 | 148 | readOutTrigger = 0; |
flatcat | 1:a220fd937508 | 149 | state = readOut_IDLE; |
flatcat | 1:a220fd937508 | 150 | |
flatcat | 0:984447b91a04 | 151 | masterClock.period_us(masterFreq_period); |
flatcat | 0:984447b91a04 | 152 | masterClock.pulsewidth_us(masterFreq_width); |
flatcat | 0:984447b91a04 | 153 | |
flatcat | 0:984447b91a04 | 154 | shiftGate.period_us(shiftGate_period); |
flatcat | 0:984447b91a04 | 155 | shiftGate.pulsewidth_us(shiftGate_width); |
flatcat | 0:984447b91a04 | 156 | |
flatcat | 1:a220fd937508 | 157 | wait(0.5); |
flatcat | 1:a220fd937508 | 158 | |
flatcat | 1:a220fd937508 | 159 | shiftGate_int.rise(checkState); |
flatcat | 1:a220fd937508 | 160 | |
flatcat | 0:984447b91a04 | 161 | raspi.baud(921600); |
flatcat | 0:984447b91a04 | 162 | while(1) { |
flatcat | 1:a220fd937508 | 163 | wait(1); |
flatcat | 0:984447b91a04 | 164 | } |
flatcat | 0:984447b91a04 | 165 | } |