◆◆ ATmega128 : USART통신, 온도측정, 실시간 모니터링 P.449 ◆◆
/*
USART 통신 P.449
온도측정 실시간 컴퓨터 모니터링 제어
DS18S20 온도측정 : PORTC.0
LCD : PORTD
0317A_USART_Temp.c
*/
#include <mega128.h> //
#include <delay.h> //
#include <stdio.h> //
// 1 Wire Bus 함수, PORTC.0에 센서 연결
#asm
.equ __w1_port = 0x15 ; PORTC
.equ __w1_bit = 0
#endasm
#include <1wire.h> //
// DS18b20 온도센서 함수
#include <ds1820.h> //
// 1 Wire bus 에 연결할 수 있는 최대 DS1820의 수
#define MAX_DS1820 8
//LCD 를 PORTD에 연결
#asm
.equ __lcd_port = 0x12 ; PORTD
#endasm
#include <lcd.h> //
unsigned char Ds1820_devices;
unsigned char Ds1820_rom_codes[MAX_DS1820][9];
void Putch(char); // USART통신 입력부분
char string[33]; // 문자열 저장 배열
char *pStr; // 문자열 포인터
void main(void)
{
unsigned char i; // 반복문
unsigned char j; // 영상 +, 영하 -
int Temp; // 온도
lcd_init(16);
// 1 Wire Bus에 연결된 센서의 수를 찾음
Ds1820_devices = w1_search( 0xf0, Ds1820_rom_codes );
// LCD 에 센서수를 출력
sprintf( string, " %u DS1820 \n Device detected ", Ds1820_devices);
lcd_puts(string); // LCD에 값 입력
delay_ms(1000);
// USART 초기화
UCSR0A = 0x00;
UCSR0B = 0b00001000; // 송신 인에이블 TXEN = 1
UCSR0C = 0b00000110; // 비동기 데이터 8비트 모드
UBRR0H = 0; // X-TAL = 16MHz, Baud = 9600
UBRR0L = 103;
lcd_init(16); // LCD 초기하
while(1){
for( i = 0 ; i < Ds1820_devices; ){
Temp = ds1820_temperature_10( & Ds1820_rom_codes[i][0]);
j = '+'; // 영상 + 값
if( Temp < 0 ){
j = '-'; // 영하 - 값
Temp = - Temp;
}//endif
// LCD 첫번째 줄에 온도 출력
if( i == 0 | i == 2 | i == 4 | i == 6 ){
sprintf( string, " Temper %u = %c%i.%u \xdfC ", ++i, j ,Temp/10, Temp%10 );
lcd_gotoxy(0,0);
}//endif
// LCD 두번째 줄에 온도 출력
else if( i == 1 | i == 3 | i == 5 | i == 7 ){
sprintf( string, "Temper %u = %c%i.%u \xdfC ", ++i, j, Temp/10, Temp%10 );
lcd_gotoxy(0,1);
}//endif
delay_ms(1000);
}//endfori
pStr = string; // 문자열포인터 주소값에 문자열을 넣는다.
lcd_puts(string);
delay_ms(1000);
while(*pStr) Putch(*pStr++); // 문자열 전송
}//endwhile
}//end main
void Putch(char data){
while( ! ( UCSR0A & 0x20 ) );
UDR0 = data; // 받은 무자열을 터미널에 뿌려준다.
}//endPutch
터미널 상태창