1 | // PIC24FJ128GA010 Configuration Bit Settings
|
2 |
|
3 | // 'C' source line config statements
|
4 |
|
5 | #include <xc.h>
|
6 |
|
7 | // CONFIG2
|
8 | #pragma config POSCMOD = HS // Primary Oscillator Select (HS Oscillator mode selected)
|
9 | #pragma config OSCIOFNC = ON // Primary Oscillator Output Function (OSC2/CLKO/RC15 functions as port I/O (RC15))
|
10 | #pragma config FCKSM = CSDCMD // Clock Switching and Monitor (Clock switching and Fail-Safe Clock Monitor are disabled)
|
11 | #pragma config FNOSC = PRIPLL // Oscillator Select (Primary Oscillator with PLL module (HSPLL, ECPLL))
|
12 | #pragma config IESO = OFF // Internal External Switch Over Mode (IESO mode (Two-Speed Start-up) disabled)
|
13 |
|
14 | // CONFIG1
|
15 | #pragma config WDTPS = PS32768 // Watchdog Timer Postscaler (1:32,768)
|
16 | #pragma config FWPSA = PR128 // WDT Prescaler (Prescaler ratio of 1:128)
|
17 | #pragma config WINDIS = ON // Watchdog Timer Window (Standard Watchdog Timer enabled,(Windowed-mode is disabled))
|
18 | #pragma config FWDTEN = OFF // Watchdog Timer Enable (Watchdog Timer is disabled)
|
19 | #pragma config ICS = PGx2 // Comm Channel Select (Emulator/debugger uses EMUC2/EMUD2)
|
20 | #pragma config GWRP = OFF // General Code Segment Write Protect (Writes to program memory are allowed)
|
21 | #pragma config GCP = OFF // General Code Segment Code Protect (Code protection is disabled)
|
22 | #pragma config JTAGEN = OFF // JTAG Port Enable (JTAG port is disabled)
|
23 |
|
24 | #define FCY 16000000UL // 8 mhz * pll(*4) => 32Mhz == 16Mhz FCY
|
25 |
|
26 | #include <stdio.h>
|
27 | #include <stdlib.h>
|
28 |
|
29 | void toggle_d9(void) {
|
30 | __builtin_btg((unsigned int *) &LATA, 6);
|
31 | }
|
32 |
|
33 | // timing and baud rate calculations
|
34 | // UBRGx = ( FCY / ( 4 * Baud ) ) -1 IF BRGH = 1
|
35 | // UBRGx = ( FCY / ( 16 * Baud ) ) -1 IF BRGH = 0
|
36 | #define BRATE 34 // 115200 baud (BREGH=1)
|
37 | #define U_ENABLE 0x8008 // enable the UART peripheral
|
38 | #define U_TX 0x0400 // enable transmission
|
39 |
|
40 | // initialize the UART2 serial port
|
41 |
|
42 | void InitU2(void) {
|
43 | U2BRG = BRATE;
|
44 | U2MODE = U_ENABLE;
|
45 | U2STA = U_TX;
|
46 | //RTS = 1; // set RTS default status
|
47 | //TRTS = 0; // make RTS output
|
48 | } // initU2
|
49 |
|
50 | // send a character to the UART2 serial port
|
51 |
|
52 | int putU2(int c) {
|
53 | //while ( CTS); // wait for !CTS, clear to send
|
54 | while (U2STAbits.UTXBF); // wait while Tx buffer full
|
55 | U2TXREG = c;
|
56 | return c;
|
57 | } // putU2
|
58 |
|
59 | // wait for a new character to arrive to the UART2 serial port
|
60 |
|
61 | char getU2(void) {
|
62 | //RTS = 0; // assert Request To Send !RTS
|
63 | while (!U2STAbits.URXDA); // wait
|
64 | //RTS = 1;
|
65 | return U2RXREG; // read from the receive buffer
|
66 | }// getU2
|
67 |
|
68 | /*
|
69 | *
|
70 | */
|
71 | int main(int argc, char** argv) {
|
72 |
|
73 | unsigned int c = 0x41;
|
74 |
|
75 | LATAbits.LATA7 = 0; // LED D10 off
|
76 | TRISAbits.TRISA7 = 0; // output
|
77 | LATAbits.LATA6 = 1; // LED D9 off
|
78 | TRISAbits.TRISA6 = 0; // output
|
79 |
|
80 | // setup/start timer1
|
81 | T1CONbits.TCKPS = 0b11; // 1:256
|
82 | T1CONbits.TCS = 0; // drive from internal clock
|
83 | T1CONbits.TON = 1; // timer on
|
84 |
|
85 | InitU2();
|
86 |
|
87 | while (1) {
|
88 | __builtin_btg((unsigned int *) &LATA, 7); // toggle LED D10
|
89 | TMR1 = 0;
|
90 | while (TMR1 < 31250) // ~500ms delay if we realy run at 16Mhz FCY.
|
91 | {
|
92 | }
|
93 |
|
94 | putU2(c++);
|
95 |
|
96 | if (c > (0x41 + 25)) { // A-Z should be received on other end.
|
97 | c = 0x41;
|
98 | putU2('\r');
|
99 | putU2('\n');
|
100 | toggle_d9(); // toggle LED D9 every time A-Z characters send.
|
101 | }
|
102 | }
|
103 |
|
104 | return (EXIT_SUCCESS
|