Created
September 4, 2013 14:07
-
-
Save Embedded-linux/6437417 to your computer and use it in GitHub Desktop.
Transmit Data From UART0 & UART1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Transmit Data from UART0/UART1 module on LPC 2148 | |
| //————————————————————————————————————————————————————————————————————————// | |
| // Program : Library for serial communication(UART) | |
| // Description : Library for about serial communication UART0 and UART1 of LPC2148 | |
| // Frequency : Crystal 12 MHz at PLL 5x(CCLK = 60 MHz),PCLK = 30 MHz | |
| // Filename : uart.h | |
| // C compiler : Keil CARM Compiler | |
| //————————————————————————————————————————————————————————————————————————// | |
| #define _PCLK 30000000 // Define PCLK for configuration baudrate | |
| #define uart1_setbaud(x) uart1_init(x) // Define function name uart1_setbaud equal uart1_init | |
| #define uart0_setbaud(x) uart0_init(x) // Define function name uart0_setbaud equal uart0_init | |
| //—————————————— Function for Initial UART1 ————————————————————// | |
| void uart1_init(unsigned int _baudrate) | |
| { | |
| unsigned short u1dl; | |
| u1dl = _PCLK/(16*_baudrate); // Calculate for U1DL value | |
| PINSEL0 |= 0x00050000; // Enable rx,tx | |
| U1LCR = 0x00000083; // 8 bit data,1 stop bit,no parity bit | |
| U1DLL = u1dl & 0xFF; // U1DL for low byte | |
| U1DLM = (u1dl>>8); // U1DL for high byte | |
| U1LCR = 0x00000003; // DLAB =0 | |
| } | |
| //—————————————— Function for send character 1 time via UART1———————————// | |
| void uart1_putc(char c) | |
| { | |
| while(!(U1LSR & 0x20)); // Wait until UART1 ready to send character | |
| U1THR = c; // Send character | |
| } | |
| //—————————————— Function for send string via UART1————————————————// | |
| void uart1_puts(char *p) | |
| { | |
| while(*p) // Point to character | |
| { | |
| uart1_putc(*p++); // Send character then point to next character | |
| } | |
| } | |
| //—————————————— Function for Initial UART0 ————————————————————// | |
| void uart0_init(unsigned int _baudrate) | |
| { | |
| unsigned short u0dl; | |
| u0dl = _PCLK/(16*_baudrate); // Calculate for U0DL value | |
| PINSEL0 |= 0x00000005; // Enable rx,tx | |
| U0LCR = 0x00000083; // 8 bit data,1 stop bit,no parity bit | |
| U0DLL = u0dl & 0xFF; // U0DL for low byte | |
| U0DLM = (u0dl>>8); // U0DL for high byte | |
| U0LCR = 0x00000003; // DLAB =0 | |
| } | |
| //—————————————— Function for send character 1 time via UART0———————————// | |
| void uart0_putc(char c) | |
| { | |
| while(!(U0LSR & 0x20)); // Wait until UART0 ready to send character | |
| U0THR = c; // Send character | |
| } | |
| //—————————————— Function for send string via UART1————————————————// | |
| void uart0_puts(char *p) | |
| { | |
| while(*p) // Point to character | |
| { | |
| uart0_putc(*p++); // Send character then point to next character | |
| } | |
| } | |
| //—————————————————————————————————————————————————————————————————————————// | |
| // Program : UART example | |
| // Description : Example for test module UART0 and UART1 | |
| // Frequency : Crystal 12 MHz at PLL 5x(CCLK = 60 MHz),PCLK = 30 MHz | |
| // Filename : uart_0_1.c | |
| // C compiler : Keil CARM Compiler | |
| //—————————————————————————————————————————————————————————————————————————// | |
| #include “lpc214x.h” // Header file for Phillips LPC2148 controller | |
| #include “uart.h” // Library for module UART0,UART1(from jx2148_include folder) | |
| #include “stdio.h” // Library for use printf function(For UART1) | |
| //————————————————— Initial system clock ————————————————————// | |
| void init() | |
| { | |
| PLL0CFG=0x24; // MSEL = 4,PSEL = 2 | |
| PLL0FEED=0xAA; // Feed process | |
| PLL0FEED=0x55; | |
| PLL0CON=0x1; | |
| PLL0FEED=0xAA; // Feed process | |
| PLL0FEED=0x55; | |
| while(!(PLL0STAT & 0x400)) ; // Wait until PLL Locked | |
| PLL0CON=0x3; // Connect the PLL as the clock source | |
| PLL0FEED=0xAA; // Feed process | |
| PLL0FEED=0x55; | |
| MAMCR=0x2; // Enabling MAM and setting number of clocks used | |
| // for Flash memory fetch (4 cclks in this case) | |
| MAMTIM=0x4; | |
| VPBDIV=0x02; // PCLK at 30 MHz | |
| } | |
| //————————————————— Main Program ————————————————————————// | |
| void main() | |
| { | |
| init(); // Initialize the system | |
| SCS = 0x03; // select the “fast” version of the I/O ports | |
| uart0_init(9600); | |
| // Initial UART0 @ 9600 bps,8 bit data ,1 stop bit ,no parity bit | |
| uart1_init(9600); | |
| // Initial UART1 @ 9600 bps,8 bit data ,1 stop bit ,no parity bit | |
| while (1) | |
| { | |
| uart0_puts(“Test UART0\r\n”); // Send string to UART0 | |
| printf(“Test UART1\r\n”); // Send string to UART1 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment