This commit is contained in:
sherlock 2025-10-30 08:57:21 +05:30
parent 7bc0f6da6a
commit a2d3f71108

View file

@ -1,125 +1,109 @@
#include "LPC17xx.h" #include <LPC17xx.h>
volatile unsigned long int duty_cycle = 300; unsigned long pwm_value = 0;
volatile unsigned long int pwm_counter = 0;
#define LED_MASK (0xFF << 4) void PWM_Init(void){
LPC_PINCON->PINSEL3 |= 0x02 << 8;
#define COL0 (1<<0) LPC_SC->PCONP |= 1 << 6;
#define COL1 (1<<1) LPC_PWM1->PR = 0;
#define COL2 (1<<23) LPC_PWM1->CTCR = 0;
#define COL3 (1<<24) LPC_PWM1->MR0 = 1000;
#define ROW0 (1<<25) LPC_PWM1->MR2 = 500;
LPC_PWM1->LER = 0x05;
#define COL_MASK_P0 (COL2 | COL3) LPC_PWM1->PCR = 1 << 10;
#define COL_MASK_P2 (COL0 | COL1) LPC_PWM1->TCR = 0x09;
NVIC_EnableIRQ(PWM1_IRQn);
static void delay_ms(unsigned int ms) {
unsigned int i, j;
for(i = 0; i < ms; i++)
for(j = 0; j < 10000; j++);
} }
static void set_column(unsigned int col) void Keyboard_Init(void){
{ LPC_PINCON->PINSEL0 &= ~(0x03 << 0);
LPC_GPIO2->FIOSET = COL_MASK_P2; LPC_PINCON->PINSEL0 &= ~(0x03 << 2);
LPC_GPIO0->FIOSET = COL_MASK_P0; LPC_PINCON->PINSEL0 &= ~(0x03 << 4);
LPC_PINCON->PINSEL0 &= ~(0x03 << 6);
switch(col) { LPC_GPIO0->FIODIR &= ~(0x0F << 0);
case 0: LPC_GPIO2->FIOCLR = COL0; break;
case 1: LPC_GPIO2->FIOCLR = COL1; break;
case 2: LPC_GPIO0->FIOCLR = COL2; break;
case 3: LPC_GPIO0->FIOCLR = COL3; break;
}
} }
static unsigned int is_row0_pressed(void) void Delay(unsigned long count){
{ unsigned long i;
return !(LPC_GPIO0->FIOPIN & ROW0); for(i = 0; i < count; i++);
} }
void PWM1_IRQHandler(void) unsigned char Read_Keyboard(void){
{ unsigned char key = 0xFF;
if (LPC_PWM1->IR & (1 << 0))
{
if(pwm_counter < duty_cycle)
LPC_GPIO0->FIOSET = LED_MASK;
else
LPC_GPIO0->FIOCLR = LED_MASK;
pwm_counter++; if(!(LPC_GPIO0->FIOPIN & (1 << 0))){
if(pwm_counter >= 3000) Delay(500000);
pwm_counter = 0; if(!(LPC_GPIO0->FIOPIN & (1 << 0))){
key = 0;
LPC_PWM1->IR = (1 << 0); while(!(LPC_GPIO0->FIOPIN & (1 << 0)));
} Delay(500000);
} }
}
int main(void) else if(!(LPC_GPIO0->FIOPIN & (1 << 1))){
{ Delay(500000);
unsigned int col_idx; if(!(LPC_GPIO0->FIOPIN & (1 << 1))){
unsigned int read_key; key = 1;
unsigned int last_key = 0xFF; while(!(LPC_GPIO0->FIOPIN & (1 << 1)));
Delay(500000);
LPC_PINCON->PINSEL0 &= ~(0xFFFF << 8); }
LPC_PINCON->PINSEL1 &= ~(0xFFFF << 14); }
LPC_PINCON->PINSEL4 &= ~(0xF << 0); else if(!(LPC_GPIO0->FIOPIN & (1 << 2))){
Delay(500000);
LPC_GPIO0->FIODIR |= LED_MASK; if(!(LPC_GPIO0->FIOPIN & (1 << 2))){
LPC_GPIO0->FIODIR |= COL_MASK_P0; key = 2;
LPC_GPIO2->FIODIR |= COL_MASK_P2; while(!(LPC_GPIO0->FIOPIN & (1 << 2)));
LPC_GPIO0->FIODIR &= ~ROW0; Delay(500000);
}
LPC_GPIO0->FIOCLR = LED_MASK; }
LPC_GPIO2->FIOSET = COL_MASK_P2; else if(!(LPC_GPIO0->FIOPIN & (1 << 3))){
LPC_GPIO0->FIOSET = COL_MASK_P0; Delay(500000);
if(!(LPC_GPIO0->FIOPIN & (1 << 3))){
LPC_PWM1->TCR = (1 << 1); key = 3;
LPC_PWM1->CTCR = 0; while(!(LPC_GPIO0->FIOPIN & (1 << 3)));
LPC_PWM1->PR = 0; Delay(500000);
LPC_PWM1->MR0 = 3000;
LPC_PWM1->MCR = (1 << 1) | (1 << 0);
LPC_PWM1->LER = (1 << 0);
NVIC_EnableIRQ(PWM1_IRQn);
NVIC_SetPriority(PWM1_IRQn, 0);
LPC_PWM1->TCR = (1 << 0) | (1 << 3);
while(1)
{
read_key = 0xFF;
for(col_idx = 0; col_idx < 4; col_idx++)
{
set_column(col_idx);
delay_ms(5);
if(is_row0_pressed()) {
read_key = col_idx;
while(is_row0_pressed()) {
delay_ms(10);
} }
break;
}
} }
LPC_GPIO2->FIOSET = COL_MASK_P2; return key;
LPC_GPIO0->FIOSET = COL_MASK_P0; }
if(read_key != 0xFF && read_key != last_key) { int main(void){
last_key = read_key; unsigned char key;
switch(read_key) { PWM_Init();
case 0: duty_cycle = 300; break; Keyboard_Init();
case 1: duty_cycle = 750; break;
case 2: duty_cycle = 1500; break; while(1){
case 3: duty_cycle = 2250; break; key = Read_Keyboard();
}
} switch(key){
case 0:
delay_ms(50); 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;
} }