Wednesday, January 14, 2015

Điều khiển quạt-lò sưởi-giao diện Glcd-Pic16f887

Posted by dientudieukhien at 12:01 PM 0 Comments
Yêu cầu: ta có 2 thiết bị là quạt gió (motor) và lò sưởi (bóng đèn). Khi nhiệt độ của phòng trên mức nhiệt độ đặt trước  ( tem Ref) thì quạt gió hoạt động, lò sưởi tắt. Ngược lại thì quạt tắt và lò sưởi được mở. Giao diện người dùng được hiển thị trên GLCD. Mức nhiệt độ Ref được điều chỉnh qua 2 button được kết nối qua RB0, RB1, LM35 kết nối RA0, quạt kết nối RB6, lò sưởi RB7, GLCD kết nối với PORTD.

Sơ đồ nguyên lý vẽ trên protues:
Điều khiển quạt gió, lò sưởi, giao diện điều khiển GLCD, pic 16f887
Điều khiển quạt và lò sưởi
Code:
/*
This project uses LM35 sensor that senses the temperature and then turns
on Fan if temperature is high and turns on Heater if temperature is low.
The Graphic LCD displays temperature value in text and graphically.
User can select reference number using buttons. This reference
number is compared with the temperature to control Fan or Heater.
The reference number is stored in EEPROM.
Temperature sensor is connected to AN0. And Vref 1.5V connected to AN3
Author: Sameer Semmoor
Shared by http://dientudieukhien.net
*/
// Glcd module connections
char GLCD_DataPort at PORTD;

sbit GLCD_CS1 at RC0_bit;
sbit GLCD_CS2 at RC1_bit;
sbit GLCD_RS  at RB3_bit;
sbit GLCD_RW  at RB4_bit;
sbit GLCD_EN  at RB5_bit;
sbit GLCD_RST at RE2_bit;

sbit GLCD_CS1_Direction at TRISC0_bit;
sbit GLCD_CS2_Direction at TRISC1_bit;
sbit GLCD_RS_Direction  at TRISB3_bit;
sbit GLCD_RW_Direction  at TRISB4_bit;
sbit GLCD_EN_Direction  at TRISB5_bit;
sbit GLCD_RST_Direction at TRISE2_bit;
// End Glcd module connections


unsigned int adc_value; //stores analog values converted to digital

float TempC;  //stores temperature value
float _TempC=0;  //copy temperature value
unsigned short comp_value; //contains temperature value without a decimal point
char TempC_text[15];

unsigned short cnt; //stores the counter value
char cnt_txt[4];
bit temp_changed_flag; //will be set if temperature value changed
bit increment_switch; //will be set if increment switch was prseed
bit decrement_switch; //will be set if decrement switch was prseed



//configuration function--------------------------------------------------------
void _init()
{
  ADC_Init();  // Initialize ADC module with default settings
  ADCON1 = 0b00000101;

  TRISA.B0 = 1; // PORT A0 as input

  //counter switches as input
  TRISB.B0 = 1; //increment
  TRISB.B1 = 1; //decrement

  TRISB.B6 = 0; PORTB.B6 = 0;//FAN as output
  TRISB.B7 = 0; PORTB.B7 = 0;//HEATER as output

  Glcd_Init();  // Initialize GLCD
  Glcd_Fill(0x00);  // Clear GLCD

  Glcd_Write_Text("Temperature Meter", 0, 0, 1);
}
//end configuration function----------------------------------------------------

//draw circle and rectangle-----------------------------------------------------
void draw()
{
  Glcd_Circle(116, 52, 11, 1);
  Glcd_Rectangle(112, 42, 120, 2, 1);
}
//end draw circle and rectangle-------------------------------------------------

//get temperature---------------------------------------------------------------
void get_adc()
{
   adc_value = ADC_Read(0); //read analog from AN0

   //calculate temperature
   TempC = adc_value * 1.5;

   TempC = TempC / 1024;

   TempC = TempC * 100;

   if(TempC != _TempC) //display temperature value if temperature changed
   {
     temp_changed_flag = 1; //set this flag because temperature value changed
     _TempC = TempC; //copyt the new temperature value
     comp_value = TempC;  //copy tmeperature value without decimal point

     floattostr(TempC,TempC_text);  //convert temperature to string

     Glcd_Write_Text("Temp: ", 0, 2, 1);
     Glcd_Write_Text(TempC_text, 30, 2, 1);  //display temeperature
     if(TempC < 1){ Glcd_Write_Text("     ", 82, 2, 1);} //if temperature value is below 1 clear -1 position
     Glcd_Write_Text("C", 78, 2, 1); //display temperature measurement unit
   }
}
//end get temperature-----------------------------------------------------------

//draw the progressbar----------------------------------------------------------
void progress_bar()
{
  //update progress bar circle
  if(tempC > 0  && tempC < 11){Glcd_Box(113, 3, 119, 41, 0);Glcd_Circle_Fill(116, 52, 10, 0);Glcd_Circle_Fill(116, 52, 2, 1);}
  if(tempC > 10  && tempC < 21){Glcd_Box(113, 3, 119, 41, 0);Glcd_Circle_Fill(116, 52, 10, 0);Glcd_Circle_Fill(116, 52, 4, 1);}
  if(tempC > 20  && tempC < 31){Glcd_Box(113, 3, 119, 41, 0);Glcd_Circle_Fill(116, 52, 10, 0);Glcd_Circle_Fill(116, 52, 6, 1);}
  if(tempC > 30  && tempC < 41){Glcd_Box(113, 3, 119, 41, 0);Glcd_Circle_Fill(116, 52, 10, 0);Glcd_Circle_Fill(116, 52, 8, 1);}
  if(tempC > 40  && tempC < 51){Glcd_Box(113, 3, 119, 41, 0);Glcd_Circle_Fill(116, 52, 10, 0);Glcd_Circle_Fill(116, 52, 10, 1);}

  //update progress bar rectangle
  if(tempC > 50  && tempC < 61){Glcd_Box(113, 3, 119, 41, 0);Glcd_Box(113, 39, 119, 41, 1);}
  if(tempC > 60  && tempC < 71){Glcd_Box(113, 3, 119, 41, 0);Glcd_Box(113, 35, 119, 41, 1);}
  if(tempC > 70  && tempC < 81){Glcd_Box(113, 3, 119, 41, 0);Glcd_Box(113, 30, 119, 41, 1);}
  if(tempC > 80  && tempC < 91){Glcd_Box(113, 3, 119, 41, 0);Glcd_Box(113, 25, 119, 41, 1);}
  if(tempC > 90  && tempC < 101){Glcd_Box(113, 3, 119, 41, 0);Glcd_Box(113, 20, 119, 41, 1);}
  if(tempC > 100  && tempC < 111){Glcd_Box(113, 3, 119, 41, 0);Glcd_Box(113, 15, 119, 41, 1);}
  if(tempC > 110  && tempC < 121){Glcd_Box(113, 3, 119, 41, 0);Glcd_Box(113, 10, 119, 41, 1);}
  if(tempC > 120  && tempC < 131){Glcd_Box(113, 3, 119, 41, 0);Glcd_Box(113, 5, 119, 41, 1);}
  if(tempC > 130  && tempC < 141){Glcd_Box(113, 3, 119, 41, 0);Glcd_Box(113, 4, 119, 41, 1);}
  if(tempC > 140  && tempC < 151){Glcd_Box(113, 3, 119, 41, 0);Glcd_Box(113, 3, 119, 41, 1);}
}
//end draw the progressbar------------------------------------------------------

//get counter value-------------------------------------------------------------
void counter()
{
   if(increment_switch == 1) //if increment swictch was pressed
   {
    cnt++;   //increment counter
    if(cnt > 148){cnt = 150;}  //counter should not exceed max
    eeprom_write(0,cnt); //store counter in eeprom
    delay_ms(300);
   }
   if(decrement_switch == 1) //if decrement swictch was pressed
   {
    cnt--; //decrement counter
    if(cnt < 1){cnt = 1;}  //counter should not go blow min
    eeprom_write(0,cnt); //store counter in eeprom
    delay_ms(300);
   }

   bytetostr(cnt,cnt_txt);  //convert counter value to string
   Glcd_Write_Text("counter: ", 0, 4, 1);
   Glcd_Write_Text(cnt_txt, 47, 4, 1);//display counter
}
//end get counter value---------------------------------------------------------


//control FAN & HEATER----------------------------------------------------------
void control_fan_heater()
{
   if(comp_value == cnt) //if weather is normal
   {
     portb.b6 = 0; //turn off fan
     portb.b7 = 0; //turn off heater
     Glcd_Write_Text("             ", 0, 6, 1); //clear text position
   }
   else if(comp_value < cnt) //if weather is cold
   {
     portb.b6 = 0; //turn off fan
     portb.b7 = 1; //turn on heater
     Glcd_Write_Text("             ", 0, 6, 1);  //clear text position
     Glcd_Write_Text("Heater ON ", 0, 6, 1); //display Heater status
   }
   else //if weather is hot
   {
     portb.b7 = 0; //turn off heater
     portb.b6 = 1; //turn on fan
     Glcd_Write_Text("             ", 0, 6, 1); //clear text position
     Glcd_Write_Text("Fan ON ", 0, 6, 1); //display fan status
   }
}
//end control FAN & HEATER------------------------------------------------------


//main program------------------------------------------------------------------
void main()
{
  _init(); //start with the configurations

  draw(); //draw the circle and rectangle

  cnt = eeprom_read(0); //get counter from eeprom
  counter(); //display counter at the beginning

  do
  {
    get_adc(); //read ADC value

    if(portb.b0 == 0){increment_switch = 1; counter();} //if increment switch was pressed set its flag and increment counter
    if(portb.b1 == 0){decrement_switch = 1; counter();} //if deccrement switch was pressed set its flag and decrement counter

    //enter here if temperature changed or any of the switches was pressed
    if(temp_changed_flag == 1 || increment_switch == 1 || decrement_switch == 1)
    {
      progress_bar();  //update progress bar
      control_fan_heater(); //control FAN or HEATER

      //reset the flags
      temp_changed_flag =0;
      increment_switch = 0;
      decrement_switch = 0;
    }

  }while(1);
}
//end main program--------------------------------------------------------------


Click vào link tải ở trên đợi 5 giây, ấn "Bỏ qua quảng cáo". Click the link above, wait for 5s and click button "Skip Ad"

Những bài viết cùng chủ đề:

Chia sẽ bài viết này

Cập nhật tin bài mới

Subscribe địa chỉ mail để nhận tin nhanh chóng. Chúng tôi sẽ giữ bí mật địa chỉ mail của bạn.

0 nhận xét:

back to top