rlc测量电容电感电阻,麻烦大神把里面的lcd程序改成数码管程序。谢谢了!
单片机源程序如下:
- #include <reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define C1 0.045
- uchar code title1[]="欢迎使用 ";
- uchar code title2[]="LRC 万用表 ";
- uchar code code_L[]="电感: ";
- uchar code code_R[]="电阻: ";
- uchar code code_C[]="电容: ";
- uchar code code_e[]="错误: ";
- uchar num;
- sbit key1 =P1^0;
- sbit key2 =P1^1;
- sbit key3 =P1^2;
- float LZ=0;
- float RX=0;
- float CX=0;
- float f=0;
- float temp_f[4];
- unsigned int tt,th1,tl1;
- unsigned int pluse=0;
- bit flag=0;
- unsigned char key_num=0;
- #define L 1
- #define R 2
- #define C 3
- sbit aa1 =P2^0;
- sbit bb1 =P2^1;
- #include<Lcd12864.h>
- void cd4051(unsigned char num)
- {
- switch(num)
- {
- case 1: bb1=0;aa1=0;break;
- case 2: bb1=0;aa1=1; break;
- case 3: bb1=1;aa1=0; break;
- default : aa1=1;aa1=1;break;
- }
- }
- void scan_key()
- {
- if(!key1){
- if(!key1){
- while(!key1);
- key_num=1;
- cd4051(L);
- Lcd_ShowSring(4,0,code_L);
- Lcd_ShowSring(1,15,"L");
- }
- }
- if(!key2){
- if(!key2){
- while(!key2);
- key_num=2;
- cd4051(R);
- Lcd_ShowSring(4,0,code_R);
- Lcd_ShowSring(1,15,"R");
- }
- }
- if(!key3){
- if(!key3){
- while(!key3);
- key_num=3;
- cd4051(C);
- Lcd_ShowSring(4,0,code_C);
- Lcd_ShowSring(1,15,"C");
- }
- }
- }
- void init() //中断初始化
- {
- //定时器0和定时器1都是方式1,16位定时器
- //定时器设置
- TMOD=0x51; //设置定时器0,方式1:16位定时器
- TH1=0x00; //定时器1高位装初值
- TL1=0x00; //定时器1低位装初值
- ET1=1; //定时器1 使能位
-
- TH0=(65535-50000)/255; //定时器1高位装初值
- TL0=(65535-50000)%255;; //定时器1高位装初值
- ET0=1; ////定时器0 使能位
- TR0=1;
- TR1=1;
- EA=1;// 总中断
- }
- void Measurement_L();//电感测量
- void Measurement_R();//电阻测量
- void Measurement_C();//电容测量
- unsigned char gather_num=0;
- unsigned char m_second=0;
- void main ()
- {
- LCDInit();
- Lcd_ShowSring(1,0,title1);
- Lcd_ShowSring(2,0,title2);
- init();
- while(1){
- scan_key();
- if(flag){
- flag=0;
- //在十ms里面记了这么多个脉冲
- // 那么一个脉冲的周期就是 50ms/tt /1000 (s) 化简得到 f 20tt
- if(gather_num<4) {
- temp_f[gather_num]=pluse*65536+256*TH1+TL1;
- gather_num++;
- }
- else{
- gather_num=0;
- f=(temp_f[0]+temp_f[1]+temp_f[2]+temp_f[3])/4.0;
- switch(key_num) {
- case 1:Measurement_L(); break;
- case 2:Measurement_R(); break;
- case 3:Measurement_C(); break;
- default :break;
- }
- }
- TH0=(65535-50000)/255; //定时器1高位装初值
- TL0=(65535-50000)%255;; //定时器1高位装初值
- TH1=0; //清零开始重新计数
- TL1=0;
- pluse=0;
- TR0=1;//开定时器计时10ms;//使能位外部中断0 数10内有多少个脉冲
- TR1=1;//进行新一轮的计数
- m_second=0;
- }
- }
- }
- void time0()interrupt 1 //定时器0 服务
- {
- TH0=60; //定时器1高位装初值
- TL0=235;; //定时器1高位装初值
- m_second++;
- if(m_second==20){
- TR1=0;//关掉定时器1
- TR0=0; // 关掉定时器0
- m_second=0;
- flag=1;
- }
- }
- void time1()interrupt 3 //定时器1 服务
- {
- pluse++;
- TH1=0;
- TL1=0;
- }
- void Measurement_L()
- {
- if(f==0){
- LZ=0;
- Lcd_ShowSring(4,0,code_L);
- }
- else {
- LZ=5.63466627*10e10/f/f;
- LZ=LZ/1.0727272727272727272727272727273;
- Lcd_ShowSring(4,0,"电感:");
- Lcd_Display_u(4,4,LZ);
- }
-
- if(LZ>99999999) {
- }
- }
- void Measurement_R()//电阻测量
- {
- if(f==0){
- RX=0;
- }
- else if( 0< f&& f<30) { //1M 7
- RX= 14400000.0/f;
- RX= RX-3000;
- RX =RX/2/0.95; //修正
- }
- else if(f <700) { //10k 650
- RX= 14400000.0/f;
- RX= RX-3000;
- RX =RX/2/0.95; //修正
- }
- else if(f <1500) { //4.7k 1209
- RX= 14400000.0/f;
- RX= RX-3000;
- RX =RX/2/0.94; //修正
- }
- else if(f <3000) { //1k 2983
- RX= 14400000.0/f;
- RX= RX-3000;
- RX =RX/2/0.91; //修正
- //0.91286863270777479892761394101877
- }
- else if(f <4500) {
- RX= 14400000.0/f;
- RX= RX-3000;
- RX =RX/2/0.74; //修正
- }
- else if(f <5000) {
- RX= 14400000.0/f;
- RX= RX-3000;
- //修正 100
- RX =RX/2/0.50; //修正
- }
- if(RX==0){
- Lcd_ShowSring(4,0,code_R);
- } else{
- Lcd_ShowSring(4,0,"电阻:");
- Value_to_ASCII1(4,3, RX);
- }
- if(RX>99999999) {
- // Lcd_ShowSring(4,0,code_e);
- }
- }
- void Measurement_C()//C测量
- {
- if(f==0){
- CX=0;
- Lcd_ShowSring(4,0,code_C);
- }
- else if(f>5000){
- CX=0;
- Lcd_ShowSring(4,0,code_C);
- }
- else {
- CX=481000.0/f/1.0276595744680851063829787234043;//修正
- Lcd_ShowSring(4,0,"电容:");
- Value_to_ASCIIC(4,3,CX);
- }
-
- if(CX>99999999) {
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////********************************************
- Description: LCD12864 驱动 ,采用串行方式,
- ********************************************/
- //******12864**************
- sbit lcd_RS = P0^2;
- sbit Lcd_Rw = P0^1;
- sbit Lcd_En = P0^0;
- #define BIT(n) (1<<(n))
- #define SID_0 Lcd_Rw=0;
- #define SID_1 Lcd_Rw=1;
- #define SCLK_0 Lcd_En=0;
- #define SCLK_1 Lcd_En=1;
- //15脚 PSB 接低电平
- //函数 delay(uchar z)
- //功能能 延时
- void Delay(uchar z)
- {
- uchar a,b;
- for(a=z;a>0;a--)
- for(b=200;b>0;b--);
- }
- void SendByte(uchar bye)
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- if((bye<<i)&0x80){SID_1;}
- else {SID_0;}
- SCLK_1;
- SCLK_0;
- }
- }
- void Lcd_WriteCom(uchar CMD)
- {
- SendByte(0xf8);
- SendByte(CMD&0xf0);
- SendByte((CMD<<4)&0xf0);
- }
- void Lcd_WriteDate(uchar Data)
- { SendByte(0xf8+2);
- SendByte(Data&0xf0);
- SendByte((Data<<4)&0xf0);
- }
- void LCDInit()
- {/* RST=0;
- delay1MS(50);
- RST=1; */
- lcd_RS=1;
- Lcd_WriteCom(0x34);//功能设置,一次送8位数据,基本指令集
- Lcd_WriteCom(0x30);//0000,1100 整体显示,游标off,游标位置off
- Lcd_WriteCom(0x01);//0000,0001 清DDRAM
- Lcd_WriteCom(0x06);//0000,0010 DDRAM地址归位
- Lcd_WriteCom(0x0c);//1000,0000 设定DDRAM 7位地址000,0000到地址计数器AC//
- }
- /*****************************
- 发送字符串
- *****************************/
- //void show_str(uchar const *s)//改AVR去掉const
- void Lcd_String(const uchar *str)
- { while(*str !='\0')
- {
- Lcd_WriteDate(*str++);
- }
- }
- ////////////////////////////////////////////////////////////////////
- //函数 Lcd_init(uchar com)
- //功能 精确屏幕显示数据
- //参数 line 液晶屏的第几行 list 第几个位置开始写 (最多8个列) Date要写的数据
- void Lcd_Display(uchar line,uchar list,uint Date)
- {
- uchar i;
- uchar ta[]={0,0,0,0,0,'\0'}; //用一个数组来装数
- list=list &0x07; //限制在 0-7列
- switch(line){
- case 1: Lcd_WriteCom(0x80+list);break; //第一行
- case 2: Lcd_WriteCom(0x90+list);break; //第二行
- case 3: Lcd_WriteCom(0x88+list);break; //第三行
- case 4: Lcd_WriteCom(0x98+list);break; //第四行
- default : break;
-
- }
- for(i=5;i>0;i--)
- { //循环四次把四个数从高到低装入ta
- ta[i-1]=Date%10+48;
- Date=Date/10;
- }
- Lcd_String(ta);
- }
- ////////////////////////////////////////////////////////////////////
- //函数 Lcd_init(uchar com)
- //功能 精确屏幕显示数据
- //参数 line 液晶屏的第几行 list 第几个位置开始写 (最多8个列) Date要写的数据
- void Lcd_Display_u(uchar line,uchar list,unsigned long Date)
- {
- uchar i;
- uchar ta[]={0,0,0,0,0,'\0'}; //用一个数组来装数
- list=list &0x07; //限制在 0-7列
- switch(line){
- case 1: // Lcd_WriteCom(0x80+list);Lcd_String(" ");
- Lcd_WriteCom(0x80+list);break; //第一行
- case 2: // Lcd_WriteCom(0x90+list);Lcd_String(" ");
- Lcd_WriteCom(0x90+list);break; //第二行
- case 3: // Lcd_WriteCom(0x88+list);Lcd_String(" ");
- Lcd_WriteCom(0x88+list);break; //第三行
- case 4: //Lcd_WriteCom(0x98+list);Lcd_String(" ");
- Lcd_WriteCom(0x98+list);break; //第四行
- default : break;
-
- }
- for(i=5;i>0;i--)
- { //循环四次把四个数从高到低装入ta
- ta[i-1]=Date%10+48;
- Date=Date/10;
- }
- //循环四次把四个数从高到低装入ta
- if(ta[0]!=48){
- Lcd_WriteDate(ta[0]);
- Lcd_WriteDate(ta[1]);
- Lcd_WriteDate(ta[2]);
- Lcd_WriteDate(ta[3]);
- Lcd_WriteDate(ta[4]);
- Lcd_WriteDate('u');
- Lcd_WriteDate('H');
-
- }
- else{
- if(ta[1]!=48){
- Lcd_WriteDate(ta[1]);
- Lcd_WriteDate(ta[2]);
- Lcd_WriteDate(ta[3]);
- Lcd_WriteDate(ta[4]);
- Lcd_WriteDate('u');
- Lcd_WriteDate('H');
- Lcd_WriteDate(' ');
- }
- else{
- if(ta[2]!=48){
- Lcd_WriteDate(ta[2]);
- Lcd_WriteDate(ta[3]);
- Lcd_WriteDate(ta[4]);
- Lcd_WriteDate('u');
- Lcd_WriteDate('H');
- Lcd_WriteDate(' ');
- Lcd_WriteDate(' ');
- }
- else{
- if(ta[3]!=48){
- Lcd_WriteDate(ta[3]);
- Lcd_WriteDate(ta[4]);
- Lcd_WriteDate('u');
- Lcd_WriteDate('H');
- Lcd_WriteDate(' ');
- Lcd_WriteDate(' ');
- Lcd_WriteDate(' ');
- }
- else{
-
- Lcd_WriteDate(ta[4]);
- Lcd_WriteDate('u');
- Lcd_WriteDate('H');
- Lcd_WriteDate(' ');
- Lcd_WriteDate(' ');
- Lcd_WriteDate(' ');
- Lcd_WriteDate(' ');
- }
-
- }
-
-
- }
-
- }
-
- }
- ////////////////////////////////////////////////////////////////////
- //函数 Lcd_init(uchar com)
- //功能 精确屏幕显示字符串
- //参数 line 液晶屏的第几行 list 第几个位置开始写 (最多8个列) *p要写的字符串
- void Lcd_ShowSring(uchar line,uchar list,uchar*p)
- {
- switch(line){
- case 1: Lcd_WriteCom(0x80+list);break; //第一行
- case 2: Lcd_WriteCom(0x90+list);break; //第二行
- case 3: Lcd_WriteCom(0x88+list);break; //第三行
- case 4: Lcd_WriteCom(0x98+list);break; //第四行
- default : break;
-
- }
- Lcd_String(p);
- }
- void Value_to_ASCII1(uchar line,uchar list,unsigned long value) //欧姆级
- {
- unsigned char temp[] = "00000000欧";
- temp[0] = value/10000000%10 + 0x30;
- temp[1] = value/1000000%10 + 0x30;
- temp[2] = value/100000%10 + 0x30;
- temp[3] = value/10000%10 + 0x30;
- temp[4] = value/1000%10 + 0x30;
- temp[5] = value/100%10 + 0x30;
- temp[6] = value/10%10 + 0x30;
- temp[7] = value%10 + 0x30;
- if(temp[0]==0x30) {
- temp[0]=' ';
- if(temp[1]==0x30) {
- temp[1]=' ';
- if(temp[2]==0x30) {
- temp[2]=' ';
- if(temp[3]==0x30) {
- temp[3]=' ';
- if(temp[4]==0x30) {
- temp[4]=' ';
- if(temp[5]==0x30) {
- temp[5]=' ';
- if(temp[6]==0x30) {
- temp[6]=' ';
- }
- }
- }
- }
-
- }
- }
- }
- Lcd_ShowSring( line,list,temp) ;
-
- }
- void Value_to_ASCIIC(uchar line,uchar list,unsigned long value)
- {
- unsigned char temp[] = "00000000nF";
- temp[0] = value/10000000%10 + 0x30;
- temp[1] = value/1000000%10 + 0x30;
- temp[2] = value/100000%10 + 0x30;
- temp[3] = value/10000%10 + 0x30;
- temp[4] = value/1000%10 + 0x30;
- temp[5] = value/100%10 + 0x30;
- temp[6] = value/10%10 + 0x30;
- temp[7] = value%10 + 0x30;
- if(temp[0]==0x30) {
- temp[0]=' ';
- if(temp[1]==0x30) {
- temp[1]=' ';
- if(temp[2]==0x30) {
- temp[2]=' ';
- if(temp[3]==0x30) {
- temp[3]=' ';
- if(temp[4]==0x30) {
- temp[4]=' ';
- if(temp[5]==0x30) {
- temp[5]=' ';
- if(temp[6]==0x30) {
- temp[6]=' ';
- }
- }
- }
- }
-
- }
- }
- }
- Lcd_ShowSring( line, list,temp) ;
- }
复制代码
所有资料51hei提供下载:
C程序.doc
(63 KB, 下载次数: 7)
|