C 언어로 K210 LED 깜박임 및 키 조절 가능
2301 단어 K210
#define PIN_LED 25
#define PIN_KEY 26
#define GPIO_LED 3
#define GPIO_KEY 2
가동 중지 시간:
plic_init();
sysctl_enable_irq();
fpioa_set_function(PIN_KEY, FUNC_GPIOHS2);
gpiohs_set_drive_mode(GPIO_KEY, GPIO_DM_INPUT);
gpiohs_set_pin_edge(GPIO_KEY, GPIO_PE_BOTH);//
gpiohs_set_irq(GPIO_KEY, 1, irq_gpiohs2);// GPIO
LED1의 입출력 포트를 높게 설정하려면:
fpioa_set_function(PIN_LED, FUNC_GPIOHS3);
gpiohs_set_drive_mode(GPIO_LED, GPIO_DM_OUTPUT);
gpio_pin_value_t value = GPIO_PV_HIGH;
gpiohs_set_pin(GPIO_LED, value);
LED1을 깜박이려면:
while (1)
{
sleep(1);
if (irq_flag)
gpiohs_set_pin(GPIO_LED, value = !value);
int val = gpiohs_get_pin(GPIO_KEY);
printf("The PIN is %d
", val);
}
인터럽트 콜백 함수 작성하기
void irq_gpiohs2(void* gp)
{
irq_flag = gpiohs_get_pin(GPIO_KEY);
printf("IRQ The PIN is %d
", irq_flag);
if (irq_flag)
gpiohs_set_pin(GPIO_LED, GPIO_PV_LOW);
else
gpiohs_set_pin(GPIO_LED, GPIO_PV_HIGH);
}
버튼을 누르면 인터럽트가 오고 인터럽트 리셋 함수가 호출되어 LED의 IO구 전압이 LOW이기 때문에 불이 켜지지 않습니다.다른 방법: gpiohs 사용irq_register 함수
uint32_t irq_flag;
#define PIN_LED 25
#define PIN_KEY 26
#define GPIO_LED 3
#define GPIO_KEY 2
uint32_t g_count;
int irq_gpiohs2(void* ctx)
{
irq_flag = gpiohs_get_pin(GPIO_KEY);
printf("IRQ The PIN is %d
", irq_flag);
uint32_t *tmp = (uint32_t *)(ctx);
printf("count is %d
", (*tmp)++);
if (!irq_flag)
gpiohs_set_pin(GPIO_LED, GPIO_PV_LOW);
else
gpiohs_set_pin(GPIO_LED, GPIO_PV_HIGH);
return 0;
}
int main(void)
{
plic_init();
sysctl_enable_irq();
fpioa_set_function(PIN_LED, FUNC_GPIOHS3);
gpiohs_set_drive_mode(GPIO_LED, GPIO_DM_OUTPUT);
gpio_pin_value_t value = GPIO_PV_HIGH;
gpiohs_set_pin(GPIO_LED, value);
fpioa_set_function(PIN_KEY, FUNC_GPIOHS2);
gpiohs_set_drive_mode(GPIO_KEY, GPIO_DM_INPUT_PULL_UP);
gpiohs_set_pin_edge(GPIO_KEY, GPIO_PE_BOTH);
gpiohs_irq_register(GPIO_KEY, 1, irq_gpiohs2, &g_count);
while (1);
}