이귀형 교수님 응용예제 프로그램의 작성
- 스위치확인과 데이터변형 예제
마이크로컨트롤러1
(MicroController1)
3강
8개의 switch중 어느 한 switch를 눌렀을 때,
어느 것이 눌렸는지 번호를 알아내는 방법을 학습한다.
8개의 switch를 4개씩 두 그룹 (SW0~3과 SW4~7)으로 나눌 때, 어느 그룹의 switch가 눌렸는지 구별하는 방법을 학습한다.
8bit data에서 상위/하위 4bit 만 변경하는 방법을 학습한다.
학습목표
1. 기본 예제 : 주어진 조건
PORTA에는 - LED 8개
PORTD에는 - push button switch 8개
PORTC에는 – 7447(또는 GAL) + 7-seg.LED 가 연결되어 있다.
프로그램을 실행하면 처음에 LED는 88H(1000 10002)를 출력하여 대기하고 있다.
다음과 같은 switch 입력동작에 따라 출력 값이 변하도록 프로그램을 작성하라.
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
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;
} }
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차 작성
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의 수정
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;
} }
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;
} }
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;
} }
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;
}