Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@3:1a4ad4c82046, 2012-08-07 (annotated)
- Committer:
- Reignbow
- Date:
- Tue Aug 07 13:41:11 2012 +0000
- Revision:
- 3:1a4ad4c82046
- Parent:
- 2:6d472c554758
- Child:
- 4:c38f7e4e8fda
SPI reads over registers (faster). All calculations as int.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Reignbow | 0:3e6ae21a6bb1 | 1 | /* PIservo |
Reignbow | 0:3e6ae21a6bb1 | 2 | implement a PI controller with USB serial gain setting to stabilize dipole trap power. |
Reignbow | 0:3e6ae21a6bb1 | 3 | Two analog inputs (control and sensor), one analog output (to RF VCA). |
Reignbow | 0:3e6ae21a6bb1 | 4 | |
Reignbow | 0:3e6ae21a6bb1 | 5 | 20120806 Andreas Steffen |
Reignbow | 0:3e6ae21a6bb1 | 6 | */ |
Reignbow | 0:3e6ae21a6bb1 | 7 | |
Reignbow | 0:3e6ae21a6bb1 | 8 | |
Reignbow | 0:3e6ae21a6bb1 | 9 | #include "mbed.h" |
Reignbow | 0:3e6ae21a6bb1 | 10 | |
Reignbow | 1:dc799997391e | 11 | DigitalOut adcCS(p8); |
Reignbow | 2:6d472c554758 | 12 | DigitalOut cyc(p10); |
Reignbow | 0:3e6ae21a6bb1 | 13 | AnalogOut vca(p18); |
Reignbow | 3:1a4ad4c82046 | 14 | SPI adc(p5,p6,p7); //talk to ad7921 |
Reignbow | 0:3e6ae21a6bb1 | 15 | Serial pc(USBTX, USBRX); |
Reignbow | 0:3e6ae21a6bb1 | 16 | |
Reignbow | 0:3e6ae21a6bb1 | 17 | |
Reignbow | 0:3e6ae21a6bb1 | 18 | Timer t; |
Reignbow | 0:3e6ae21a6bb1 | 19 | Ticker commTick; |
Reignbow | 0:3e6ae21a6bb1 | 20 | Ticker intTick; |
Reignbow | 0:3e6ae21a6bb1 | 21 | |
Reignbow | 3:1a4ad4c82046 | 22 | int pGain = 1; |
Reignbow | 3:1a4ad4c82046 | 23 | int iGain = 100; |
Reignbow | 3:1a4ad4c82046 | 24 | int integrator = 0; //the building up integrator value |
Reignbow | 0:3e6ae21a6bb1 | 25 | |
Reignbow | 0:3e6ae21a6bb1 | 26 | void serialComm(){ |
Reignbow | 0:3e6ae21a6bb1 | 27 | char c; |
Reignbow | 3:1a4ad4c82046 | 28 | int i; |
Reignbow | 0:3e6ae21a6bb1 | 29 | |
Reignbow | 0:3e6ae21a6bb1 | 30 | if (pc.readable()) { |
Reignbow | 0:3e6ae21a6bb1 | 31 | c = pc.getc(); |
Reignbow | 3:1a4ad4c82046 | 32 | pc.scanf("%i", &i); |
Reignbow | 0:3e6ae21a6bb1 | 33 | switch (c) { |
Reignbow | 0:3e6ae21a6bb1 | 34 | case 'p': |
Reignbow | 3:1a4ad4c82046 | 35 | pGain = i; |
Reignbow | 3:1a4ad4c82046 | 36 | pc.printf("Changed p gain to %i.\n",pGain); |
Reignbow | 0:3e6ae21a6bb1 | 37 | break; |
Reignbow | 0:3e6ae21a6bb1 | 38 | case 'i': |
Reignbow | 3:1a4ad4c82046 | 39 | iGain = i; |
Reignbow | 3:1a4ad4c82046 | 40 | pc.printf("Changed i gain to %i.\n",iGain); |
Reignbow | 3:1a4ad4c82046 | 41 | if (i==0) |
Reignbow | 3:1a4ad4c82046 | 42 | integrator = 0; |
Reignbow | 0:3e6ae21a6bb1 | 43 | break; |
Reignbow | 0:3e6ae21a6bb1 | 44 | default: |
Reignbow | 0:3e6ae21a6bb1 | 45 | pc.printf("Command not understood.\n",iGain); |
Reignbow | 3:1a4ad4c82046 | 46 | pc.printf("Read %c and %i.\n",c,i); |
Reignbow | 0:3e6ae21a6bb1 | 47 | break; |
Reignbow | 0:3e6ae21a6bb1 | 48 | } |
Reignbow | 0:3e6ae21a6bb1 | 49 | } |
Reignbow | 0:3e6ae21a6bb1 | 50 | } |
Reignbow | 0:3e6ae21a6bb1 | 51 | |
Reignbow | 0:3e6ae21a6bb1 | 52 | |
Reignbow | 0:3e6ae21a6bb1 | 53 | int main() { |
Reignbow | 1:dc799997391e | 54 | int ctl=0; |
Reignbow | 3:1a4ad4c82046 | 55 | int pd=0; |
Reignbow | 3:1a4ad4c82046 | 56 | int err=0; |
Reignbow | 0:3e6ae21a6bb1 | 57 | pc.printf("mbed restarted!\n"); |
Reignbow | 0:3e6ae21a6bb1 | 58 | |
Reignbow | 3:1a4ad4c82046 | 59 | adc.format(16,2); |
Reignbow | 3:1a4ad4c82046 | 60 | adc.frequency(5000000); |
Reignbow | 1:dc799997391e | 61 | |
Reignbow | 3:1a4ad4c82046 | 62 | commTick.attach(&serialComm,0.5); //check serial every half second |
Reignbow | 2:6d472c554758 | 63 | cyc = 0; |
Reignbow | 0:3e6ae21a6bb1 | 64 | |
Reignbow | 0:3e6ae21a6bb1 | 65 | while(1) { |
Reignbow | 3:1a4ad4c82046 | 66 | cyc = cyc^1; //toggle debug pin |
Reignbow | 2:6d472c554758 | 67 | |
Reignbow | 3:1a4ad4c82046 | 68 | //stagger read and output |
Reignbow | 3:1a4ad4c82046 | 69 | //The SPI read takes a long time, so start it by writing to the data |
Reignbow | 3:1a4ad4c82046 | 70 | //register, do sth. else (create new output), then get the value |
Reignbow | 3:1a4ad4c82046 | 71 | //and start the read from the other channel |
Reignbow | 3:1a4ad4c82046 | 72 | |
Reignbow | 3:1a4ad4c82046 | 73 | //begin SPI transfer |
Reignbow | 1:dc799997391e | 74 | adcCS = 0; |
Reignbow | 3:1a4ad4c82046 | 75 | LPC_SSP1->DR = 3<<13; //initialize SSP1 transfer by writing to data register; select ch 1 |
Reignbow | 3:1a4ad4c82046 | 76 | |
Reignbow | 3:1a4ad4c82046 | 77 | err = ctl - pd; //convert to volt |
Reignbow | 3:1a4ad4c82046 | 78 | integrator += err*iGain; |
Reignbow | 3:1a4ad4c82046 | 79 | t.reset(); //reset timer to get next integration time |
Reignbow | 3:1a4ad4c82046 | 80 | vca.write_u16( (err * pGain + integrator)>>10 ); //analog output |
Reignbow | 3:1a4ad4c82046 | 81 | |
Reignbow | 3:1a4ad4c82046 | 82 | //read SPI transfer results |
Reignbow | 3:1a4ad4c82046 | 83 | while(!(LPC_SSP1->SR & 0x04)) {} |
Reignbow | 3:1a4ad4c82046 | 84 | pd = LPC_SSP1->DR & 0xFFF; //last 12 bits are data |
Reignbow | 3:1a4ad4c82046 | 85 | adcCS = 1; |
Reignbow | 1:dc799997391e | 86 | |
Reignbow | 3:1a4ad4c82046 | 87 | //begin transfer from other channel |
Reignbow | 3:1a4ad4c82046 | 88 | adcCS = 0; |
Reignbow | 3:1a4ad4c82046 | 89 | LPC_SSP1->DR = 1; //initialize SSP1 transfer by writing to data register; select ch 0 |
Reignbow | 3:1a4ad4c82046 | 90 | |
Reignbow | 3:1a4ad4c82046 | 91 | err = ctl-pd; //convert to volt |
Reignbow | 3:1a4ad4c82046 | 92 | integrator += err*iGain; |
Reignbow | 3:1a4ad4c82046 | 93 | vca.write_u16( (err * pGain + integrator)>>10 ); //analog output |
Reignbow | 3:1a4ad4c82046 | 94 | |
Reignbow | 3:1a4ad4c82046 | 95 | //read SPI transfer results |
Reignbow | 3:1a4ad4c82046 | 96 | while(!(LPC_SSP1->SR & 0x04)) {} |
Reignbow | 3:1a4ad4c82046 | 97 | ctl = LPC_SSP1->DR & 0xFFF; //last 12 bits are data |
Reignbow | 3:1a4ad4c82046 | 98 | adcCS = 1; |
Reignbow | 3:1a4ad4c82046 | 99 | |
Reignbow | 3:1a4ad4c82046 | 100 | //pc.printf("Measuring ctl=%i and pd=%i, err=%i, integrator= %i\n",ctl,pd,err,integrator); |
Reignbow | 2:6d472c554758 | 101 | |
Reignbow | 0:3e6ae21a6bb1 | 102 | } |
Reignbow | 0:3e6ae21a6bb1 | 103 | } |