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
| #include <16F887.h>
#device adc=10
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=20000000) //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0xA0) //FORCE_HW, stream=I2CS
#define LCD_ENABLE_PIN PIN_D2
#define LCD_RS_PIN PIN_D0
#define LCD_RW_PIN PIN_D1
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
#include <lcd.c>
float temp = 0, old_val = 0;
int8 ctr = 0;
union float2bytes {
float fval;
int8 bytes[4];
} fvalue;
int8 data = '0';
BYTE addr, buffer[16];
#INT_SSP
void ssp_interupt (){
BYTE incoming, state;
state = i2c_isr_state();
if(state < 0x80) //Master is sending data
{
incoming = i2c_read();
if(state == 1) //First received byte is address
addr = incoming;
if(state == 2) //Second received byte is data
data = incoming;
}
//else if(state == 0x80){
//i2c_read();
//i2c_write('U');
//}
else if(state >= 0x80) //Master is requesting data
{
i2c_write(fvalue.bytes[ctr]);
ctr++;
if(ctr < 4)i2c_read();
if(ctr==4){i2c_read(0); ctr = 0;}
//i2c_write('M');
}
}
void main()
{
int16 adc_val = 0;
//set_tris_c(0xFF);
setup_adc_ports(sAN0|VREF_VREF);
setup_adc(ADC_CLOCK_DIV_2);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
delay_ms(250);
lcd_init();
delay_ms(250);
lcd_putc("\fSlave 1...\n");
set_adc_channel(0);
delay_us(20);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
while(TRUE)
{
adc_val = read_adc();
delay_ms(20);
temp = (float)(adc_val * 0.196078431372549);
fvalue.fval = temp;
if(old_val != temp){
printf(lcd_putc, "\a%7.3f", temp);
printf("Temperature is: %7.3f", temp);
printf(" Degree Centigrade\r\n");
printf("%7.3f\r\n", fvalue.fval);
printf("%lu\r\n", adc_val);
}
if(data == 'K'){
printf("%c\r\n", data);
data = '\0';
}
//delay_ms(1000);
old_val = temp;
}
} |