利用DAC0808进行DA转换,利用ADC0832进行AD转换
右上角两个按钮INT1和CHANGE
运行之后,程序处于待机状态,点击change按钮进行AD和DA切换,在DA实验中,INT1进行波形切换,有三角波,方波,锯齿波,正弦波。
#include#include #define DAC XBYTE[0x7FFF] unsigned int u ; unsigned int a,b,c,d,t,p,A1,A2; unsigned char VOL=0; unsigned char code sin[]={ 127,130,133,136,139,143,146,149,152,155,158,161,164,167,170,173,176,179,182,184,187,190,193,195,198,200,203,205,208,210, 213,215,217,219,221,224,226,228,229,231,233,235,236,238,239,241,242,244,245,246,247,248,249,250,251,251,252,253,253,254, 254,254,254,254,255,254,254,254,254,254,253,253,252,251,251,250,249,248,247,246,245,244,242,241,239,238,236,235,233,231, 229,228,226,224,221,219,217,215,213,210,208,205,203,200,198,195,193,190,187,184,182,179,176,173,170,167,164,161,158,155, 152,149,146,143,139,136,133,130,127,124,121,118,115,111,108,105,102,99,96,93,90,87,84,81,78,75,72,70,67,64,61,59,56,54,51, 49,46,44,41,39,37,35,33,30,28,26,25,23,21,19,18,16,15,13,12,10,9,8,7,6,5,4,3,3,2,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,2,3,3,4, 5,6,7,8,9,10,12,13,15,16,18,19,21,23,25,26,28,30,33,35,37,39,41,44,46,49,51,54,56,59,61,64,67,70,72,75,78,81,84,87,90,93, 96,99,102,105,108,111,115,118,121,124 }; unsigned char code juchi[]={ 0,4,8,12,16,20,24,28,32,36,40,45,49,53,57,61,65,69,73,77,81,85,89,93,97,101,105,109,113,117,121,125,130,134,138,142, 146,150,154,158,162,166,170,174,178,182,186,190,194,198,202,206,210,215,219,223,227,231,235,239,243,247,251,255 }; unsigned char code sanjiao[]={ 0,8,16,24,32,40,48,56,64,72,80,88,96,104,112,120,128,136,144,152,160,168,176,184,192,200,208,216,224,232,240,248, 248,240,232,224,216,208,200,192,184,176,168,160,152,144,136,128,120,112,104,96,88,80,72,64,56,48,40,32,24,16,8,0 }; unsigned char code fangbo[]={ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 200,200,200,200,200,200,200,200,200,200,200,200,200,200,200,200,200,200,200,200,200,200,200,200,200, 200,200,200,200,200,200,200,200,200,200,200,200,200,200,200,200,200,200,200,200,200,200,200,200,200 }; unsigned char Distab[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10}; void Delay(int xms){while(xms--){unsigned char i, j;i = 2;j = 239;do{while (--j);} while (--i);}} void smgca(locating,number){ switch(locating) { case 1:P2=0x01;break; case 2:P2=0x02;break; case 3:P2=0x04;break; case 4:P2=0x08;break; } switch(number) { case 0:P0 = 0xC0;break;case 1:P0 = 0xF9;break; case 2:P0 = 0xA4;break;case 3:P0 = 0xB0;break; case 4:P0 = 0x99;break;case 5:P0 = 0x92;break; case 6:P0 = 0x82;break;case 7:P0 = 0xF8;break; case 8:P0 = 0x80;break;case 9:P0 = 0x90;break; } Delay(3); P0 = 0xFF; } void ADC(){ P3_0=0; P3_0=1; Delay(5); P3_0=0; while(P3_7==0); P3_1 = 1; VOL = P1; P3_1 = 0; } void show(){ unsigned t1,t2,t3,t4; t1 = VOL/51; t2 = (VOL%51)*10/51; t3 = ((((VOL%51)*10)%51)*10+25)/51; t4 = ((((((VOL%51)*10)%51)*10+25)%51)*10)/51-4; if(t3>9){t3=9;} if(t3<0){t3=0;} if(t4>9){t4=9;} if(t4<0){t4=0;} P2 = 0x01; P0 = Distab[t1]; Delay(3); P2 = 0x00; P0 = 0xFF; smgca(2,t2); smgca(3,t3); smgca(4,t4); } void fangb(){ while(a){ for(u=0;u<100;u++){ DAC = fangbo[u]; } } } void sinb(){ while(b){ for(u=0;u<256;u++){ DAC = sin[u]; } } } void juchib(){ while(c){ for(u=0;u<64;u++){ DAC = juchi[u]; } } } void sanjiaob(){ while(d){ for(u=0;u<64;u++){ DAC = sanjiao[u]; } } } void INT1main() interrupt 2{ switch(t){ case 0 : {a=1;b=0;c=0;d=0; t++;break;} case 1 : {a=0,b=1,c=0,d=0; t++;break;} case 2 : {a=0,b=0,c=1,d=0; t++;break;} case 3 : {a=0,b=0,c=0,d=1; t++;break;} } if(t>3){t=0;} } void INT0main() interrupt 0{ switch(p){ case 0 : {a=0;b=0;c=0;d=0;A1=1;A2=0; p++;break;} case 1 : {a=0,b=0,c=0,d=0;A1=0;A2=1; p++;break;} } if(p>1){p=0;} } void main(){ EA = 1; EX0 = 1; IT0 = 1; EX1 = 1; IT1 = 1; while(1){ while(A1){ fangb(); sinb(); juchib(); sanjiaob(); } while(A2){ ADC(); show(); } } }



