2
2
#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock = 8MHz)
#include <lcd.c>
#define DS18B20_PIN PIN_B6
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;
}
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;
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);
}
}
}
}
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;
}
}
int16 b=0;
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
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");
}
}