거북이처럼 천천히

Pointer 본문

Embedded Programming/Atmega 128A (실습)

Pointer

유로 청년 2024. 5. 25. 15:59

1. 환경 : Microchip Studio

2. 목표 : Pointer 개념을 활용하여 GPIO Control 함으로서 Pointer의 개념 정비 및 기본 활용 학습

3. 구현 내용 : Shift 연산자와 Bit OR 연산자를 이용하여 1번쩨 비트 ~ 7번째 비트까지 누적하여 LED 출력

4. Source code

  • main.c 
  • controlLED.c (LED에 관한 함수)
  • common.h (라이브러리, 기본 헤더 파일을 include 한 파일)
  • personal.h (함수 원형 선언 및 상수 선언)
#include "personal.h"

int main(void) {
   initDDR();
   
   // 임의의 데이터를 초기화
   uint8_t data = PORTD1;
   
   while(1) {
      control_LED(&data);   
   }   
}
#include "personal.h"

// 1. initialization Data Direction Registor.
void initDDR() {
   // PORT D의 8핀을 출력으로 정의 및 설정
   DDRD = 0xff;
}

// 2. GPIO Output
void GPIO_Output(uint8_t data) {
   PORTD = data;
}

// 3. Left shift + accumulation
void left_shift(uint8_t *p_data) {
   // accumulation   
   *p_data = *p_data<<1;
   *p_data |= PORTD1;
   
   GPIO_Output(*p_data);
}

// 4. Right shift + accumulation
void right_shift(uint8_t *p_data) {
   // accumulation
   *p_data >>= 1;
   
   GPIO_Output(*p_data);
}

// 5. Control LED
void control_LED(uint8_t *data) {
   PORTD = *data;
   _delay_ms(TIME);
   
   for(uint8_t i=0; i<7; i++) {
      left_shift(data);
      _delay_ms(TIME);
   }
   
   for(uint8_t i=1; i<8; i++) {
      right_shift(data);
      _delay_ms(TIME);
   }
}
#ifndef COMMON_H_
#define COMMON_H_

#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>

#endif /* COMMON_H_ */
#ifndef PERSONAL_H_
#define PERSONAL_H_

#include "common.h"

// define constant
#define TIME 200

// function prototype.
void initDDR();
void GPIO_Output(uint8_t data);
void left_shift(uint8_t *p_data);
void right_shift(uint8_t *p_data);
void control_LED(uint8_t *data);

#endif /* PERSONAL_H_ */

 

※ 일반적인 C언어에서의 Pointer의 개념 및 사용과 크게 다르지 않다.

 


5. 구현