Vous êtes sur la page 1sur 6

/****************************************************************

*
* Control Temperature - with PIC16f887
*
****************************************************************/
/****************************************************************
*
*
Module
: PIC_Ctrl_Temp.c
*
Description : Read ADC from AN4 channel
*
Set temperature by Keypad4x4
Use bulb and Fan-DC to Control temperature
******************************************
*
Tool
: HI-TECH PIC
*
Chip
: 16F887
*
History
: 10/11/2012
*
*
Author
: BK-B Team
*
Notes
: Code worked but it has not been perfect yet
*
****************************************************************/
/****************************************************************
* IMPORT
****************************************************************/
#include <htc.h>
#include <pic.h>
#include "lcd.h"
__CONFIG (MCLRE_ON&WDTE_OFF&FOSC_HS&CP_OFF&FCMEN_ON&LVP_OFF&BOR4V_BOR21V);
#define _XTAL_FREQ

4000000

/*****************************************************************
GLOBAL VARIABLE
******************************************************************/
char x;
int a, tram, chuc, donvi,i,j,T,Expect_temp,k, Current_Temp;
unsigned char Quet_Hang[4]={0x10,0x20,0x40,0x80};
unsigned char QuetHangCot[4][4]={{0x11,0x12,0x14,0x18},
{0X21,0X22,0X24,0X28},
{0X41,0X42,0X44,0X48},
{0X81,0X82,0X84,0X88}};
unsigned char GiaTri[4][4]={{7,4,1,0x00},
{8,5,2,0X00},
{9,6,3,0X00},
{0Xff,0,0X10,0X20}};
//0xff is enter - 0x10 is backspace
unsigned char Number[3];
/*****************************************************************
* ROUTINES
******************************************************************/
void port_init()
{

ANS4 = 1; // Select ADC AN1


ANSELH = 0;
TRISA = 0xff;
//RA3 - Input de doc ADC
TRISD = 0;
//PortD - OutPut LED
TRISC = 0;
TRISB=0b00001111;
PORTB = 0b11110000;
}
//---------------------------------------------------------------void adc_init()
{
// ADC conversion clock: = Fosc/8
// ADCS <1:0> = 00
Fosc/2
// ADCS <1:0> = 01
Fosc/8
// ADCS <1:0> = 10
Fosc/32
// ADCS <1:0> = 11
F_RC
ADCS1 = 0;
ADCS0 = 1;
// Voltage reference: Internal Vref
// VCFG <1:0> = 00
Internal Vref
// VCFG <1:0> = 11
External Vref
VCFG1 = 0;
VCFG0 = 0; ///vss
// Select Input chanel: AN3
// CHS <3:0> = 0000
Select
// CHS <3:0> = 0001
Select
// CHS <3:0> = 0010
Select
// ...
// CHS <3:0> = 1101
Select
CHS3 = 0;
CHS2 = 1;
CHS1 = 0;
CHS0 = 0;

AN0
AN1
AN2
AN13

// Result format: Right


// ADFM = 1 Right justified
// ADFM = 0 Left justified
ADFM = 1;
// Turn on ADC Module
ADON = 1;
// delay to wait for adc module init.
__delay_ms (1);
}
//---------------------------------------------------------------void int_init()
{
GIE = 1;
//Global Interrupt Enable
PEIE = 1;
//Peripheral Interrupt Enable
ADIE = 1;
//ADC Interrupt Enable
ADIF = 0;
//Clear ADC Interrupt Flag
}

//---------------------------------------------------------------void reset_expect_temp() // xoa expect_temp old de nhap expect_temp new


{
lcd_gotoxy(13,1);
lcd_putc(32);
lcd_gotoxy(14,1);
lcd_putc(32);
lcd_gotoxy(15,1);
lcd_putc(32);
}
//---------------------------------------------------------------void set_temp()
{
reset_expect_temp();
k=0;
//set temp on LCD //----------------------------------------------

while (k<=2)
{
rst_2:;
for (i=0;i<4;i++)
for (j=0;j<4;j++)
{
rst_1:;
PORTB=Quet_Hang[i];
if(PORTB==QuetHangCot[i][j])
{
__delay_ms(250);
T=GiaTri[i][j];
Number[k]=T;
if(k==0)
{
lcd_gotoxy(13,1);
lcd_putc(T+48);
}
if(k==1)
{
lcd_gotoxy(14,1);
lcd_putc(T+48);
}
if(k==2)
{
lcd_gotoxy(15,1);
lcd_putc(T+48);
}
k++;
}
if(PORTB==0b01001000)
{
__delay_ms(250);
k--;
if(k==0)
{
lcd_gotoxy(13,1);
lcd_putc(32);

}
if(k==1)
{
lcd_gotoxy(14,1);
lcd_putc(32);
}
goto rst_1;
}
}
}
}
// after setting temp on LCD
while (RB4=1&RB3==0) //waiting for active
{
RB4=0;
RB6=1;
if(RB6==1&RB3==1) //waiting for backspace
{
__delay_ms(250);
k--;
lcd_gotoxy(15,1);
lcd_putc(32);
goto rst_2;
}
RB4=1;
RB6=0;
RD7=0;
}
RD7=1;
}
//---------------------------------------------------------------void hex_bcd()
{
tram = a/100;
a = a%100;
chuc = a/10;
donvi = a%10;
}
//---------------------------------------------------------------void Xuat_LCD(tram,chuc,donvi)
{
tram=tram+48;
chuc=chuc+48;
donvi=donvi+48;
__delay_ms(20);
lcd_gotoxy(0,0);
lcd_puts("Current temp");
lcd_gotoxy(13,0);
lcd_putc(tram);
lcd_gotoxy(14,0);
lcd_putc(chuc);
lcd_putc(donvi);
lcd_gotoxy(0,1);

lcd_puts("Expect temp");
__delay_ms(20);
}
//---------------------------------------------------------------void interrupt isr()
{
int ADC_result;
if (PEIE && ADIE && ADIF)
{
ADIF = 0;
//Clear ADC Interrupt Flag
ADC_result = (ADRESH<<8)|ADRESL; //Read ADC result
Current_Temp=(ADC_result*5*100)/1024;
{
a=Current_Temp;
hex_bcd();
Xuat_LCD(tram,chuc,donvi);
}
}
}
/****************************************************************
* MAIN
****************************************************************/
/* -- void main
*
* Description
*
* Parameters
* Return
* Notes
*/

(void)

-----------------------------------------

: Module LCD, Configure port, adc module and enable interrupt


then wait for ADC interrupt
: --: --: ---

void main(void)
{
lcd_init();
port_init();
adc_init();
int_init();
while(1)
{
__delay_us(50);
//delay between 2 AD conversions
GO_DONE = 1;
//Set GODONE bit to start conversion
RB4=1;
if(RB3==1)
{
__delay_ms(250); // chong' rung phim'
RD6=0;
// set up - off fan+bulb
RD5=0;
set_temp();
__delay_ms(250);
}

Expect_temp=Number[0]*100+Number[1]*10+Number[2];
if(Expect_temp>a)
{
RD6=1; // On bulb
RD5=0; // off fan
}
else if (Expect_temp<a)
{
RD6=0; //off bulb
RD5=1; //on fan
}
else
{
RD5=0; //off bulb
RD6=0; //off fan
}
};
}
/****************************************************************
* END OF PIC_Ctrl_Temp.c
****************************************************************/

Vous aimerez peut-être aussi