From 0290f584b3c18c1a1994ef4db60b216098d23771 Mon Sep 17 00:00:00 2001 From: sherlock Date: Thu, 23 Oct 2025 11:44:39 +0530 Subject: [PATCH] doc update ESD --- ES/Lab/Lab11/PWM.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 ES/Lab/Lab11/PWM.c diff --git a/ES/Lab/Lab11/PWM.c b/ES/Lab/Lab11/PWM.c new file mode 100644 index 0000000..92c643e --- /dev/null +++ b/ES/Lab/Lab11/PWM.c @@ -0,0 +1,58 @@ +#include "LPC17xx.h" + +unsigned long int duty_cycle = 0; +int direction = 1; +int step = 50; + +void PWM1_IRQHandler(void) +{ + if (LPC_PWM1->IR & (1 << 0)) + { + if (direction == 1) + { + 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->IR = (1 << 0); + } +} + +int main(void) +{ + LPC_PINCON->PINSEL3 |= (2 << 14); + + LPC_PWM1->TCR = (1 << 1); + LPC_PWM1->CTCR = 0; + LPC_PWM1->PR = 0; + + LPC_PWM1->MR0 = 3000; + LPC_PWM1->MR4 = duty_cycle; + + LPC_PWM1->MCR = (1 << 1) | (1 << 0); + LPC_PWM1->PCR = (1 << 12); + + LPC_PWM1->LER = (1 << 0) | (1 << 4); + + NVIC_EnableIRQ(PWM1_IRQn); + + LPC_PWM1->TCR = (1 << 0) | (1 << 3); + + while (1) + ; +}