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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
| #include<pic.h>
#define ent1 T1CON=T1CON|0b00000111 #define dist1 T1CON=T1CON&0b00000110
#define load RB3 #define motor RB0 #define heater RB2 #define pump RB1
//enable timer1 //disable timer1
int convert[16]='0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F';
unsigned int x,temp,adc_value,countdata,digit1,digit2,digit3,digit4,digit5,voltage,current,tem perature,level,speed;
void timer_config(); void timer_read();
void control();
void hextobcd();
void acd_config();
void adc_data_accure(); void acd_read(int);
void delay(int);
void main()
{
TRISA=0xFF; // PORTA is input TRISC=0xFF; // PORTC is input TRISD=0x00; // PORTD is input TRISB=0x00; // PORTB is output
18
PORTB=0X00;
TXSTA=0b00100100; RCSTA=0b10010000; SPBRG=25;
voltage=0; current=0; temperature=0; level=0; speed=0; load=0; motor=0; heater=0; pump=0;
acd_config(); timer_config();
delay(50);
while(1)
{
voltage=0; current=0; temperature=0; level=0; speed=0;
TMR1L=0x00; TMR1H=0x00;
ent1;
for(x=0;x<=100;x++) {
if (RCIF==1)
{
control();
//configure serial port //configure serial port
//Clear timer1 //Clear timer1
//enable timer 1
19
} delay(50); }
dist1; //disable timer 1
speed=TMR0;
timer_read(); speed=(temp*12); for(temp=1;temp<=30;temp++) {
acd_read(0); temperature=adc_value+temperature;
acd_read(1); level=adc_value+level;
acd_read(2); current=adc_value+current;
acd_read(4); voltage=adc_value+voltage; delay(1);
}
voltage=(voltage/30); current=(current/30); temperature=(temperature/30); level=(level/30);
TXREG='F'; delay(100);
temp=voltage; hextobcd(); TXREG=convert[digit3]; delay(100); TXREG=convert[digit4]; delay(100); TXREG=convert[digit5]; delay(100);
20
temp=current; hextobcd();
TXREG=convert[digit3]; delay(100); TXREG=convert[digit4]; delay(100); TXREG=convert[digit5]; delay(100);
temp=temperature; hextobcd();
TXREG=convert[digit3]; delay(100); TXREG=convert[digit4]; delay(100); TXREG=convert[digit5]; delay(100);
temp=level; hextobcd();
TXREG=convert[digit2]; delay(100); TXREG=convert[digit3]; delay(100); TXREG=convert[digit4]; delay(100); TXREG=convert[digit5]; delay(100);
temp=speed;
hextobcd(); TXREG=convert[digit2]; delay(100); TXREG=convert[digit3]; delay(100); TXREG=convert[digit4]; delay(100);
21
TXREG=convert[digit5]; delay(100);
} }
void control() { temp=RCREG;
if(temp=='1') {
load=1;
}
else if(temp=='2') {
load=0;
}
else if(temp=='3') {
motor=1;
}
else if(temp=='4') {
motor=0;
}
else if(temp=='5') {
heater=1;
}
else if(temp=='6') {
heater=0;
}
22
else if(temp=='7') {
pump=1;
}
else if(temp=='8') {
pump=0;
}
else
{ delay(10); }
if(OERR==1) {
SREN=0; temp=RCREG; temp=RCREG; CREN=0; CREN=1; SREN=1;
} delay(100); }
//////////////////////////////////////////////////////////////////////////////////////////
void timer_config()
{
T1CON=T1CON|0b00000110; //configure timer 1 as counter to count frequency //OPTION_REG=0b0010000; //configure timer 0 as counter to count frequency }
//////////////////////////////////////////////////////////////////////////////////////////
void timer_read()
{
temp=TMR1L;
temp+=(TMR1H<<8);
}
//collect TMR1L value
//collect TMR1H value and add with TMR1L value
23
////////////////////////////////////////////////////////////////////////////////////////// void hextobcd()
{
countdata=temp;
countdata=(countdata/10000); digit1=countdata; countdata=(countdata*10000); temp=(temp-countdata);
countdata=temp; countdata=(countdata/1000); digit2=countdata; countdata=(countdata*1000); temp=(temp-countdata);
countdata=temp; countdata=(countdata/100); digit3=countdata; countdata=(countdata*100); temp=(temp-countdata); countdata=temp; countdata=(countdata/10); digit4=countdata; countdata=(countdata*10); temp=(temp-countdata);
digit5=temp; }
////////////////////////////////////////////////////////////////////////////////////////// void acd_config()
{
ADCON1=0b10000001; ADCON0=0b01000001; delay(1);
}
////////////////////////////////////////////////////////////////////////////////////////// void acd_read(int channel)
24
{
if (channel==0)
{ ADCON0=0b01000001; adc_data_accure();
}
else if (channel==1)
{
ADCON0=0b01001001;
adc_data_accure();
}
else if (channel==2)
{
ADCON0=0b01010001;
adc_data_accure();
}
else if (channel==3)
{
ADCON0=0b01011001;
adc_data_accure();
}
else if (channel==4)
{
ADCON0=0b01100001;
adc_data_accure();
}
else if (channel==5)
{
ADCON0=0b01101001;
adc_data_accure();
}
else
{
}
} //////////////////////////////////////////////////////////////////////////////////////////
void adc_data_accure() {
25
delay(1);
ADCON0=ADCON0|0b00000100;
delay(1);
adc_value=ADRESL; //collect ADRESL value adc_value+=(ADRESH<<8); //collect ADRESH value and add with ADRESL value
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
void delay(int time) {
int i,j; for(j=1;j<=time;j++) {
for(i=0;i<=64;i++) {
}
}
} |