MCs Lab Programs - No Errors
MCs Lab Programs - No Errors
1:
Using Keil software, observe the various Registers, Dump, CPSR, with a simple
Assembly Language Programs (ALP).
#include <lpc214x.h>
void UART0_Init(void){
//UART Initialization
PINSEL0 |= 0x00000005; //Enable UART0 pins P0.0(TXD0) and P0.1(RXD0)
U0LCR = 0x83; //8-bit data, 1 stop bit, Enable DLAB
U0DLL = 0x61; //Set baud rate to 9600 (for PCLK = 15MHz)
U0DLM = 0x00; //DLM = 0 for baud rate 9600
U0LCR = 0x03; //Disable DLAB, 8-bit data, 1 stop bit
}
void UART0_SendChar(char c) {
while(!(U0LSR & 0x20)); //Wait until the UART0 to transmit
U0THR = c; //Transmit character
}
char UART0_ReceiveChar(void) {
while(!(U0LSR & 0x01)); //Wait until data is received
return U0RBR; //Read and return received character
}
void UART0_SendString(const char *str) {
while(*str) {
UART0_SendChar(*str++);
}
}
void UART0_ReceiveString(char *str, int maxLength){
char c;
int i=0;
while(i<maxLength-1){
c= UART0_ReceiveChar();
if(c=='\r'||c=='\n'){ //End of input on newline or carriage return
break;
}
str[i++]=c;
UART0_SendChar(c); //Echo the received character
}
str[i]='\0'; //Null-terminate the string
}
void toLowerString(char *str) {
while(*str) {
*str=toLower(*str);
str++;
}
}
void toUpperString(char *str) {
while(*str) {
*str=toUpper(*str);
str++;
}
}
int main() {
char str[100];
UART0_Init();
//Prompt and convert string to lowercase
UART0_SendString("Enter a string in Uppercase: ");
UART0_ReceiveString(str, sizeof(str));
UART0_SendString("\r\nLowercase conversion: ");
toLowerString(str);
UART0_SendString(str);
UART0_SendString("\r\n");
return 0;
}
//Function definitions
//Function to convert character to lowercase
char toLower(char ch){
if(ch>='A' && ch<='Z'){
return ch+('a'-'A');
} else{
return ch; //Return unchanged if not uppercase
}
}
//Function to convert character to uppercase
char toUpper(char ch){
if(ch>='a' && ch<='z'){
return ch-('a'-'A');
} else{
return ch;
}
}