Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 /* * File: uart1.c * Author: User2 * * Created on April 10, 2017, 11:21 AM */ #include "xc.h" #include "p24fj64ga006.h" _CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx1) _CONFIG2(FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_HS & FNOSC_PRIPLL) #define FCY 8000000 #define BAUDRATE 9600 #define BRGVAL ((FCY/BAUDRATE)/16)-1 unsigned int i; void InitClock() { // PLLFBD = 78; // M = 160 CLKDIV = 0x0043; //CLKDIVbits.PLLPOST = 1; // N2 = 4 //CLKDIVbits.PLLPRE = 3; // N1 = 5 while(OSCCONbits.LOCK != 1) {}; } void Init_UART(void) { // configure U2MODE U1MODEbits.UARTEN = 0; // Bit15 TX, RX DISABLED, ENABLE at end of func U1MODEbits.USIDL = 0; // Bit13 Continue in Idle U1MODEbits.IREN = 0; // Bit12 No IR translation U1MODEbits.RTSMD = 1; // Bit11 Simplex Mode U1MODEbits.UEN = 0; // Bits8,9 TX,RX enabled, CTS,RTS not U1MODEbits.WAKE = 0; // Bit7 No Wake up (since we don't sleep here) U1MODEbits.LPBACK = 0; // Bit6 No Loop Back U1MODEbits.ABAUD = 0; // Bit5 No Autobaud (would require sending '55') U1MODEbits.RXINV = 0; // Bit4 IdleState = 1 U1MODEbits.BRGH = 0; // Bit3 16 clocks per bit period U1MODEbits.PDSEL = 0; // Bits1,2 8bit, No Parity U1MODEbits.STSEL = 0; // Bit0 One Stop Bit U1BRG = BRGVAL; // baud rate // Load all values in for U1STA SFR U1STAbits.UTXISEL1 = 0; //Bit15 Int when Char is transferred (1/2 config!) U1STAbits.UTXINV = 0; //Bit14 N/A, IRDA config U1STAbits.UTXISEL0 = 0; //Bit13 Other half of Bit15 U1STAbits.UTXBRK = 0; //Bit11 Disabled U1STAbits.UTXEN = 0; //Bit10 TX pins controlled by periph U1STAbits.UTXBF = 0; //Bit9 *Read Only Bit* U1STAbits.TRMT = 0; //Bit8 *Read Only bit* U1STAbits.URXISEL = 0; //Bits6,7 Int. on character recieved U1STAbits.ADDEN = 0; //Bit5 Address Detect Disabled U1STAbits.RIDLE = 0; //Bit4 *Read Only Bit* U1STAbits.PERR = 0; //Bit3 *Read Only Bit* U1STAbits.FERR = 0; //Bit2 *Read Only Bit* U1STAbits.OERR = 0; //Bit1 *Read Only Bit* U1STAbits.URXDA = 0; //Bit0 *Read Only Bit* IFS0bits.U1TXIF = 0; // Clear the Transmit Interrupt Flag IEC0bits.U1TXIE = 1; // Enable Transmit Interrupts IFS0bits.U1RXIF = 0; // Clear the Recieve Interrupt Flag IEC0bits.U1RXIE = 1; // Enable Recieve Interrupts U1MODEbits.UARTEN = 1; // And turn the peripheral on U1STAbits.UTXEN = 1; /* wait at least 104 usec (1/9600) before sending first char */ for(i = 0; i < 4160; i++) { Nop(); } U1TXREG = 'a'; // Transmit one character } void __attribute__((__interrupt__)) _U1TXInterrupt(void) { IFS0bits.U1TXIF = 0; // clear TX interrupt flag U1TXREG = 'a'; // Transmit one character } int main(void) { InitClock(); //char message[] = "Bienvenue au lab"; //Initialisation UART Init_UART(); while(1) { //LATAbits.LATA0 = !LATAbits.LATA0; //Delay_1ms(500); } }
_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx1)
_CONFIG2(FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_XT & FNOSC_FRCPLL)
#define FCY 8000000
#define BAUDRATE 9600
#define BRGVAL ((FCY/BAUDRATE)/16)-1
// #define BRGVAL 0x1A0
unsigned int i;
void InitClock() {
//PLLFBD = 78; // M = 160
//CLKDIV = 0x0043;
CLKDIVbits.RCDIV0=0;
CLKDIVbits.RCDIV1=0;
CLKDIVbits.RCDIV2=0;
//CLKDIVbits.PLLPOST = 1; // N2 = 4
//CLKDIVbits.PLLPRE = 3; // N1 = 5
while(OSCCONbits.LOCK != 1) {};
}
void Init_UART(void)
{
U1MODEbits.UARTEN = 0; // Bit15 TX, RX DISABLED, ENABLE at end of func
U1MODEbits.USIDL = 0; // Bit13 Continue in Idle
U1MODEbits.IREN = 0; // Bit12 No IR translation
U1MODEbits.RTSMD = 1; // Bit11 Simplex Mode
U1MODEbits.UEN = 0; // Bits8,9 TX,RX enabled, CTS,RTS not
U1MODEbits.WAKE = 0; // Bit7 No Wake up (since we don't sleep here)
U1MODEbits.LPBACK = 0; // Bit6 No Loop Back
U1MODEbits.ABAUD = 0; // Bit5 No Autobaud (would require sending '55')
U1MODEbits.RXINV = 0; // Bit4 IdleState = 1
U1MODEbits.BRGH = 0; // Bit3 16 clocks per bit period
U1MODEbits.PDSEL = 0; // Bits1,2 8bit, No Parity
U1MODEbits.STSEL = 0; // Bit0 One Stop Bit
//U1BRG = BAUDRATEREG1; // baud rate
U1BRG = BRGVAL;
// Load all values in for U1STA SFR
U1STAbits.UTXISEL1 = 0; //Bit15 Int when Char is transferred (1/2 config!)
U1STAbits.UTXINV = 0; //Bit14 N/A, IRDA config
U1STAbits.UTXISEL0 = 0; //Bit13 Other half of Bit15
U1STAbits.UTXBRK = 0; //Bit11 Disabled
U1STAbits.UTXEN = 0; //Bit10 TX pins controlled by periph
U1STAbits.UTXBF = 0; //Bit9 *Read Only Bit*
U1STAbits.TRMT = 0; //Bit8 *Read Only bit*
U1STAbits.URXISEL = 0; //Bits6,7 Int. on character recieved
U1STAbits.ADDEN = 0; //Bit5 Address Detect Disabled
U1STAbits.RIDLE = 0; //Bit4 *Read Only Bit*
U1STAbits.PERR = 0; //Bit3 *Read Only Bit*
U1STAbits.FERR = 0; //Bit2 *Read Only Bit*
U1STAbits.OERR = 0; //Bit1 *Read Only Bit*
U1STAbits.URXDA = 0; //Bit0 *Read Only Bit*
IFS0bits.U1TXIF = 0; // Clear the Transmit Interrupt Flag
IEC0bits.U1TXIE = 1; // Enable Transmit Interrupts
IFS0bits.U1RXIF = 0; // Clear the Recieve Interrupt Flag
IEC0bits.U1RXIE = 1; // Enable Recieve Interrupts
U1MODEbits.UARTEN = 1; // And turn the peripheral on
U1STAbits.UTXEN = 1;
}
CLKDIVbits.RCDIV = 0;
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?