1 | /*
|
2 | * File: main.c
|
3 |
|
4 | #include <stdio.h>
|
5 | #include <stdlib.h>
|
6 | #include <p18F85J50.h>
|
7 | #include "global_var.h"
|
8 | #include "usart.h"
|
9 | #include <delays.h>
|
10 |
|
11 | // CONFIG1L
|
12 | #pragma config WDTEN = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on SWDTEN bit))
|
13 | #pragma config PLLDIV = 1 // PLL Prescaler Selection bits (No prescale (4 MHz oscillator input drives PLL directly))
|
14 | #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable bit (Reset on stack overflow/underflow enabled)
|
15 | #pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
|
16 |
|
17 | // CONFIG1H
|
18 | #pragma config CPUDIV = OSC1 // CPU System Clock Postscaler (No CPU system clock divide)
|
19 | #pragma config CP0 = OFF // Code Protection bit (Program memory is not code-protected)
|
20 |
|
21 | // CONFIG2L
|
22 | #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator, HS used by USB)
|
23 | #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
|
24 | #pragma config IESO = OFF // Two-Speed Start-up (Internal/External Oscillator Switchover) Control bit (Two-Speed Start-up disabled)
|
25 |
|
26 | // CONFIG2H
|
27 | #pragma config WDTPS = 32768 // Watchdog Timer Postscaler Select bits (1:32768)
|
28 |
|
29 | // CONFIG3L
|
30 | #pragma config EASHFT = OFF // External Address Bus Shift Enable bit (Address shifting disabled, address on external bus reflects the PC value)
|
31 | #pragma config MODE = MM // External Memory Bus Configuration bits (Microcontroller mode - External bus disabled)
|
32 | #pragma config BW = 16 // Data Bus Width Select bit (16-bit external bus mode)
|
33 | #pragma config WAIT = OFF // External Bus Wait Enable bit (Wait states on the external bus are disabled)
|
34 |
|
35 | // CONFIG3H
|
36 | #pragma config CCP2MX = DEFAULT // ECCP2 MUX bit (ECCP2/P2A is multiplexed with RC1)
|
37 | #pragma config ECCPMX = DEFAULT // ECCPx MUX bit (ECCP1 outputs (P1B/P1C) are multiplexed with RE6 and RE5; ECCP3 outputs (P3B/P3C) are multiplexed with RE4 and RE3)
|
38 | #pragma config PMPMX = DEFAULT // PMP Pin Multiplex bit (PMP pins placed on EMB)
|
39 | #pragma config MSSPMSK = MSK7 // MSSP Address Masking Mode Select bit (7-Bit Address Masking mode enable)
|
40 |
|
41 |
|
42 | //////////////////////////////////////////////////////////////////////////
|
43 | // //
|
44 | // Interrupt-Steuerung //
|
45 | // //
|
46 | //////////////////////////////////////////////////////////////////////////
|
47 |
|
48 | void service(void);
|
49 | #pragma code einsprung = 0x08
|
50 | void hilfe (void)
|
51 | {
|
52 | _asm GOTO service _endasm
|
53 | }
|
54 | #pragma code
|
55 | #pragma interrupt service
|
56 |
|
57 | #define USE_AND_MASKS
|
58 |
|
59 |
|
60 | ////////////////////////////////////////////////////
|
61 | // Globale Variablen //
|
62 | ////////////////////////////////////////////////////
|
63 | unsigned char ini=0, c=0, i=0;
|
64 | unsigned char Msg[] = "\r\nEingabe: ", ps[]="PS,ABBACDDC000000000000000000001234";
|
65 | unsigned char UART1Config = 0, baud = 0, Buff[length];
|
66 | unsigned char on[] ="sr,20000000", sf[]="SF,1", ss[]="SS,00000001";
|
67 | unsigned char res[]="R,1", pc[]="PC,ABBACDDC000000000000000000001234,16,01";
|
68 |
|
69 | void init(void) //Initialisierung von Ports, Timer, Interrupts
|
70 | {
|
71 |
|
72 | PORTA = 0x00;
|
73 | TRISA = 0b00000000;
|
74 | LATA = 0x00;
|
75 |
|
76 | WDTCON = WDTCON|0x10; // analoge Eingänge zu digitalen I/O's
|
77 | ANCON0 = 0xFF;
|
78 | ANCON1 = 0xFF;
|
79 | WDTCON = WDTCON&0xEF;
|
80 |
|
81 |
|
82 | PORTB = 0x00;
|
83 | TRISB = 0b00000000;
|
84 | LATB = 0x00;
|
85 |
|
86 | /************************
|
87 | * Port C -> Bluetooth
|
88 | * PC7: I UART_TX -> RX
|
89 | * PC6: O UART_RX <- TX
|
90 | * PC5: I UART_RTS
|
91 | * PC4: O UART_CTS
|
92 | * PC3: O WAKE_SW: Deep Sleep Wake; active-high to wake module from Deep Sleep
|
93 | * PC2: O CMD/MLDP:Input; Edge triggered; Change from High to Low to enter CMD
|
94 | mode from MLDP mode.
|
95 | * PC1: O WAKE_HW: Hardware wake from Dormant state. Setting the
|
96 | WAKE_HW (pin15) high wakes the module from Dormant mode.
|
97 | * PC0: O WS: Default function is an output used for Activity Indicator.
|
98 | * High level indicates module is awake and active.
|
99 | ***********************/
|
100 |
|
101 | PORTC = 0x00;
|
102 | TRISC = 0b10010001;
|
103 | LATC = 0b00101110;
|
104 | SPEN=1; //bit SPEN (RCSTA1<7>) must be set (=1)
|
105 |
|
106 | /************************
|
107 | * Port D -> LEDs
|
108 | * PD7: Ausgang
|
109 | * PD6: Ausgang
|
110 | * PD5: Ausgang
|
111 | * PD4: Ausgang
|
112 | * PD3: Ausgang
|
113 | * PD2: Ausgang
|
114 | * PD1: MLDP_EV: Active-high indicates MLDP data received or UART console
|
115 | * data pending. Low level indicates no events.
|
116 | * PD0: CONNECTION: Active-high indicates the module is connected to a
|
117 | * remote device. Active-low indicates a disconnected state.
|
118 | ***********************/
|
119 |
|
120 | PORTD = 0x00;
|
121 | TRISD = 0b00000011;
|
122 | LATD = 0b00000000;
|
123 |
|
124 | PORTE = 0x00;
|
125 | TRISE = 0b00000000;
|
126 | LATE = 0x00;
|
127 |
|
128 | PORTF = 0x00;
|
129 | TRISF = 0b00000000;
|
130 | LATF = 0x00;
|
131 |
|
132 | PORTG = 0x00;
|
133 | TRISG = 0b00000000;
|
134 | LATG = 0x00;
|
135 |
|
136 | PORTH = 0x00;
|
137 | TRISH = 0b11111111;
|
138 | LATH = 0x00;
|
139 |
|
140 | PORTJ = 0x00;
|
141 | TRISJ = 0b11111111;
|
142 | LATJ = 0x00;
|
143 |
|
144 |
|
145 | /***********************
|
146 | * Interrupt-Steuerung
|
147 | **********************/
|
148 | GIE = 1; // alle Interrupts aktiviert
|
149 | PEIE = 1;
|
150 | }
|
151 |
|
152 |
|
153 |
|
154 | //Send one char
|
155 | void send_c(unsigned char data)
|
156 | {
|
157 | while(!PIR1bits.TX1IF);
|
158 | TXREG1 = data;
|
159 | }
|
160 |
|
161 | //Send a string
|
162 | void send_s(unsigned char *str)
|
163 | {
|
164 | while (*str)
|
165 | {
|
166 | send_c(*str); // send character pointed to by s
|
167 | str++; // increase pointer location to the next character
|
168 | }
|
169 | }
|
170 |
|
171 |
|
172 | void disp(void)
|
173 | {
|
174 | //Lediglich paar LEDs
|
175 | }
|
176 |
|
177 |
|
178 | int main(void)
|
179 | {
|
180 | init();
|
181 |
|
182 | ////Open UART1////
|
183 | UART1Config = USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_BRGH_HIGH;
|
184 | baud = 12;
|
185 | Open1USART(UART1Config,baud);
|
186 |
|
187 |
|
188 | while(1)
|
189 | {
|
190 | if(PORTJbits.RJ0==1)
|
191 | {
|
192 | send_s(on);
|
193 | send_c(13);
|
194 | Delay10KTCYx(250);
|
195 | }
|
196 | }
|
197 | }
|
198 |
|
199 | void service (void)
|
200 | {
|
201 | if(RC1IF == 1)
|
202 | {
|
203 | if(i<length)
|
204 | {
|
205 | Buff[i] = Read1USART(); //read the byte from rx register
|
206 | if(Buff[i] == 0x0D) //check for return key
|
207 | {
|
208 | //disp();
|
209 | for(i;i>0;i--)
|
210 | Buff[i] = 0x00; //clear the array
|
211 | i=0;
|
212 | return;
|
213 | }
|
214 | i++;
|
215 | //RC1IF = 0; // clear rx flag
|
216 | }
|
217 | else
|
218 | {
|
219 | for(i;i>0;i--)
|
220 | Buff[i] = 0x00; //clear the array
|
221 | i=0;
|
222 | return;
|
223 | }
|
224 | }
|
225 | }
|