|
______
IN)----|__100_|-------
______ | | |
REF)----|__10K_|------------| |---(GND)
______ | | |
RX)---------|__RT _|-------
![](http://cache.amobbs.com/bbs_upload782111/files_9/ourdev_261002.jpg)
(原文件名:测温.jpg)
使用夏普传真上拆的LCD的测试图;
这是主程序,不含RT转换函数和LCD显示,稍加修改就能用:
#include <avr/io.h> //MCU为ATmega48
#include <stdio.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <avr/sleep.h>
#include "lcd.h"
#include "lcdTimer.h"
#include "r2t.h"
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
//电阻测试引脚
#define RESPOR PORTD
#define RESDDR DDRD
#define RESPIN PIND
#define INP 2
#define RXP 1
#define RFP 0
//开始计时
#define STARCT() TCCR1B = 0x01; //1=不分频;2=8分频内部计数器
unsigned char buff[17];
#define TESTN 10 //测试平均数
int Vout;
ISR(SIG_OUTPUT_COMPARE0A)
{
uint ms;
ms++;
if(ms > Vout) LCD_write_char(15,'1');
else LCD_write_char(15,'0');
if(ms > 5000) ms = 0;
}
ISR(SIG_INTERRUPT0)
{
//停止计数
TCCR1B = 0;
EIMSK &= ~_BV(INT0); //禁用外部中断0
}
void resinit(void)
{
///////////////////////////////////////////
/////////////电阻测试初始化////////////////
//端口设置为输入
RESPOR &= ~(_BV(RFP) | _BV(INP) | _BV(RXP));
RESDDR &= ~(_BV(RFP) | _BV(INP) | _BV(RXP));
//定时器设置
TCCR1A = 0x00;
//TCCR1B = 0x02; //8分频内部计数器
TCCR1C = 0x00;
//TIMSK1 |= _BV(TOIE1); //开定时中断;
TIFR1 |= _BV(TOV1); //清定时中断;
//输入中断设置
EICRA = 0x2; //外部中断0为引脚下降沿触发
EIFR |= _BV(INT0);
EIMSK |= _BV(INT0); //允许外部中断0
}
void testrf(void)
//测试基准电阻
{
////////////////电阻时间测试//////////////////
//端口设置为输入
RESPOR &= ~(_BV(RFP) | _BV(INP) | _BV(RXP));
RESDDR &= ~(_BV(RFP) | _BV(INP) | _BV(RXP));
//放电
RESDDR |= _BV(INP);
RESPOR |= _BV(INP);
Delay_10ms(50);
RESDDR &= ~_BV(INP);
RESPOR &= ~_BV(INP);
//清零
TCNT1 = 0;
//开外部中断
EIMSK |= _BV(INT0);
EIFR |= _BV(INT0);
EIMSK |= _BV(INT0); //允许外部中断0
//充电
RESPOR &= ~_BV(RFP);
RESDDR |= _BV(RFP);
//开始计数
STARCT();
//等待转换结果
while((RESPIN & _BV(INP)))
{
set_sleep_mode(0);
}
}
void testrx(void)
//测试输入电阻
{
////////////////电阻时间测试//////////////////
//端口设置为输入
RESPOR &= ~(_BV(RFP) | _BV(INP) | _BV(RXP));
RESDDR &= ~(_BV(RFP) | _BV(INP) | _BV(RXP));
//放电
RESDDR |= _BV(INP);
RESPOR |= _BV(INP);
Delay_10ms(50);
RESDDR &= ~_BV(INP);
RESPOR &= ~_BV(INP);
//清零
TCNT1 = 0;
//开外部中断
EIMSK |= _BV(INT0);
EIFR |= _BV(INT0);
EIMSK |= _BV(INT0); //允许外部中断0
//充电
RESPOR &= ~_BV(RXP);
RESDDR |= _BV(RXP);
//开始计数
STARCT();
//等待转换结果
while((RESPIN & _BV(INP))) set_sleep_mode(0);
}
int main(void)
{
timers_init(); //初始化系统定时器
LCD_init(); //初始化LCD1602
write_command(0x01); //清屏--对应清屏指令
Delay_10ms(2);
resinit(); //初始化电阻测试
sei();
while(1)
{
float tmprf,tmprx,tmp;
uint bufrf[TESTN],bufrx[TESTN],h,l;
uchar i,tn;
if(++tn >=TESTN) tn = 0;
testrf();
bufrf[tn] = TCNT1;
testrx();
bufrx[tn] = TCNT1;
tmprf = 0;
for(i=0;i<TESTN;i++)
{
tmprf += bufrf;
}
tmprf /= TESTN;
tmprx = 0;
for(i=0;i<TESTN;i++)
{
tmprx += bufrx;
}
tmprx /= TESTN;
tmp = (tmprx * 10)/tmprf;
h = r2t(tmp)/10;
l = r2t(tmp)%10;
// sprintf(buff,"R=%d,T=%d.%dC",(int)(tmp*10),h,l);
sprintf(buff,"O=%d,T=%d.%d ",Vout,h,l);
LCD_write_string(0,buff);
// Vout = pid(r2t(tmp),Vout,200);
// if(Vout >=5000) Vout = 5000;
// if(Vout <=0) Vout=0;
}
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|