⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lc_meter.c

📁 电感表的程序和电路原理图。 ATmega8单片机。
💻 C
📖 第 1 页 / 共 2 页
字号:
/*****************************************************
S1:校正。     拨至“校正”位置,开始自校,等屏上显示频率稳定后拨至“测量”位置 ,显示标准电容和电感值。同时按B1和B2退出校正模式。
S2: 1-2接通。脉冲测量频率最大6MHz,不能调触发电平;2-3接通,脉冲测量频率最大1MHz,可调整触发电平,用T1设定,JP1短接显示屏显示触发电平大小。


B1: 
     电容测量时: 按下回零
     电感测量时: 按下回零
     频率测量时: 按下切换显示频率或周期时间
     脉冲测量时:按下切换显示频率+占宽比或周期时间+占宽比或高低电平时间
       计数器时:按下回零
     温度测量时:按下设定上下温度报警点

B2:
     频率测量时:按下按频率测量-〉脉冲测量-〉计数器-〉温度测量-〉频率测量 循环切换
     电容电感测量时:按下开始频率测量
     设定温度报警点时:设定温度加
B3:
     频率测量时:按下开始电容电感测量 
     电容电感测量时:测量电感或电容切换
     计数器时:按下暂停或重启
     设定温度报警点时:设定温度减


进入温度测量功能时查一遍18B20的个数,然后按查到的个数显示各个器件的温度。


Chip type           : ATmega88V
Clock frequency     : 16.000000 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 128
*****************************************************/

#include <mega88.h>
#include <delay.h>
#include <stdlib.h>
#include "lcd1602.h"
#asm
   .equ __w1_port=0x08 ;PORTC
   .equ __w1_bit=0
#endasm
//#include <1wire.h>
#include <ds18b20.h>
#define MAX_DS1820 2    // maximum number of DS1820 devices connected to the 1 Wire bus
#define TCNT1	(*(unsigned int *)0x84)
#define ICR1	(*(unsigned int *)0x86)
#define T0_OFF TCCR0B=0x00
#define T0_ON  TCCR0B=0x05
#define SET_T0_TIME TCNT0=0x64
#define T1_OFF TCCR1B=0x00
#define T1_ON  TCCR1B=0x07
#define C_stand 900.0    //标准电容值(pF)
#define B1 PIND.2
#define B2 PIND.3
#define B3 PIND.6
#define K1 PORTD.1
#define K2 PORTD.0
#define AD_IE_OFF ADCSRA&=~(1<<7)
#define AD_IE_ON  ADCSRA|=(1<<7)
#define AD_START  ADCSRA|=(1<<6)
unsigned char str[10];
unsigned char rom_codes[MAX_DS1820][9];
unsigned char alarm_rom_codes[MAX_DS1820][9];
unsigned char time0_expend;   //定时器0扩展计数器
unsigned char trig_time;      //频率闸门时间
unsigned char mode;
unsigned char tempture_mode;
unsigned char ds18B20_devices;
unsigned char temp_tp,temp_bs,;
unsigned int  top_time,base_time,count_h;
unsigned char Capture_div;
float   trig_voltage;
unsigned long frequnce;
bit  t1_over_flower;
bit  error_over_flower;
bit  lower_pri;
bit  save_stray;
bit  init_senor;
unsigned char  mode_using;
bit test_complete;
bit zero; 
eeprom float c1;          
eeprom float l1;
eeprom signed int alarm_t1_max,alarm_t1_min,alarm_t2_max,alarm_t2_min;
eeprom unsigned long f1;
signed int alarm[4];
float cs;
float ls;
float cx;
float lx;


void int_inital_on(void)
{
  EICRA=0x0A;
  EIFR=0x03;
  EIMSK=0x03;
  PCIFR=0x04;
  PCICR=0x04;
  PCMSK2=0x40;
 
}
void int_inital_off(void)
{
  EIMSK=0x00;
  EIFR=0x03;
  PCMSK2=0x00;
  PCIFR=0x04;
}
void cal_set_par(unsigned long f2)
{
 c1=C_stand*f2*f2/((float)f1*f1-(float)f2*f2);
 l1=(1000000000000000000.0/(39.4784176*f1*f1*c1));
}

float cal_c(unsigned long f2)
{
 float temp;
 temp=(((float)f1*f1)/((float)f2*f2)-1)*c1;
 //temp=((1.0/(39.4784176*l1*(1.0E-6)*f2*f2)))*(1.0E12)-c1;
 return temp;
}

float cal_l(unsigned long f2)
{
 float temp;
 temp=(((float)f1*f1)/((float)f2*f2)-1)*l1;
 //temp=(1.0/(39.4784176*c1*(1.0E-12)*f2*f2))*(1.0E6)-l1;
 return temp;
}

void disp_trig_vol(void)
{
 LCD_put_str(0,0,"Trig:    V/    S");
 ftoa(trig_voltage,2,str);
 LCD_put_str2(5,0,str);
 ftoa(trig_time/100.0,2,str);
 LCD_put_str2(11,0,str);
 }

void disp_fr_vol(unsigned char x,unsigned char y,float frec)
{
   if(frec<1000)
       {
        ftoa(frec,2,str);
        LCD_put_str2(x,y,str);
       }
   else
       {
        if(frec<1000000)
          {
            ftoa(frec/1000.0,3,str);
            LCD_put_str2(x,y,str);
            LCD_put_c('K');
          }
       else
          {
            ftoa(frec/1000000.0,4,str);
            LCD_put_str2(x,y,str);
            LCD_put_c('M');
          }   
      }
   LCD_put_c('H');
   LCD_put_c('z');
}

void disp_fr(void)
{
   
           delay_ms(200/trig_time);
           LCD_put_str(0,1,"                ");
           LCD_put_str(0,1,"Freq=");
           disp_fr_vol(5,1,frequnce);
           if(lower_pri==1)
              {
               if(trig_time<100)
                  trig_time*=10;
               else
                  trig_time=200;   
              } 
          
}

void disp_cycle(void)
{
   
           delay_ms(200/trig_time);
           LCD_put_str(0,1,"                ");
           LCD_put_str(0,1,"Cycle=");
           
           if(frequnce<1000)
             {
              if(frequnce==0)
                  {
                   LCD_goto_xy(7,1);
                   LCD_put_c(1);
                   LCD_put_c(' ');
                  }
              else
                  {
                   ftoa(1.0E3/frequnce,3,str);
                   LCD_put_str2(6,1,str);
                   LCD_put_c('m'); 
                  }  
             }
           else
             {
              if(frequnce<1000000)
                 {
                   ftoa(1.0E6/frequnce,3,str);
                   LCD_put_str2(6,1,str);
                   LCD_put_c('u');
                 }
              else
                 {
                   ftoa(1.0E9/frequnce,3,str);
                   LCD_put_str2(6,1,str);
                    LCD_put_c('n');
                 }   
             }
           LCD_put_c('S');
           if(lower_pri==1)
              {
               if(trig_time<100)
                  trig_time*=10;
               else
                  trig_time=200;   
              } 
          
}

void frequnce_test(void)
{
 error_over_flower=1;
 while(error_over_flower==1)
  {
  AD_IE_OFF;
  T0_OFF;
  T1_OFF;
  TIMSK0=0;
  TIMSK1=0;
  TIFR0|=1;
  TIFR1|=1;
  
  t1_over_flower=0;
  lower_pri=1;
  
  SET_T0_TIME;
  TCNT1H=0;
  TCNT1L=0;
 
 
  TIMSK1=0x01;
  TIMSK0=0x01;
  T1_ON;
  T0_ON;
  time0_expend=0;
  while(time0_expend<trig_time);
  AD_IE_ON;
  if(error_over_flower==1)
          {
           if(trig_time>1)
               if(trig_time==200)
                  trig_time=100;
               else
                  trig_time/=10;    
           else
               {
                LCD_clear();
                LCD_put_str(0,1,"Freq= ");
                LCD_put_c(1);
                delay_ms(1000);
               }
          }
  }           
}
void disp_time(unsigned char x,unsigned  char y,float time)
{
if(time<1000)
    {
     ftoa(time,3,str);
     LCD_put_str2(x,y,str); 
     LCD_put_c('u');
    }
else
    {
     if(time<1000000)
         {
          ftoa(time/1000,3,str);
          LCD_put_str2(x,y,str); 
          LCD_put_c('m');
         
         }
     else
         {
          ftoa(time/1000000,3,str);
          LCD_put_str2(x,y,str); 
           
         } 
     } 
 
LCD_put_c('S');               
if(time<100)
    {
      LCD_put_c(1);
    }
}

void disp_duty1(void)
{
float temp_b;
temp_b=100.0*(top_time+2.6)/((float)top_time+base_time+4.938);
if(temp_b>99.99)
       temp_b=99.99;
if(temp_b==0)
       temp_b=0.01;    
ftoa(temp_b,2,str);
LCD_put_str2(6,0,str);
LCD_put_c(' ');
LCD_put_c('%');
}
void disp_duty(void)
{
unsigned int temp;
float temp_t,temp_b;

switch (Capture_div)
     {
     case 1:
        {
         temp=1;
        }break;
     case 2:
        {
         temp=8;
        }break;   
     case 3:
        {
         temp=64;
        }break;
     case 4:
        {
          temp=256;
        }break;
     case 5:
        {
          temp=1024;
        }break; 
     default: break;      
     }
 
 switch (mode_using)
     {
      case 0:
          {
           temp_t=(temp*((float)top_time+base_time))/16+4.938;
           if(temp_t==0)
                temp_t=1.0E8;
           LCD_put_str(0,0," Duty=          "); 
           LCD_put_str(0,1,"Frequ=          "); 
           disp_fr_vol(6,1,1.0E6/temp_t);
           disp_duty1();
           
          }break;
      case 1:
          {
           temp_t=(temp*((float)top_time+base_time))/16+4.938;
           LCD_put_str(0,0," Duty=          "); 
           LCD_put_str(0,1,"Cycle=          "); 
           disp_time(6,1,temp_t);
           disp_duty1();
          
          }break;
      default:
          {
           LCD_put_str(0,0,"Time_T:          "); 
           LCD_put_str(0,1,"Time_B:          "); 
           temp_t=(temp*(float)top_time)/16+2.6;
           disp_time(7,0,temp_t);
           temp_t=(temp*(float)base_time)/16+2.338;
           disp_time(7,1,temp_t); 
          
          }break;            
      }
 
 
}

void duty_test(void)
{
 unsigned char i;
 test_complete=0; 
 AD_IE_OFF;
 TCCR1B=0x00;
 TIFR1=0xFF;
 
 TCNT1H=0x00;
 TCNT1L=0x00;
 count_h=0;
 TIMSK1=0x21;
 temp_tp=Capture_div|0x40;
 temp_bs=Capture_div;
 i=0;
 //disp_duty(); 
 while(test_complete==0&&mode==3)
   { 
    delay_ms(200);
    i++;
    if(i>50)
       {
        LCD_clear();
        LCD_put_str(0,0,"Frequnce Too    ");
        LCD_put_str(0,1,"Lower!");
        
       } 
   }
 
 AD_IE_ON;   
 if(t1_over_flower==0)
    {
     disp_duty();           
     if((top_time<8000)&&(base_time<8000)&&(Capture_div>1))
       {
         Capture_div--;
         
         TCCR1B|=Capture_div;
       } 
    }
 else
    {
     if(Capture_div<5)
           {
            Capture_div++;
            TCCR1B|=Capture_div;
            t1_over_flower=0;
           } 
      else
           {
            error_over_flower=1;
           }    
     }  
 
}

 
void count_test(void)
{
 
 AD_IE_OFF;
 TCCR1B=0x00;
 count_h=0;
 TCNT1H=0x00;
 TCNT1L=0x00;
 mode_using=1;
 TIMSK1=0x01;
} 
void self_Calibrating(void)
{
unsigned char i;
unsigned long f2_temp;
unsigned long f_temp[4];
LCD_put_str(0,0,"Self-Calibrating"); 
i=0;
int_inital_off();
while(i==0)
     {
      if(PIND.4==0)
        {
         frequnce_test();
         disp_fr();
         delay_ms(100);
         if(PIND.4==0)  
             {
                f2_temp=frequnce;
             }
         else
             {
              delay_ms(200);
              if(PIND.4==1)
                  i=1; 
             }    
        }     
     }
delay_ms(2000);
for(i=0;i<4;i++)
  {
   frequnce_test();
   f_temp[i]=frequnce;
  }
f1=(f_temp[0]+f_temp[1]+f_temp[2]+f_temp[3])/4;
cal_set_par(f2_temp);
LCD_put_str(0,0,"Calibrated:C1/L1"); 
LCD_put_str(0,1,"     pF /     uH"); 
ftoa(c1,1,str);
LCD_put_str2(0,1,str); 
ftoa(l1,1,str);
LCD_put_str2(10,1,str); 
while(B1||B2) ;   
int_inital_on();
}

void test_c(void)
{
frequnce_test();
if(save_stray==1)
  {
   save_stray=0;
   cs=cal_c(frequnce);
  }
cx=cal_c(frequnce)-cs;

LCD_clear();
LCD_put_str(0,0,"Capacitance:");
LCD_goto_xy(0,1);
LCD_put_c('C');
LCD_put_c('x');
LCD_put_c('=');
if(cx<1000)
    {
     ftoa(cx,1,str);
     LCD_put_str2(3,1,str); 
     LCD_put_c(' ');
     LCD_put_c('p');
    }
else
    {
     if(cx<1000000)
       {
        ftoa(cx/1000.0,3,str);
        LCD_put_str2(3,1,str); 
        LCD_put_c(' ');
        LCD_put_c('n');
       }
     else
       {
        ftoa(cx/1000000.0,3,str);
        LCD_put_str2(3,1,str); 
        LCD_put_c(' ');
        if(cx>1500000)
           {
            LCD_put_c(1);
            LCD_put_c(' ');
           }
        LCD_put_c('u');
         
       }   
   }    
LCD_put_c('F');
}

void test_l(void)
{

frequnce_test();
if(save_stray==1)
  {
   save_stray=0;
   ls=cal_l(frequnce);
  }
lx=cal_l(frequnce)-ls;
LCD_clear();
LCD_put_str(0,0,"Inductor:");
LCD_goto_xy(0,1);
LCD_put_c('L');
LCD_put_c('x');
LCD_put_c('=');
if(lx<1000)
    {
     ftoa(lx,3,str);
     LCD_put_str2(3,1,str); 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -