2016년 4월 28일 목요일

http://binworld.kr/6 http://miobot.tistory.com/24

int1.c INT7(interrupt 7)을 이용한 외부 인터럽트 테스트

// int1.c
// INT7(interrupt 7)을 이용한 외부 인터럽트 테스트
// INT7 스위치를 누를 때마다 LED를 차례로 켜고
// FND를 차례로 증가시킨다.

#include <avr/io.h>
#include <avr/interrupt.h>

#define FND_ON 0xe3 // PE4=0

unsigned char fnd[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, // 0~4
  0x92, 0x82, 0xf8, 0x80, 0x98, // 5~9
  0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e}; // AbCdEF

volatile unsigned char int_cnt = 0;

void int_init();

// ISR - Interrupt Service Routine
ISR(INT7_vect) { // SIGNAL(SIG_INTERRUPT7)
int_cnt++;
}

int main(void) {
unsigned char led = 0xfe;
unsigned char bin = 0;

DDRD = 0x0f;
PORTD = 0xff;

DDRA = 0xff;
PORTA = 0xff;

DDRE = 0x3f; // 0011 1111, PE7=0(INT7용 값 입력), PE4=1(FND_ON)
PORTE = FND_ON; // PE4에 0 출력

int_init(); // interrupt 초기화 루틴
sei(); // SREG bit 7을 1로 세팅(Set Enable Interrupt)

while(1) {
if(int_cnt) {
int_cnt = 0;

PORTD = led; // LED 이동
led <<= 1;
led |= 0xf1;
if(led == 0xff)
led = 0xfe;

PORTA = fnd[bin]; // FND 표시
bin++;
if(bin >= 16)
bin = 0;

} // if

} // while

return(0);
} // main


// interrupt 초기화 루틴
void int_init() {
EIMSK = 0x80; // 1000 0000 (Int7을 사용하겠다.)
EICRB = 0Xc0; // 10 00 00 00(falling edge에서 동작하게 해준다.)
// 0x40 : falling/rising edge
// 0x80 : falling edge
// 0xc0 : rising edge

}

AVR 설정


v

2016년 4월 19일 화요일

AVR Proteus

가상avr

sw3.c

// sw3.c
// switch input test
// PD(port D) : SW7,6,5,4 LED3,2,1,0

#include <avr/io.h>
#include <util/delay.h>
void switch4();
void switch5();

int main(void) {
unsigned char led;
int dir = 0;
DDRD = 0x0f; // 0000:input, 1111:output

while(1) {
led = PIND | 0x0f; // 1111 1111 : light off // when switch not pressed,
if(led != 0xff) { // when switch pressed,
led >>= 4;
led |= 0xf0;

if(led == 0xfe)
switch4();
else if(led == 0xfd)
switch5();
else if(led == 0xfc){
switch4();
switch5();
}
else if(led == 0xfb)
PORTD = !led;

}
else // if switch not pressed,
PORTD = 0xff; // led off

_delay_ms(100);

}//while

return(0);

}

void switch4(){
while(1){
led <<= 1; // shift left 1bit
led |= 0xf1; // 1111 0001, 우측에 1을 삽입
if(led == 0xff)
led = 0xfe;
PORTD = led; // LED on concerning sw pressed,
}
}

void switch5(){
while(1){
led >>= 1;
led |= 0x80;
if(led == 0xff)
led = 0xf7;
PORTD = led; // LED on concerning sw pressed,
}
}

sw2.c

// sw1.c
// switch input test
// PD(port D) : SW7,6,5,4 LED3,2,1,0

#include <avr/io.h>
#include <util/delay.h>
void switch4();
void switch5();

int main(void) {
unsigned char led;
DDRD = 0x0f; // 0000:input, 1111:output

while(1) {
led = PIND | 0x0f; // 1111 1111 : light off // when switch not pressed,
if(led != 0xff) { // when switch pressed,
led >>= 4;
led |= 0xf0;

if(led == 0xfe)
switch4();
else if(led == 0xfd)
switch5();
else if(led == 0xfc){
switch4();
switch5();
}
else if(led == 0xfb)
PORTD = !led;

}
else // if switch not pressed,
PORTD = 0xff; // led off

_delay_ms(100);

}//while

return(0);

}

void switch4(){
led <<= 1; // shift left 1bit
led |= 0xf1; // 1111 0001, 우측에 1을 삽입
if(led == 0xff)
led = 0xfe;
PORTD = led; // LED on concerning sw pressed,
}

void switch5(){
led >>= 1;
led |= 0x80;
if(led == 0xff)
led = 0xf7;
PORTD = led; // LED on concerning sw pressed,
}

led2.c

// led2.c
// led on & off
// define 문을 이용해서 필요한 부분만 컴파일 및 실행

#include <avr/io.h>
#include <util/delay.h>
#define TEST 1

int main(void) {
#if TEST == 1 // #ifdef TEST
unsigned char led = 0xfe; // 1111 1110
#else
unsigned char led = 0xf7; // 1111 0111
#endif

DDRD = 0x0f;
// upper4bit : input
// under4bit :

while(1) {
PORTD = led; // on
_delay_ms(1000); // delay

#if TEST == 1
led <<= 1; // shift left 1bit
led |= 0xf1; // 1111 0001, 우측에 1을 삽입
if(led == 0xff)
led = 0xfe;



#else
led >>= 1;
led |= 0x80;
if(led == 0xff)
led = 0xf7;



#endif
} // while

return(0);

}