1 /******************************************************************************/
3 /******************************************************************************/
6 #include <xc.h> /* XC8 General Include File */
7 #elif defined(HI_TECH_C)
8 #include <htc.h> /* HiTech General Include File */
13 #include <stdint.h> /* For uint8_t definition */
14 #include <stdbool.h> /* For true/false definition */
16 #include "system.h" /* System funct/params, like osc/peripheral config */
17 #include "user.h" /* User funct/params, such as InitApp */
19 #include "onewire.h" /* 1-wire bus functions */
21 /******************************************************************************/
22 /* User Global Variable Declaration */
23 /******************************************************************************/
25 #define LED1 PORTAbits.RA4
27 /* i.e. uint8_t <variable_name>; */
36 /******************************************************************************/
38 /******************************************************************************/
42 // long time period counter, 572 steps is 5 minutes
44 uint16_t auto_counter = 0;
48 if (automode) auto_counter++;
57 void timer_disable() {
62 const char * banner = "\r\n\n\nPIC 1-Wire Bridge system. Press 'H' for help.\r\n";
76 bool usermode = false;
78 /* Configure the oscillator for the device */
79 ConfigureOscillator();
81 /* Initialize I/O and Peripherals for application */
94 if (TRISAbits.TRISA5 == 1) {
96 prompt[5] = PORTAbits.RA5 ? '1':'0';
99 prompt[5] = PORTAbits.RA5 ? '1':'0';
103 if (!msg_empty()) continue;
106 if (auto_counter >= 572) {
107 inbuff[inlen++] = 't';
112 if (!msg_recvready()) continue;
114 char cmd = msg_recv();
116 if (cmd == '\r' || cmd == '\n') {
117 if (usermode) msg_write("\r\n");
119 if (cmd == 'h' || cmd == 'H') {
121 msg_write("\nHelp:\r\n");
122 while (!msg_empty());
123 msg_write("E - Enumerate the Bus\r\n");
124 msg_write("R - Reset Bus\r\n");
125 while (!msg_empty());
126 msg_write("0,1,3 - bus to Low, High, Tristate\r\n");
127 msg_write("I - Read (one) Device ID\r\n");
128 while (!msg_empty());
129 msg_write("P - Any device parasitic powered?\r\n");
130 msg_write("S - Read scratchpad memory\r\n");
131 while (!msg_empty());
132 msg_write("T - read temperature of all devices\r\n");
133 msg_write("A - Auto-mode. read every 5 mins.\r\n");
134 while (!msg_empty());
135 msg_write("M - Manual mode.\r\n");
136 msg_write("B - Test battery voltage\r\n");
137 while (!msg_empty());
140 if (cmd == 'a' || cmd == 'A') {
142 msg_write("\rAUTO mode enabled. 5 minute timer.\r\n");
144 msg_write("AUTO ON\n");
147 if (cmd == 'm' || cmd == 'M') {
149 msg_write("\rAUTO mode disabled.\r\n");
151 msg_write("AUTO OFF\n");
154 if (cmd == 'r' || cmd == 'R') {
156 bool present = OW_reset();
158 if (usermode) msg_write("\r");
160 msg_write("RESET Device detected.\r\n");
162 msg_write("RESET No Devices detected.\r\n");
164 if (cmd == 'e' || cmd == 'E')
167 if (usermode) msg_write("\r");
172 if (usermode) msg_write("ENUM ");
173 for (char j=0; j<8; j++) {
174 tohex(val, romid[j]);
177 if (usermode) msg_write("\r\n"); else msg_write("\n");
181 if (count==0) msg_write("ERROR No devices found\r\n");
186 if (cmd == 'i' || cmd == 'I') {
189 if (usermode) msg_write("\rID ");
190 for (char j=0; j<8; j++) {
191 tohex(val, romid[j]);
194 if (usermode) msg_write("\r\n"); else msg_write("\n");
196 if (cmd == 'p' || cmd == 'P') {
197 if (usermode) msg_write("\r");
198 bool para = OW_parasite();
201 msg_write("PARA Yes\r\n");
203 msg_write("PARA No\r\n");
205 if (para) msg_write("PARA\n"); else msg_write("DIRECT\n");
208 if (cmd == 's' || cmd =='S') {
210 romid[0] = 0; // select all devices
211 OW_read_block(0xBE, scratch, 9);
212 if (usermode) msg_write("\rSCRATCH");
213 for (char j=0; j<9; j++) {
215 tohex(val, scratch[j]);
219 if (usermode) msg_write("\r\n"); else msg_write("\n");
221 if (cmd == 't' || cmd == 'T') {
225 // oddly, cant do a mass-convert with parasitic
226 // they seem to all reset when you try
230 // now step through every unit and read the value
234 // convert takes along time, dont trip watchdog
236 // ask device to do conversion
240 // empty the scratch pad
241 for (char j=0; j<9; j++) scratch[j]=0;
243 // read this devices scratch pad
244 OW_read_block(0xBE, scratch, 9);
246 // print rom id and temperature
247 if (usermode) msg_write("\r");
249 for (char j=0;j<8;j++) {
250 tohex(val, romid[j]);
254 tohex(val, scratch[1]);
256 tohex(val, scratch[0]);
258 if (usermode) msg_write("\r\n"); else msg_write("\n");
262 if (!usermode) msg_write("END\n");
269 msg_write("\rBUS low\r\n");
277 msg_write("\rBUS high\r\n");
285 msg_write("\rBUS to tri-state\r\n");
287 msg_write("TRISTATE\n");
291 // this is to test that the watchdog actually works
292 msg_write("Play dead.\r\n");
297 bool line = read_OW();
299 if (usermode) msg_write("\rBUS HIGH\r\n");
303 if (usermode) msg_write("\rBUS LOW\r\n");
304 else msg_write("LOW\n");
307 if (cmd == 'b' || cmd == 'B')
309 // Read Vdd by sampling FVR
310 FVRCONbits.ADFVR = 0b10; // 2.048v // 1.024v
311 FVRCONbits.FVREN = 1; // enable
312 while (!FVRCONbits.FVRRDY) {};
313 ADCON1bits.ADFM = 1; // right justify
314 ADCON0bits.CHS = 0b11111; // FVR input
315 ADCON1bits.ADPREF = 0b00; // +ve ref Vdd
316 ADCON0bits.ADON = 1; // enable ADC
318 ADCON0bits.GO = 1; // start conversion
319 while (ADCON0bits.GO) {}; // wait til finished
322 volt = (ADRESH << 8) | ADRESL;
323 uint16_t mv = (8192 / volt) * 1024;
326 msg_write("VOLTAGE ");
334 ADCON0bits.ADON = 0; // turn off ADC
335 FVRCONbits.FVREN = 0; // turn of FVR
339 msg_write("ERROR Press H for Help\n");
341 msg_write("\rERROR Unknown command '");
343 msg_write("' Press 'H' for help.\r\n");
347 if (usermode) msg_write(prompt);
351 /* make a hex string from a value
352 * expect 3 char array */
353 void tohex(char val[], uint8_t i)
357 val[0] = 'A' + (bt - 10);
362 val[1] = 'A' + (bt - 10);
369 /* is the queue empty yet */
372 if (outp == 0) return 1;
376 /* new message in the queue */
377 void msg_write(const char *msg)
379 char * p = outbuff + outlen;
380 while (outlen < sizeof(outbuff) && *msg != 0) {
391 void msg_writebyte(const char msg)
393 if (outlen+1 >= (uint8_t)sizeof(outbuff)) return;
394 outbuff[outlen++] = msg;
402 void putch(char data)
407 /* called from interrupt routine to send next char */
408 void msg_sendnext(void)
410 /* we have finished, turn off the iterrupt */
411 if (outp == 0 || *outp == 0) {
421 /* called from interrupt routine to receive next byte */
422 void msg_recvnext(void)
424 while (PIR1bits.RCIF) {
425 bool err = RCSTAbits.FERR;
428 /* bad char, skip it */
431 /* our input buffer has overflowed */
432 if (inlen > sizeof(inbuff)) return;
435 inbuff[inlen++] = new;
439 bool msg_recvready(void)
441 if (inlen > 0) return 1;
447 if (inlen == 0) return 0;
449 /* record and disable the interupt */
450 bool in = PIE1bits.RCIE;
453 char new = inbuff[0];
456 /* shuffle them down one */
457 for (char i=0;i<inlen;i++)
458 inbuff[i] = inbuff[i+1];
460 /* restore the interupt */
466 void int_disable(void)
471 void int_enable(void)