This commit is contained in:
sherlock 2025-10-23 12:36:23 +05:30
parent d1cd6b537c
commit 161a7b1ee3

View file

@ -1,33 +1,22 @@
#include "LPC17xx.h" #include "LPC17xx.h"
#include <stdio.h>
unsigned long int duty_cycle = 0; unsigned long int duty_cycle = 0;
int direction = 1; unsigned long int new_duty = 0;
int step = 50; int press_count = 0;
void delayMs(unsigned int ms)
{
unsigned int i, j;
for (i = 0; i < ms; i++)
for (j = 0; j < 20000; j++);
}
void PWM1_IRQHandler(void) void PWM1_IRQHandler(void)
{ {
if (LPC_PWM1->IR & (1 << 0)) if (LPC_PWM1->IR & (1 << 0))
{ {
if (direction == 1) LPC_PWM1->MR4 = new_duty;
{
duty_cycle += step;
if (duty_cycle >= 3000)
{
duty_cycle = 3000;
direction = 0;
}
}
else
{
duty_cycle -= step;
if (duty_cycle <= 0)
{
duty_cycle = 0;
direction = 1;
}
}
LPC_PWM1->MR4 = duty_cycle;
LPC_PWM1->LER = (1 << 4); LPC_PWM1->LER = (1 << 4);
LPC_PWM1->IR = (1 << 0); LPC_PWM1->IR = (1 << 0);
} }
@ -35,23 +24,47 @@ void PWM1_IRQHandler(void)
int main(void) int main(void)
{ {
char key;
LPC_PINCON->PINSEL3 |= (2 << 14); LPC_PINCON->PINSEL3 |= (2 << 14);
LPC_PWM1->TCR = (1 << 1); LPC_PWM1->TCR = (1 << 1);
LPC_PWM1->CTCR = 0; LPC_PWM1->CTCR = 0x00;
LPC_PWM1->PR = 0; LPC_PWM1->PR = 0;
LPC_PWM1->MR0 = 3000; LPC_PWM1->MR0 = 3000;
LPC_PWM1->MR4 = duty_cycle; LPC_PWM1->MR4 = duty_cycle;
LPC_PWM1->MCR = (1 << 1) | (1 << 0); LPC_PWM1->MCR = (1 << 1) | (1 << 0);
LPC_PWM1->PCR = (1 << 12); LPC_PWM1->PCR = (1 << 12);
LPC_PWM1->LER = (1 << 0) | (1 << 4); LPC_PWM1->LER = (1 << 0) | (1 << 4);
NVIC_EnableIRQ(PWM1_IRQn); NVIC_EnableIRQ(PWM1_IRQn);
LPC_PWM1->TCR = (1 << 0) | (1 << 3); LPC_PWM1->TCR = (1 << 0) | (1 << 3);
while (1); printf("Press any key to change brightness...\n");
while (1)
{
key = getchar();
if (key)
{
press_count++;
if (press_count == 1)
new_duty = 300;
else if (press_count == 2)
new_duty = 1500;
else if (press_count == 3)
new_duty = 2700;
else
{
new_duty = 0;
press_count = 0;
}
printf("Duty cycle set to %lu\r\n", new_duty);
delayMs(500);
}
}
} }