0% found this document useful (0 votes)
23 views8 pages

2

Uploaded by

Huy Nguyễn
Copyright
© © All Rights Reserved
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)
23 views8 pages

2

Uploaded by

Huy Nguyễn
Copyright
© © All Rights Reserved
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/ 8

#define LCD_RS_PIN PIN_D0

#define LCD_RW_PIN PIN_D1


#define LCD_ENABLE_PIN PIN_D2
#define LCD_DATA4 PIN_D3
#define LCD_DATA5 PIN_D4
#define LCD_DATA6 PIN_D5
#define LCD_DATA7 PIN_D6
//End LCD module connections

#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock = 8MHz)
#include <lcd.c>
#define DS18B20_PIN PIN_B6

///////////////Function lay tin hieu tu Ds18b20

signed int16 raw_temp;


float temp;
int1 ds18b20_start(){
output_low(DS18B20_PIN); // Send reset pulse to the
DS18B20 sensor
output_drive(DS18B20_PIN); // Configure DS18B20_PIN pin as
output
delay_us(500); // Wait 500 us
output_float(DS18B20_PIN); // Configure DS18B20_PIN pin as
input
delay_us(100); //wait to read the DS18B20
sensor response
if (!input(DS18B20_PIN)) {
delay_us(400); // Wait 400 us
return TRUE; // DS18B20 sensor is present
}
return FALSE;
}

void ds18b20_write_bit(int1 value){


output_low(DS18B20_PIN);
output_drive(DS18B20_PIN); // Configure DS18B20_PIN pin as
output
delay_us(2); // Wait 2 us
output_bit(DS18B20_PIN, value);
delay_us(80); // Wait 80 us
output_float(DS18B20_PIN); // Configure DS18B20_PIN pin as
input
delay_us(2); // Wait 2 us
}
void ds18b20_write_byte(int8 value){
int8 i;
for(i = 0; i < 8; i++)
ds18b20_write_bit(bit_test(value, i));
}

int1 ds18b20_read_bit(void) {
int1 value;
output_low(DS18B20_PIN);
output_drive(DS18B20_PIN); // Configure DS18B20_PIN pin as
output
delay_us(2);
output_float(DS18B20_PIN); // Configure DS18B20_PIN pin as
input
delay_us(5); // Wait 5 us
value = input(DS18B20_PIN);
delay_us(100); // Wait 100 us
return value;
}

int8 ds18b20_read_byte(void) {
int8 i, value = 0;
int16 value_firing=3000; for(i = 0; i < 8; i++)
shift_right(&value, 1, ds18b20_read_bit());
return value;
}

int1 ds18b20_read(int16 *raw_temp_value) {


if (!ds18b20_start()) // Send start pulse
return FALSE;
ds18b20_write_byte(0xCC); // Send skip ROM command
ds18b20_write_byte(0x44); // Send start conversion command
while(ds18b20_read_byte() == 0); // Wait for conversion complete
if (!ds18b20_start()) // Send start pulse
return FALSE; // Return 0 if error
ds18b20_write_byte(0xCC); // Send skip ROM command
ds18b20_write_byte(0xBE); // Send read command
*raw_temp_value = ds18b20_read_byte(); // Read temperature LSB byte and store
it on raw_temp_value LSB byte
*raw_temp_value |= (int16)(ds18b20_read_byte()) << 8; // Read temperature MSB
byte and store it on raw_temp_value MSB byte
return TRUE; // OK --> return 1
}

//////////////////// Khai bao bien


char message2[] = "Set :00.0 C 00";
char message3[] = " P I D ";
char message4[] = "00.0 00.0 00.0 ";
signed int16 temp_read,PID_error;
int16 temp_set=300;
int16 a=200;
int16 *value_firing=&a;
int16 b=0,d_tr=0,e=0;
int8 c=22;
int8 *days_left=&c;

float P=905;
float I=606;
float D=303;

//////////// chuyen doi va hien thi nhiet do,ngay can ap con lai,thong so P I D
void digit_Temp(int16 num){
int8 a,b,c;
a = (num / 100) % 10 + 48;
b = (num / 10) % 10 + 48;
c = num % 10 + 48;
message2[5]=a;
message2[6]=b;
message2[8]=c;

}
void digit_days(int8 num){
int8 a,b;

a = (num / 10) % 10 + 48;


b = num % 10 + 48;
message2[12]=a;
message2[13]=b;

void digit_P(int16 num){


int8 a,b,c;
a = (num / 100) % 10 + 48;
b = (num / 10) % 10 + 48;
c = num % 10 + 48;
message4[0]=a;
message4[1]=b;
message4[3]=c;
}
void digit_I(int16 num){
int8 a,b,c;
a = (num / 100) % 10 + 48;
b = (num / 10) % 10 + 48;
c = num % 10 + 48;
message4[5]=a;
message4[6]=b;
message4[8]=c;
}
void digit_D(int16 num){
int8 a,b,c;
a = (num / 100) % 10 + 48;
b = (num / 10) % 10 + 48;
c = num % 10 + 48;
message4[10]=a;
message4[11]=b;
message4[13]=c;
}

//////////// dieu chinh va chuyen do nhiet do set


void adjust(){
output_float(PIN_B2);
output_float(PIN_B3);
output_float(PIN_B4);
if(input(PIN_B2)==1)
{
if(input(PIN_B3)==0){
delay_ms(200);
if(input(PIN_B3)==1){

temp_set=temp_set + 1;
digit_temp(temp_set);
}
else {
temp_set=temp_set+10;
digit_temp(temp_set);
}
}

if(input(PIN_B4)==0){
delay_ms(200);
if(input(PIN_B4)==1){
temp_set=temp_set - 1;
digit_temp(temp_set);
}
else {
temp_set=temp_set - 10;
digit_temp(temp_set);
}
}
}
}

/////////// dieu chinh va chuyen doi thong so P I D


void adjust_PID(){
if(input(PIN_B2)==0)
{
lcd_putc('\f');
while(input(PIN_B2)==0){
if(input(PIN_B2)==1)
break;
lcd_gotoxy(1, 1); // Go to column 1 row 1
printf(lcd_putc, message3);
while(TRUE){

if(input(PIN_B3)==0){
delay_ms(200);
if(input(PIN_B3)==1){

P=P+1;
digit_P(P);
}
else {
P=P+10;
digit_P(P);
}
}
if(input(PIN_B4)==0){
delay_ms(200);
if(input(PIN_B4)==1){
P=P-1;
digit_P(P);
}
else {
P=P-10;
digit_P(P);
}
}
lcd_gotoxy(1, 2);
printf(lcd_putc, message4);
if(input(PIN_B5)==0){
delay_ms(200);
if(input(PIN_B5)==1){
break;
delay_us(100);
}
}
}
while(TRUE){

if(input(PIN_B3)==0){
delay_ms(200);
if(input(PIN_B3)==1){

I=I + 1;
digit_I(I);
}
else {
I=I+10;
digit_I(I);
}
}

if(input(PIN_B4)==0){
delay_ms(200);
if(input(PIN_B4)==1){
I=I-1;
digit_I(I);
}
else {
I=I - 10;
digit_I(I);
}
}
lcd_gotoxy(1, 2);
printf(lcd_putc, message4);

if(input(PIN_B5)==0){
delay_ms(200);
if(input(PIN_B5)==1){
break;
delay_us(100);
}
}
}
while(TRUE){
{
if(input(PIN_B3)==0){
delay_ms(200);
if(input(PIN_B3)==1){

D=D + 1;
digit_D(D);
}
else {
D=D+10;
digit_D(D);
}
}
if(input(PIN_B4)==0){
delay_ms(200);
if(input(PIN_B4)==1){
D=D-1;
digit_D(D);
}
else {
D=D - 10;
digit_D(D);
}
}
lcd_gotoxy(1, 2); // Go to column 1 row 1
printf(lcd_putc, message4);
if(input(PIN_B5)==0){
delay_ms(200);
if(input(PIN_B5)==1){
break;
delay_us(100);
delay_ms(1000);
}
}
if(input(PIN_B2)==1)
break;
}

}
}
}
}

////////// ngat ngoai nhan tin hieu ngat khi dien ap AC=0 thi diem nay lay lam
moc de mo goc phat triac
#INT_EXT
void EXT_isr()
{
output_drive(PIN_B1);
delay_us(*value_firing);
output_high(PIN_B1);
delay_us(200);
output_low(PIN_B1);
}

///////// ngat timer 1 thoi gian mo dong co va so ngay con phai ap trung
#INT_TIMER1
void Ngat_TIMER1()
{
b=b+1;

d_tr=d_tr+1;
if(d_tr>599){
output_high(PIN_B7);
d_tr=0;

}
if(d_tr>20&&d_tr<22){
output_low(PIN_B7);
}
if(b>100){
c=c-1;
b=0;
}
}

////////////// ham main


void main(){
float P=905;
float I=606;
float D=303;
int16 value_firing;
int PID_p = 0; int PID_i = 0; int PID_d = 0;
int16 PID_error=0,PID_value=0;
int16 timePrev,time,elapsedTime,previous_error;
int16 error_prev=0;

int16 b=0;

lcd_init(); // Initialize LCD module


lcd_putc('\f'); // Clear LCD

output_drive(PIN_B1);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1); //khoi tao timer 1 dao dong trong
8MHZ che do bo chia = 1
enable_interrupts(INT_TIMER1); //khoi tao ngat timer1
enable_interrupts (INT_EXT); //khoi tao ngat ngoai
enable_interrupts (INT_EXT_L2H); //ngat khi tin hieu tu muc low den
muc high

enable_interrupts (GLOBAL); //khoi tao ngat cuc bo


output_float(PIN_B5);
output_float(PIN_B6);
output_drive(PIN_B7);

set_timer1(0); //dat gia tri timer1 = 0

while(TRUE){ //ham lap lien tuc doc gia tri tu


cam bien va hien thi

adjust();
adjust_PID();
PID_error = temp_set - temp_read ;
//Calculate the P value
PID_p = P/10 * PID_error;
//Calculate the I value in a range on +-3
PID_i = PID_i + (I/10 * PID_error);
timePrev = time ; // the previous time is stored
before the actual time read
time = get_timer1(); // actual time read
elapsedTime = (time - timePrev)/1000000;
//Now we can calculate the D calue
PID_d = 0.01*(D/10)*((PID_error - previous_error)/elapsedTime);
//Final total PID value is the sum of P + I + D
PID_value = PID_p + PID_i + PID_d;
if(PID_value < 200)
{ PID_value = 200; }
if(PID_value > 9800)
{ PID_value = 9800; }
a=3000;

if(ds18b20_read(&raw_temp))
{
temp = (float)raw_temp / 16; // Convert temperature raw value
into degree Celsius (temp in °C = raw/16)
lcd_gotoxy(6, 1); // Go to column 5 row 2
printf(lcd_putc, "%0.1f", temp);
lcd_putc(223); // Print degree symbol ( ° )
lcd_putc("C "); // Print 'C '
}
digit_days(*days_left);
lcd_gotoxy(1, 1); // Go to column 3 row 1
printf(lcd_putc, "Temp:");
lcd_gotoxy(1, 2); // Go to column 1 row 1
printf(lcd_putc, message2);
lcd_gotoxy(13, 1); // Go to column 3 row 1
printf(lcd_putc, "DAYS");

}
}

You might also like