• 검색 결과가 없습니다.

마이크로컨트롤러1

N/A
N/A
Protected

Academic year: 2022

Share "마이크로컨트롤러1"

Copied!
12
0
0

로드 중.... (전체 텍스트 보기)

전체 글

(1)

이귀형 교수님 응용예제 프로그램의 작성

- 스위치확인과 데이터변형 예제

마이크로컨트롤러1

(MicroController1)

3강

(2)

8개의 switch중 어느 한 switch를 눌렀을 때,

어느 것이 눌렸는지 번호를 알아내는 방법을 학습한다.

8개의 switch를 4개씩 두 그룹 (SW0~3과 SW4~7)으로 나눌 때, 어느 그룹의 switch가 눌렸는지 구별하는 방법을 학습한다.

8bit data에서 상위/하위 4bit 만 변경하는 방법을 학습한다.

학습목표

(3)

1. 기본 예제 : 주어진 조건

PORTA에는 - LED 8개

PORTD에는 - push button switch 8개

PORTC에는 – 7447(또는 GAL) + 7-seg.LED 가 연결되어 있다.

프로그램을 실행하면 처음에 LED는 88H(1000 10002)를 출력하여 대기하고 있다.

다음과 같은 switch 입력동작에 따라 출력 값이 변하도록 프로그램을 작성하라.

(4)

1. 기본 예제 : 주어진 조건

SW0,1,2,3중 하나를 누르면,

해당 switch의 번호를 PORTC의 하위 4-bit 7- segment LED에 출력한다.

이 때, PORTC의 상위 4-bit의 값은 변하지 않는다.

SW4,5,6,7중 하나를 누르면,

해당 switch의 번호를 PORTC의 상위 4bit 7- segment LED에 출력한다.

이 때, PORTC의 하위 4-bit의 값은 변하지 않는다.

SW0~3중 하나와 SW4~7중 하나를 (즉 2개의 switch 를) 동시에 누르면,

PORTC로 ①② 동작이 동시에 출력한다.

switch를 교대로 누르면 ①②③동작을 반복한다.

1

2

3

<동작 예>

88

↓ ⇐ SW2를 누르면 82

↓ ⇐ SW5를 누르면

52

↓ ⇐ SW3을 누르면 53

↓ ⇐ SW7을 누르면

73

↓ ⇐ SW6을 누르면

63

↓ ⇐ SW1을 누르면 61

↓ ⇐ SW0을 누르면 60

↓ ⇐ SW4를 누르면

40

↓ ⇐ SW7, 3을 누르면

73

↓ ⇐ SW5, 1을 누르면

51

4

(5)

2. Program의 작성 main의 1차 작성

// ***example program --- 1단계 ***

#include <avr/io.h>

unsigned char inswitch;

unsigned char sw3210, sw7654;

unsigned char ON_low_switch_no;

#define INITIAL_NUMBER 0x88

#define OFF 0

#define ON 1

void check_ON_switch();

int main(void)

{ unsigned char outdata;

DDRA= 0xFF;

DDRC= 0xFF;

DDRD= 0x00;

outdata=INITIAL_NUMBER;

PORTA=PORTC=outdata;

while(1) {

while((inswitch=PIND)==0x00);

check_ON_switch();

if(sw7654==OFF && sw3210==ON)

outdata=(outdata & 0xF0)|ON_low_switch_no;

PORTA=PORTC=outdata;

} }

(6)

2. Program의 작성

void check_ON_switch()

{ unsigned char low_check_bit, switch_no;

low_check_bit=0x01;

sw3210=OFF;

sw7654=OFF;

ON_low_switch_no=0;

for(switch_no=0;switch_no<=3;switch_no++){

if((inswitch & low_check_bit)!=0x00){

ON_low_switch_no=switch_no;

sw3210=ON;

break;

}

else low_check_bit= low_check_bit<<1; // else 생략가능 }

}

함수의 1차 작성

(7)

2. Program의 작성

// ***example program --- 2단계 ***

#include <avr/io.h>

unsigned char inswitch;

unsigned char sw3210, sw7654;

unsigned char ON_low_switch_no, ON_high_switch_no;

… int main(void) { …

while(1){

if (sw7654==OFF && sw3210==ON)

outdata=(outdata&0xF0)|ON_low_switch_no;

else if(sw7654==ON && sw3210==OFF)

outdata=(ON_high_switch_no<<4)|(outdata&0x0F);

PORTA=PORTC=outdata;

} }

main의 수정

(8)

2. Program의 작성 함수의 수정

// ***example program --- 2단계 ***

#include <avr/io.h>

… unsigned char inswitch;

unsigned char sw3210, sw7654;

unsigned char ON_low_switch_no, ON_high_switch_no;

… int main(void) {

while(1){

if (sw7654==OFF && sw3210==ON)

outdata=(outdata&0xF0)|ON_low_switch_no;

else if(sw7654==ON && sw3210==OFF) outdata=(ON_high_switch_no<<4)|(outdata&0x0F);

PORTA=PORTC=outdata;

} }

(9)

2. Program의 작성 main의 추가수정

#include <avr/io.h>

int main(void) { …

while(1) {

while((inswitch=PIND)==0x00);

check_ON_switch();

if (sw7654==OFF && sw3210==ON) outdata=(outdata&0xF0)|ON_low_switch_no;

else if(sw7654==ON && sw3210==OFF) outdata=(ON_high_switch_no<<4)|(outdata&0x0F);

else if(sw7654==ON && sw3210==ON) outdata=(ON_high_switch_no<<4)|ON_low_switch_no;

PORTA=PORTC=outdata;

} }

(10)

2. Program의 작성 최종 program list

// *** example program ***

#include <avr/io.h>

unsigned char inswitch;

unsigned char sw3210, sw7654;

unsigned char ON_low_switch_no, ON_high_switch_no;

#define INITIAL_NUMBER 0x88

#define OFF 0

#define ON 1

void check_ON_switch();

int main(void)

{ unsigned char outdata;

DDRA= 0xFF;

DDRC= 0xFF;

DDRD= 0x00;

outdata=INITIAL_NUMBER;

PORTA=PORTC=outdata;

while(1) {

while((inswitch=PIND)==0x00);

check_ON_switch();

if (sw7654==OFF && sw3210==ON) outdata=(outdata&0xF0)|ON_low_switch_no;

else if(sw7654==ON && sw3210==OFF)

outdata=(ON_high_switch_no<<4)|(outdata&0x0F);

else if(sw7654==ON && sw3210==ON)

outdata=(ON_high_switch_no<<4)|ON_low_switch_no;

PORTA=PORTC=outdata;

} }

(11)

2. Program의 작성 최종 program list

void check_ON_switch() {

unsigned char

low_check_bit,high_check_bit,switch_no;

low_check_bit =0x01;

high_check_bit=0x10;

sw3210=OFF;

sw7654=OFF;

ON_low_switch_no=0;

ON_high_switch_no=0;

for(switch_no=0;switch_no<=3;switch_no++){

if ((inswitch & low_check_bit)!=0x00){

ON_low_switch_no=switch_no;

sw3210=ON;

break;

} else low_check_bit= low_check_bit<<1;

}

for(switch_no=4;switch_no<=7;switch_no++){

if ((inswitch & high_check_bit)!=0x00){

ON_high_switch_no=switch_no;

sw7654=ON;

break;

else high_check_bit= high_check_bit<<1; }

} }

(12)

8개중 어느 한 switch 를 눌렀을 때, 그 번호를 알아내는 방법은 8bit 입력데이터를 SW0 부터 bit 1과 AND 연산으로 체크하며,

switch ON이 확인되면, 번호를 기억하고 체크동작을 끝낸다.

SW0~3과 SW4~7에서, 어느 그룹의 switch가 눌렸는지

구별하려면, 사전에 2개의 변수 (state indicator)를 이용하여 OFF로 한 후,

4개씩 별도로 switch check하여,

switch ON이 확인되면, 해당 변수만 OFF  ON 으로 바꾼다.

8bit data에서 상위(또는 하위) 4bit 만 변경하려면, bit 연산 X AND 0 = 0, X AND 1 = X를 이용하여

먼저 old data AND 0x0F 하여 상위 4bit를 clear한 후,

(old data AND 0x0F) OR (newdata<<4) 하여 상위 4bit를 바꾼다.

학습정리

참조

관련 문서

전주대학교에서 진행하는 해외 봉사는 분명 봉사의 형태를 띠고 있습니다만, 우리 학생들을 성장시키기 위한 교육으로서의 의미가 더 큰지도 모릅니다.. 그런 의미에서

그러나 WLS는 다음과

나이를 입력하면 그 나이 에 따라 발급가능한지 여부를 알려주는

DNS서버에 다음과 같은 zone파일을 생성하여 DNS를 운영하려고 한다.. 다음은 설정 하려는 도메인에 대한

어느 경제의 거시경제상태가 다음과 같은 모형으로 나타내어질 수 있다고 가정하자.. 가계와 기업만이 존재하는

예를 들어 링크모델은 위와 같은 모델 속성을 가지고 있으면서, 추가로 다음과 같은 링크속성을 더 가지고 있습니다...

전원선과 입력선을 근접하여 설치할 경우 전원선에는 라인 필터나 바리스터를 사용하고 입력선에는 쉴드 와이어를 사용하십시오.. 강한 자기력 및 고주파 노이즈가

 authors와 books를 authors의 surname 값과 books의 name 값이 같은 자료를 한 행으로 하여 합친다... 사용예를