📄 lc._c
字号:
#include <iom16v.h>
#include <macros.h>
#include <stdlib.h>
#include "LCD.h"
#define C1 1 //C1=1uF
#define L1 100 //L1=100uH
unsigned int last_TCNT1,Cx,Lx;
unsigned int Cn; //分布电容,单位pf
unsigned char buf[8]="";
/*------------------------------函数原型声明------------------------------*/
void delay_nus (unsigned char n);
void delay_nms (unsigned char n);
void timer1_init (void);
void timer2_init (void);
void calc_Cn (void);
void calc_Cx (void);
void calc_Lx (void);
/*------------------------------------------------------------------------*/
void main(void)
{
unsigned char k=5;
/*----取消-----
OSCCAL=0xA9;
-------------*/
DDRA|=LCD_DATA; // 数据为输出
DDRC|=LCD_RS|LCD_EN; //置位RS和EN
DDRD=0x00;
PORTD=0xFF;
LCD_init();
while (--k)
delay_nms(100);
CLI(); //disable all interrupts
timer1_init();
timer2_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x40; //timer interrupt sources
SEI();
LCD_write_char(0x01,0);
delay_nms(2);
while(1);
}
#pragma interrupt_handler timer2_ovf_isr:5
void timer2_ovf_isr(void)
{
unsigned char PD;
TCCR2 = 0x00; //stop Timer1
TCCR1B= 0x00; //stop Timer2
last_TCNT1=TCNT1;
PD=PIND;
if(PD|0xFE==0xFE) //PORTD.0为0,校准模式,不接Cx和Lx
{
LCD_write_char(0x01,0);
delay_nms(2);
LCD_set_xy(0,0);
LCD_write_char(0,'C');
LCD_write_char(0,'n');
LCD_write_char(0,'=');
calc_Cn();
LCD_write_string(0,1,"TCNT1=");
LCD_write_string(1,1,itoa(buf,last_TCNT1,10));
}
else //测量模式,接入被测元件Cx或Lx
{
LCD_write_char(0x01,0);
delay_nms(2);
LCD_set_xy(0,0);
LCD_write_char(0,'C');
LCD_write_char(0,'x');
LCD_write_char(0,'=');
calc_Cx();
LCD_set_xy(0,1);
LCD_write_char(0,'L');
LCD_write_char(0,'x');
LCD_write_char(0,'=');
calc_Lx();
}
timer1_init();
timer2_init();
}
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0x00;
TCNT1L = 0x00;
TCCR1A = 0x00;
/*----取消-----
OCR1AH = 0x00;
OCR1AL = 0x39;
OCR1BH = 0x00;
OCR1BL = 0x39;
ICR1H = 0x00;
ICR1L = 0x39;
-------------*/
TCCR1B = 0x06; //start,外部下降沿触发
}
void timer2_init(void)
{
TCCR2 = 0x00; //stop
ASSR = 0x00; //同步计算模式
TCNT2 = 0x0A; //置初值,定时0.25s
/*----取消-----
OCR2=0x9C;
-------------*/
TCCR2 = 0x07; //start,系统时钟1024分频
}
void calc_Cn(void)
{
float f=1/(last_TCNT1*4)*1000000;
Cn=(1/(39.4384*f*f*L1)-C1)/1000000;
LCD_write_string(3,0,itoa(buf,Cn,10));
LCD_write_char(0,'p');
LCD_write_char(0,'F');
}
void calc_Cx(void)
{
float f=1/(last_TCNT1*4)*1000000;
Cx=(1/(39.4384*f*f*L1)-C1)/1000000-Cn;
LCD_write_string(3,1,itoa(buf,Cn,10));
LCD_write_char(0,'p');
LCD_write_char(0,'F');
}
void calc_Lx(void)
{
float f=1/(last_TCNT1*4)*1000000;
Lx=1/(39.4384*f*f*(C1+Cn*1000000));
LCD_write_string(3,1,itoa(buf,Cn,10));
LCD_write_char(0,'u');
LCD_write_char(0,'H');
}
/*--------------------------------延时函数--------------------------------*/
void delay_nus(unsigned char n) //N us延时函数
{
while (--n)
{
asm("nop");
}
}
void delay_1ms(void) //1ms延时函数
{
unsigned char i=10,j=100;
while (--i)
while (--j)
asm("nop");
}
void delay_nms(unsigned char n) //N ms延时函数
{
while (--n)
delay_1ms();
}
/*------------------------------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -