Name: Sudhanshu Kumar
College Regd. No.: 2201030100
Cranes Regd. No.:
CL202408310180209
WEEK 2 LAB WORK
DAY 1:
#include <LPC17xx.h>
#include <stdint.h>
#define RS (1 << 10) // RS pin is connected to P0.10
#define EN (1 << 11) // EN pin is connected to P0.11
#define VALUE_PINS (0xFF << 15) // Data pins are connected to P0.15 to P0.22
void lcd_cmd_write(char cmd);
void lcd_dat_write(char dat);
void delay_ms(uint32_t ms);
int main()
{
// Set pins P0.10, P0.11, and P0.15 to P0.22 as output
LPC_GPIO0->FIODIR |= RS;
LPC_GPIO0->FIODIR |= EN;
LPC_GPIO0->FIODIR |= VALUE_PINS;
// Initialize the LCD in 8-bit mode
lcd_cmd_write(0x38); // Function set: 8-bit, 2-line, 5x8 dots
lcd_cmd_write(0x0E); // Display ON, Cursor ON
lcd_cmd_write(0x01); // Clear display
// Write characters to display "AYAN"
lcd_dat_write('A'); // Display 'A'
lcd_dat_write('Y'); // Display 'Y'
lcd_dat_write('A'); // Display 'A'
lcd_dat_write('N'); // Display 'N'
delay_ms(5000); // Wait for 5 seconds
lcd_cmd_write(0x01); // Clear the screen
// Function to send commands to the LCD
void lcd_cmd_write(char cmd)
{
LPC_GPIO0->FIOCLR = VALUE_PINS; // Clear data pins
LPC_GPIO0->FIOSET = (cmd << 15); // Set data pins with command
LPC_GPIO0->FIOCLR = RS; // RS = 0 for command
LPC_GPIO0->FIOSET = EN; // Generate Enable pulse
delay_ms(10); // Delay for LCD to process command
LPC_GPIO0->FIOCLR = EN; // Clear Enable pin
}
// Function to send data to the LCD
void lcd_dat_write(char dat)
{
LPC_GPIO0->FIOCLR = VALUE_PINS; // Clear data pins
LPC_GPIO0->FIOSET = (dat << 15); // Set data pins with data
LPC_GPIO0->FIOSET = RS; // RS = 1 for data
LPC_GPIO0->FIOSET = EN; // Generate Enable pulse
delay_ms(10); // Delay for LCD to process data
LPC_GPIO0->FIOCLR = EN; // Clear Enable pin
}
// Function to generate a delay in milliseconds
void delay_ms(uint32_t ms)
{
uint32_t i, j;
for (i = 0; i < ms; i++)
{
for (j = 0; j < 3000; j++) // Approximate delay for 1 millisecond
{
__NOP(); // No operation (CPU idle)
}
}
}
DAY 2:
// Include necessary header files for microcontroller-specific definitions
#include<stdint.h>
#include<LPC17xx.h>
#include<stdio.h>
// Define macros for RS, EN, and Value Pins
#define RS (1<<10) // RS pin is connected to P0.10
#define EN (1<<11) // EN pin is connected to P0.11
#define VP (0xFF <<15) // Data pins are connected to P0.15 to P0.22
// Function prototypes
void lcd_cmd_write(char c); // Function to send commands to the LCD
void lcd_data_write(char d); // Function to send data to the LCD
void delay(uint32_t s); // Function to introduce a delay
void lcd_str_write(char *pstr); // Function to write a string to the LCD
#include"lcd.h" // Include additional LCD-specific header file
#include<stdint.h>
int main(){
int i=0; // Initialize counter variable
char st[6]; // Buffer to store the string to be displayed
// Configure pins P0.10, P0.11, and P0.15 to P0.22 as output
LPC_GPIO0->FIODIR|=RS;
LPC_GPIO0->FIODIR|=VP;
LPC_GPIO0->FIODIR|=EN;
// Initialize the LCD
lcd_cmd_write(0x38); // Function set: 8-bit, 2-line, 5x8 dots
lcd_cmd_write(0x0E); // Display ON, Cursor ON
lcd_cmd_write(0x01); // Clear screen
// Infinite loop to display incrementing numbers
while(1){
sprintf(st,"%02d",i); // Convert integer to string
lcd_str_write(st); // Display the string on the LCD
delay(100); // Delay for readability
i+=1; // Increment counter
if(i==100){i=0;}; // Reset counter after 99
lcd_cmd_write(0x87); // Move cursor back to position
}
delay(7000); // Wait before clearing the screen
lcd_cmd_write(0x01); // Clear the screen
}
#include"lcd.h" // Include additional LCD-specific header file
// Function to send commands to the LCD
void lcd_cmd_write(char c){
LPC_GPIO0->FIOCLR=VP; // Clear data pins
LPC_GPIO0->FIOSET=c<<15; // Set data pins with command
LPC_GPIO0->FIOCLR=RS; // RS = 0 for command
LPC_GPIO0->FIOSET=EN; // Generate Enable pulse
delay(10); // Delay for LCD to process command
LPC_GPIO0->FIOCLR=EN; // Clear Enable pin
}
// Function to send data to the LCD
void lcd_data_write(char d){
LPC_GPIO0->FIOCLR=VP; // Clear data pins
LPC_GPIO0->FIOSET=d<<15; // Set data pins with data
LPC_GPIO0->FIOSET=RS; // RS = 1 for data
LPC_GPIO0->FIOSET=EN; // Generate Enable pulse
delay(10); // Delay for LCD to process data
LPC_GPIO0->FIOCLR=EN; // Clear Enable pin
}
// Function to write a string to the LCD
void lcd_str_write(char *pstr){
while(*pstr!='\0'){ // Loop through each character in the string
lcd_data_write(*pstr); // Write character to LCD
pstr++; // Move to the next character
}
}
// Function to generate a delay
void delay(uint32_t s){
uint32_t i,j;
for(i=0;i<s;i++){
for(j=0;j<1200;j++){} // Nested loop for delay
}
}
// Include necessary header files for microcontroller-specific definitions
#include<stdint.h>
#include<LPC17xx.h>
#include<stdio.h>
// Define macros for RS, EN, and Value Pins
#define RS (1<<10) // RS pin is connected to P0.10
#define EN (1<<11) // EN pin is connected to P0.11
#define VP (0xFF <<15) // Data pins are connected to P0.15 to P0.22
// Function prototypes
void lcd_cmd_write(char c); // Function to send commands to the LCD
void lcd_data_write(char d); // Function to send data to the LCD
void delay(uint32_t s); // Function to introduce a delay
void lcd_str_write(char *pstr); // Function to write a string to the LCD
#include"lcd.h" // Include additional LCD-specific header file
// Function to send commands to the LCD
void lcd_cmd_write(char c){
LPC_GPIO0->FIOCLR=VP; // Clear data pins
LPC_GPIO0->FIOSET=c<<15; // Set data pins with command
LPC_GPIO0->FIOCLR=RS; // RS = 0 for command
LPC_GPIO0->FIOSET=EN; // Generate Enable pulse
delay(10); // Delay for LCD to process command
LPC_GPIO0->FIOCLR=EN; // Clear Enable pin
}
// Function to send data to the LCD
void lcd_data_write(char d){
LPC_GPIO0->FIOCLR=VP; // Clear data pins
LPC_GPIO0->FIOSET=d<<15; // Set data pins with data
LPC_GPIO0->FIOSET=RS; // RS = 1 for data
LPC_GPIO0->FIOSET=EN; // Generate Enable pulse
delay(10); // Delay for LCD to process data
LPC_GPIO0->FIOCLR=EN; // Clear Enable pin
}
// Function to write a string to the LCD
void lcd_str_write(char *pstr){
while(*pstr!='\0'){ // Loop through each character in the string
lcd_data_write(*pstr); // Write character to LCD
pstr++; // Move to the next character
}
}
// Function to generate a delay
void delay(uint32_t s){
uint32_t i,j;
for(i=0;i<s;i++){
for(j=0;j<1200;j++){} // Nested loop for delay
}
}
#include"lcd.h" // Include additional LCD-specific header file
#include<stdint.h>
int main(){
int h=12,m=59,s=45; // Initialize clock variables
char stime[6]; // Buffer to store the time string
// Configure pins P0.10, P0.11, and P0.15 to P0.22 as output
LPC_GPIO0->FIODIR|=RS;
LPC_GPIO0->FIODIR|=VP;
LPC_GPIO0->FIODIR|=EN;
// Initialize the LCD
lcd_cmd_write(0x38); // Function set: 8-bit, 2-line, 5x8 dots
lcd_cmd_write(0x0E); // Display ON, Cursor ON
lcd_cmd_write(0x01); // Clear screen
lcd_cmd_write(0x84); // Set cursor position
// Infinite loop to display the clock
while(1){
sprintf(stime,"%02d:%02d:%02d",h,m,s); // Format time as HH:MM:SS
lcd_cmd_write(0x01); // Clear screen
lcd_str_write(stime); // Display time on the LCD
delay(500); // Delay for half a second
s+=1; // Increment seconds
if(s==60){ s=0; m+=1; } // Increment minutes after 59 seconds
if(m==60){ m=0; h+=1; } // Increment hours after 59 minutes
if(h==24){ h=0; } // Reset hours after 23
lcd_cmd_write(0x87); // Set cursor position for next iteration
}
delay(7000); // Wait before clearing the screen
lcd_cmd_write(0x01); // Clear the screen
}
#include<stdint.h>
#include<LPC17xx.h>
#include<string.h>
#include"fun.h"
#define ROW (0x0F << 4) //port2.7 to 2.4
#define COL (0x0F)//port2.3 to port2.0
int main(){
uint32_t code[] = {0x0E, 0x0D, 0x0B, 0x07};
char ktab[4][4] = {
{'0','1','2','3'},
{'4','5','6','7'},
{'8','9','A','B'},
{'C','D','E','F'}
};
char correct_password[] = "4180";
char entered_password[5] = {0};
int password_index = 0;
uint32_t i, j, col_val;
LPC_GPIO2->FIODIR |= ROW; // make row pins as the output
LPC_GPIO2->FIODIR &= ~COL; // make col pins as input pins
LPC_GPIO0->FIODIR |= RS;
LPC_GPIO0->FIODIR |= VP;
LPC_GPIO0->FIODIR |= EN;
lcd_cmd_write(0x38); // 8 bit mode 2 line
lcd_cmd_write(0x0E); // cursor on display on
lcd_cmd_write(0x01); // clear screen
lcd_cmd_write(0x0C);
lcd_str_write("Enter Password:");
lcd_cmd_write(0xC0);
while(1){
for(i = 0; i < 4; i++){
LPC_GPIO2->FIOCLR = ROW;
LPC_GPIO2->FIOSET = code[i] << 4;
col_val = LPC_GPIO2->FIOPIN & 0x0F;
for(j = 0; j < 4; j++){
if(col_val == code[j]){
break;
}
}
if(col_val != 0x0F) {
char pressed_key = ktab[i][j];
lcd_data_write(pressed_key);
if(password_index < 4){
entered_password[password_index++] = pressed_key;
}
if(password_index == 4){
entered_password[4] = '\0';
lcd_cmd_write(0xC0);
if(strcmp(entered_password, correct_password) == 0){
lcd_str_write("Correct Password!");
while(1);
} else {
lcd_str_write("Wrong Password!");
delay(1000);
lcd_cmd_write(0x01); // Clear display
lcd_str_write("Enter Password:");
lcd_cmd_write(0xC0);
password_index = 0;
entered_password[0] = '\0';
}
}
delay(200);
}
}
}
}
DAY 3:
#include <LPC17xx.h>
#include "lcd.h"
// Declare the lcd_dat_write function if not declared in lcd.h
void lcd_dat_write(char data);
#define ROW_PINS (0x0F << 4)
#define COL_PINS (0x0F) // p2.3-p2.0 column pins
int main()
{
uint8_t code[4] = {0x0E, 0x0D, 0x0B, 0x07};
char key[4][4] = {{'0', '1', '2', '3'},
{'4', '5', '6', '7'},
{'8', '9', 'A', 'B'},
{'C', 'D', 'E', 'F'}};
uint8_t i, j, col_val;
LPC_GPIO2->FIODIR |= ROW_PINS; // make row pins as output pins
LPC_GPIO2->FIODIR &= ~COL_PINS; // make column pins as input pins
lcd_init();
lcd_cmd_write(0x0C);
lcd_str_write("Key Pressed : ");
while (1)
{
for (i = 0; i < 4; i++)
{
LPC_GPIO2->FIOCLR = ROW_PINS;
LPC_GPIO2->FIOSET = code[i] << 4;
col_val = LPC_GPIO2->FIOPIN & 0x0F;
for (j = 0; j < 4; j++)
{
if (col_val == code[j])
{
break;
}
}
if (col_val != 0x0F)
{
lcd_dat_write(key[i][j]);
lcd_cmd_write(0x10);
}
}
}
}
DAY 4:
#include<stdint.h>
#include<LPC17xx.h>
#include"fun.h"
#define CH_SEL (1<<2)
#define CL_DIV (0xFF << 8)
#define B_MOD (0x01 << 16)
#define P_UP (0x01 << 21)
#define SC (0x01 << 24)
#define DN (0x01U << 31)
#define tc 100.0f // Conversion factor for LM35-like sensor
int main(){
uint32_t Su_val;
float volt, temp;
char Sval[16];
LPC_SC->PCONP |=(1<<12); //power on of adc
LPC_PINCON->PINSEL1 |=(1<<18); //p0 25 as ADC ch-2 input pin
LPC_PINCON->PINSEL1 &=~(1<<19); //p0 25 as ADC ch-2
LPC_ADC->ADCR|=CH_SEL; //select adc channel 2
LPC_ADC->ADCR&=~CL_DIV; // ADC = PCLK/(0+1)
LPC_ADC->ADCR&=~B_MOD; // Select ADC Software
LPC_ADC->ADCR|=P_UP; // make ADC Operation
LPC_GPIO0->FIODIR|=RS;
LPC_GPIO0->FIODIR|=VP;
LPC_GPIO0->FIODIR|=EN;
lcd_cmd_write(0x38); //8 bit mode 2 line
lcd_cmd_write(0x0E);//cursor on display on
lcd_cmd_write(0x01);//clear screen
lcd_cmd_write(0x80);
while(1){
LPC_ADC->ADCR|=SC;//start conversion
while((LPC_ADC->ADDR2 & DN)==0){
//waiting for completion of conversion
}
Su_val = (LPC_ADC->ADDR2>>4) & 0XFFF;
volt = (Su_val) * (3.3/4096.0); // Convert to voltage
temp = volt * 100.0;
sprintf(Sval,"ADC Op: %.1f C", temp);
lcd_str_write(Sval);
delay(2000);
lcd_cmd_write(0x01);
lcd_cmd_write(0x80);
}
}
DAY 5:
//Toggle LED at every 2 sec
//#include"fun.h"
#include<LPC17xx.h>
#define ALL_LED (0xFF<<19)
int main(){
LPC_GPIO1->FIODIR|=ALL_LED;
LPC_GPIO1->FIOCLR=ALL_LED;
LPC_SC->PCONP |=(1 << 1);
LPC_TIM0->PR=0; //delay = sount /pclk= (pr+1)*20000000
LPC_TIM0->MR0=2000000;
LPC_TIM0->MCR=(1 << 1)|(1 << 0);
LPC_TIM0->TCR= (1 << 1); //load pc=0
LPC_TIM0->TCR= (1 << 0); //start timer
while(1){
LPC_GPIO1->FIOSET=ALL_LED;
while((LPC_TIM0->IR & (1 << 0))==0) { }
LPC_TIM0->IR |=(1 << 0);
LPC_GPIO1->FIOCLR=ALL_LED;
while((LPC_TIM0->IR & (1 << 0))==0) { }
LPC_TIM0->IR |=(1 << 0);
}
}
Day06
#include"fun.h"
int main(){
char stime[16];
char sdate[16];
LPC_GPIO0->FIODIR|=RS;
LPC_GPIO0->FIODIR|=VP;
LPC_GPIO0->FIODIR|=EN;
lcd_cmd_write(0x38); //8 bit mode 2 line
lcd_cmd_write(0x0E);//cursor on display on
lcd_cmd_write(0x01);//clear screen
lcd_cmd_write(0x84);
LPC_SC->PCONP |=(1<<9);
LPC_RTC->CCR=0x00; //Disable time counters
LPC_RTC->HOUR=11;
LPC_RTC->MIN=5;
LPC_RTC->SEC=0;
LPC_RTC->DOM=14;
LPC_RTC->MONTH=12;
LPC_RTC->YEAR=2024;
LPC_RTC->CCR=0x01;
while(1){
sprintf(stime,"TIME:%02d:%02d:%02d",LPC_RTC->HOUR,LPC_RTC->SEC,LPC_RTC->SEC);
lcd_str_write(stime);
lcd_cmd_write(0xC0);
sprintf(sdate,"DATE:%02d-%02d-%02d",LPC_RTC->DOM,LPC_RTC->MONTH,LPC_RTC-
>YEAR);
lcd_str_write(sdate);
lcd_cmd_write(0x80);
#include"fun.h"
#define Buzz (1 << 27)
int main(){
char stime[16];
char sdate[16];
LPC_GPIO1->FIODIR|=Buzz;
LPC_GPIO0->FIODIR|=RS;
LPC_GPIO0->FIODIR|=VP;
LPC_GPIO0->FIODIR|=EN;
lcd_cmd_write(0x38); //8 bit mode 2 line
lcd_cmd_write(0x0E);//cursor on display on
lcd_cmd_write(0x01);//clear screen
// lcd_cmd_write(0x84);
LPC_SC->PCONP |=(1<<9);
LPC_RTC->CCR=0x00; //Disable time counters
LPC_RTC->HOUR=12;
LPC_RTC->MIN=31;
LPC_RTC->SEC=0;
LPC_RTC->DOM=14;
LPC_RTC->MONTH=12;
LPC_RTC->YEAR=2024;
LPC_RTC->CCR=0x01;
while(1){
sprintf(stime,"TIME:%02d:%02d:%02d",LPC_RTC->HOUR,LPC_RTC->MIN,LPC_RTC->SEC);
lcd_str_write(stime);
lcd_cmd_write(0xC0);
sprintf(sdate,"DATE:%02d-%02d-%02d",LPC_RTC->DOM,LPC_RTC->MONTH,LPC_RTC-
>YEAR);
lcd_str_write(sdate);
lcd_cmd_write(0x80);
if((LPC_RTC->ILR & (1<<1))!=0) {
LPC_GPIO1->FIOSET=Buzz;
delay(10000);
LPC_GPIO1->FIOCLR=Buzz;
}else{
LPC_GPIO1->FIOCLR=Buzz;
#include "lcd.h"
#define Buzz (1 << 27)
int main() {
char stime[16];
char sdate[16];
// GPIO and LCD Initialization
LPC_GPIO1->FIODIR |= Buzz;
LPC_GPIO0->FIODIR |= RS;
LPC_GPIO0->FIODIR |= VP;
LPC_GPIO0->FIODIR |= EN;
lcd_cmd_write(0x38); // 8 bit mode 2 line
lcd_cmd_write(0x0E); // cursor on display on
lcd_cmd_write(0x01);
// RTC Configuration
LPC_SC->PCONP |= (1 << 9);
LPC_RTC->CCR = 0x00; // Disable time counters
// Set initial time to New Year's Eve
LPC_RTC->HOUR = 23;
LPC_RTC->MIN = 59;
LPC_RTC->SEC = 50;
LPC_RTC->DOM = 31;
LPC_RTC->MONTH = 12;
LPC_RTC->YEAR = 2024;
// New Year Alarm Configuration
LPC_RTC->ALHOUR = 0;
LPC_RTC->ALMIN = 0;
LPC_RTC->ALDOM = 1;
LPC_RTC->ALMON = 1;
LPC_RTC->ALYEAR = 2025;
LPC_RTC->AMR = 0x01;
// Enable RTC
LPC_RTC->CCR = 0x01;
while(1) {
// Format and display time
sprintf(stime, "TIME:%02d:%02d:%02d",
LPC_RTC->HOUR, LPC_RTC->MIN, LPC_RTC->SEC);
lcd_str_write(stime);
lcd_cmd_write(0xC0);
// Format and display date
sprintf(sdate, "DATE:%02d-%02d-%02d",
LPC_RTC->DOM, LPC_RTC->MONTH, LPC_RTC->YEAR);
lcd_str_write(sdate);
lcd_cmd_write(0x80);
// New Year Detection
if (LPC_RTC->YEAR == 2025 &&
LPC_RTC->MONTH == 1 &&
LPC_RTC->DOM == 1 &&
LPC_RTC->HOUR == 0 &&
LPC_RTC->MIN == 0) {
// New Year Celebration
LPC_GPIO1->FIOSET = Buzz; // Buzzer on
lcd_cmd_write(0x01); // Clear LCD
lcd_str_write("Happy New Year!");
delay(10000); // Celebration delay
LPC_GPIO1->FIOCLR = Buzz; // Buzzer off
} else {
LPC_GPIO1->FIOCLR = Buzz; // Ensure buzzer is off
// return 0;