Initial import of onewire-to-usb bridge
[onewire] / user.c
1 /******************************************************************************/
2 /* Files to Include                                                           */
3 /******************************************************************************/
4
5 #if defined(__XC)
6     #include <xc.h>         /* XC8 General Include File */
7 #elif defined(HI_TECH_C)
8     #include <htc.h>        /* HiTech General Include File */
9 #endif
10
11 #include <stdint.h>         /* For uint8_t definition */
12 #include <stdbool.h>        /* For true/false definition */
13
14 #include "system.h"
15 #include "user.h"
16
17 /******************************************************************************/
18 /* User Functions                                                             */
19 /******************************************************************************/
20
21 /* <Initialize variables in user.h and insert code for user algorithms.> */
22
23 void InitApp(void)
24 {
25     /* set watchdog to 4 seconds and enable it */
26     WDTCONbits.WDTPS = 0b01100;
27     WDTCONbits.SWDTEN = 1;
28    
29     /* Select pins 4&5 as the uart - page 102 */
30     APFCONbits.RXDTSEL = 0; /* RX/DT on RA1 */
31     APFCONbits.TXCKSEL = 0; /* TX/CK on RA0 */
32     TRISAbits.TRISA1 = 1;   /* RA5 as input  */
33     TRISAbits.TRISA0 = 0;   /* RA4 as output */
34
35     ANSELA = 0; /* no analog */
36
37
38     /* enable weak pullups, clear them all */
39     WPUA = 0;
40     //WPUAbits.WPUA0 = 0x00000100; // RA0 pullup for 1-wire bus
41     OPTION_REGbits.nWPUEN = 1;
42
43     /* Enabling transmitter 26.1.1.1 page 259 - TX/CK I/O pin */
44     TXSTAbits.TXEN = 1;
45     TXSTAbits.SYNC = 0;
46     RCSTAbits.SPEN = 1;
47     /* Enabling receiver 26.1.2 page 262 - RX/DT I/O pin */
48     RCSTAbits.CREN = 1;
49
50     /* 4MHz clock, 57k6 baud */
51     TXSTAbits.BRGH = 1;
52     BAUDCONbits.BRG16 = 1;
53     //SPBRG = 16; // 57k6
54     SPBRG = 51; // 19k2
55     //SPBRG = 103; // 9k6
56
57     // enable debug LED on RA4
58     TRISAbits.TRISA4 = 0;
59     ANSELAbits.ANSA4 = 0;
60
61     // setup timer1
62
63     // enable the timer (off for now, not gated mode)
64     T1CONbits.TMR1ON = 0;
65     T1GCONbits.TMR1GE = 0;
66     T1CONbits.TMR1CS = 0b00; // instruction clock
67     T1CONbits.T1CKPS = 0b11; // div 8
68     
69
70     // enable TIMER1 interupt
71     PIE1bits.TMR1IE = 1;
72
73     /* Enable interrupts */
74     INTCONbits.GIE = 1;
75     INTCONbits.PEIE = 1;
76
77     /* enable uart receiver interupt */
78     PIE1bits.RCIE = 1;
79 }
80