void RS485_init (void) { // TxD: Port 0.15 // RxD: Port 0.16 LPC_PINCON->PINSEL0 &= ~0xC0000000; LPC_PINCON->PINSEL0 |= 0x40000000; LPC_PINCON->PINSEL1 &= ~0x00000003; LPC_PINCON->PINSEL1 |= 0x00000001; // Initialisation routines LPC_SC->PCONP |=(1 <<4); // Set PCUART bit LPC_UART1->LCR = 0x83; // 8 bits, 1 stopbit, no parity, DLAB = 1 //Baudrate: 25E6/16/81 = 19.290 Baud LPC_UART1->DLM = 0; LPC_UART1->DLL = 81; LPC_UART1->LCR = 0x03; // 8 bits, 1 stopbit, no parity, DLAB = 0 LPC_UART1->FCR = 0x07; // FIFO Enable } |
LPC_UART1->FDR = 0x01; // DIVADDVAL = 0 -- MULVAL = 1 |
[SIZE=2]LPC_PINCON->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]PINSEL1[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] &= ~0x00000003;[/SIZE] |
[SIZE=2][COLOR=#7f0055][B][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/B] [LEFT][/COLOR][/SIZE][SIZE=2][B]RS485_init[/B] ([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2])[/SIZE][/LEFT] [LEFT][SIZE=2]{[/SIZE][/LEFT] [LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// TxD: Port 0.15[/COLOR][/SIZE][/COLOR][/SIZE] [SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// RxD: Port 0.16[/COLOR][/SIZE][/COLOR][/SIZE] [LEFT][SIZE=2]LPC_PINCON->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]PINSEL0[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] &= 0x3FFFFFFF;[/SIZE][/LEFT] [/LEFT] [SIZE=2]LPC_PINCON->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]PINSEL0[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] |= 0x40000000;[/SIZE] [SIZE=2]LPC_PINCON->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]PINSEL1[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] &= 0xFFFFFFFC;[/SIZE] [SIZE=2]LPC_PINCON->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]PINSEL1[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] |= 0x00000001;[/SIZE] [SIZE=2]LPC_PINCON->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]PINMODE0[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] &= 0x3FFFFFFF;[/SIZE] [SIZE=2]LPC_PINCON->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]PINMODE0[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] |= 0x80000000;[/SIZE] [LEFT][SIZE=2]LPC_PINCON->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]PINMODE1[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] &= 0xFFFFFFFC;[/SIZE] [LEFT][SIZE=2]LPC_PINCON->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]PINMODE1[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] |= 0x00000002;[/SIZE][/LEFT] [/LEFT] [SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f][/COLOR][/SIZE][/COLOR][/SIZE] [SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// Initialisation routines[/COLOR][/SIZE][/COLOR][/SIZE] [SIZE=2]LPC_SC->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]PCONP[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] |= 0x00000010; [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// Set PCUART bit[/COLOR][/SIZE][/COLOR][/SIZE] [SIZE=2]LPC_SC->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]PCLKSEL0[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] |= 0x00000100; [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// Set PCLK_UART1 to 0x01 = CCLK/1[/COLOR][/SIZE][/COLOR][/SIZE] [SIZE=2]LPC_UART1->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]LCR[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = 0x83; [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// 8 bits, 1 stopbit, no parity, DLAB = 1[/COLOR][/SIZE][/COLOR][/SIZE] [SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// Baudrate: 19.200 Baud[/COLOR][/SIZE][/COLOR][/SIZE] [SIZE=2]LPC_UART1->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]DLM[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = 0;[/SIZE] [SIZE=2]LPC_UART1->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]DLL[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = 52;[/SIZE] [LEFT][SIZE=2]LPC_UART1->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]LCR[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = 0x03; [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// 8 bits, 1 stopbit, no parity, DLAB = 0[/COLOR][/SIZE][/COLOR][/SIZE] [SIZE=2]LPC_UART1->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]FDR[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = 0x01; [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// DIVADDVAL = 0 -- MULVAL = 1[/COLOR][/SIZE][/COLOR][/SIZE][/LEFT] [SIZE=2]LPC_UART1->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]FCR[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = 0x07; [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// FIFO Enable[/COLOR][/SIZE][/COLOR][/SIZE] [SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//LPC_UART1->TER = 0x80;[/COLOR][/SIZE][/COLOR][/SIZE] [LEFT][SIZE=2]}[/SIZE][/LEFT] [LEFT]AND[/LEFT] [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][B]UART1_PutChar[/B] ([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]unsigned[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]char[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ch)[/SIZE][/LEFT] [SIZE=2]{[/SIZE] [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055] while[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (!(LPC_UART1->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]LSR[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] & 0x20)){[/SIZE] [SIZE=2] led_red_on();[/SIZE] [LEFT][SIZE=2] }[/SIZE] [SIZE=2] led_red_off();[/SIZE] [SIZE=2] LPC_UART1->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]THR[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = ch;[/SIZE] [SIZE=2]}[/SIZE][/LEFT] [/LEFT] |
// *********************** // Function to set up UART void UART0_Init(int baudrate) { int pclk; unsigned long int Fdiv; // PCLK_UART0 is being set to 1/4 of SystemCoreClock pclk = SystemCoreClock / 4; // Turn on power to UART0 LPC_SC->PCONP |= PCUART0_POWERON; // Turn on UART0 peripheral clock LPC_SC->PCLKSEL0 &= ~(PCLK_UART0_MASK); LPC_SC->PCLKSEL0 |= (0 << PCLK_UART0); // PCLK_periph = CCLK/4 // Set PINSEL0 so that P0.2 = TXD0, P0.3 = RXD0 LPC_PINCON->PINSEL0 &= ~0xf0; LPC_PINCON->PINSEL0 |= ((1 << 4) | (1 << 6)); LPC_UART0->LCR = 0x83; // 8 bits, no Parity, 1 Stop bit, DLAB=1 Fdiv = ( pclk / 16 ) / baudrate ; // Set baud rate LPC_UART0->DLM = Fdiv / 256; LPC_UART0->DLL = Fdiv % 256; LPC_UART0->LCR = 0x03; // 8 bits, no Parity, 1 Stop bit DLAB = 0 LPC_UART0->FCR = 0x07; // Enable and reset TX and RX FIFO } |