diff --git a/ES/Lab/Lab11/PWM_Keyboard.c b/ES/Lab/Lab11/PWM_Keyboard.c index e1e2325..b0cb2e0 100644 --- a/ES/Lab/Lab11/PWM_Keyboard.c +++ b/ES/Lab/Lab11/PWM_Keyboard.c @@ -1,33 +1,22 @@ #include "LPC17xx.h" +#include unsigned long int duty_cycle = 0; -int direction = 1; -int step = 50; +unsigned long int new_duty = 0; +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) { 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->MR4 = new_duty; LPC_PWM1->LER = (1 << 4); LPC_PWM1->IR = (1 << 0); } @@ -35,23 +24,47 @@ void PWM1_IRQHandler(void) int main(void) { + char key; + LPC_PINCON->PINSEL3 |= (2 << 14); - LPC_PWM1->TCR = (1 << 1); - LPC_PWM1->CTCR = 0; + LPC_PWM1->CTCR = 0x00; 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); + 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); + } + } }