分享一个好资料给大家学习单片机技术
工程包含PCF8591驱动文件,可以通过改变传入参数轻松改变ADC四通道电压测量,调用DAC函数产生DAC。
可以产生正弦波,方波,三角板和锯齿波。
使用四个数码管作为显示,一个独立按键作为模式切换。
PCF8591电压表+四种波形发生器仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include <reg51.h>
- #include <PCF8591.h>
- #include <seg.h>
- sbit KEY = P3^7;
- unsigned char code sin_num[]={
- 0,0,0,0,0,0,0,0,1,1,1,1,1,2,2,2,
- 2,3,3,4,4,4,5,5,6,6,7,7,8,8,9,9,
- 10,10,11,12,12,13,14,15,15,16,17,18,18,19,20,21,
- 22,23,24,25,25,26,27,28,29,30,31,32,34,35,36,37,
- 38,39,40,41,42,44,45,46,47,49,50,51,52,54,55,56,
- 57,59,60,61,63,64,66,67,68,70,71,73,74,75,77,78,
- 80,81,83,84,86,87,89,90,92,93,95,96,98,99,101,102,
- 104,106,107,109,110,112, 113,115,116,118,120,121,123,124,126,128,
- 129,131, 132,134,135, 137, 139,140,142,143,145,146,148, 149,151,153,
- 154,156,157,159,160,162,163,165, 166,168,169,171,172,174,175,177,
- 178,180,181, 182,184,185,187,188,189,191,192,194,195,196,198,199,
- 200,201,203,204,205,206,208,209,210,211,213,214,215,216,217,218,
- 219,220,221,223,224,225,226,227,228,229,230,230,231,232,233,234,
- 235,236,237,237,238,239,240,240,241,242,243,243,244,245,245,246,
- 246,247,247,248,248,249,249,250,250,251,251, 251,252,252,253,253,
- 253,253,254,254,254,254,254,255,255,255,255, 255,255,255,255,255
- }; //这是正弦波上升半周期的采样
- uchar Recv_Buffer;//数据接收缓冲
- uchar Voltage[]="0.000V ";//数据分解为电压x.xx
- float Vol_Am; //放大后的电压值
- uchar key_ms;
- uchar mode;
- void delay(unsigned int x)//延时
- {
- while(--x);
- }
- void key_pro()
- {
- if(KEY == 0) key_ms++;
- if(key_ms >= 5)
- {
- if(KEY == 0)
- {
- key_ms = 0;
- mode++;
- if(mode >= 5) mode = 0;
- while(!KEY);
- }
- }
- }
- void DAC_Wave()
- {
- int i;
- switch(mode)
- {
- case 1: //三角波
- for(i=0;i<255;i++)
- {
- DAC_PCF(i);
- key_pro();
- }
- for(i=255;i>0;i--)
- {
- DAC_PCF(i);
- key_pro();
- }
- break;
-
- case 2://方波
- DAC_PCF(0);
- delay(2500);
- DAC_PCF(0xff);
- delay(2500);
- break;
-
- case 3://三角波
- for(i=255;i>0;i--)
- {
- DAC_PCF(i);
- key_pro();
- }
- break;
-
- case 4: //正弦波 采用数组采样值 波形更好
- for(i=0;i<255;i++)
- {
- DAC_PCF(sin_num[i]);
- key_pro();
- }
- for(i=255;i>=0;i--)
- {
- DAC_PCF(sin_num[i]);
- key_pro();
- }
- break;
- default : DAC_PCF(0x00);//否则为0 防错
- }
- }
- // 转换模数转换后得到的值
- void Convert_To_Voltage(uchar val)
- {
- Vol_Am = val;
- Vol_Am = Vol_Am*5/255*1000;
- Voltage[4]=(uint)Vol_Am%10+'0';
- Voltage[3]=(uint)Vol_Am/10%10+'0';
- Voltage[2]=(uint)Vol_Am/100%10+'0';
- Voltage[0]=(uint)Vol_Am/1000%10+'0';
- }
- void dis_pro()
- {
- if(mode == 0)
- {
- ISendByte(0x00);
- Recv_Buffer=IRcvByte();
- Convert_To_Voltage(Recv_Buffer);
- seg_display((uint)Vol_Am);
- }
- else
- {
- seg_display_One(mode);
- DAC_Wave();
- }
- }
- void main()
- {
- while(1)
- {
- key_pro();
- dis_pro();
- }
- }
复制代码
所有资料51hei提供下载:
电压表.zip
(108.96 KB, 下载次数: 152)
|