?? lc_meter.c
字號:
/*****************************************************
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 + -