0% found this document useful (0 votes)
17 views

Sample Code

Uploaded by

shravanibhor3084
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Sample Code

Uploaded by

shravanibhor3084
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

a countdown timer with 7-segment display and button control using an LPC1768 microcontroller:

`
#include "LPC17xx.h"
#include "lpc17xx_gpio.h"
#include "lpc17xx_adc.h"
#include "lpc17xx_timer.h"

// 7-segment display pin definitions


#define SEG_PIN_A 7
#define SEG_PIN_B 8
#define SEG_PIN_C 9
#define SEG_PIN_D 10
#define SEG_PIN_E 11
#define SEG_PIN_F 12
#define SEG_PIN_G 13
#define SEG_PIN_DOT 14
#define SEG_PIN_SELECT_1 15
#define SEG_PIN_SELECT_2 16

// Button pin definitions


#define BUTTON_INC_PIN 17
#define BUTTON_DEC_PIN 18

// Display buffer for each digit (0-9) on the 7-segment display


const uint8_t DISP_BUF[10] = { 0x3F, 0x06, 0x5B, 0x4F, 0x66,
0x6D, 0x7D, 0x07, 0x7F, 0x6F };

// Settings for the countdown timer (in seconds)


uint32_t timer_seconds = 0;
uint32_t timer_start = 0;
uint32_t timer_elapsed = 0;

// Initialize the 7-segment display pins as digital outputs


void init_7seg_pins() {
GPIO_SetDir(1, (1 << SEG_PIN_A) | (1 << SEG_PIN_B) | (1 << SEG_PIN_C) | (1 <<
SEG_PIN_D) |
(1 << SEG_PIN_E) | (1 << SEG_PIN_F) | (1 << SEG_PIN_G) | (1 <<
SEG_PIN_DOT) |
(1 << SEG_PIN_SELECT_1) | (1 << SEG_PIN_SELECT_2), 1);
GPIO_ClearValue(1, (1 << SEG_PIN_A) | (1 << SEG_PIN_B) | (1 << SEG_PIN_C) | (1
<< SEG_PIN_D) |
(1 << SEG_PIN_E) | (1 << SEG_PIN_F) | (1 << SEG_PIN_G) | (1 <<
SEG_PIN_DOT) |
(1 << SEG_PIN_SELECT_1) | (1 << SEG_PIN_SELECT_2));
}

// Display a decimal number (0-99) on the 7-segment display


void display_number(int number) {
uint8_t first_digit = number / 10;
uint8_t second_digit = number % 10;
GPIO_SetValue(1, (1 << SEG_PIN_SELECT_1));
GPIO_ClearValue(1, (1 << SEG_PIN_SELECT_2));
GPIO_ClearValue(1, (1 << SEG_PIN_DOT));
GPIO_SetValue(1, DISP_BUF[first_digit] << SEG_PIN_A);
GPIO_ClearValue(1, DISP_BUF[first_digit] ^ 0x7F << SEG_PIN_A);
GPIO_ClearValue(1, DISP_BUF[second_digit] << SEG_PIN_A);
GPIO_ClearValue(1, DISP_BUF[second_digit] ^ 0x7F << SEG_PIN_A);
delay_ms(2); // delay for stable display
GPIO_SetValue(1, (1 << SEG_PIN_SELECT_2));
GPIO_ClearValue(1, (1 << SEG_PIN_SELECT_1));
GPIO_ClearValue(1, (1 << SEG_PIN_DOT));
GPIO_SetValue(1, DISP_BUF[first_digit] << SEG_PIN_A);
GPIO_ClearValue(1, DISP_BUF[first_digit] ^ 0x7F << SEG_PIN_A);
GPIO_ClearValue(1, DISP_BUF[second_digit] << SEG_PIN_A);
GPIO_ClearValue(1, DISP_BUF[second_digit] ^ 0x7F << SEG_PIN_A);
delay_ms(2); // delay for stable display
}

// Button interrupt handler for increment button


void Button_Inc_Handler(void) {
if (timer_seconds < 5999) { // limit timer to 99 minutes and 59 seconds
timer_seconds++;
display_number(timer_seconds);
}
}

// Button interrupt handler for decrement button


void Button_Dec_Handler(void) {
if (timer_seconds > 0) {
timer_seconds--;
display_number(timer_seconds);
}
}

// Initialize the button pins as digital inputs with internal pull-up resistors and interrupts
void init_button_pins() {
GPIO_SetDir(1, (1 << BUTTON_INC_PIN) | (1 << BUTTON_DEC_PIN), 0); // set as
input
GPIO_SetValue(1, (1 << BUTTON_INC_PIN) | (1 << BUTTON_DEC_PIN)); // enable
pull-up resistors
GPIO_IntCmd(1, (1 << BUTTON_INC_PIN) | (1 << BUTTON_DEC_PIN), 0); // disable
interrupts
NVIC_ClearPendingIRQ(EINT3_IRQn);
NVIC_EnableIRQ(EINT3_IRQn);
NVIC_SetPriority(EINT3_IRQn, 2);
GPIO_IntCmd(1, (1 << BUTTON_INC_PIN) | (1 << BUTTON_DEC_PIN), 1); // enable
interrupts
}

// Initialize the countdown timer by setting the start time and elapsed time to 0
void init_timer() {
timer_start = 0;
timer_elapsed = 0;
}

// Start the countdown timer with the set time interval


void start_timer() {
timer_start = Timer_GetValue(LPC_TIM0);
timer_elapsed = 0;
}

// Update the countdown timer by computing the elapsed time and remaining time
void update_timer() {

timer_elapsed += (Timer_GetValue(LPC_TIM0) - timer_start);


timer_start = Timer_GetValue(LPC_TIM0);
if (timer_elapsed > (timer_seconds * 1000000)) { // 1 second = 1000000 microseconds
// Time's up!
} else {
uint32_t remaining_time = ((timer_seconds * 1000000) - timer_elapsed) / 1000000;
display_number(remaining_time);
}
}

int main(void) {
SystemInit();
SystemCoreClockUpdate();
init_7seg_pins();
init_button_pins();
init_timer();
LPC_TIM0->PR = 0xFFFF; // set prescaler to max value to generate 1 MHz clock
LPC_TIM0->MCR |= (1 << 0); // enable interrupt on MR0 match (1 second)
NVIC_ClearPendingIRQ(TIMER0_IRQn);
NVIC_EnableIRQ(TIMER0_IRQn);
NVIC_SetPriority(TIMER0_IRQn, 1);
__enable_irq();
while(1) {
// wait for timer interrupt
}
}

// Timer interrupt handler for updating the countdown timer


void TIMER0_IRQHandler(void) {
if (LPC_TIM0->IR & (1 << 0)) { // MR0 interrupt
update_timer();
LPC_TIM0->IR |= (1 << 0); // clear interrupt flag
}
}

// External interrupt handler for button interrupts


void EINT3_IRQHandler(void) {
if ((LPC_GPIOINT->IO2IntStatR & (1 << BUTTON_INC_PIN)) != 0) {
Button_Inc_Handler();
LPC_GPIOINT->IO2IntClr = (1 << BUTTON_INC_PIN);
}
if ((LPC_GPIOINT->IO2IntStatR & (1 << BUTTON_DEC_PIN)) != 0) {
Button_Dec_Handler();
LPC_GPIOINT->IO2IntClr = (1 << BUTTON_DEC_PIN);
}
}`

This code initializes the necessary pins for the 7-segment display and buttons, sets up interrupts
for the buttons and a timer interrupt for updating the countdown timer every second, and
implements functions for updating and displaying the remaining time on the display. The code
still needs modification, such as implementing the time's up action or adding additional features,
but it should provide a starting point for you.

You might also like