// LCD 電池内部抵抗測定装置
// LCD: Reset(1), SCL(2), SDA(3), Vss(4)、Vdd(5)
// ResetはVddに繋いでおく
// LCD : 16F690
// SDA(3) : PORTC.F4(6)
// SCL(2) : PORTC.F3(7)
#include "i2c.h"
#include "I2CLCD.h"
#define keta 1000
float R01=47;
sbit BattSW at PORTC.B1; //(15)
char buff[16];
void main() {
unsigned int V, Vsw, E, tmp, HighDigit, LowDigit;
float InnerR, openE;
int i;
OSCCON = 0x75; // 内部OSC8MHzで動作させる
PORTA = 0xff; // PORTAを0にする
TRISA = 0x07; // 下3bit入力、その他出力
ANSEL = 0x07; // アナログ入力をON/OFF
ANSELH = 0; // アナログ入力をOFF
OPTION_REG |= 0x80;
PORTC = 0xff; // PORTCを0xffにする
TRISC = 0xe5; // PORTCを入力に設定する
LCDinit();
BattSW = 0; // off
ADCON0.ADON = 1;
ADCON0.ADFM = 1; // right justify
while(1) {
ADCON0.CHS0 = 0;
Delay_us(1);
ADCON0.GO =1;
while(ADCON0.GO);
E = ADRESH;
E = E*256 + ADRESL; // E
BattSW = 1; // on
Delay_ms(1000); // ONtime
ADCON0.CHS0 = 0; // V
Delay_us(1);
ADCON0.GO =1;
while(ADCON0.GO);
V = ADRESH;
V = V*256 + ADRESL;
ADCON0.CHS0 =1; // Vsw
Delay_us(1);
ADCON0.GO =1;
while(ADCON0.GO);
Vsw = ADRESH;
Vsw = Vsw*256 + ADRESL;
BattSW = 0; // off
InnerR = R01*keta*(E-V)/(V-Vsw)+0.5;
clear();
HighDigit = (unsigned int)InnerR/keta;
openE = E*3.3/1024*100+0.5;
writeString(" E="); writeInt((unsigned int)openE/100);writeCh('.');
writeInt((unsigned int)openE%100);writeString(" V\n");
writeString(" r="); writeInt(HighDigit);writeCh('.');
LowDigit = (unsigned int)InnerR%keta;
writeInt(LowDigit);writeString(" ohm");
FloatToStr(InnerR/100.0,buff);
Delay_ms(1000);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|