0% found this document useful (0 votes)
3 views26 pages

C Program Questions

Uploaded by

MADHUSHREE RONAD
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views26 pages

C Program Questions

Uploaded by

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

Write an embedded C program to toggle alternate LEDS.

/*-------------------------------------------------
Embedded C Program for LPC2148
Toggle Alternate LEDs

LEDs connected to Port0 pins P0.0 to P0.7


Pattern1 = LEDs at even positions ON
Pattern2 = LEDs at odd positions ON
--------------------------------------------------*/

#include <lpc214x.h>

void delay(void);

int main()
{
PINSEL0 = 0x00000000; // Configure Port0 as GPIO
IODIR0 = 0x000000FF; // P0.0 to P0.7 as output

while(1)
{
IOCLR0 = 0x000000FF; // Clear all LEDs
IOSET0 = 0x55; // 01010101 -> Alternate LEDs ON
delay();

IOCLR0 = 0x000000FF;
IOSET0 = 0xAA; // 10101010 -> Other alternate LEDs ON
delay();
}
}

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

Write an embedded C program to interface a DC motor with LPC2148.

/*-------------------------------------------------
Embedded C Program to Interface DC Motor
with LPC2148 Microcontroller

Motor connected through driver (L293D / Relay)

P0.0 -> Motor Input 1


P0.1 -> Motor Input 2
Forward Rotation : IN1=1 IN2=0
Reverse Rotation : IN1=0 IN2=1
Stop : IN1=0 IN2=0
--------------------------------------------------*/

#include <lpc214x.h>

#define M1 (1<<0) // P0.0


#define M2 (1<<1) // P0.1

void delay(void);

int main(void)
{
PINSEL0 = 0x00000000; // Configure P0.0 and P0.1 as GPIO
IODIR0 |= (M1 | M2); // Set as output

while(1)
{
/* Motor Forward */
IOSET0 = M1;
IOCLR0 = M2;
delay();

/* Motor Stop */
IOCLR0 = (M1 | M2);
delay();

/* Motor Reverse */
IOCLR0 = M1;
IOSET0 = M2;
delay();

/* Motor Stop */
IOCLR0 = (M1 | M2);
delay();
}
}

void delay(void)
{
unsigned int i,j;
for(i=0; i<500; i++)
for(j=0; j<6000; j++);
}

Write an embedded C program to interface stepper motor with LPC2148.


include <lpc214x.h>
unsigned int step[4] = {0x01, 0x02, 0x04, 0x08};

void delay(void);

int main(void)
{
int i;

PINSEL0 = 0x00000000; // Configure Port0 as GPIO


IODIR0 = 0x0000000F; // P0.0 to P0.3 as output

while(1)
{
for(i = 0; i < 4; i++) // Clockwise rotation
{
IOCLR0 = 0x0F; // Clear previous step
IOSET0 = step[i]; // Apply next step
delay();
}
}
}

void delay(void)
{
unsigned int i, j;
for(i = 0; i < 300; i++)
for(j = 0; j < 5000; j++);
}

Write an embedded C program to display the sum of two numbers in UART.


/*-------------------------------------------------
Embedded C Program for LPC2148
Display Sum of Two Numbers using UART0

Example:
Number1 = 25
Number2 = 15
Sum = 40

Output on Serial Terminal:


SUM = 40
--------------------------------------------------*/

#include <lpc214x.h>
#include <stdio.h>

void UART0_Init(void);
void UART0_TxChar(char ch);
void UART0_TxString(char *str);
void delay(void);

int main(void)
{
char msg[20];
int a = 25, b = 15, sum;

sum = a + b;

UART0_Init();

sprintf(msg, "SUM = %d\r\n", sum);

while(1)
{
UART0_TxString(msg);
delay();
}
}

/* UART0 Initialization */
void UART0_Init(void)
{
PINSEL0 = 0x00000005; // P0.0=TXD0, P0.1=RXD0
U0LCR = 0x83; // 8-bit data, 1 stop bit, DLAB=1
U0DLL = 97; // 9600 baud rate for 15MHz PCLK
U0DLM = 0;
U0LCR = 0x03; // DLAB=0
}

/* Transmit one character */


void UART0_TxChar(char ch)
{
while(!(U0LSR & 0x20)); // Wait until THR empty
U0THR = ch;
}

/* Transmit string */
void UART0_TxString(char *str)
{
while(*str)
{
UART0_TxChar(*str++);
}
}

/* Delay */
void delay(void)
{
unsigned int i,j;
for(i=0;i<500;i++)
for(j=0;j<5000;j++);
}

Write an embedded C program to monitor the state of the switch. If switch is 1, turn on
LEDs else turn on DC motor.

#include <lpc214x.h>

#define SW (1<<0)

#define LEDS ((1<<1)|(1<<2)|(1<<3)|(1<<4))

#define M1 (1<<5)
#define M2 (1<<6)

int main(void)
{
PINSEL0 = 0x00000000; // Configure GPIO

IODIR0 |= (LEDS | M1 | M2); // LEDs and motor as output


IODIR0 &= ~SW; // Switch as input

while(1)
{
if(IOPIN0 & SW) // If switch = 1
{
IOSET0 = LEDS; // LEDs ON

IOCLR0 = M1; // Motor OFF


IOCLR0 = M2;
}
else // If switch = 0
{
IOCLR0 = LEDS; // LEDs OFF

IOSET0 = M1; // Motor ON


IOCLR0 = M2; // Forward direction
}
}
}

Write an embedded C program to display the sum of even numbers and odd numbers
using a 7 segment display.

#include <lpc214x.h>
unsigned char seg_code[10] =
{
0x3F, //0
0x06, //1
0x5B, //2
0x4F, //3
0x66, //4
0x6D, //5
0x7D, //6
0x07, //7
0x7F, //8
0x6F //9
};

#define TENS (1<<8)


#define UNITS (1<<9)

void delay(void);
void display_num(int num);

int main(void)
{
int even_sum = 20;
int odd_sum = 25;

PINSEL0 = 0x00000000;
IODIR0 = 0x000003FF; // P0.0 to P0.9 output

while(1)
{
display_num(even_sum); // Display 20
delay();

display_num(odd_sum); // Display 25
delay();
}
}

/* Display two digit number */


void display_num(int num)
{
int tens, units, i;

tens = num / 10;


units = num % 10;

for(i=0; i<500; i++) // Multiplexing


{
/* Tens digit */
IOCLR0 = 0x3FF;
IOSET0 = seg_code[tens];
IOSET0 = TENS;
delay();

/* Units digit */
IOCLR0 = 0x3FF;
IOSET0 = seg_code[units];
IOSET0 = UNITS;
delay();
}
}

void delay(void)
{
unsigned int i,j;
for(i=0;i<20;i++)
for(j=0;j<3000;j++);
}

Write an embedded C program to toggle LEDs. If all LEDs are ON, display “ALL ON”
on UART else “ALL OFF”.

#include <lpc214x.h>

#define LEDS 0x000000FF // P0.0 to P0.7 LEDs

void UART0_Init(void);
void UART0_TxChar(char ch);
void UART0_TxString(char *str);
void delay(void);

int main(void)
{
PINSEL0 = 0x00000005; // P0.0=TXD0, P0.1=RXD0
PINSEL1 = 0x00000000;

IODIR0 |= LEDS; // LEDs as output

UART0_Init();

while(1)
{
/* Turn all LEDs ON */
IOSET0 = LEDS;
UART0_TxString("ALL ON\r\n");
delay();

/* Turn all LEDs OFF */


IOCLR0 = LEDS;
UART0_TxString("ALL OFF\r\n");
delay();
}
}

/* UART0 Initialization */
void UART0_Init(void)
{
U0LCR = 0x83; // 8-bit, 1 stop bit, DLAB=1
U0DLL = 97; // 9600 baud rate for 15MHz PCLK
U0DLM = 0x00;
U0LCR = 0x03; // DLAB=0
}

/* Send one character */


void UART0_TxChar(char ch)
{
while(!(U0LSR & 0x20));
U0THR = ch;
}

/* Send string */
void UART0_TxString(char *str)
{
while(*str)
{
UART0_TxChar(*str++);
}
}

/* Delay */
void delay(void)
{
unsigned int i,j;
for(i=0;i<500;i++)
for(j=0;j<5000;j++);
}

Write an embedded C program to count the number of times the switch is pressed. Use
UART to display the count.

#include <lpc214x.h>
#include <stdio.h>

#define SW (1<<0)

void UART0_Init(void);
void UART0_TxChar(char ch);
void UART0_TxString(char *str);
void delay_ms(void);
int main(void)
{
unsigned int count = 0;
char msg[30];

PINSEL0 = 0x00000005; // P0.0 TXD0, P0.1 RXD0


PINSEL1 = 0x00000000;

IODIR0 &= ~SW; // Switch as input

UART0_Init();

UART0_TxString("Press Count:\r\n");

while(1)
{
if(IOPIN0 & SW) // If switch pressed
{
delay_ms(); // Debounce delay

if(IOPIN0 & SW) // Confirm press


{
count++;

sprintf(msg, "Count = %u\r\n", count);


UART0_TxString(msg);

while(IOPIN0 & SW); // Wait till switch released


delay_ms();
}
}
}
}

/* UART0 Initialization */
void UART0_Init(void)
{
U0LCR = 0x83; // 8-bit, 1 stop bit, DLAB=1
U0DLL = 97; // 9600 baud @15MHz PCLK
U0DLM = 0x00;
U0LCR = 0x03; // DLAB=0
}

/* Send one character */


void UART0_TxChar(char ch)
{
while(!(U0LSR & 0x20));
U0THR = ch;
}
/* Send string */
void UART0_TxString(char *str)
{
while(*str)
{
UART0_TxChar(*str++);
}
}

/* Simple delay for debounce */


void delay_ms(void)
{
unsigned int i,j;
for(i=0;i<100;i++)
for(j=0;j<3000;j++);
}

Write an embedded C program to count the number of times the switch is pressed. Use a
7 segment display to show the output (count).

#include <lpc214x.h>

#define SW (1<<0) // Switch at P0.0

/* 7-Segment codes for Common Cathode */


unsigned char seg_code[10] =
{
0x3F, //0
0x06, //1
0x5B, //2
0x4F, //3
0x66, //4
0x6D, //5
0x7D, //6
0x07, //7
0x7F, //8
0x6F //9
};

void delay(void);

int main(void)
{
unsigned int count = 0;

PINSEL0 = 0x00000000; // GPIO mode

IODIR0 &= ~SW; // Switch as input


IODIR0 |= 0x000000FE; // P0.1 to P0.7 as output
while(1)
{
/* Display count */
IOCLR0 = 0x000000FE;
IOSET0 = (seg_code[count] << 1);

/* Detect switch press */


if(IOPIN0 & SW)
{
delay(); // Debounce

if(IOPIN0 & SW)


{
count++;

if(count > 9)
count = 0;

while(IOPIN0 & SW); // Wait for release


delay();
}
}
}
}

/* Delay for debounce */


void delay(void)
{
unsigned int i, j;
for(i = 0; i < 100; i++)
for(j = 0; j < 3000; j++);
}

Write an embedded C program to display even numbers on a 7 segment display if the


switch is OFF. If the switch is ON, display the odd numbers.
#include <lpc214x.h>

#define SW (1<<0)

/* Common Cathode 7-Segment Codes */


unsigned char seg_code[10] =
{
0x3F, //0
0x06, //1
0x5B, //2
0x4F, //3
0x66, //4
0x6D, //5
0x7D, //6
0x07, //7
0x7F, //8
0x6F //9
};

void delay(void);

int main(void)
{
int i;

PINSEL0 = 0x00000000; // GPIO mode

IODIR0 &= ~SW; // Switch as input


IODIR0 |= 0x000000FE; // P0.1 to P0.7 as output

while(1)
{
if(IOPIN0 & SW) // Switch ON -> Odd numbers
{
for(i = 1; i <= 9; i += 2)
{
IOCLR0 = 0x000000FE;
IOSET0 = (seg_code[i] << 1);
delay();

if(!(IOPIN0 & SW))


break; // Change immediately if switch OFF
}
}
else // Switch OFF -> Even numbers
{
for(i = 0; i <= 8; i += 2)
{
IOCLR0 = 0x000000FE;
IOSET0 = (seg_code[i] << 1);
delay();

if(IOPIN0 & SW)


break; // Change immediately if switch ON
}
}
}
}

/* Delay Function */
void delay(void)
{
unsigned int i, j;
for(i = 0; i < 200; i++)
for(j = 0; j < 3000; j++);
}

Write an embedded C program to interface a DC motor with LPC2148 microcontroller.


Display the direction of the motor on UART.

/*-------------------------------------------------

Embedded C Program for LPC2148

Interface DC Motor with LPC2148

Display motor direction on UART

Connections:

P0.0 -> Motor Input 1

P0.1 -> Motor Input 2

UART0 -> Serial Terminal

Forward : IN1=1, IN2=0

Reverse : IN1=0, IN2=1

--------------------------------------------------*/

#include <lpc214x.h>

#define M1 (1<<0)

#define M2 (1<<1)

void UART0_Init(void);

void UART0_TxChar(char ch);

void UART0_TxString(char *str);


void delay(void);

int main(void)

PINSEL0 = 0x00000005; // P0.0=TXD0, P0.1=RXD0

PINSEL1 = 0x00000000;

IODIR0 |= (M1 | M2); // Motor pins as output

UART0_Init();

while(1)

/* Motor Forward */

IOSET0 = M1;

IOCLR0 = M2;

UART0_TxString("Motor Direction : FORWARD\r\n");

delay();

/* Stop */

IOCLR0 = (M1 | M2);

UART0_TxString("Motor Stopped\r\n");

delay();

/* Motor Reverse */

IOCLR0 = M1;

IOSET0 = M2;
UART0_TxString("Motor Direction : REVERSE\r\n");

delay();

/* Stop */

IOCLR0 = (M1 | M2);

UART0_TxString("Motor Stopped\r\n");

delay();

/* UART0 Initialization */

void UART0_Init(void)

U0LCR = 0x83; // 8-bit, 1 stop bit, DLAB=1

U0DLL = 97; // 9600 baud rate for 15 MHz PCLK

U0DLM = 0x00;

U0LCR = 0x03; // DLAB=0

/* Send one character */

void UART0_TxChar(char ch)

while(!(U0LSR & 0x20));

U0THR = ch;

/* Send string */
void UART0_TxString(char *str)

while(*str)

UART0_TxChar(*str++);

/* Delay Function */

void delay(void)

unsigned int i, j;

for(i = 0; i < 500; i++)

for(j = 0; j < 5000; j++);

Write an embedded C program to interface a DC motor with LPC2148 microcontroller.


Display the direction of the motor on UART.

/* DC Motor with LPC2148

Display Direction on UART

*/

#include <lpc214x.h>

#define M1 (1<<0)

#define M2 (1<<1)
void uart_init();

void uart_string(char *p);

void delay();

int main()

PINSEL0 = 0x00000005; // UART0 pins

IODIR0 = M1 | M2; // Motor pins output

uart_init();

while(1)

/* Forward */

IOSET0 = M1;

IOCLR0 = M2;

uart_string("FORWARD\r\n");

delay();

/* Reverse */

IOCLR0 = M1;

IOSET0 = M2;

uart_string("REVERSE\r\n");

delay();

}
void uart_init()

U0LCR = 0x83; // 8-bit, DLAB=1

U0DLL = 97; // 9600 baud

U0DLM = 0;

U0LCR = 0x03; // DLAB=0

void uart_string(char *p)

while(*p)

while(!(U0LSR & 0x20));

U0THR = *p++;

void delay()

int i,j;

for(i=0;i<300;i++)

for(j=0;j<5000;j++);

}
Write an embedded C program to interface a stepper motor with LPC2148
microcontroller. Display the direction of the motor on UART.

/* Stepper Motor with LPC2148

Display Direction on UART

*/

#include <lpc214x.h>

unsigned int step[4] = {0x01, 0x02, 0x04, 0x08};

void uart_init();

void uart_string(char *p);

void delay();

int main()

int i;

PINSEL0 = 0x00000005; // UART0 pins

IODIR0 = 0x0F; // P0.0 to P0.3 as output

uart_init();

while(1)

/* Clockwise Direction */

uart_string("CLOCKWISE\r\n");
for(i=0; i<4; i++)

IOCLR0 = 0x0F;

IOSET0 = step[i];

delay();

/* Anticlockwise Direction */

uart_string("ANTICLOCKWISE\r\n");

for(i=3; i>=0; i--)

IOCLR0 = 0x0F;

IOSET0 = step[i];

delay();

void uart_init()

U0LCR = 0x83; // 8-bit, DLAB=1

U0DLL = 97; // 9600 baud

U0DLM = 0;

U0LCR = 0x03; // DLAB=0

}
void uart_string(char *p)

while(*p)

while(!(U0LSR & 0x20));

U0THR = *p++;

void delay()

int i,j;

for(i=0;i<300;i++)

for(j=0;j<5000;j++);

Write an embedded C program to active a buzzer. If the buzzer is ON, display “ON” on
UART else “OFF”.

/* Buzzer with LPC2148


If buzzer ON -> Display ON on UART
Else -> Display OFF on UART
*/

#include <lpc214x.h>

#define BUZZER (1<<0) // Buzzer connected to P0.0

void uart_init();
void uart_string(char *p);
void delay();

int main()
{
PINSEL0 = 0x00000005; // UART0 pins
IODIR0 = BUZZER; // Buzzer pin output

uart_init();

while(1)
{
/* Buzzer ON */
IOSET0 = BUZZER;
uart_string("ON\r\n");
delay();

/* Buzzer OFF */
IOCLR0 = BUZZER;
uart_string("OFF\r\n");
delay();
}
}

void uart_init()
{
U0LCR = 0x83; // 8-bit, DLAB=1
U0DLL = 97; // 9600 baud rate
U0DLM = 0;
U0LCR = 0x03; // DLAB=0
}

void uart_string(char *p)


{
while(*p)
{
while(!(U0LSR & 0x20));
U0THR = *p++;
}
}

void delay()
{
int i, j;
for(i=0; i<300; i++)
for(j=0; j<5000; j++);
}

Write an embedded C program to interface a switch and DC motor with LPC2148


microcontroller. If the switch is ON, turn the motor in clockwise else anticlockwise.

/* Switch + DC Motor with LPC2148

If switch ON -> Clockwise


Else -> Anticlockwise

*/

#include <lpc214x.h>

#define SW (1<<0) // Switch at P0.0

#define M1 (1<<1) // Motor input1

#define M2 (1<<2) // Motor input2

int main()

PINSEL0 = 0x00000000; // GPIO mode

IODIR0 &= ~SW; // Switch as input

IODIR0 |= (M1 | M2); // Motor pins as output

while(1)

if(IOPIN0 & SW) // Switch ON

IOSET0 = M1; // Clockwise

IOCLR0 = M2;

else // Switch OFF

IOCLR0 = M1; // Anticlockwise

IOSET0 = M2;
}

Write an embedded C program to interface a switch and stepper motor with LPC2148
microcontroller. If the switch is ON, turn the motor in Clockwise else anticlockwise.

/* Switch + Stepper Motor with LPC2148

If switch ON -> Clockwise

Else -> Anticlockwise

*/

#include <lpc214x.h>

#define SW (1<<0) // Switch at P0.0

unsigned int step[4] = {0x01, 0x02, 0x04, 0x08};

/* P0.0 to P0.3 -> Stepper through driver */

void delay();

int main()

int i;

PINSEL0 = 0x00000000; // GPIO mode


IODIR0 &= ~SW; // Switch input

IODIR0 |= 0x0F; // P0.0 to P0.3 output

while(1)

if(IOPIN0 & SW) // Switch ON -> Clockwise

for(i=0; i<4; i++)

IOCLR0 = 0x0F;

IOSET0 = step[i];

delay();

else // Switch OFF -> Anticlockwise

for(i=3; i>=0; i--)

IOCLR0 = 0x0F;

IOSET0 = step[i];

delay();

void delay()
{

int i,j;

for(i=0; i<300; i++)

for(j=0; j<5000; j++);

You might also like