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

Dependencies:   mbed

Committer:
flatcat
Date:
Mon Aug 18 02:00:24 2014 +0000
Revision:
2:7751080fb267
Parent:
1:a220fd937508
Child:
3:83cb6eb61adf
Dumps to serial.. but the whole thing is waaay too slow..

Who changed what in which revision?

UserRevisionLine numberNew 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 2:7751080fb267 33 double imageData;
flatcat 1:a220fd937508 34 int sensitivity = medium;
flatcat 1:a220fd937508 35 int pixelTotal = 3694;
flatcat 1:a220fd937508 36 int leadingDummyElements = 16;
flatcat 1:a220fd937508 37 int leadShieldedElements = 13;
flatcat 1:a220fd937508 38 int headerElements = 3;
flatcat 1:a220fd937508 39 const int signalElements = 3648;
flatcat 1:a220fd937508 40 int trailingDummyElements = 14;
flatcat 1:a220fd937508 41 int pixelCount;
flatcat 1:a220fd937508 42 int readOutTrigger;
flatcat 1:a220fd937508 43 int state;
flatcat 1:a220fd937508 44
flatcat 1:a220fd937508 45 #define readOut_Begin 1
flatcat 1:a220fd937508 46 #define readOut_ACTIVE 2
flatcat 1:a220fd937508 47 #define readOut_LeadingDummy 3
flatcat 1:a220fd937508 48 #define readOut_LeadingShielded 4
flatcat 1:a220fd937508 49 #define readOut_headerElements 5
flatcat 1:a220fd937508 50 #define readOut_signalElements 6
flatcat 1:a220fd937508 51 #define readOut_trailingDummy 7
flatcat 1:a220fd937508 52 #define readOut_integrationTime 8
flatcat 1:a220fd937508 53 #define readOut_IDLE 9
flatcat 1:a220fd937508 54 #define readOut_Finish 0
flatcat 0:984447b91a04 55
flatcat 0:984447b91a04 56 #define MV(x) ((0xFFF*x)/3300)
flatcat 0:984447b91a04 57
flatcat 1:a220fd937508 58 float pixelValue[signalElements];
flatcat 1:a220fd937508 59
flatcat 1:a220fd937508 60 void error()
flatcat 1:a220fd937508 61 {
flatcat 1:a220fd937508 62 while(1) {
flatcat 1:a220fd937508 63 LED = !LED;
flatcat 1:a220fd937508 64 wait(0.5);
flatcat 1:a220fd937508 65 }
flatcat 1:a220fd937508 66 }
flatcat 1:a220fd937508 67
flatcat 1:a220fd937508 68 void checkState()
flatcat 1:a220fd937508 69 {
flatcat 1:a220fd937508 70 if (readOutTrigger == 1) {
flatcat 1:a220fd937508 71 // state = readOut_LeadingDummy;
flatcat 1:a220fd937508 72 }
flatcat 1:a220fd937508 73 switch (state) {
flatcat 1:a220fd937508 74 case readOut_Begin:
flatcat 1:a220fd937508 75 readOutTrigger = 1;
flatcat 1:a220fd937508 76 state = readOut_ACTIVE;
flatcat 1:a220fd937508 77 // ICG = 1;
flatcat 1:a220fd937508 78 LED = 1;
flatcat 1:a220fd937508 79 break;
flatcat 1:a220fd937508 80 case readOut_ACTIVE:
flatcat 1:a220fd937508 81 ICG = 1;
flatcat 1:a220fd937508 82 state = readOut_LeadingDummy;
flatcat 1:a220fd937508 83 break;
flatcat 1:a220fd937508 84 case readOut_LeadingDummy:
flatcat 1:a220fd937508 85 pixelCount++;
flatcat 1:a220fd937508 86 if (pixelCount == leadingDummyElements) {
flatcat 1:a220fd937508 87 pixelCount = 0;
flatcat 1:a220fd937508 88 state = readOut_LeadingShielded;
flatcat 1:a220fd937508 89 }
flatcat 1:a220fd937508 90 break;
flatcat 1:a220fd937508 91 case readOut_LeadingShielded:
flatcat 1:a220fd937508 92 pixelCount++;
flatcat 1:a220fd937508 93 if (pixelCount == leadShieldedElements) {
flatcat 1:a220fd937508 94 pixelCount = 0;
flatcat 1:a220fd937508 95 state = readOut_headerElements;
flatcat 1:a220fd937508 96 }
flatcat 1:a220fd937508 97 break;
flatcat 1:a220fd937508 98 case readOut_headerElements:
flatcat 1:a220fd937508 99 pixelCount++;
flatcat 1:a220fd937508 100 if (pixelCount == headerElements) {
flatcat 1:a220fd937508 101 pixelCount = 0;
flatcat 1:a220fd937508 102 state = readOut_signalElements;
flatcat 1:a220fd937508 103 }
flatcat 1:a220fd937508 104 break;
flatcat 1:a220fd937508 105 case readOut_signalElements:
flatcat 1:a220fd937508 106 pixelCount++;
flatcat 1:a220fd937508 107 // pixelValue[pixelCount] = imageIn.read();
flatcat 2:7751080fb267 108 // imageData = ((imageIn.read_u16() * 5.0) / 4096.0);
flatcat 2:7751080fb267 109 raspi.printf("%4.20f \r\n", (imageIn.read_u16() * 5.0) / 4096.0);
flatcat 1:a220fd937508 110 LED = !LED;
flatcat 1:a220fd937508 111 if (pixelCount == signalElements) {
flatcat 1:a220fd937508 112 pixelCount = 0;
flatcat 1:a220fd937508 113 state = readOut_trailingDummy;
flatcat 1:a220fd937508 114 }
flatcat 1:a220fd937508 115 break;
flatcat 1:a220fd937508 116 case readOut_trailingDummy:
flatcat 1:a220fd937508 117 pixelCount++;
flatcat 1:a220fd937508 118 if (pixelCount == trailingDummyElements) {
flatcat 1:a220fd937508 119 pixelCount = 0;
flatcat 1:a220fd937508 120 state = readOut_integrationTime;
flatcat 2:7751080fb267 121 ICG = 0;
flatcat 1:a220fd937508 122 }
flatcat 1:a220fd937508 123 break;
flatcat 1:a220fd937508 124 case readOut_integrationTime:
flatcat 1:a220fd937508 125 wait_us(sensitivity);
flatcat 2:7751080fb267 126 state = readOut_Finish;
flatcat 2:7751080fb267 127 raspi.printf("---\r\n");
flatcat 1:a220fd937508 128 break;
flatcat 1:a220fd937508 129 case readOut_Finish:
flatcat 1:a220fd937508 130 state = readOut_IDLE;
flatcat 2:7751080fb267 131 wait_us(sensitivity);
flatcat 1:a220fd937508 132 LED = 0;
flatcat 1:a220fd937508 133 ICG = 1;
flatcat 1:a220fd937508 134 break;
flatcat 1:a220fd937508 135 case readOut_IDLE:
flatcat 1:a220fd937508 136 if (ICG == 1) {
flatcat 1:a220fd937508 137 ICG = 0;
flatcat 1:a220fd937508 138 state = readOut_Begin;
flatcat 1:a220fd937508 139 }
flatcat 1:a220fd937508 140 break;
flatcat 1:a220fd937508 141 default:
flatcat 1:a220fd937508 142 break;
flatcat 1:a220fd937508 143 }
flatcat 1:a220fd937508 144 }
flatcat 1:a220fd937508 145
flatcat 0:984447b91a04 146 int main()
flatcat 0:984447b91a04 147 {
flatcat 1:a220fd937508 148 ICG = 1;
flatcat 1:a220fd937508 149 LED = 0;
flatcat 1:a220fd937508 150 pixelCount = 0;
flatcat 1:a220fd937508 151 readOutTrigger = 0;
flatcat 1:a220fd937508 152 state = readOut_IDLE;
flatcat 1:a220fd937508 153
flatcat 0:984447b91a04 154 masterClock.period_us(masterFreq_period);
flatcat 0:984447b91a04 155 masterClock.pulsewidth_us(masterFreq_width);
flatcat 0:984447b91a04 156
flatcat 0:984447b91a04 157 shiftGate.period_us(shiftGate_period);
flatcat 0:984447b91a04 158 shiftGate.pulsewidth_us(shiftGate_width);
flatcat 0:984447b91a04 159
flatcat 2:7751080fb267 160 raspi.baud(921600);
flatcat 1:a220fd937508 161 wait(0.5);
flatcat 1:a220fd937508 162
flatcat 1:a220fd937508 163 shiftGate_int.rise(checkState);
flatcat 1:a220fd937508 164
flatcat 0:984447b91a04 165 raspi.baud(921600);
flatcat 0:984447b91a04 166 while(1) {
flatcat 1:a220fd937508 167 wait(1);
flatcat 0:984447b91a04 168 }
flatcat 0:984447b91a04 169 }