// uart_test.c /* Copyright (C) 2017 H.Poetzl ** ** This program is free software: you can redistribute it and/or ** modify it under the terms of the GNU General Public License ** as published by the Free Software Foundation, either version ** 2 of the License, or (at your option) any later version. */ // ------------------------------------------------ // configuration #define NO_BIT_DEFINES #include #include #define CONFIG(k, n) __code static char __at _ ## k __ ## k = n CONFIG(CONFIG1, _FEXTOSC_OFF & _RSTOSC_HFINT1 & _CSWEN_ON & _CLKOUTEN_OFF); CONFIG(CONFIG2, _MCLRE_ON & _WDTE_OFF & _PPS1WAY_OFF); #define CPU_NDIV 0 #define out_ser_ch(c) do { \ while (!PIR1bits.TXIF); \ TX1REG = (c); \ __asm \ NOP \ __endasm; \ } while (0) #define out_ser_str0(s) do { \ const char *_p = (s); \ \ while (*_p) \ out_ser_ch(*_p++); \ } while (0) #define out_ser_str(s, n) do { \ const char *_p = (s); \ unsigned char _n = (n); \ \ while (_n--) \ out_ser_ch(*_p++); \ } while (0) #define out_ser_flush() do { \ while (!PIR1bits.TXIF); \ while (!TX1STAbits.TRMT); \ } while (0) // -------------------------------------------------- // and our main entry point void main() { // all digital ANSELA = 0; ANSELB = 0; ANSELC = 0; // all input TRISA = 0xFF; TRISB = 0xFF; TRISC = 0xFF; // all push pull ODCONA = 0x00; ODCONB = 0x00; ODCONC = 0x00; // all outputs '1' LATA = 0xFF; LATB = 0xFF; LATC = 0xFF; /* configure oscillator */ OSCCON3bits.CSWHOLD = 0; OSCCON1bits.NDIV = CPU_NDIV; /* configure EUSART */ TX1STA = 0; TX1STAbits.TXEN = 1; TX1STAbits.BRGH = 1; RC1STA = 0; RC1STAbits.SPEN = 1; RC1STAbits.CREN = 0; BAUD1CON = 0; BAUD1CONbits.BRG16 = 1; BAUD1CONbits.WUE = 0; SP1BRG = 0; SP1BRGH = 0; /* disable unused modules */ PMD0 = 0b00000101; /* All but Clock Net,CLKR,FVR */ PMD1 = 0b11111010; /* All but Timer 0,2 */ PMD2 = 0b01000110; /* All but ADC */ PMD3 = 0b11111110; /* All but CCP1 */ PMD4 = 0b00000010; /* All but UART1*/ PMD5 = 0b00011101; /* All but CLC1 */ /* PPS */ PPSLOCKbits.PPSLOCKED = 0; RA0PPS = 0b10100; /* TX1 on ICSPDAT */ while (1) { uint16_t i; out_ser_str0("Hello World!\n"); out_ser_flush(); for (i=0; i<20000; i++); } }