?? chap11.c
字號:
// Chapter 11 6811 C programs// Jonathan W. Valvano// This software accompanies the book,// Embedded Microcomputer Systems: Real Time Interfacing// published by Brooks Cole, 1999// Program 11.1. Software used to test how many bits are really needed.void DACout8(unsigned int code){ DACout(code&0xFFF0);} // ignore bottom 4 bitsvoid DACout10(unsigned int code){ DACout(code&0xFFFC);} // ignore bottom 2 bits// Program 11.2. Periodic interrupt used to create the analog output waveform.unsigned int wave(unsigned int t){float result,time;time=2*pi*((float)t)/1000.0; // integer t in msec into floating point time in secondsresult=2048.0+1000.0*cos(31.25*time)-500.0*sin(125.0*time);return (unsigned int) result;}// Program 11.3. Periodic interrupt used to create the analog output waveform.// MC68HC11A8#define Rate 2000#define OC5 0x08unsigned int Time; // Inc every 1ms #pragma interrupt_handler TOC5handler()void TOC5handler(void){ TFLG1=OC5; // Ack interrupt TOC5=TOC5+Rate; // Executed every 1 ms Time++; DACout(wave(Time));}// Program 11.4. Simple data structure for the waveform.unsigned int I; // incremented every 1msconst unsigned int wave[32]= {3048,2675,2472,2526,2755,2957,2931,2597,2048,1499,1165,1139,1341,1570,1624,1421,1048,714,624,863,1341,1846,2165,2206,2048,1890,1931,2250,2755,3233,3472,3382};// Program 11.5. Periodic interrupt used to create the analog output waveform.// MC68HC11A8#define Rate 2000#define OC5 0x08#pragma interrupt_handler TOC5handler()void TOC5handler(void){ TFLG1=OC5; // Ack interrupt TOC5=TOC5+Rate; // Executed every 1 ms if((++I)==32) I=0; DACout(wave[I]);}// Program 11.6. Data structure with time and value for the waveform.int I; // incremented every 1msint J; // index into these two tablesconst int t[10]= {0,2,6,10,14,18,22,25,30,32}; // time in msecconst int wave[10]={3048,2472,2931,1165,1624,624,2165,1890,3472,3048}; //last=first// Program 11.7. Periodic interrupt used to create the analog output waveform.// MC68HC11A8#define Rate 2000#define OC5 0x08#pragma interrupt_handler TOC5handler()void TOC5handler(void){ TFLG1=OC5; // Ack interrupt TOC5=TOC5+Rate; // Executed every 1 ms if((++I)==32) {I=0; J=0;} if(I==t[J]) DACout(wave[J]); else if (I==t[J+1]){ J++; DACout(wave[J]);} else DACout(wave[J]+((wave[J+1]-wave[J]) *(I-t[J]))/(t[J+1]-t[J]));}// Program 11.8. Data structure with delta time and value for the waveform.unsigned int I; // incremented every sampleconst unsigned int wave[32]= { 3048,2675,2472,2526,2817,2981,2800,2337,1901,1499,1165,1341,1570,1597,1337, 952, 662, 654, 863,1210,1605,1950,2202,2141,1955,1876,2057,2366,2755,3129,3442,3382};const unsigned int dt[32]= { // time increment in 500 ns cycles 2000,2000,2000,2500,2500,2000,2000,1500,1500,2000,4000,2000,2500,2000,2000,2000, 2000,1500,1500,1500,1500,2000,2500,2000,2000,2000,1500,1500,1500,2000,2500,2000};// Program 11.9. Periodic interrupt used to create the analog output waveform.// MC68HC11A8#define OC5 0x08#pragma interrupt_handler TOC5handler()void TOC5handler(void){ TFLG1=OC5; // Ack interrupt if((++I)==32) I=0; TOC5=TOC5+dt[I]; // variable rate DACout(wave[I]);}// Program 11.10. Software implementation of a ramp A/D.unsigned int rampAD(void){ unsigned int DOUT; // guess DOUT = 0; // start at minimum, V0=-5.00 (offset binary) do { DACout(DOUT); // set D/A output, V0 DOUT++; // ramp } while((!Z())&&(DOUT<4096)); // stop when V0>Vin or DOUT=4096 return(DOUT);}// Program 11.11. Software implementation of a tracking A/D.int DOUT; // guessvoid main(void){ DOUT = 2048; // start in the middle, V0=0.00 (offset binary) while(1) { DACout(DOUT); // set D/A output, V0 if(Z()) { // check input if(DOUT>0) // don't go below 0 DOUT--; // V0>Vin so decrement } else { if(DOUT<4095) DOUT++; // V0<Vin so increment } }}// Program 11.12. Interrupting software implementation of a tracking A/D.#pragma interrupt_handler TOC5handler()void TOC5handler(void){ TFLG1=OC5; // ack C5F TC5=TC5+rate; DACout(DOUT); // set D/A output, V0 if(Z()) { // check input if(DOUT>0) // don't go below 0 DOUT--; // V0>Vin so decrement } else { if(DOUT<4095) DOUT++; // V0<Vin so increment } DACout(DOUT); // set D/A output, V0}// Program 11.13. Software implementation of a successive approximation A/D.unsigned int SuccAproxAD(void){ unsigned int DOUT,bit; DOUT = 0; for(bit=2048;bit;bit>>1){ DOUT |= bit; // try to turn on this bit DACout(DOUT); // set D/A output, V0 if(Z()) DOUT ^= bit; // too big, so remove this bit } return(DOUT);} }}// Program 11.14. Software implementation of a sigma-delta A/D.unsigned char DOUT; // 8-bit sampleunsigned char SUM; // number of times Z=0 and V0=1unsigned char CNT; // 8-bit counter#pragma interrupt_handler TOC5handler()void TOC5handler(void){ TFLG1=OC5; // ack C5F TC5=TC5+rate; // interrupt 256 times faster than the A/D output rate if(Z()) // check input DACout(0); // too high, set D/A output, V0=0 else { DACout(1); // too low, set D/A output, V0=+5v SUM++; } if(++CNT==0){ // end of 256 loops? DOUT=SUM; // new sample SUM=0; // get ready for the next }}// Program 11.15. Software implementation of first derivative using a multiple access circular queue.// MC68HC11A8#define Rate 2000#define OC5 0x08unsigned int x[4]; // MACQ (mV)unsigned int d; // derivative (V/s)#pragma interrupt_handler TOC5handler()void TOC5handler(void){ TFLG1=OC5; // Ack interrupt TOC5=TOC5+Rate; // Executed every 1 ms x[3]=x[2]; // shift MACQ data x[2]=x[1]; // units of mV x[1]=x[0]; x[0]=Adin(); // current data d=x[0]+3*x[1]-3*x[2]-x[3];} // mV/msvoid ritual(void) { asm(" sei"); // make atomic TMSK1|=OC5; // Arm output compare 5 TFLG1=OC5; // Initially clear OC5F TOC5=TCNT+Rate; // First one in 1 msasm(" cli"); }// Program 11.18. Assembly software to sample data using the A/D.// MC68HC11A8 void Init(void){ OPTION = 0x80;} // Activate A/D #define CCF 0x80unsigned char A2D(unsigned char chan){ ADCTL=chan; // Start A/D while ((ADCTL & CCF) == 0){}; return(ADR1); }
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -