This commit is contained in:
sherlock 2025-10-30 09:22:28 +05:30
parent a2d3f71108
commit 5c427867cd
2 changed files with 67 additions and 95 deletions

View file

@ -3,107 +3,59 @@
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);
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);
LPC_PINCON->PINSEL0 &= ~0x0FFF;
LPC_GPIO0->FIODIR &= ~0x0F;
}
void Delay(unsigned long count){
unsigned long i;
for(i = 0; i < count; i++);
for(unsigned long i = 0; i < count; i++);
}
unsigned char Read_Keyboard(void){
unsigned char key = 0xFF;
if(!(LPC_GPIO0->FIOPIN & (1 << 0))){
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);
if(!(LPC_GPIO0->FIOPIN & (1 << 0))){
key = 0;
while(!(LPC_GPIO0->FIOPIN & (1 << 0)));
Delay(500000);
}
return i;
}
}
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;
}
return 0xFF;
}
int main(void){
unsigned char key;
unsigned char key;
const unsigned long pwm_values[] = {100, 250, 500, 750};
PWM_Init();
Keyboard_Init();
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;
while(1){
key = Read_Keyboard();
if(key < 4){
LPC_PWM1->MR2 = pwm_values[key];
LPC_PWM1->LER |= 0x04;
}
}
return 0;
return 0;
}
void PWM1_IRQHandler(void){
LPC_PWM1->IR |= 0x01;
LPC_PWM1->IR |= 0x01;
}