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:
Tue Aug 19 08:52:40 2014 +0000
Revision:
9:841663cf312c
Parent:
8:5fcd8a2149f0
Child:
10:f16d2d5bd04d
This works..;

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 DigitalOut ICG(PB_3);
flatcat 0:984447b91a04 15 AnalogIn imageIn(A0);
flatcat 1:a220fd937508 16 DigitalOut LED(LED1);
flatcat 0:984447b91a04 17 Serial raspi(USBTX, USBRX);
flatcat 0:984447b91a04 18
flatcat 6:fe473ca8b625 19 int masterFreq_period = 20; //microseconds
flatcat 6:fe473ca8b625 20 int masterFreq_width = 10; //microseconds
flatcat 8:5fcd8a2149f0 21 int shiftGate_period = 66; //microseconds
flatcat 8:5fcd8a2149f0 22 int shiftGate_width = 33; //microseconds
flatcat 1:a220fd937508 23
flatcat 3:83cb6eb61adf 24 int none = 0;
flatcat 1:a220fd937508 25 int veryLow = 1;
flatcat 1:a220fd937508 26 int low = 100;
flatcat 9:841663cf312c 27 int mediumLow = 1000;
flatcat 1:a220fd937508 28 int medium = 100000;
flatcat 1:a220fd937508 29 int high = 1000000;
flatcat 1:a220fd937508 30 int veryHigh = 10000000;
flatcat 1:a220fd937508 31
flatcat 2:7751080fb267 32 double imageData;
flatcat 9:841663cf312c 33 int sensitivity = mediumLow;
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 5:cbf08f942178 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 6:fe473ca8b625 57 double pixelValue[signalElements] = { 0 };
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 6:fe473ca8b625 76 // raspi.printf("+++\r\n");
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 6:fe473ca8b625 106 pixelValue[pixelCount] = imageIn.read_u16();
flatcat 6:fe473ca8b625 107 // raspi.printf("%i\t%4.12f\r\n", pixelCount,(imageIn.read_u16() * 5.0) / 4096.0);
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 2:7751080fb267 118 ICG = 0;
flatcat 1:a220fd937508 119 }
flatcat 1:a220fd937508 120 break;
flatcat 1:a220fd937508 121 case readOut_integrationTime:
flatcat 1:a220fd937508 122 wait_us(sensitivity);
flatcat 2:7751080fb267 123 state = readOut_Finish;
flatcat 6:fe473ca8b625 124 for (int pixelNumber=0; pixelNumber<signalElements; pixelNumber++) {
flatcat 8:5fcd8a2149f0 125 raspi.printf("%i\t%4.12f\r\n", pixelNumber, 5 - ((pixelValue[pixelNumber] * 5) / 4096.0));
flatcat 6:fe473ca8b625 126 }
flatcat 6:fe473ca8b625 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 4:4eedf8292719 137 // ICG = 0;
flatcat 4:4eedf8292719 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 4:4eedf8292719 167 char c = raspi.getc();
flatcat 6:fe473ca8b625 168 switch (c) {
flatcat 4:4eedf8292719 169 case 'r':
flatcat 4:4eedf8292719 170 ICG = 0;
flatcat 4:4eedf8292719 171 state = readOut_Begin;
flatcat 4:4eedf8292719 172 break;
flatcat 4:4eedf8292719 173 default:
flatcat 4:4eedf8292719 174 break;
flatcat 4:4eedf8292719 175 }
flatcat 0:984447b91a04 176 }
flatcat 0:984447b91a04 177 }