100% found this document useful (1 vote)
38 views9 pages

Microcontroller Lab: UART & Motor Control

The program initializes the port pins of the microcontroller that are connected to the 7-segment display as output. It then uses a for loop to display the hexadecimal digits from 0 to F on the display with a delay between each digit. For each digit, the corresponding port pins are set to display that digit on the 7-segment display. After displaying all digits, it repeats the process continuously.

Uploaded by

H S Parangi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
38 views9 pages

Microcontroller Lab: UART & Motor Control

The program initializes the port pins of the microcontroller that are connected to the 7-segment display as output. It then uses a for loop to display the hexadecimal digits from 0 to F on the display with a delay between each digit. For each digit, the corresponding port pins are set to display that digit on the 7-segment display. After displaying all digits, it repeats the process continuously.

Uploaded by

H S Parangi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Microcontroller and Embedded Systems Lab 18CSL48

9. Display “Hello World” message using Internal UART.

Aim: Write a program to Display “Hello World” message using Internal UART.

Program: [/* Use ALS Kit only*/]


#include<lpc214x.h>

void uart_init(void);
unsigned int delay;
unsigned char *ptr;
unsigned char arr[]="HELLO WORLD\r";

int main()
{
while(1)
{
uart_init();
ptr = arr;
while(*ptr!='\0')
{
U0THR=*ptr++;
while(!(U0LSR & 0x40)== 0x40);
for(delay=0;delay<=600;delay++);
}
for(delay=0;delay<=60000;delay++);
}
}
void uart_init(void)
{
PINSEL0=0X0000005; //select TXD0 and RXD0 lines
U0LCR = 0X00000083; //enable baud rate divisor loading and
U0DLM = 0X00; //select the data format
U0DLL = 0x13; //select baud rate 9600 bps
U0LCR = 0X00000003;

Dept. of Computer Science & Engineering Page 2


Microcontroller and Embedded Systems Lab 18CSL48

10. Interface and Control a DC Motor.

Aim: Write a program to Interface and Control a DC Motor.

Program: [/* Use ALS Kit only*/]

#include<lpc214x.h>

void clock_wise(void);
void anti_clock_wise(void);
unsigned int j=0;

int main()
{
IO0DIR= 0X00000900;
IO0SET= 0X00000100; //P0.8 should always high.

while(1)
{
clock_wise();
for(j=0;j<400000;j++); //delay
anti_clock_wise();
for(j=0;j<400000;j++); //delay
} //End of while(1)
} //End of Main

void clock_wise(void)
{
IO0CLR = 0x00000900; //stop motor and also turn off relay
for(j=0;j<10000;j++); //small delay to allow motor to turn off
IO0SET = 0X00000900; //Selecting the P0.11 line for clockwise
//and turn on motor
}

void anti_clock_wise(void)
{
IO0CLR = 0X00000900; //stop motor and also turn off relay
for(j=0;j<10000;j++); //small delay to allow motor to turn off
IO0SET = 0X00000100; //not selecting the P0.11 line for Anticlockwise
}

Dept. of Computer Science & Engineering Page 3


Microcontroller and Embedded Systems Lab 18CSL48

11. Interface a Stepper motor and rotate it in clockwise and anti-clockwise direction.

Aim: Write a Program to Interface a Stepper motor and rotate it in clockwise and anti-clockwise direction.

Program:
// Description: Stepper Motor Rotating Clockwise direction after certain
delay rotating Anticlockwise direction.
// Pin Connection: P1.28 to P1.31 connected to Stepper motor driver
(ULN2003/2803)
//************************************************************************

#include<lpc214x.h> // Header file for LPC2148

//**** Function Declaration ********//

void delay(void);
void stepper_clock(void);
void stepper_Aclock(void);

//*************** END of Function Declaration **************//

//*************** MAIN Program **************//

int main()
{
PINSEL2 = 0x00000000; // P1.16 to P1.31 configured as GPIO

IODIR1 = 0xFFFFFFFF; // P1.16 to P1.31 configured as ouput port

while(1)
{
stepper_clock(); // Function calling (P1.28 to P1.31 port pins
//connected to stepper motor)
delay();

stepper_Aclock();
delay(); // Function calling (P1.28 to P1.31 port pins
//connected to stepper motor)
}
}

//*************** END of MAIN Program **************//

//*************** Delay Program **************//

void delay(void)
{
unsigned int i,j;

for(i=0;i<1000;i++)
for(j=0;j<1000;j++);

Dept. of Computer Science & Engineering Page 4


Microcontroller and Embedded Systems Lab 18CSL48

}
//*************** END of Delay Program **************//

//********* Stepper MOtor Clockwise and Anti clockwise Program**********//

void stepper_Aclock(void) //Stepper Motor Anti-Clockwise subprogram


{
unsigned int i;

for(i=0;i<5;i++)
{
IOSET1 = 0x90000000;
delay();
IOCLR1 = 0x90000000;
delay();
IOSET1 = 0xC0000000;
delay();
IOCLR1 = 0xC0000000;
delay();
IOSET1 = 0x60000000;
delay();
IOCLR1 = 0x60000000;
delay();
IOSET1 = 0x30000000;
delay();
IOCLR1 = 0x30000000;
delay();
}
}

void stepper_clock(void) /Stepper Motor Clockwise subprogram


{
unsigned int i;

for(i=0;i<5;i++)
{
IOSET1 = 0x30000000;
delay();
IOCLR1 = 0x30000000;
delay();
IOSET1 = 0x60000000;
delay();
IOCLR1 = 0x60000000;
delay();
IOSET1 = 0xC0000000;
delay();
IOCLR1 = 0xC0000000;
delay();
IOSET1 = 0x90000000;
delay();
IOCLR1 = 0x90000000;
delay();

Dept. of Computer Science & Engineering Page 5


Microcontroller and Embedded Systems Lab 18CSL48

IOSET1 = 0x30000000;
delay();
IOCLR1 = 0x30000000;
delay();
}
}

//**** END of Stepper Motor Clockwise and Anti clockwise Program***********//

Dept. of Computer Science & Engineering Page 6


Microcontroller and Embedded Systems Lab 18CSL48

13. Interface a DAC and generate Triangular and Square waveforms.

Aim: Write a Program to Interface a DAC and generate Triangular and Square waveforms.

Program: [/* Use ALS Kit only*/]


(i) TRIANGULAR WAVEFORM
#include <LPC21xx.h>
unsigned long int temp=0x00000000;

int main()
{
unsigned int i=0;

IO0DIR=0x00FF0000;

while(1)
{
// output 0 to FE
for(i=0;i!=0xFF;i++)
{
temp=i;
temp = temp << 16;
IO0PIN=temp;
}
// output FF to 1
for(i=0xFF; i!=0;i--)
{
temp=i;
temp = temp << 16;
IO0PIN=temp;
}
}//End of while(1)
}//End of main()

(ii) SQUARE WAVEFORM


#include <lpc21xx.h>
unsigned int var;
void delay(void);

int main()
{
PINSEL0 = 0x00000000 ; // Configure P0.0 to P0.15 as GPIO
PINSEL1 = 0x00000000 ; // Configure P0.16 to P0.31 as GPIO
IO0DIR = 0x00FF0000 ;

while(1)
{
IO0PIN = 0x00000000;

Dept. of Computer Science & Engineering Page 7


Microcontroller and Embedded Systems Lab 18CSL48

var= 0x00000000;
delay();
IO0PIN = 0x00FF0000;
var= 0x00FF0000;
delay();
}
}

void delay(void)
{
unsigned int i=0;
for(i=0;i<=95000;i++);
}

Dept. of Computer Science & Engineering Page 8


Microcontroller and Embedded Systems Lab 18CSL48

15. Demonstrate the use of an external interrupt to toggle an LED On/Off.


Aim: Write a program to demonstrate the use of an external interrupt to toggle an LED On/Off.

Program:
// Description: Using PORT-1 all port pins of are making HIGH with certain
// delay and all port pin made LOW.
#include<lpc214x.h> // Header File for LPC2148

void delay(void); // function declaration (delay function declaring)(no


//return type and no argument passing)

int main()
{
PINSEL2 = 0x00000000; //P1.16 to P1.31 configured as GPIO using PINSEL1 reg.

IODIR1 = 0xFFFF0000; // P1.16 to P1.31 as OUTPUT Port

while(1)
{
IOSET1 = 0xFFFF0000; // P1.16 to P1.31 set to high
delay(); // function call (delay function calling)
IOCLR1 = 0xFFFF0000; // P1.16 to P1.31 set to low
delay(); // function call (delay function calling)
}
}

//************* END of MAIN PROGRAM *****************************//

//****************** Delay Function Starts **********//

void delay(void) // Function Definition


{
unsigned int i,j; // Variable declaration

for(i=0;i<1000;i++)
for(j=0;j<1000;j++);
}

//****************** END of Delay Function **********//

Dept. of Computer Science & Engineering Page 9


Microcontroller and Embedded Systems Lab 18CSL48

16. Display the Hex digits 0 to F on a 7-segment LED interface, with an appropriate delay
intermediately

Aim: Write a program to Display the Hex digits 0 to F on a 7-segment LED interface, with an appropriate
delay intermediately. a = P0.10
a
b = P0.11
----
c = P0.12
f| g |b
d = P0.13
|----|
e = P0.18
e| |c
f = P0.19
---- . dot
g = P0.20
d
dot = P0.21
Program (/*Use ALS Kit only*/)
#include <LPC21xx.h>
unsigned int delay, count=0, Switchcount=0;
unsigned int Disp[16]={0x003F0000, 0x00060000, 0x005B0000, 0x004F0000,
0x00660000,0x006D0000, 0x007D0000, 0x00070000, 0x007F0000, 0x006F0000,
0x00770000,0x007C0000, 0x00390000, 0x005E0000, 0x00790000, 0x00710000 };
#define SELDISP1 0x10000000 //P0.28
#define SELDISP2 0x20000000 //P0.29
#define SELDISP3 0x40000000 //P0.30
#define SELDISP4 0x80000000 //P0.31
#define ALLDISP 0xF0000000 //Select all display
#define DATAPORT 0x00FF0000 //P0.16 to P0.23: Data lines to drive 7-Segments

int main (void)


{
PINSEL0 = 0x00000000;
PINSEL1 = 0x00000000;
IO0DIR = 0xF0FF0000;
IO1DIR = 0x01000000;

while(1)
{ //Display values on Seven Segment
IO0SET |= ALLDISP;
IO0CLR = 0x00FF0000;
for(delay=0;delay<100;delay++)
IO0SET = Disp[Switchcount]; // displays values 0 to F one after other
for(delay=0;delay<1000000;delay++)
{
}
Switchcount++;
if(Switchcount == 16) // after F go back to 0
{
Switchcount = 0;
}
}}

Dept. of Computer Science & Engineering Page 10

Common questions

Powered by AI

Controlling a DC motor involves simply switching the polarity of the motor terminals to achieve clockwise or anti-clockwise motion. The program sets `P0.11` and `P0.8` for controlling rotation and always keeping P0.8 high to maintain power to the motor . On the other hand, controlling a stepper motor requires sequentially energizing coils to move the rotor in small, precise increments. The stepper motor program uses port pins `P1.28` to `P1.31` to step the motor clockwise or anti-clockwise in sequence, which is performed through functions `stepper_clock()` and `stepper_Aclock()`, thus requiring more complex logic than a DC motor .

Function structuring plays a critical role in managing complexity in embedded systems programming by modularizing code into logical units. This practice promotes reusability, readability, and easier maintenance. In the provided examples, functions like `uart_init()`, `clock_wise()`, `anti_clock_wise()`, `stepper_clock()`, and `stepper_Aclock()` delineate specific tasks such as initializing the UART, controlling motor directions, and managing step sequences . By compartmentalizing tasks, developers can make isolated changes and quickly understand and troubleshoot specific functionalities without spanning the entire codebase. Functions encapsulate configuration details and operational sequence, helping to systematically address the embedded system's multitasking nature .

The `PINSEL` register configures pin functions within the microcontroller, allowing the switching between GPIO and peripheral functions like UART, timers, etc. In the UART program, `PINSEL0` is set to `0x0000005` to designate certain pins as TXD0 and RXD0 lines, enabling data transmission and reception for UART communication . The consistent application of `PINSEL` settings across various examples emphasizes its role in customizing I/O operations to fit specific functional needs of the application . In the context of DAC waveform generation, `PINSEL` and `IO0DIR` configurations ensure the correct functioning of GPIO pins, indicating its integral role in preparing the microcontroller for specific peripheral interactions .

Delays in motor control programs are crucial for ensuring that motors operate at a manageable speed, allowing for controlled movement rather than abrupt start-stop behavior. They provide time for the desired motor state transitions, reducing mechanical wear and synchronizing motor actions with other processes, such as the control logic in the main loop. In the DC motor program, delays are used after setting or clearing IO lines to ensure smooth transitions between clockwise and anti-clockwise motions . Similarly, for stepper motors, delays provide the necessary time for each directional shift, ensuring consistent and accurate stepping .

The triangular waveform program increments an unsigned integer from 0 to 254 and then decrements back to 0, shifting the result 16 bits left before writing to `IO0PIN` for DAC output . In contrast, the square waveform program involves toggling the `IO0PIN` between `0x00000000` and `0x00FF0000`, indicating a jump between low and high states without gradual change, reflecting the characteristic on-off nature of square waves. Both programs use a delay function to control speed and repetition rate of the waves .

The external interrupt mechanism to toggle an LED works by configuring the necessary port pins as outputs using `IODIR1 = 0xFFFF0000`, meaning `P1.16` to `P1.31` act as output ports. Within an infinite loop, `IOSET1` is used to set these port pins high, turning on the LEDs, followed by `IOCLR1` to set the pins low, turning off the LEDs. The process is repeated with delays in between to make the toggling discernible to the human eye .

Delays in 7-segment display refresh cycles are crucial for ensuring visual stability and readability. Properly chosen delays prevent flickering and ensure each digit is visible long enough for human perception. If delays are too short, the display might appear to flicker, leading to a poor user experience with possibly indistinguishable digits. Conversely, excessively long delays can slow down the update rate, making transitions sluggish and unresponsive, especially in applications needing fast dynamic updates. The delay structure in the program includes a for loop where `delay` is used to ensure necessary visibility time for each digit by iterating over `Disp[]` to provide stable output .

Interfacing a DAC to produce different waveforms involves configuring data lines through GPIO settings, writing appropriate digital values, and controlling timing to generate the desired analog output. In the triangular waveform program, the DAC output is linearly incremented then decremented, shifting values left by 16 bits to meet DAC data line requirements . For the square wave, pins are simply toggled between high and low states without complex transitions, reflecting its stepped nature. Both waveforms rely on precise delays to maintain structure and repetition integrity .

Displaying hexadecimal digits on a 7-segment LED interface can be challenging because each number requires a specific combination of segments to be illuminated. This complexity increases with hexadecimal numbers requiring more combinations up to 15. The program addresses these challenges by predefining an array `Disp[]` containing bit patterns that represent each hexadecimal digit from 0 to F in a format like `0x003F0000` for '0' . The program selects appropriate patterns based on an incrementing index, `Switchcount`, and applies it to `IO0SET` to light up the correct segments on the LED. Consistent, clear syntax and structured iteration help manage complexity effectively .

The UART initialization in the provided program for displaying the "Hello World" message involves setting the correct configurations for communication. The function `uart_init()` configures the transmission (TXD0) and reception (RXD0) lines of the UART by setting `PINSEL0` to `0x0000005`. It sets the word length and enables divisor latches for baud rate configuration via `U0LCR = 0x83`. The baud rate is specified as 9600 bps through `U0DLL = 0x13` and `U0DLM = 0x00`. Finally, the divisor is activated, and the format is set again by resetting `U0LCR` to `0x03` .

You might also like