MIT-Curricular/ES/Lab/Lab11/PWM_Keyboard.c
2025-10-30 09:22:28 +05:30

61 lines
1.2 KiB
C

#include <LPC17xx.h>
unsigned long pwm_value = 0;
void PWM_Init(void){
LPC_PINCON->PINSEL3 |= 0x02 << 8;
LPC_SC->PCONP |= 1 << 6;
LPC_PWM1->PR = 0;
LPC_PWM1->CTCR = 0;
LPC_PWM1->MR0 = 1000;
LPC_PWM1->MR2 = 500;
LPC_PWM1->LER = 0x05;
LPC_PWM1->PCR = 1 << 10;
LPC_PWM1->TCR = 0x09;
NVIC_EnableIRQ(PWM1_IRQn);
}
void Keyboard_Init(void){
LPC_PINCON->PINSEL0 &= ~0x0FFF;
LPC_GPIO0->FIODIR &= ~0x0F;
}
void Delay(unsigned long count){
for(unsigned long i = 0; i < count; i++);
}
unsigned char Read_Keyboard(void){
for(unsigned char i = 0; i < 4; i++){
if(!(LPC_GPIO0->FIOPIN & (1 << i))){
Delay(500000);
if(!(LPC_GPIO0->FIOPIN & (1 << i))){
while(!(LPC_GPIO0->FIOPIN & (1 << i)));
Delay(500000);
return i;
}
}
}
return 0xFF;
}
int main(void){
unsigned char key;
const unsigned long pwm_values[] = {100, 250, 500, 750};
PWM_Init();
Keyboard_Init();
while(1){
key = Read_Keyboard();
if(key < 4){
LPC_PWM1->MR2 = pwm_values[key];
LPC_PWM1->LER |= 0x04;
}
}
return 0;
}
void PWM1_IRQHandler(void){
LPC_PWM1->IR |= 0x01;
}