Skip to content

Instantly share code, notes, and snippets.

@surinoel
Last active July 14, 2019 06:37
Show Gist options
  • Save surinoel/0a5c0f5148435e4d6fd7d751f184d7ff to your computer and use it in GitHub Desktop.
Save surinoel/0a5c0f5148435e4d6fd7d751f184d7ff to your computer and use it in GitHub Desktop.
/*
* exti.c
*
* Created: 2019-07-07 오전 10:56:40
* Author: user
*/
#include "exti.h"
volatile int bcnt;
volatile unsigned long receiveData;
ISR(INT2_vect)
{
int tcnt = TCNT0; // 측정된 시간
int isOVF = TIFR & (1<<TOV0); // overflow 체크, 되었다면 잘못된 데이터
if(bcnt == 32) { // 리드 코드라면
if((tcnt > 201) && (tcnt < 221) && (isOVF == 0)) { // 일반
receiveData = 0;
bcnt = 0;
}
else if((tcnt > 166) && (tcnt < 186) && (isOVF == 0)) { // 반복
print_receive_data(1);
}
else { // 잘못된 데이터
bcnt = 32;
}
}
else {
if(tcnt > 40 || (isOVF != 0)) { // 잘못된 코드
bcnt = 32;
}
else {
if(tcnt > 26) { // 수신 1
receiveData = (receiveData << 1) + 1;
}
else { // 수신 0
receiveData = (receiveData << 1);
}
if(bcnt == 31) { // 수신 완료
print_receive_data(0);
_delay_ms(50); // 채터링 방지
EIFR |= (1<<INTF2);
}
bcnt += 1; // 비트 카운트 하나 증가
}
}
TCNT0 = 0;
TIFR |= (1<<TOV0); // overflow가 생겼다면 flag 초기화
}
void exti2_init(void)
{
EIMSK |= (1<<INT2);
EICRA |= (1<<ISC01);
sei();
}
void print_receive_data(int repeat)
{
if(repeat == 1) {
printf("Repeat... \r\n");
}
else {
printf("0x%lX\r\n", receiveData);
}
}
/*
* irled.c
*
* Created: 2019-07-07 오전 10:41:52
* Author : user
*/
#include <avr/io.h>
#include "uart.h"
#include "exti.h"
extern volatile int bcnt;
int main(void)
{
uart0_init();
TCCR0 |= (1<<CS00) | (1<<CS01) | (1<<CS02); // 타이머 1024
exti2_init(); // 하강엣지 인터럽트 초기화
bcnt = 32; // 시작신호 초기화
while (1)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment