You are on page 1of 16

/* Display "Hello World"message using Internal UART */

#include <lpc17xx.h>
#include "uart.h"
int main()
{
uint8_t i;
char a[] = " \n\r Hello World ";
uart_init(9600);
while (1)
{

for(i=0; a[i]; i++)


uart_TxChar(a[i]);
}
}

2. Interface and Control a DC Motor.

* @Connections:
* SDA ------------------> P2.2
* SCL ------------------> P2.3
* 5V ------------------> P2.0
* switch is connected to P2.5 */
---------------------------------------------------------------------------------------------
#include <lpc17xx.h>
#include "dcmotor.h"
#include "pwm.h"
#include "blink.h"

#define Switch 5 /* switch is connected to P2.5 */


#define Pin_1 2
#define Pin_2 3
uint8_t dutyCycle = 50;

/* start the main program */


int main()
{
uint32_t switchStatus;

/* Initialize the DC Motor */


DC_Motor_Init(2,3);

/* Initialize PWM for Motor Speed Control */


PWM_Init();

/* Set PWM for Motor Speed Control, 100 = Max Speed, 1 = Min
Speed */
PWM_Set_Duty(dutyCycle);

while(1)
{
/* Turn On all the leds and wait for one second */
switchStatus = GPIO_Read(Switch); /* Read the switch status
*/

if(switchStatus == 1) /* If switch == 1, motor runs in


clockwise direction */
{
/* Rotate Dc Motor Clockwise */
Motor_Rotate_Clockwise(2, 3);
/* set delay */
delay(5000);
}
else /* If switch == 0, motor runs in anticlockwise
direction */
{
/* Rotate Dc Motor Anti-Clockwise */
Motor_Rotate_AntiClockwise(2, 3);
/* set delay */
delay(5000);
}
}
}

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


direction

* @Connections:
* SDA ------------------> P2.0
* SCL ------------------> P2.1
* TX ------------------> P2.2
* RX ------------------> P2.3
/* switch is connected to P2.5 */
---------------------------------------------------------------------------------------------
#include <lpc17xx.h>
#include "stepper.h"
#include "blink.h"
#define Switch 5 /* switch is connected to P2.5 */
uint16_t i;
int main()
{
uint32_t switchStatus;

/* Configure GPIO pins for steeper motor windings */


Stepper(0,1,2,3);
while(1)
{
/* Turn On all the leds and wait for one second */
switchStatus = GPIO_Read(Switch); /* Read the switch status
*/

if(switchStatus == 1) /* If switch == 1, motor runs in


clockwise direction */
{
/* for sometime run the motor in forward direction */
for(i = 1000; i > 0; i--)
Motor_Forward_Direction();
}
else
{
/* for sometime run the motor in forward direction */

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


Motor_Reverse_Direction();
}
}
}

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

* @brief Generates triangular wave on pin P0.26 based on switch status


* if switch is 1, square wave is generated
* If switch is 0, triangle wave is generated
* P2.5 -> switch on development board
---------------------------------------------------------------------------------------------
#include <lpc17xx.h>
#include "DAC.h"

#define Switch 5 /* switch is connected to P2.5 */


#define HIGH 1023 /* maximum value for 10 bit DAC is 2^10 =
1024 */
#define LOW 0

uint16_t value = 0;
int main(void)
{
uint32_t switchStatus;

/* Initialize the DAC Peripheral */


DAC_Init();

/* Initialize the input pin */


GPIO_Init_Input(Switch);

/* Binary value used for Digital To Analog Conversion */

while(1)
{

/* Turn On all the leds and wait for one second */


switchStatus = GPIO_Read(Switch); /* Read the switch status */
if(switchStatus == 1) /* If switch == 1, square wave is generated */

{
set_DAC(HIGH);
DELAY_ms(10);

set_DAC(LOW);
DELAY_ms(10);
}
else /* If switch == 0, triangular wave is generated */

{
for(value = 0; value < 1023; value++)
set_DAC(value);
for(value = 1022; value > 0; value--)
set_DAC(value);
}
}
}

Output

Generates triangular wave on pin P0.26 based on switch status


If switch is 1, square wave is generated
If switch is 0, triangle wave is generated

5. Using the Internal PWM module of ARM controller, generate PWM


and vary its duty cycle
* @brief Generate PWM on P2.0
* DutyCycle varies from 0-100% and back from 100%- 0%.*/

#include <lpc17xx.h>
#include "pwm.h"
/* variable to hold dutycycle value */
uint8_t dutyCycle;
int main(void)
{
/* Initialize the PWM Peripheral */.;/

PWM_Init();
/* Infinite loop */
while(1)
{
/* Increase dutycycle for from 0 to 100% */
for(dutyCycle = 0; dutyCycle < 100; dutyCycle++)
{
/* Set the PWM */
PWM_Set_Duty(dutyCycle);

/* set delay */
DELAY_ms(5);
}
for(dutyCycle = 100; dutyCycle > 0; dutyCycle--)
{
/* Set the PWM */
PWM_Set_Duty(dutyCycle);
/* set delay */
DELAY_ms(5);
}
}
}

6.Display the Hex Digits 0 to F a 7-segment LED with an appropriate


delay in between.
/* Connections:
*A (development board)->P2.2
*B (development board)->P2.3
*C (development board)->P2.4
*D (development board)->P2.5
*E(development board)->P2.6
*F (development board)->P2.7
*G (development board)->P2.8
DIG1/DIG2/DIG3/DIG4(devp board)->P2.9*\
---------------------------------------------------------------------------------------------

#include <lpc17xx.h>
#include "7segment.h"
#include "delay.h"
uint8_t ch[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
uint8_t i=0;
int main()
{
uint8_t data;
SEG_INIT(2,3,4,5,6,7,8,9);
while(1)
{
for(i=0;i<16;i++)
{
switch(ch[i])
{
case '0':data =0x40;break;
case '1':data =0x79;break;
case '2':data =0x24;break;
case '3':data =0x30;break;
case '4':data =0x19;break;
case '5':data =0x12;break;
case '6':data =0x02;break;
case '7':data =0x78;break;
case '8':data =0x00;break;
case '9':data =0x18;break;
case 'A':data =0x08;break;
case 'B':data =0x00;break;
case 'C':data =0x46;break;
case 'D':data =0x40;break;
case 'E':data =0x06;break;
case 'F':data =0x0E;break;
default:data =0x40;break;
}
Display(data);
DELAY_ms(50);
}
}
}

Output
The 7Segment Display displays the character initialized in the
code.
7.Demonstrate the use of an external interrupt to toggle an LED On/Off

/* Connections
* Connect P2.11 to switch(development board)
* Connect P2.3 to led(development board)
* When switch is pressed led = ~led*/
---------------------------------------------------------------------------------------------
#include <lpc17xx.h>
#include "ExtInt.h"

/* LED assigned to P2.3, pin number can be changed */


#define LED 3

/* Interrupt Handler */
void EINT1_IRQHandler(void)
{
/* Toggle the LED (P2_3) */
GPIO_PinToggle(LED);
}
int main()
{
/* Initialize the external interrupt 1 */
ExtInt_Init();

/* Initalize the GPIO pin as output */


GPIO_Init(LED);

/* Enable the EINT1 interrupts */


NVIC_EnableIRQ(EINT1_IRQn);

/* Infinite loop */
while(1)
{
// Do nothing
}
}

8. Interface a 4x4 keyboard and display the key code on an LCD.

/* Connections
D0 (LCD) -> P1.19
D1 (LCD) -> P1.20
D2 (LCD) -> P1.21
D3 (LCD) -> P1.22
D4 (LCD) -> P1.23
D5 (LCD) -> P1.24
D6 (LCD) -> P1.25
D7 (LCD) -> P1.26
RS (LCD) -> P1.27
EN (LCD) -> P1.28
RW(LCD)-> GND
C1 (Keypad on development board) -> P2.0
C2 (Keypad on development board) -> P2.1
C3 (Keypad on development board) -> P2.2
C4 (Keypad on development board) -> P2.3
R1 (Keypad on development board) -> P2.4
R2 (Keypad on development board) -> P2.5
R3 (Keypad on development board) -> P2.6
R4 (Keypad on development board) -> P2.7*/
---------------------------------------------------------------------------------------------
#include <lpc17xx.h>
#include "LCD.h"
#include "keypad.h"
char keypad(void);
char c;
char aData[] = "---SAHYADRI---";

/* start the main program */


int main()
{
/* Initialize the GPIO pins for LCD */
/* PORT1 D0, D7, RS, EN */
LCD_Init(19, 20, 21, 22, 23, 24, 25, 26, 27, 28);
delay(100);

/* Initialize the GPIO pins for Keypad */


/* PORT2 C1 R1 */
Keypad_Init(0, 1, 2, 3, 4, 5, 6, 7);
delay(100);

/* Set LCD parameters like contrast, backlight, cursor position etc


*/
CMD_WRT(0x38); /* 8 bit, 2 line, 5x7 dots */

CMD_WRT(0x0f); /* display on, cursor blinking */


CMD_WRT(0x06); /* Entry Mode */
CMD_WRT(0x01); /* Clear display Screen */

delay(100); /* required for lcd to process the command */

CMD_WRT(0X80); /* force cursor to beginning of 1st row */


LCD_WRT(aData);
CMD_WRT(0XC0); /* force cursor to beginning of 2nd row */

while(1)
{
/* get the key pressed */
c = keypad();

/* write the key to LCD */


DATA_WRT(c);

delay(100);
}
}
Output

When the Key is pressed, Corresponding key is displayed on the


LCD. For example, if key ‘1’ is pressed, on LCD it should show
“Key Pressed is 1”

9.Interface a simple Switch and display its status through Relay, Buzzer
and LED.

/* Connections */
/* Switch = P2.5
* led = P2.0
* buzzer = P2.1
* 5V= P2.2*/
---------------------------------------------------------------------------------------------
#include <lpc17xx.h>
#include "blink.h"
#define Switch 5
#define Led 0
#define buzzer 1
#define relay 2
/* start the main program */
int main()
{
uint32_t switchStatus;
/* Initialize the output pins for led, buzzer and relay */
GPIO_Init_Output(buzzer);
GPIO_Init_Output(Led);
GPIO_Init_Output(relay);

/* Initialize the input pin */


GPIO_Init_Input(Switch);

LED_OFF(Led);
LED_OFF(buzzer);
LED_OFF(relay);

while(1)
{
/* Turn On all the leds and wait for one second */
switchStatus = GPIO_Read(Switch); /* Read the switch status
*/

if(switchStatus == 1) /* Turn ON/OFF LEDs


depending on
switch status */
{
LED_ON(Led);
LED_ON(buzzer);
LED_ON(relay);
}
else
{
LED_OFF(Led);
LED_OFF(buzzer);
LED_OFF(relay);
}
}
}
Output
When the switch is pressed LED is ON, buzzer is ON and relay is
ON.
When the switch is not pressed LED is OFF, buzzer is OFF and
relay is OFF.

10.Measure ambient temperature using a sensor and SPI ADC IC

* SCK ----------------> P0.15


* CS ----------------> P0.19
* S0 ----------------> P0.17
* S1 ----------------> P0.18
* 5V ----------------> P1.14 */

#include <lpc17xx.h>
#include "spi_adc.h"
int main(void)
{
int adcValue;
SystemInit();

/* Initialize the UART for 9600 baud rate */


uart_init(9600);

/* Initialize MCP3008 */
SPI_ADC_Init();

/* Allow the device to get configured by giving delay */


delay_ms(1000);

while(1)
{
/* get the surrounding temperature using the lm35 temperature
sensor */
adcValue = get_temperature();

uart_TxChar('T');
uart_TxChar(':');

/* Convert hex value to ascii so that it can be displayed on


hyperterminal */
Display_in_ASCII(adcValue);

uart_TxChar('\r');
uart_TxChar('\n');
/* we do not want to read the temperature eve14ry milliseconds, so
delay */
delay_ms(1000);
}
}

You might also like