#include <mega128.h>
#include <delay.h>
#include <stdio.h>
#include <math.h>
#include <io.h>
#include <interrupt.h>
float ntc_val;
float temperature;
float sensor_adc = 0;
unsigned int trans_temp = 0;
int SG_data[10]={0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
int SGD_data[10]={0xBf, 0x86, 0xDb, 0xCf, 0xE6, 0xEd, 0xFd, 0x87, 0xff, 0xEf};
int SG_PORTC[5]={0, 0x21, 0x22, 0x24, 0x28};<---- PORTC 5 끄기
int digit[4]={0,0,0,0};
void init_port(void)
{
PORTA = 0x00;
DDRA = 0xff;
PORTB = 0x00;
DDRB = 0xff;
PORTC = 0x00;
DDRC = 0xff;
}
void init_devices(void)
{
TIMSK = 0x41; //타이머0,2 오버플로우 사용
// ADC 설정
// ADC Voltage Reference: AREF pin
// ADC Auto Trigger Source: ADC Stopped
ACSR=0x80;
SFIOR=0x00;
ADMUX=0x40;
ADCSRA=0x8D;
}
void temperature_function()
{
//ADC 값 읽어 오기
ADMUX = 0x40;
ADCSRA |= 0x40;
while ((ADCSRA & 0x10)==0);
sensor_adc = (float)(ADCW);
ntc_val = (10000 * sensor_adc) / (1023 - sensor_adc);
temperature = (706.6 * pow(ntc_val,-0.1541)) - 146;
trans_temp = (unsigned int)(temperature*10);
digit[3] = trans_temp/1000;
digit[2] = (trans_temp - digit[3]*1000)/100;
digit[1] = (trans_temp - digit[3]*1000 - digit[2]*100)/10;
digit[0] = (trans_temp - digit[3]*1000 - digit[2]*100 - digit[1]*10);
if(temperature>0){
PORTC=~SG_PORTC[1];
PORTA=SG_data[digit[3]];
delay_ms(1);
PORTC=~SG_PORTC[2];
PORTA=SG_data[digit[2]];
delay_ms(1);
PORTC=~SG_PORTC[3];
PORTA=SGD_data[digit[1]];
delay_ms(1);
PORTC=~SG_PORTC[4];
PORTA=SG_data[digit[0]];
delay_ms(1);
}
else if(temperature<0){
PORTC=~SG_PORTC[1];
PORTA=0x40;
delay_ms(1);
PORTC=~SG_PORTC[2];
PORTA=SG_data[digit[3]];
delay_ms(1);
PORTC=~SG_PORTC[3];
PORTA=SG_data[digit[2]];
delay_ms(1);
PORTC=~SG_PORTC[4];
PORTA=SG_data[digit[1]];
delay_ms(1);
}
else
PORTA=0x04;
}
void main(void)
{
init_port();
init_devices();
while (1)
{
temperature_function();
}
}
'프로그래밍 > AVR Atmega' 카테고리의 다른 글
Atemga 세그먼트 출력 (0) | 2017.10.19 |
---|---|
NTSF4 써미스터 온도센서 (0) | 2017.10.19 |
atmega128 인터럽트 (0) | 2017.10.19 |