0% found this document useful (0 votes)
27 views4 pages

Code Do An 3

The document contains code for a PIC microcontroller to measure temperature using an ADC and display the results on an LCD. It initializes ports and peripherals, reads the ADC, converts the value to Celsius, displays the temperature on the LCD, and indicates status using LEDs.

Uploaded by

nguyenquangduy93
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views4 pages

Code Do An 3

The document contains code for a PIC microcontroller to measure temperature using an ADC and display the results on an LCD. It initializes ports and peripherals, reads the ADC, converts the value to Celsius, displays the temperature on the LCD, and indicates status using LEDs.

Uploaded by

nguyenquangduy93
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <16f877a.

h>
#include <def_16f877a.h>
#device *=16 ADC =10
#fuses NOWDT,HS,NOPUT,NOPROTECT,NODEBUG,NOBROWNOUT,NOLVP,NOCPD,NOWRT
h chan cho PIC
#use delay(clock = 20000000)
#use FAST_IO(d)
#use FAST_IO(b)
#use FAST_IO(a)
#use FAST_IO(c)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=9)
#include "lcd_lib_4bit.c"
#INCLUDE<math.h>

//cau hin

////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////
int8 high,low;
int1 do_F;
float value,nhietdoF;
////////////////////////////////////////// Khai bo cc hm s? d?ng///////////////////
//////////////////////////////////////
void Chuyen_BCD();
void HienThi();
void BaoDong();
void LedTrangThai(int8 nhietdo);
/////////////////////////////////////////// Hm chuy?n d?i ///////////////////////
////////////////////////////////////////////////////
void Chuyen_BCD(int8 c)
{
low = c%10;
high= c/10;
low = low + 0x30;
high = high + 0x30;;
}
//////////////////////////////////////////////// Hm bo d?ng //////////////////////
///////////////////////////////////////////////////////
void BaoDong()
{
output_high(pin_C1);
delay_ms(10);
output_low(pin_C1);
delay_ms(400);
}
//////////////////////////////////////////////// Hm led tr?ng thi ///////////////
/////////////////////////////////////////////////////////////
void LedTrangThai()
{
PORTC=0XFF;
if(value<15)
{
output_low(pin_D2); //led vang sang
output_high(pin_C5);//led do tat
output_high(pin_D3);//led xanh tat

delay_ms(100);
}
else if(value>=15&&value<39)
{
output_low(pin_D3); //led xanh sang
output_high(pin_D2); //led vang tat
output_high(pin_C5); //led do tat
delay_ms(100);
}
else
{
output_low(pin_C5);
output_high(pin_D2);
output_high(pin_D3);
delay_ms(10);
BaoDong();
output_high(pin_C5);
delay_ms(10);
}
}
///////////////////////////////////////////////////////// Hm Chuy?n ?i Sang ? F ///
////////////////////////////////////////////////////////////////////
#INT_EXT
void ChuyenDoF()
{
if(do_F==1)
{
do_F=0;
//Do C
}
else
{
do_F=1;
//Do F
}
}

//////////////////////////////////////////////////////////////// Chuong Trnh Chnh


//////////////////////////////////////////////////////////////////////////
void main()
{
int8 i;
int16 tmp;
do_F =0 ;
set_tris_D(0);
set_tris_A(1);
set_tris_B(0x01);
set_tris_C(0);
PORTC=0XFF;
//Khoi tao ngat
enable_interrupts (INT_EXT);
ext_int_edge(H_TO_L);
enable_interrupts (GLOBAL);
//Khoi tao LCD

LCD_Init();
LCD_SetPosition(0);
printf(LCD_PutChar," NHIET DO PHONG ");
delay_ms(300);
//Khoi tao ADC
setup_ADC(ADC_CLOCK_INTERNAL);
setup_ADC_Ports(AN0);
set_ADC_Channel(0);
delay_ms(10);
//Nhi?t d? m?u
value = Read_ADC();
value = (value - 558.5)/2.048;
Chuyen_BCD((int8)value);
i=0;
while(true)
{
i++;
if(i==50)
{
value = Read_ADC();
value=(value - 558.5)/2.048;
if (do_F==1) nhietdoF = 1.8*value + 32;
Chuyen_BCD((int8)value);
LedTrangThai();
LCD_PutCmd(0xC0);
LCD_SetPosition(0xC1);
//kiem tra nhiet do co tren 100 do ko ?
if((int8)value>99)
{
tmp=100;
}
else
{
tmp=0;
}
if (do_F == 0)
{
if(tmp == 100)
{
if((int1)high == 0)
{
printf(LCD_PutChar,"
LCD_PutChar(low);
printf(LCD_PutChar,"
}
else if((int1)high==1)
{
printf(LCD_PutChar,"
LCD_PutChar(low);
printf(LCD_PutChar,"
}

10");

C");

=
C");

11");

}
else
{
if((int1)high == 0)
{
printf(LCD_PutChar," T = ");
LCD_PutChar(low);
//printf(LCD_PutChar," ");
//printf(LCD_PutChar," ");
printf(LCD_PutChar,"
C");
}
else
{
printf(LCD_PutChar," T = ");
LCD_PutChar(high);
LCD_PutChar(low);
//printf(LCD_PutChar," ");
printf(LCD_PutChar," C");
}
}
}
else
{
printf(LCD_PutChar," T = ");
printf(LCD_PutChar," %u ",(int8)nhietdoF);
printf(LCD_PutChar," F");
}
i=0;
}
}
}

You might also like