PIC Vietnam

Go Back   PIC Vietnam > Microchip PIC > Cơ bản về vi điều khiển và PIC

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Bài Trong Ngày Vi điều khiển

Cơ bản về vi điều khiển và PIC Những bài hướng dẫn cơ bản nhất để làm quen với vi điều khiển PIC

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 26-05-2012, 12:30 PM   #12
victory_1410
Nhập môn đệ tử
 
Tham gia ngày: May 2010
Bài gửi: 3
:
Trích:
Nguyên văn bởi tdm View Post
thôi thì để tôi chèn giúp bạn vài dòng lệnh để code hoàn chỉnh vậy.
Code:
#include <16F877A.h>
#include <def_877a.h>
#device *=16 adc=10
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT,NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
#include <lcd_lib_4bit.c> // Thu vien ham cho LCD

int8 low,high,min,max,i,sida=0xaa;
int1 do_F;
float value;
#define nut1 RC2
#define nut2 RC3
#define nut3 RC4
#INT_EXT
void test()
   {
   if (do_F == 1) do_F=0;
   else do_F=1;
   }
   void banphim(void);
   void convert_bcd(int8 x);
   
//-----------------------------------------------------------------
void main()
   {
   min =0;      //nhiet do mim 
   max =40;    //nhiet do max 
   do_F =0 ;
   i = 10 ;
   trisa = 0xFF;
   trisb = 0x01;
   trisc = 0b00011100;
   output_low(pin_C0);
   output_low(pin_C1);
    //==== Khoi tao cho ngat ngoai===============================================
   enable_interrupts (INT_EXT);
   ext_int_edge(H_TO_L);
   enable_interrupts (GLOBAL);
   //=========== Khoi tao che do cho bo ADC=====================================
   setup_adc_ports(AN0);
   setup_adc(ADC_CLOCK_INTERNAL);
   LCD_init();
   LCD_putcmd(0x01);
   LCD_putchar("do_nhiet_do:");
   LCD_putcmd(0xC0);
   LCD_putchar("cho ty nhe ...");
   delay_us(10);// Lay mau nhiet do lan dau tien
   value=(float)read_adc();
   value = (value - 558.5)/2.048; // For 5V supply
   // value = (value - 754.8)/2.048; // For 3.7V Supply
   // value = (value - 698.2)/2.048; // For 4V supply
   convert_bcd(value); // Tach so tram, chuc, donvi de hien thi len LED 7
   delay_ms(100);
   LCD_putcmd(0xC0);
   LCD_putchar("Ok               ");
   delay_ms(100);
   while(1)
      {
      banphim();
      if (i==10)
         {
          if(sida==0xaa)
          {
         value = read_adc();
         value=(value-558.5)/2.048;
         
         if (do_F==1) 
            value=1.8*value+32;
         convert_bcd(value);

         LCD_putcmd(0xC0);
         printf(LCD_putchar,"Nhiet do la:");
         LCD_putchar(high); LCD_putchar(low);
         
         if (do_F==0) 
            printf(LCD_putchar," C");
         else 
            printf(LCD_putchar," F");
         i=0;
          }
          else
          {
          }
         }//
        
   if (value <= max & min <=value) 
       {
          output_high(pin_C1);
       }
        else output_low(pin_C1);
   if(value > max ||value < min)
          output_high(pin_C0);
       else output_low(pin_C0); 
      i++;
      }
   }
//The End=======================================================================
void convert_bcd(int8 x)
   {
   low=x%10;      //chia lay phan du, so hang don vi
   high=x/10;     //tach hang tram va hang chuc
   low = low + 0x30;
   high = high + 0x30;
   }
void banphim()
{ 
if (nut1 == 0)
{
sida=~sida;
if(sida!=0xaa)
{
LCD_putcmd(0x01);
LCD_putchar("nhap nhiet do:");
LCD_putcmd(0xC0);
printf(LCD_putchar,"gia tri MAX:%d",max);
}
 else
 {
 LCD_putcmd(0x01);
 LCD_putchar("do_nhiet_do:");
 value = read_adc();
         value=(value-558.5)/2.048;
         
         if (do_F==1) 
            value=1.8*value+32;
         convert_bcd(value);

         LCD_putcmd(0xC0);
         printf(LCD_putchar,"Nhiet do la:");
         LCD_putchar(high); LCD_putchar(low);
         
         if (do_F==0) 
            printf(LCD_putchar," C");
         else 
            printf(LCD_putchar," F");
 }
while(nut1 == 0)
{
}
}
else if (nut3 == 0)
{
  if(sida!=0xaa)
  {
  max = max - 1;
  LCD_putcmd(0xC0);
  printf(LCD_putchar,"gia tri MAX:%d",max);
  }
else
   {
   }
   while(nut3 == 0)//cho nha phim
   {
    }
   delay_ms(10);//chong nay phim
}
else if (nut2 == 0)
{
  if(sida!=0xaa)
  {
  max = max +1;
  LCD_putcmd(0xC0);
  printf(LCD_putchar,"gia tri MAX:%d",max);
  }
   else
   {
   }
   while(nut2 == 0)//cho nha phim
   {
    }
   delay_ms(10);//chong nay phim
}
else
{
}
}
http://www.youtube.com/watch?v=nvtwwuzRkZI
cảm ơn anh Minh nhiều ! em mới tìm hiểu Pic có được những bài như thế này rất hữu ích với em!
Mong nhận được những bài hay từ anh!
victory_1410 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 


Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


Múi giờ GMT. Hiện tại là 06:29 PM.


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam