PIC Vietnam

Go Back   PIC Vietnam > Robotics > Cảm biến

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ảm biến Camera, siêu âm, hồng ngoại, gyro, la bàn...

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 30-06-2008, 07:50 AM   #1
andyhug
Đệ tử 1 túi
 
andyhug's Avatar
 
Tham gia ngày: Jun 2008
Nơi Cư Ngụ: Lê Duẩn - Hà Nội
Bài gửi: 23
:
giao tiep PIC16F877-DS1620......?

Các bác xem giúp em đoạn chương trình sau, em mô phỏng bằng Proteus7.2 SP2 mà nhiệt độ đo được hiển thị trên LCD LMB162A toàn là 127 độ? Các bác chỉ giáo cho em hàm DS1620_read. Thanks các bác! Quên, em dung trình biên dịch CCSC for Pic 4.049.

Code:
//*-----------------------------------------------------------------------------
//* Tac gia       : Pham Thanh Cong
//* Ngay viet     : 18/06/2008
//* Phan cung     : PIC16F877, LCD 2x16[5x7]
//* Chuong trinh  : do nhiet do dung DS1620 hien thi tren LCD LMB162A
//*=============================================================================*/
#include <16F877.h>
#include <stdlib.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP  // dat cau hinh cho PIC
#use delay(clock=4000000)
#byte PORTB    = 0x06
#define LCD PORTB
#bit RA2         = 0x05.2
#bit RA1         = 0x05.1
#bit RA0         = 0x05.0
#define RS  RA0            // chan chon thanh ghi =0 lenh hay =1 du lieu
#define RW  RA1            // =1 doc hay =0 ghi
#define E   RA2            // H-to-L de cho fep LCD
#bit RD1        = 0x08.1  // chan data cua DS1620
#bit RD2        = 0x08.2  // chan dong ho
#bit RD3        = 0x08.3  //
#define DQ     RD1         // =0 output, =1 input
#define CLK    RD2
#define RST    RD3

// khai bao cac ham su dung trong chuong trinh
void comandwrt(void);
void datawrt(void);
void lcd_gotoxy(byte x, byte y);
void main(void);
void DS1620_InitTemp(void);
unsigned int DS1620_GetTemp(void);
void DS1620_Write(unsigned char Data);
int DS1620_Read(void);


const unsigned char dongchu[]="Nhiet do:";
int i = 0;

//xxxxxxxxxxxxxxxxxxxxxxxxxxx chuong trinh LCD xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//*Ham yeu cau goi lenh dieu khien LCD*/
void comandwrt(void)
{
 RS = 0;
 RW = 0;
 E  = 1;
 E  = 0;
 delay_ms(1);
}
//*Ham yeu cau goi du lieu hien thi len LCD*/
void datawrt(void)
{
 RS = 1;
 RW = 0;
 E  = 1;
 E  = 0;
 delay_ms(1);
}
void lcd_gotoxy(int x, int y)
{  int address;
   switch(y)
      {
         case 1:  address=0;
                  address+=x-1;
                  LCD = (0x80 + address);
                  break;
         case 2:  address=0;
                  address+=x-1;
                  LCD = (0xC0 + address);
                  break;
         default : break; 
      }
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxx chuong trinh DS1620 xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void DS1620_InitTemp(void)
{
// Sets up the config register to poll continuously for Temp.
// DS1620 also starts taking temperature readings.
      DS1620_Write(0x0C); // dat cau hinh Config
      DS1620_Write(0x00); // che do chuyen doi lien tuc
} // End DS1620_InitTemp


unsigned int DS1620_GetTemp(void)
{
// Returns hex value of last temperature reading.
unsigned int16 Data;
      DS1620_Write(0xAA);     // doc nhiet do 
      DS1620_Write(0xA2);
      Data = DS1620_Read();   // nhiet do luu vao bien data
      if(bit_test(Data,8))
      {
         Data = -Data / 2;
      }
      Data = Data / 2;
return(Data);
} // End DS1620_GetTemp


void DS1620_Write(unsigned char Data)
{
   unsigned char i;
   output_low(pin_D1);      // DQ=0, thiet lap DQ la cong ra
   output_high(pin_D3);     // RST=1, activiate DS1620
   for (i=1; i<=8; i++) 
   {
      output_low(pin_D2);     // CLK=0
      DQ = (Data && 1);       // DQ=data AND 1, tuc la xuat data ra chan RD1 cua PIC
      output_high(pin_D2);    // CLK=1
      delay_us(4);     
      Data = Data >> 1; // dich fai data di 1 bit
   }
   output_low(pin_D3);        // RST=0, deactiviate DS1620
   delay_us(10);
}   

int DS1620_Read(void)
{
   int16 Data;
   unsigned char i;
   output_high(pin_D3); // RST = 1
   DQ = 1; // DQ = 1 : input
   DS1620_Write(0xAA);  
   for (i=1; i<=9; i++) 
   {
      output_low(pin_D2);  // CLK = 0
      Data |= DQ; // Data = DQ  
      output_high(pin_D2); // CLK = 1  
      delay_us(10);
      Data = Data << 1;
   }   
   output_low(pin_D3);  // RST = 0
   return(Data);
}

void display(int temp)
{
   char string[8];
   lcd_gotoxy(5,2);
   comandwrt();
   delay_us(2);
   itoa(temp,10,string);
   for(i=0;i<=8;i++)
   {
      LCD = string[i]; // xuat nhiet do ra o dang ki tu so
      datawrt();
      delay_us(10);
   }
   delay_us(10);
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx *Ham main xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
void main(void)
{
   int data;
   set_tris_B(0);   // out put
   set_tris_A(0);
   delay_ms(100);   //   Tao tre 100ms cho LCD khoi dong

   LCD = 0x38;      //   Hai hang, ma tran dot 5*7, 8 bit interface
   comandwrt();
   LCD = 0x0C;      //   Bat hien thi, tat con tro
   comandwrt();
   
   lcd_gotoxy(3,1);      //   Vi tri cot 3, hang 1
   comandwrt();
   delay_us(2);
   for (i=0;i<=8;i++)
   {
      LCD = dongchu[i]; // xuat chu ra LCD
      datawrt();
      delay_us(10);
   }
   DS1620_InitTemp();
   while(1)
   {
      DS1620_Write(0xEE);           // bat dau chuyen doi lien tuc
      delay_ms(1);                  // tre 1000 us cho qua trinh chuyen doi
      DS1620_write(0x22);           // dung chuyen doi
      data  = DS1620_GetTemp();     // lay du lieu
      delay_us(1000);
      display(data);
   }
 
}
__________________
Sống ở trên đời cần phải có một tấm lòng để gió cuốn đi!
xxxxxxxxxxxxxxxxxxx Trịnh Công Sơn xxxxxxxxxxxxxxxxxxx

thay đổi nội dung bởi: namqn, 26-07-2008 lúc 02:07 AM. Lý do: định dạng code
andyhug 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à 10:59 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