This is code for showing temperature sensed value of UART0 of lpc2138
#include <stdio.h> #include"LPC213.h" // Define LPC2148 Header File
#define CR 0x0D #define DONE 0x80000000 #define START 0x01000000 #define PRESET 0x00230600
void Delay (); void init_serial (void); int putchar (int ch); int getchar(void); int main () { unsigned long Val; char *Ptr = "Current Temperature is:"; VPBDIV = 0x02; //pclk @ 30MHz init_serial(); PINSEL1 = 0x01 << 24; //P0.28 configure as ADC0.1 AD0CR = PRESET | 0x02; AD0CR |= START; //Start Conversion NOW while (1) { do { Val = AD0GDR; } while ((Val & DONE) == 0); Val = ((AD0GDR >> 6) & 0x3FF); while (*Ptr) { putchar(*Ptr++); } printf ("%4d ", Val); putchar(getchar()); // Echo terminal
} //printf ("\xF8\F \r"); }
void Delay () { unsigned int i,j; for (i=0;i<50;i++) for (j=0;j<500;j++); }
void init_serial (void) { PINSEL0 = 0x00000005; // Enable RxD0 and TxD0 U0LCR = 0x00000083; //8 bits, no Parity, 1 Stop bit U0DLL = 0x000000C3; //9600 Baud Rate @ 30MHz VPB Clock U0LCR = 0x00000003; } int putchar (int ch) { if (ch == '\n') { while (!(U0LSR & 0x20)); U0THR = CR; } while (!(U0LSR & 0x20)); return (U0THR = ch); } int getchar(void) { while (!(U0LSR & 0x01)); return (U0RBR); }
This is the above code which shows following errors when I add studio.h header file in code
compiling adc.c... adc.c(9): warning: #1295-D: Deprecated declaration Delay - give arg types adc.c(11): error: #79: expected a type specifier adc.c(12): error: #55: too many arguments in macro invocation adc.c(12): error: #79: expected a type specifier adc.c(55): error: #79: expected a type specifier adc.c(55): error: #141-D: unnamed prototyped parameters not allowed when body is present adc.c(65): error: #55: too many arguments in macro invocation adc.c(65): error: #79: expected a type specifier adc.c(65): error: #141-D: unnamed prototyped parameters not allowed when body is present Target not created
And if I dont add studio.h header file then printf function doesn't work. What should I do in this case? I guess I need printf function in order to print ADC value to the UART terminal.
Yeah I have done that and I am getting error and warning free hex file. But if I load that file in processor , it doesnt show any kinda character on UART.
But If I remove studio header file and printf function from code then it shows transmitted string on UART.
May I know why doesn't it show when I include that? Is anything wrong in it?
I have another code too
/* FOSC = 12MHz CCLK = 60MHz PCLK = 15MHz */ #include <LPC213X.H> //#include<stdio.h>
void UART0_Init(void); void UART0_Write(unsigned char value); void UART0_Write_Text(unsigned char msg[]); unsigned char UART0_Read(void);
void ADC0_Init(void); unsigned int ADC0_Read(void); void Delay_ms(unsigned long times); unsigned long adc_data; int main() { unsigned char msg[] = "EMBEDDED LAB"; unsigned char LM35_Temperature[] = "TEMP. MONITOR"; //unsigned char data_received[] = "TEMP VALUE:"; unsigned char ones,tens,hundreds,thousands; unsigned long temp;
UART0_Init(); Delay_ms(10); UART0_Write_Text(msg); UART0_Write(10); UART0_Write(13);
UART0_Write_Text(LM35_Temperature); UART0_Write(10); UART0_Write(13); Delay_ms(500);
ADC0_Init(); while(1) { adc_data = ADC0_Read(); adc_data = adc_data*3300; adc_data = adc_data/1023; //Value of Voltage in Milli Volts // printf("%4d",adc_data); temp = adc_data; ones = temp % 10; temp = temp / 10; tens = temp % 10; temp = temp / 10; hundreds = temp % 10; temp = temp / 10; thousands = temp % 10; ones |= 0x30; tens |= 0x30; hundreds |= 0x30; thousands |= 0x30;
printf("%d",thousands); printf("%d",hundreds); printf("%d",tens); printf("."); printf("%d",ones); printf(" "); printf("C");
Delay_ms(10); } }
void Delay_ms(unsigned long times) { unsigned long i,j; for(j=0;j<times;j++) for(i=0;i<7500;i++); }
void UART0_Init(void) { PINSEL0 = 0x00000005; //P0.0 as TX0 and P0.1 as RX0 U0LCR = 0x83; //Enable access to Divisor Latches //and Set 8 bit Character Length with 1 Stop bit and Parity Disabled //Access to Divisor Latches is Enabled, in order to write Baud Rate Generator Registers //Values to be written in Baud Rate Registers U0DLM and U0LL /* Formula is Baud_Rate = PCLK*MulVal / [(16*(256*U0DLM+U0DLL)*(MulVal + DivAddVal))] Example:- MulVal = 1; DivAddVal = 0; Baud_Rate = 9600; PCLK = 15MHz U0DLM = 0; Hence, U0DLL = 15000000/(9600*16) = 97.65625 = 98 U0DLL = 98 = 0x62 */ U0DLM = 0x00; U0DLL = 0x62; //Baud Rate of 9600 U0LCR = 0x03; //Disable Access to Divisor Latches } void UART0_Write(unsigned char value) { /* THRE bit can be extracted by this U0LSR & 0x20 THRE = 0 means data is present. THRE = 1 means register is empty. In order to transmit data, we have to wait will the THRE = 1, then only we can transmit data. */ while(!(U0LSR&0x20)); //THRE = 0 stay here U0THR = value; } void UART0_Write_Text(unsigned char msg[]) { while(*msg) { UART0_Write(*msg); msg++; } } unsigned char UART0_Read(void) { /*Receiver Data Ready = U0LSR.0 bit RDR bit can be extracted by this U0LSR & 0x01 RDR = 0 means no Data is Received in U0RBR RDR = 1 means that Data is present in U0RBR */ while(!(U0LSR & 0x01)); //RDR = 0 stay here return (U0RBR); }
void ADC0_Init(void) {
AD0CR = 1<<21; //A/D is Operational AD0CR = 0<<21; //A/D is in Power Down Mode PCONP = (PCONP &0x001817BE) | (1UL<<12); PINSEL0 = 0x00; PINSEL1 = 0x00400000; //P0.27 is Configured as Analog to Digital Converter Pin AD0.0 AD0CR = 0x00200401; //CLKDIV=4,Channel-0.0 Selected,A/D is Operational /* A/D Clock = PCLK /(CLKDIV+1); */ } unsigned int ADC0_Read(void) { unsigned long adc_data; AD0CR |= 1UL<<24; //Start Conversion do { adc_data = AD0GDR; }while(!(adc_data & 0x80000000)); //Wait untill the DONE bits Sets AD0CR &= ~0x01000000; //Stops the A/D Conversion adc_data = adc_data >> 6; adc_data = adc_data & 0x3FF; //Clearing all other Bits return (adc_data); }
But In this code I am not much sure about syntax of bold statements in code. Pls help me out with this..
Read the posting tips lest we assume you're a fool. http://www.keil.com/forum/tips.asp
Could it be that if you don't have the printf() it doesn't call the SWI and fault? Could you use a debugger and identify what's going on? Could you look at the .MAP and see what's linking in?
The compiler gives errors if the syntax is wrong. The linker gives errors if the binding is wrong. Neither can tell if your code is functionally correct.
Like I said, go review how to actually retarget the stdio/console functionality in Keil.
http://www.keil.com/forum/58917/
You are hard to help since you don't like to post readable code.
Why do you think printf() text should end up on the UART? What have you done to make that happen? How would the compiler/linker know that your UART should be seen as a console?
By the way - have you really found many posts that have recommended the use of a dumb busy-loop to get a trusty 1ms delay?