1 | #include <16f870.h>
|
2 |
|
3 | #use delay(clock=4000000)
|
4 | #fuses XT,NOWDT,NOLVP
|
5 |
|
6 | //DS1820 Commandos
|
7 | #define SKIP_ROM 0xCC /* 204*/
|
8 |
|
9 | #define CONVERT_T 0x44 /*68*/
|
10 | #define READ_SCRATCHPAD 0xBE /*190*/
|
11 |
|
12 | #define Rx_DS18S20 PIN_C4 // Rx TempSensor
|
13 | #define Tx_DS18S20 PIN_C5 // Tx TempSensor
|
14 |
|
15 | char D_Presence;
|
16 | char D_ShortCircuit;
|
17 |
|
18 | enum BITSTELLE {
|
19 | Bit0=1,BIT1=2,BIT2=4,BIT3=8,BIT4=16,BIT5=32,BIT6=64,BIT7=128 };
|
20 |
|
21 | #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
|
22 |
|
23 | int temp_1,temp_2;
|
24 | long int null,eins;
|
25 | int scratchpad[8];
|
26 |
|
27 |
|
28 | void D_Reset(void)
|
29 | {
|
30 | set_tris_c(0x99);
|
31 | output_low(Tx_DS18S20); // Pin des DS18S20 auf low setzen
|
32 | delay_us(480);
|
33 | output_high(Tx_DS18S20); // Pin des DS18S20 auf high setzen
|
34 | delay_us(40);
|
35 | D_Presence = input(Rx_DS18S20);
|
36 | delay_us(350);
|
37 | D_ShortCircuit = input(Rx_DS18S20);
|
38 | delay_us(60);
|
39 | }
|
40 |
|
41 | void temp_init()
|
42 | {
|
43 | output_low(Tx_DS18S20); // Pin des DS18S20 auf low setzen
|
44 | delay_us(480); // 480us warten
|
45 | while (input(Rx_DS18S20)); // pruefen ob der 1-wire bus auf low
|
46 | gezogen wird,
|
47 | delay_ms(5);
|
48 | }
|
49 |
|
50 | void temp_send(unsigned char code)
|
51 | { int n=0;
|
52 |
|
53 | for(; n < 8; n++)
|
54 | { if(bit_test(code,n))
|
55 | {
|
56 | output_low(Tx_DS18S20); //sende 0
|
57 | delay_us(2); //int write slot
|
58 | output_high(Tx_DS18S20); //send 1
|
59 | }
|
60 | else
|
61 | {
|
62 | output_low(Tx_DS18S20); //sende 0
|
63 | }
|
64 | delay_us(65); //duration für 1 und fertig
|
65 | output_high(Tx_DS18S20);
|
66 | delay_us(2);
|
67 | }
|
68 | delay_us(100); //1 Millisekunde
|
69 | }
|
70 |
|
71 | unsigned char temp_read(void)
|
72 | { unsigned char in=0,j=0;
|
73 |
|
74 | delay_us(400);
|
75 | for(; j < 8; j++)
|
76 | {
|
77 | delay_us(1);
|
78 | output_low(Tx_DS18S20); // init read
|
79 | delay_us(3);
|
80 | if (input(Rx_DS18S20)) bit_set(in,j);
|
81 | delay_us(60);
|
82 | }
|
83 | return(in);
|
84 | }
|
85 |
|
86 |
|
87 | void main()
|
88 | { int i;
|
89 |
|
90 | printf("Hallo\r\n");
|
91 |
|
92 | D_Reset();
|
93 | printf("Presence:%d
|
94 | ShortCircuit:%d\r\n",D_Presence,D_ShortCircuit);
|
95 |
|
96 | while(1)
|
97 | { temp_init();
|
98 | temp_send(SKIP_ROM);
|
99 | temp_send(CONVERT_T);
|
100 | delay_ms(500); //warten bis Temp Convert
|
101 | abgeschlossen ist...
|
102 |
|
103 | temp_send(SKIP_ROM);
|
104 | temp_send(READ_SCRATCHPAD);
|
105 |
|
106 | for (i=0; i<8;i++)
|
107 | { scratchpad[i] = temp_read();
|
108 | printf("i:%d/v:%d %d
|
109 | %d",i,scratchpad[i],D_Presence,D_ShortCircuit);
|
110 | }
|
111 |
|
112 | printf("Temperatur: %d, %02u C\r\n",temp_2,temp_1);
|
113 | delay_ms(500);
|
114 | }
|
115 | }
|