BASIC4MCU | 질문게시판 | 답변 : codevision pms7003 코드 오류 (atmega128)
페이지 정보
작성자 master 작성일2020-08-21 21:40 조회4,177회 댓글1건
https://www.basic4mcu.com/bbs/board.php?bo_table=gac&wr_id=13233
본문
// MCU BASIC: https://www.basic4mcu.com// DateTime : 2020-08-21 오후 9:46:59// by Ok-Hyun Park//#include <mega128.h>#include <delay.h>#include <string.h>#include <stdio.h>#include <stdlib.h>#include <lcd.h>//char str[100];char Langth[2],uart1_buf[32],step,rx_counter,rx_endF,PMData[26];unsigned char data,status;unsigned int checksum,PM1,PM1_1,PM1_2,PM25,PM25_1,PM25_2,PM10,PM10_1,PM10_2;//void TX1_char(char c){ while(!(UCSR1A&0x20)); UDR1=c; }void TX1_STR(char *s){ while(*s)TX1_char(*s++); }//void PM(void){PM1=((PMData[0]<<8)+PMData[1]);PM25=((PMData[2]<<8)+PMData[3]);PM10=((PMData[4]<<8)+PMData[5]);PM1_1=((PMData[6]<<8)+PMData[7]);PM25_1=((PMData[8]<<8)+PMData[9]);PM10_1=((PMData[10]<<8)+PMData[11]);PM1_2=((PMData[16]<<8)+PMData[17]);PM25_2=((PMData[18]<<8)+PMData[19]);PM10_2=((PMData[22]<<8)+PMData[23]);sprintf(str,"%d,%d,%d,%d,%d,%d,%d,%d,%d,\n\r",PM1,PM25,PM10,PM1_1,PM25_1,PM10_1,PM1_2,PM25_2,PM10_2);TX1_STR(str); //-->uart1로 출력}//interrupt[USART0_RXC]void usart1_rx_isr(void){status=0,data=0; // 상태값이랑 데이터 값 초기값 설정status=UCSR1A; // ucsr1a는 송수신 동작을 제어하거나 송수신 상태를 저장하는 기능 수행data=UDR1; // data에 uart1 레지스터 값 저장if((status&0x1C)==0){switch(step){case 0: if(data==0x42){ step++; checksum+=data; } break; // 시작 바이트1case 1: if(data==0x4d){ step++; rx_counter=0; checksum+=data; } break; // 시작 바이트2case 2: Langth[0]=data; step++; checksum+=data; break; // 길이 바이트1case 3: Langth[1]=data; step++; checksum+=data; break; // 길이 바이트2case 4: uart1_buf[rx_counter++]=data; if(Langth[1]-2==rx_counter)step++; checksum+=data; break; // 미세먼지 데이터 26 바이트case 5: if(((checksum>>8)&0xFF)==data)step++; break; // 체크섬 바이트1case 6: if((checksum&0xFF)==data){ rx_endF=1; step=0; checksum=0; memcpy(PMData,uart1_buf,26); } break; // PMdata의 26바이트 데이터를 uart1_buf 변수로 복사 // 체크섬 바이트2default: checksum=0; step=0; break;}}}//void main(void){char p25,p10;int a,b;DDRA=0x00; // LCD 입력(PD0~PD7)DDRC=0xE0; // LCD RS,RW,E(PC7,6,5)출력 1110 0000//UCSR1B=0x08;UCSR1B=0x98; UBRR0L=103; // USART1 Baud rate: 9600SREG=0x80; // #asm("sei")와 같은 인터럽트 인에이블 설정lcd_init(16);while(1){a=PM25; b=PM10;if(rx_endF){ rx_endF=0; PM(); }lcd_gotoxy(0,0); lcd_putsf("PM2.5="); sprintf(&p25,"%d",a); lcd_puts(&p25); lcd_putsf(" "); // pm2.5 데이터 출력lcd_gotoxy(0,1); lcd_putsf("PM10="); sprintf(&p10,"%d",b); lcd_puts(&p10);lcd_putsf(" "); // pm10 데이터 출력delay_ms(10); // 0.1sec 딜레이}}
UCSR1B=0x08;UCSR1B=0x90;위는 init() 함수에서 설정 한 것이고아래는 메인함수에서 설정 한 것입니다.UCSR1B=0x98;폴링방식의 송신과 인터럽트 수신을 하려면 이렇게 수정하세요
댓글 1
조회수 4,177JESSI님의 댓글
JESSI 작성일답변 정말 감사합니다!!