BASIC4MCU | 질문게시판 | 답변 : AD 내부 인터럽트를 이용해 조도센서를 통한 LED 제어
페이지 정보
작성자 master 작성일2023-11-21 06:45 조회919회 댓글1건본문
#define F_CPU 16000000L
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdio.h>
//
volatile int a=0;
//
void UART1_tx(char d){ while(!(UCSR1A&(1<<UDRE1))); UDR1=d; }
char UART1_rx(){ while(!(UCSR1A&(1<<RXC1))); return UDR1; }
void UART1_str(char *s){ while(*s)UART1_tx(*s++); }
//
ISR(ADC_vect){
a=ADC;
if(a<512)PORTB|=1; else PORTB&=~1;
ADCSRA|=(1<<ADSC);
}
//
int main(void){
int i; char str[30];
DDRB=1;
ADMUX=0x40; ADCSRA=0xCF;
UCSR1B=0x18; UBRR1L=103;
sei();
while(1){
_delay_ms(1000);
cli(); i=a; sei();
sprintf(str,"ADC Value: %d\n",i); UART1_str(str);
}
return 0;
}
ADC 인터럽트를 사용 할 필요가 없지 않나요?
#define F_CPU 16000000L
#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
//
void UART1_tx(char d){ while(!(UCSR1A&(1<<UDRE1))); UDR1=d; }
char UART1_rx(){ while(!(UCSR1A&(1<<RXC1))); return UDR1; }
void UART1_str(char *s){ while(*s)UART1_tx(*s++); }
//
int main(void){
int i; char str[30];
DDRB=1;
ADMUX=0x40; ADCSRA=0xE7;
UCSR1B=0x18; UBRR1L=103;
while(1){
_delay_ms(1000);
i=ADC;
sprintf(str,"ADC Value: %d\n",i); UART1_str(str);
}
return 0;
}
댓글 1
조회수 919푸글릿님의 댓글
푸글릿 작성일감사합니다!