BASIC4MCU | 질문게시판 | ATMEGA128 UART 질문입니다.
페이지 정보
작성자 메가 작성일2022-12-02 17:54 조회809회 댓글0건
https://www.basic4mcu.com/bbs/board.php?bo_table=gac&wr_id=21983
본문
조도센서 측정값을 FND로 나타내고 값이 커질 수록 LED가 많이 채워지는 프로그램을 작성했습니다.여기에 uart통신 이용해서 시리얼로 측정값을 보고 싶은데 main에서 어떻게 작성해야할지 막힙니다.#include <avr/io.h>#include <avr/interrupt.h>#define CLR 0#define SET 1#define T_START 184#define INTERVAL 20#define AD_CHECK 0x10#define AD_CH0 0x00#define AD_CH1 0x01#define _9600BPS 95#define _19200BPS 47#define _38400BPS 23#define _76800BPS 11/* 변수 정의 */int digit;int Data, Data_1000, Data_100, Data_10, Data_1;int tmr_cnt;char tmr_flag;char sec;const char FND_TABLE[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x27, 0x7F, 0x67};// FND NUMBER table 0, 1, 2, 3, 4, 5, 6, 7, 8, 9// TIMER0 OVERFLOW 인터럽트 루틴ISR(TIMER0_OVF_vect){TCNT0 = T_START;if(++tmr_cnt == INTERVAL){tmr_cnt = 0;tmr_flag = SET;}if(digit == 0){PORTD &= 0x0F;PORTC = ~FND_TABLE[Data_1000];PORTD |= 0x10;}else if(digit == 1){PORTD &= 0x0F;PORTC = ~FND_TABLE[Data_100];PORTD |= 0x20;}else if(digit == 2){PORTD &= 0x0F;PORTC = ~FND_TABLE[Data_10];PORTD |= 0x40;}else if(digit == 3){PORTD &= 0x0F;PORTC = ~FND_TABLE[Data_1];PORTD |= 0x80;}if(++digit > 3) digit = 0;}void Init_IOport(void){DDRC = 0xFF;PORTC = 0xFF;DDRB = 0xff;PORTB = 0x00;DDRD = 0xFF;PORTD = 0x0F;}void Init_Interrupt(void){TCCR0 = 0x07; // Normal timer, clk / 1024 분주비 설정TIMSK = 0x01; // Timer0 Overflow Interrupt 설정TCNT0 = T_START; // Timer0 초기값 설정SREG = 0x80; // Enable Global Interrupt}ISR(TIMER0_OVF_vect){TCNT0 = T_START;if(++tmr_cnt >= 200){tmr_cnt = 0;tmr_flag = SET;}}void Init_Serial_0(void){char tmp;UBRR0H = 0;UBRR0L = _9600BPS;UCSR0A = 0x00;UCSR0B = 0x08;UCSR0C = 0x06;tmp = UDR0;}void TX0_char(char data){while((UCSR0A & 0x20) == 0x00);UDR0 = data;}void TX0_string(char *string){while(*string != '\0'){TX0_char(*string);string++;}while ((UCSR0A & 0x20)== 0x00);}void Init_Serial_1(void){char tmp;UBRR1H = 0;UBRR1L = _9600BPS;UCSR1A = 0x00;UCSR1B = 0x08; //tx 보내기UCSR1C = 0x06;tmp = UDR1;}void TX1_char(char data){while((UCSR1A & 0x20) == 0x00); // tx buffer(UDR1) 비었는지 확인UDR1 = data;}void TX1_string(char *string){while(*string !='\0'){TX1_char(*string);string++;}}void Init_intrerrupt(void){TCCR0 = 0x07;TIMSK = 0x01;TCNT0 = T_START;SREG = 0x80;}int main(void){/* Replace with your application code */Init_IOport();Init_Interrupt();Init_Serial_0(); //시리얼 포트 uart0 초기화Init_Serial_1();ADMUX = AD_CH1; // Set A/D channel 1(CDS), 외부 AREF 설정, 결과값 우측으로 채움ADCSRA = 0x87; // 0b 1000 0111, A/D operation ON(AD 회로 구성), Prescale = 128while (1){ADCSRA = 0xC7; // ADSC = 1, A/D Conversion Startwhile((ADCSRA & AD_CHECK) != AD_CHECK);Data = ADC; // ADC = ADCH , ADCL;if(tmr_flag == SET){tmr_flag = CLR;Data_1000 = Data / 1000;Data_100 = (Data % 1000) / 100;Data_10 = ((Data % 1000) % 100) / 10;Data_1 = ((Data % 1000) % 100) % 10;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 if(Data < 800) PORTB = ~0xFF;}}}
댓글 0
조회수 809등록된 댓글이 없습니다.