전체 8개의 led 有
sw0 누르면 두개씩 누적되어 켜진다 (ex. 2개, 4개, 6개, 8개)
sw1 누르면 켜져있는 led 모두 꺼진다
1. main.c
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include "driver/button/button.h"
#include "driver/led/led.h"
#include "driver/ap/ledMachine/ledMachine.h"
int main(void)
{
ledMachine_init();
while (1)
{
ledMachine_RUN();
}
}
2. driver
1) button
- button.h
#ifndef BUTTON_H_
#define BUTTON_H_
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
typedef struct _button{
volatile uint8_t *DDR;
volatile uint8_t *PIN;
uint8_t pinNum;
uint8_t prevState;
}button_t;
enum {PUSHED, RELEASED};
enum {ACT_PUSHED, ACT_RELEASED, ACT_NONE};
uint8_t buttonGetState();
void Button_init(button_t *btn, volatile uint8_t *ddr, volatile uint8_t *pin, uint8_t pinNum);
#endif /* BUTTON_H_ */
- button.c
#include "button.h"
void Button_init(button_t *btn, volatile uint8_t *ddr, volatile uint8_t *pin, uint8_t pinNum){
btn->DDR = ddr;
btn->PIN = pin;
btn->pinNum = pinNum;
btn->prevState = RELEASED;
*btn->DDR &= ~(1<<btn->pinNum);
}
// PULL - UP type : 눌렸을때 입력되는 비트가 0
uint8_t buttonGetState(button_t *btn){
static uint8_t prevState = 1;
uint8_t curState = *btn->PIN & (1<<btn->pinNum);
if ((curState == PUSHED) && (btn->prevState == RELEASED)){
_delay_ms(10);
btn->prevState = PUSHED;
return ACT_PUSHED;
}
else if ((curState != PUSHED) && (btn->prevState == PUSHED)){
_delay_ms(10);
btn->prevState = RELEASED;
return ACT_RELEASED;
}
return ACT_NONE;
}
2) led
- led.h
/*
* led.h
*
* Created: 2024-09-20 오전 11:26:30
* Author: kccistc
*/
#ifndef LED_H_
#define LED_H_
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#define LED_DDR DDRD
#define LED_PORT PORTD
#define LED_PIN PIND
typedef struct _led{
volatile uint8_t *DDR;
volatile uint8_t *PIN;
uint8_t pinNum;
}led_t;
void led_init();
void led_writeData(uint8_t data);
void led_allOFF();
void led_allON();
- led.c
#include "led.h"
// led initializing
void led_init(){
LED_DDR = 0xff;
}
// led lighting
void led_writeData(uint8_t data){
LED_PORT = data;
}
void led_allOFF(){
LED_PORT = 0x00;
}
void led_allON(){
LED_PORT = 0xff;
}
'하만 세미콘 아카데미 8기 > 임베디드 시스템을 위한 sw 구조설계' 카테고리의 다른 글
atmega128a - simple traffic system_v2 (+driver) (0) | 2024.09.22 |
---|---|
atmega128a - simple traffic system (0) | 2024.09.22 |
240911 avr atmega led & switch 실습 - 1 (0) | 2024.09.13 |