MIT-Curricular/ES/Lab/Lab11/PWM_Keyboard.c
2025-10-30 08:57:21 +05:30

109 lines
3.1 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 &= ~(0x03 << 0);
LPC_PINCON->PINSEL0 &= ~(0x03 << 2);
LPC_PINCON->PINSEL0 &= ~(0x03 << 4);
LPC_PINCON->PINSEL0 &= ~(0x03 << 6);
LPC_GPIO0->FIODIR &= ~(0x0F << 0);
}
void Delay(unsigned long count){
unsigned long i;
for(i = 0; i < count; i++);
}
unsigned char Read_Keyboard(void){
unsigned char key = 0xFF;
if(!(LPC_GPIO0->FIOPIN & (1 << 0))){
Delay(500000);
if(!(LPC_GPIO0->FIOPIN & (1 << 0))){
key = 0;
while(!(LPC_GPIO0->FIOPIN & (1 << 0)));
Delay(500000);
}
}
else if(!(LPC_GPIO0->FIOPIN & (1 << 1))){
Delay(500000);
if(!(LPC_GPIO0->FIOPIN & (1 << 1))){
key = 1;
while(!(LPC_GPIO0->FIOPIN & (1 << 1)));
Delay(500000);
}
}
else if(!(LPC_GPIO0->FIOPIN & (1 << 2))){
Delay(500000);
if(!(LPC_GPIO0->FIOPIN & (1 << 2))){
key = 2;
while(!(LPC_GPIO0->FIOPIN & (1 << 2)));
Delay(500000);
}
}
else if(!(LPC_GPIO0->FIOPIN & (1 << 3))){
Delay(500000);
if(!(LPC_GPIO0->FIOPIN & (1 << 3))){
key = 3;
while(!(LPC_GPIO0->FIOPIN & (1 << 3)));
Delay(500000);
}
}
return key;
}
int main(void){
unsigned char key;
PWM_Init();
Keyboard_Init();
while(1){
key = Read_Keyboard();
switch(key){
case 0:
pwm_value = 100;
LPC_PWM1->MR2 = pwm_value;
break;
case 1:
pwm_value = 250;
LPC_PWM1->MR2 = pwm_value;
break;
case 2:
pwm_value = 500;
LPC_PWM1->MR2 = pwm_value;
break;
case 3:
pwm_value = 750;
LPC_PWM1->MR2 = pwm_value;
break;
default:
break;
}
LPC_PWM1->LER |= 0x04;
}
return 0;
}
void PWM1_IRQHandler(void){
LPC_PWM1->IR |= 0x01;
}