BASIC4MCU | 질문게시판 | 답변 : ATMEGA128 UART 질문입니다.
페이지 정보
작성자 master 작성일2022-12-03 10:58 조회337회 댓글0건본문
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
//
#define _9600BPS 95
#define _19200BPS 47
#define _38400BPS 23
#define _76800BPS 11
//
volatile char SEG[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x27,0x7F,0x67};
volatile int tmr_cnt=0,digit=0,Data,d[4]={0,};
volatile char tmr_flag=0,buf[30];
//
void TX0_char(char d){ while((UCSR0A&0x20)==0); UDR0=d; }
void TX1_char(char d){ while((UCSR1A&0x20)==0); UDR1=d; }
void TX0_str(char *s){ while(*s)TX0_char(*s++); }
void TX1_str(char *s){ while(*s)TX1_char(*s++); }
//
ISR(TIMER0_OVF_vect){
TCNT0=T_START;
if(++tmr_cnt>=20){ tmr_cnt=0; tmr_flag=1; }
//
PORTD=0; PORTC=~SEG[d[digit]]; PORTD=0x10<<digit;
if(++digit>3)digit=0;
}
//
ISR(TIMER0_OVF_vect){
TCNT0=184;
if(++tmr_cnt>=200){ tmr_cnt=0; tmr_flag=SET; }
}
//
int main(void){
DDRC=0xFF; PORTC=0xFF; DDRB=0xff; DDRD=0x0F; PORTD=0x0F;
//
UBRR0L=95; UCSR0B=0x08; // 9600
UBRR1L=95; UCSR1B=0x08;
//
ADMUX=1; ADCSRA=0xE7;
//
TCCR0=0x07; TCNT0=184; TIMSK=0x01;
SREG=0x80;
while(1){
if(tmr_flag){ tmr_flag=0;
Data=ADC;
//
if (Data<400)PORTB=0xFF;
else if(Data<500)PORTB=0x7F;
else if(Data<550)PORTB=0x3F;
else if(Data<600)PORTB=0x1F;
else if(Data<650)PORTB=0x0F;
else if(Data<700)PORTB=0x07;
else if(Data<750)PORTB=0x03;
else PORTB=0x00;
//
d[0]=Data/1000; d[1]=Data/100%10;
d[2]=Data/10%10; d[3]=Data%10;
sprintf(buf,"ADC=%4d\r\n",Data);
//TX0_str(buf); // UART0 송신
TX1_str(buf); // UART1 송신
}
}
}
댓글 0
조회수 337등록된 댓글이 없습니다.