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.
Diff: main.cpp
- Revision:
- 3:1a4ad4c82046
- Parent:
- 2:6d472c554758
- Child:
- 4:c38f7e4e8fda
diff -r 6d472c554758 -r 1a4ad4c82046 main.cpp --- a/main.cpp Tue Aug 07 12:07:32 2012 +0000 +++ b/main.cpp Tue Aug 07 13:41:11 2012 +0000 @@ -11,7 +11,7 @@ DigitalOut adcCS(p8); DigitalOut cyc(p10); AnalogOut vca(p18); -SPI adcSPI(p5,p6,p7); //talk to ad7921 +SPI adc(p5,p6,p7); //talk to ad7921 Serial pc(USBTX, USBRX); @@ -19,83 +19,85 @@ Ticker commTick; Ticker intTick; -float pGain = 0.5; -float iGain = 1000.; -float integrator = 0.; //the building up integrator value +int pGain = 1; +int iGain = 100; +int integrator = 0; //the building up integrator value void serialComm(){ char c; - float f; + int i; if (pc.readable()) { c = pc.getc(); - pc.scanf("%f", &f); + pc.scanf("%i", &i); switch (c) { case 'p': - pGain = f; - pc.printf("Changed p gain to %f.\n",pGain); + pGain = i; + pc.printf("Changed p gain to %i.\n",pGain); break; case 'i': - iGain = f; - pc.printf("Changed i gain to %f.\n",iGain); - if (f==0) - integrator = 0.; + iGain = i; + pc.printf("Changed i gain to %i.\n",iGain); + if (i==0) + integrator = 0; break; default: pc.printf("Command not understood.\n",iGain); - pc.printf("Read %c and %f.\n",c,f); + pc.printf("Read %c and %i.\n",c,i); break; } } } -void limitIntegrator(){ - if (integrator > 3.3) { - integrator = 3.3; - } - if (integrator < 0) { - integrator = 0; - } -} int main() { - float err=0; - float out=0.; int ctl=0; - int pd=0; + int pd=0; + int err=0; pc.printf("mbed restarted!\n"); - adcSPI.format(16,2); - adcSPI.frequency(5000000); + adc.format(16,2); + adc.frequency(5000000); - commTick.attach(&serialComm,0.5); //check serial every half second - intTick.attach(&limitIntegrator,0.001); //check integrator overrun every ms - - t.start(); + commTick.attach(&serialComm,0.5); //check serial every half second cyc = 0; while(1) { - cyc = cyc ^ 1; //toggle debug pin - //read in from AD7921 over SPI - adcCS = 0; - pd = adcSPI.write(3<<13); //select ch 1 - pd = (pd) & 0xFFF; - adcCS = 1; + cyc = cyc^1; //toggle debug pin + //stagger read and output + //The SPI read takes a long time, so start it by writing to the data + //register, do sth. else (create new output), then get the value + //and start the read from the other channel + + //begin SPI transfer adcCS = 0; - ctl = adcSPI.write(1); // select ch 0 - //take the last 12 bits that carry data - ctl = (ctl) & 0xFFF; - adcCS = 1; - - err = ctl*3.3/4095. - pd*3.3/4095.; //convert to volt - integrator += err * iGain*1.e-6 * t.read_us(); - t.reset(); //reset timer to get next integration time - - out = 1.0* err * pGain + integrator; //analog output - vca.write(out); + LPC_SSP1->DR = 3<<13; //initialize SSP1 transfer by writing to data register; select ch 1 + + err = ctl - pd; //convert to volt + integrator += err*iGain; + t.reset(); //reset timer to get next integration time + vca.write_u16( (err * pGain + integrator)>>10 ); //analog output + + //read SPI transfer results + while(!(LPC_SSP1->SR & 0x04)) {} + pd = LPC_SSP1->DR & 0xFFF; //last 12 bits are data + adcCS = 1; - // pc.printf("Measuring ctl=%i and pd=%i, err=%f, vca is %f\n",ctl,pd,err,out); + //begin transfer from other channel + adcCS = 0; + LPC_SSP1->DR = 1; //initialize SSP1 transfer by writing to data register; select ch 0 + + err = ctl-pd; //convert to volt + integrator += err*iGain; + vca.write_u16( (err * pGain + integrator)>>10 ); //analog output + + //read SPI transfer results + while(!(LPC_SSP1->SR & 0x04)) {} + ctl = LPC_SSP1->DR & 0xFFF; //last 12 bits are data + adcCS = 1; + + //pc.printf("Measuring ctl=%i and pd=%i, err=%i, integrator= %i\n",ctl,pd,err,integrator); } }