基于51单片机的TCL1865电压测量仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include<reg52.h>
- #include<absacc.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define ulong unsigned long
- #define disp_dot 0x2e
- #define disp_V 0x56
- #define data1602 P0 // 定义LCD1602数据接口
- uchar code disp_code[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
- ulong voltage_data;
- sbit rs1602 = P2^4;
- sbit rw1602 = P2^5;
- sbit e1602 = P2^6;
- sbit CONV=P1^0;
- sbit SCK=P1^1;
- sbit SDO=P1^2;
- sbit SDI =P1^3;
- void delay (uint ms)
- {
- uint i,j;
- for(i=ms;i>0;i--)
- for(j=110;j>0;j--);
- }
- /* LCD1602写入数据 */
- void write_1602(uchar wdata)
- {
- data1602 = wdata;
- rs1602 = 1;
- rw1602 = 0;
- e1602 = 1;
- delay(4);
- e1602 = 0;
- }
-
- /*LCD1602写命令*/
- void write_order(uchar odata)//
- {
- data1602 = odata;
- rs1602 = 0;
- rw1602 = 0;
- e1602 = 1;
- delay(4);
- e1602 = 0;
- }
- /*LCD1602初始化*/
- void init_1602(void)//
- {
- write_order(0x01); // 清屏幕
- write_order(0x38); // 数据长度为8位,双行显示,5*7字符
- write_order(0x0c); // 打开显示开关,光标不闪烁
- write_order(0x06); // 地址计数递增,显示屏不移动
- }
- /*LCD1602写位置*/
- void position(uchar row,uchar colum)
- {
- uchar pos;
- if(row==1)
- {
- pos=0x80+colum;
- }
- else
- {
- pos=0xC0+colum;
-
- }
- write_order(pos);
- }
- /*LCD1602字符串*/
- void write_byte(uchar *s)//
- {
- for(;*s!='\0';s++)
- {
- write_1602(*s);
- }
- }
- /*读LTC1865通道0的AD值*/
- void read_adc_ch0(void)
- {
- uchar j;
- voltage_data = 0;
- SDO = 1;
- CONV = 1; // 开启AD转换
- SCK = 1;
- CONV = 0;
- SCK = 0;
- for(j=15;j>0;j--)
- {
- SCK = 0;
- if(j == 15)
- {
- SDI = 1;
- }
- if(j == 14)
- {
- SDI = 0;
- }
- if(SDO==1)
- {
- voltage_data = voltage_data + 1;
- }
- voltage_data <<= 1;
- SCK = 1;
- }
- CONV = 1;
- SCK = 1;
- }
- /*读LTC1865通道1的AD值*/
- void read_adc_ch1(void)
- {
- uchar j;
- voltage_data = 0;
- SDO = 1;
- CONV = 1; // 开启AD转换
- SCK = 1;
- CONV = 0;
- SCK = 0;
- for(j=15;j>0;j--)
- {
- SCK = 0;
- if(j == 15)
- {
- SDI = 1;
- }
- if(j == 14)
- {
- SDI = 1;
- }
- if(SDO==1)
- {
- voltage_data = voltage_data + 1;
- }
- voltage_data <<= 1;
- SCK = 1;
- }
- CONV = 1;
- SCK = 1;
- }
- /* 显示通道电压*/
- void disp_voltage(uchar i)
- {
- switch(i)
- {
- case 0:
- read_adc_ch0();
- position(1,0);
- write_byte("CHANNEL0=");
- break;
- case 1:
- read_adc_ch1();
- position(1,0);
- write_byte("CHANNEL1=");
- break;
- }
- voltage_data = (voltage_data*5000)/65536; // AD采样值处理
- write_order(0x89);
- write_1602(disp_code[voltage_data/1000]);
- write_order(0x8b);
- write_1602(disp_code[voltage_data%1000/100]);
- write_order(0x8c);
- write_1602(disp_code[voltage_data%1000%100/10]);
- write_order(0x8d);
- write_1602(disp_code[voltage_data%1000%100%10]);
- }
- /* 发送通道电压*/
- void send_voltage(uchar i)
- {
- switch(i)
- {
- case 0:
- SBUF=67;while(!TI);TI=0;
- SBUF=72;while(!TI);TI=0;
- SBUF=65;while(!TI);TI=0;
- SBUF=78;while(!TI);TI=0;
- SBUF=78;while(!TI);TI=0;
- SBUF=69;while(!TI);TI=0;
- SBUF=76;while(!TI);TI=0;
- SBUF=48;while(!TI);TI=0;
- SBUF=61;while(!TI);TI=0;
- read_adc_ch0();
- break;
- case 1:
- SBUF=67;while(!TI);TI=0;
- SBUF=72;while(!TI);TI=0;
- SBUF=65;while(!TI);TI=0;
- SBUF=78;while(!TI);TI=0;
- SBUF=78;while(!TI);TI=0;
- SBUF=69;while(!TI);TI=0;
- SBUF=76;while(!TI);TI=0;
- SBUF=49;while(!TI);TI=0;
- SBUF=61;while(!TI);TI=0;
- read_adc_ch1();
- break;
- }
- voltage_data = (voltage_data*5000)/65536; // AD采样值处理
- SBUF=voltage_data/1000+48;while(!TI);TI=0;
- SBUF=46;while(!TI);TI=0;
- SBUF=voltage_data%1000/100+48;while(!TI);TI=0;
- SBUF=voltage_data%1000%100/10+48;while(!TI);TI=0;
- SBUF=voltage_data%1000%100%10+48;while(!TI);TI=0;
- SBUF=86;while(!TI);TI=0;
- SBUF=32;while(!TI);TI=0;
- SBUF=32;while(!TI);TI=0;
- }
- /*写入小数点和电压单位符号*/
- void disp_symbol(void)
- {
- position(1,10);
- write_1602(disp_dot);
- position(1,15);
- write_1602(disp_V);
-
- }
- void disp_logo()
- {
- unsigned char i;
- unsigned char *p;
- p="VOLTAGE MEASURE";
- delay(10);
- position(1,0);
- write_byte(" HELLO WORLD ");
- for(i=0;i<15;i++)
- {
- position(2,i);
- write_1602(*p);
- p++;
- delay(250);
- }
- delay(250);
- i=0;
- }
- void uart_init(void)
- {
- TMOD=0x20;
- SM0=0;
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
电压测量系统.rar
(66.47 KB, 下载次数: 77)
|