Hi,
Here are the soft/hardware tools I used: Software: Uvision4 V4.03q Board: CP-JR-LPC2368 debug set up with Ulink2
I am a beginner to microcontroller programming. I tried to run a simple UART0 with serial window as I/O interface using the Retarget.c and Serial.c sample (see below) provided by Keil website with the main function below. My hardware setup is just the board and ulink2 connected to PC and the board. and I had flash and start a debug session.
**My question is how can I view the printf string (which i think is the input) through the serial window UART0(which i think is the output)? Thanks in advance for any advice/help!
#include <stdio.h> /* prototype declarations for I/O functions */ #include <LPC23xx.H>
/****************/ /* main program */ /****************/ // Import external functions from Serial.c file extern void init_serial (void);
int main (void) { init_serial(); printf ("uart testing\n");
while (1) { }
}
/******************************************************************************/ /* RETARGET.C: 'Retarget' layer for target-dependent low level functions */ /******************************************************************************/ #include <stdio.h> #include <rt_misc.h>
#pragma import(__use_no_semihosting_swi)
extern int sendchar(int ch); /* in serial.c */
struct __FILE { int handle; /* Add whatever you need here */ }; FILE __stdout;
int fputc(int ch, FILE *f) { return (sendchar(ch)); }
int ferror(FILE *f) { /* Your implementation of ferror */ return EOF; }
void _ttywrch(int ch) { sendchar(ch); }
void _sys_exit(int return_code) { label: goto label; /* endless loop */ }
/******************************************************************************/ /* SERIAL.C: Low Level Serial Routines */ /******************************************************************************/
#include <LPC23xx.H> // LPC23xx definitions
#define UART0 // Use UART 0 for printf
// If UART 0 is used for printf #ifdef UART0 #define UxFDR U0FDR #define UxLCR U0LCR #define UxDLL U0DLL #define UxDLM U0DLM #define UxLSR U0LSR #define UxTHR U0THR #define UxRBR U0RBR // If UART 1 is used for printf #elif defined(UART1) #define UxFDR U1FDR #define UxLCR U1LCR #define UxDLL U1DLL #define UxDLM U1DLM #define UxLSR U1LSR #define UxTHR U1THR #define UxRBR U1RBR #endif
void init_serial (void) { // Initialize Serial Interface #ifdef UART0 PINSEL0 |= 0x00000050; // Enable TxD0 and RxD0 #elif defined (UART1) PINSEL0 |= 0x40000000; // Enable TxD1 PINSEL1 |= 0x00000001; // Enable RxD1 #endif UxFDR = 0; // Fractional divider not used UxLCR = 0x83; // 8 bits, no Parity, 1 Stop bit UxDLL = 78; // 9600 Baud Rate @ 12.0 MHZ PCLK UxDLM = 0; // High divisor latch = 0 UxLCR = 0x03; // DLAB = 0 }
// Implementation of sendchar (also used by printf function to output data) int sendchar (int ch) { // Write character to Serial Port
while (!(UxLSR & 0x20));
return (UxTHR = ch); }
int getkey (void) { // Read character from Serial Port
while (!(UxLSR & 0x01));
return (UxRBR); }
Thanks Rose
Have you tried to spent but just a little time with the user manual for the processor?
Have you made sure that you understand what every line of code actually does?
Do you think it is safe to set PINSEL1=0x003C000 for TXD3&RXD3?
What about other pins that requires specific settings? A write to PINSEL1 affects P0.16 ... P0.31, i.e. 16 pins. TXD3 and RXD3 are just two pins - what about the other 14 pins?
When playing with PINSELx registers, it is normally much better to to use and/or to just modify the configuration for pins you want to change, and leave the other bits untouched.
Never ever do dumb copy/paste. Make sure you understand the code, or you will be lost.