|
rlc測(cè)量電容電感電阻,麻煩大神把里面的lcd程序改成數(shù)碼管程序。謝謝了!
單片機(jī)源程序如下:
- #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[]="錯(cuò)誤: ";
- 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() //中斷初始化
- {
- //定時(shí)器0和定時(shí)器1都是方式1,16位定時(shí)器
- //定時(shí)器設(shè)置
- TMOD=0x51; //設(shè)置定時(shí)器0,方式1:16位定時(shí)器
- TH1=0x00; //定時(shí)器1高位裝初值
- TL1=0x00; //定時(shí)器1低位裝初值
- ET1=1; //定時(shí)器1 使能位
-
- TH0=(65535-50000)/255; //定時(shí)器1高位裝初值
- TL0=(65535-50000)%255;; //定時(shí)器1高位裝初值
- ET0=1; ////定時(shí)器0 使能位
- TR0=1;
- TR1=1;
- EA=1;// 總中斷
- }
- void Measurement_L();//電感測(cè)量
- void Measurement_R();//電阻測(cè)量
- void Measurement_C();//電容測(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里面記了這么多個(gè)脈沖
- // 那么一個(gè)脈沖的周期就是 50ms/tt /1000 (s) 化簡(jiǎn)得到 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; //定時(shí)器1高位裝初值
- TL0=(65535-50000)%255;; //定時(shí)器1高位裝初值
- TH1=0; //清零開始重新計(jì)數(shù)
- TL1=0;
- pluse=0;
- TR0=1;//開定時(shí)器計(jì)時(shí)10ms;//使能位外部中斷0 數(shù)10內(nèi)有多少個(gè)脈沖
- TR1=1;//進(jìn)行新一輪的計(jì)數(shù)
- m_second=0;
- }
- }
- }
- void time0()interrupt 1 //定時(shí)器0 服務(wù)
- {
- TH0=60; //定時(shí)器1高位裝初值
- TL0=235;; //定時(shí)器1高位裝初值
- m_second++;
- if(m_second==20){
- TR1=0;//關(guān)掉定時(shí)器1
- TR0=0; // 關(guān)掉定時(shí)器0
- m_second=0;
- flag=1;
- }
- }
- void time1()interrupt 3 //定時(shí)器1 服務(wù)
- {
- 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()//電阻測(cè)量
- {
- 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測(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 驅(qū)動(dòng) ,采用串行方式,
- ********************************************/
- //******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 接低電平
- //函數(shù) delay(uchar z)
- //功能能 延時(shí)
- 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);//功能設(shè)置,一次送8位數(shù)據(jù),基本指令集
- Lcd_WriteCom(0x30);//0000,1100 整體顯示,游標(biāo)off,游標(biāo)位置off
- Lcd_WriteCom(0x01);//0000,0001 清DDRAM
- Lcd_WriteCom(0x06);//0000,0010 DDRAM地址歸位
- Lcd_WriteCom(0x0c);//1000,0000 設(shè)定DDRAM 7位地址000,0000到地址計(jì)數(shù)器AC//
- }
- /*****************************
- 發(fā)送字符串
- *****************************/
- //void show_str(uchar const *s)//改AVR去掉const
- void Lcd_String(const uchar *str)
- { while(*str !='\0')
- {
- Lcd_WriteDate(*str++);
- }
- }
- ////////////////////////////////////////////////////////////////////
- //函數(shù) Lcd_init(uchar com)
- //功能 精確屏幕顯示數(shù)據(jù)
- //參數(shù) line 液晶屏的第幾行 list 第幾個(gè)位置開始寫 (最多8個(gè)列) Date要寫的數(shù)據(jù)
- void Lcd_Display(uchar line,uchar list,uint Date)
- {
- uchar i;
- uchar ta[]={0,0,0,0,0,'\0'}; //用一個(gè)數(shù)組來裝數(shù)
- 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--)
- { //循環(huán)四次把四個(gè)數(shù)從高到低裝入ta
- ta[i-1]=Date%10+48;
- Date=Date/10;
- }
- Lcd_String(ta);
- }
- ////////////////////////////////////////////////////////////////////
- //函數(shù) Lcd_init(uchar com)
- //功能 精確屏幕顯示數(shù)據(jù)
- //參數(shù) line 液晶屏的第幾行 list 第幾個(gè)位置開始寫 (最多8個(gè)列) Date要寫的數(shù)據(jù)
- void Lcd_Display_u(uchar line,uchar list,unsigned long Date)
- {
- uchar i;
- uchar ta[]={0,0,0,0,0,'\0'}; //用一個(gè)數(shù)組來裝數(shù)
- 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--)
- { //循環(huán)四次把四個(gè)數(shù)從高到低裝入ta
- ta[i-1]=Date%10+48;
- Date=Date/10;
- }
- //循環(huán)四次把四個(gè)數(shù)從高到低裝入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(' ');
- }
-
- }
-
-
- }
-
- }
-
- }
- ////////////////////////////////////////////////////////////////////
- //函數(shù) Lcd_init(uchar com)
- //功能 精確屏幕顯示字符串
- //參數(shù) line 液晶屏的第幾行 list 第幾個(gè)位置開始寫 (最多8個(gè)列) *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) //歐姆級(jí)
- {
- 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) ;
- }
復(fù)制代碼
所有資料51hei提供下載:
C程序.doc
(63 KB, 下載次數(shù): 7)
2019-6-20 08:55 上傳
點(diǎn)擊文件名下載附件
|
|