PIC Vietnam

Go Back   PIC Vietnam > Microchip PIC > Analog - Bộ nhớ - Bảo mật - Cảm biến nhiệt độ

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

Analog - Bộ nhớ - Bảo mật - Cảm biến nhiệt độ Thảo luận việc sử dụng và thay thế các sản phẩm Analog, Memory, KEELOG và cảm biến nhiệt độ của Microchip cho các thiết kế

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 08-03-2009, 10:41 AM   #1
notbadday
Nhập môn đệ tử
 
Tham gia ngày: Oct 2007
Bài gửi: 5
:
Question Cần giúp đỡ về giao tiếp 1-wire với ds1820

Em đang viết 1 đoạn chương trình để hiển thị nhiệt độ lên LCD dùng cảm biến ds1820 và pic6f877a. Em đang gặp rắc rối trong việc giao tiếp với ds1820. Cụ thể là không hiện được nhiệt độ. Mấy anh xem giúp,có gì góp ý thì em cảm ơn rất nhiều.
Đây là code CCS, có mượn đoạn code phần LCD của bác nhh (hy vọng bác ko phản đối )
Code:
#include <16F877A.h>
#include <DEFS_16F877A.h>
#include <stdlib.h>

#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#define E  RD2
#define RS RD0
#define RW RD1
#define DQ RC0
#DEFINE TRISC 0x07
#byte lcd_data = 0x06   // Dia chi PORTB

/* Khai bao nguyen mau cac ham su dung */
byte lcd_read_byte();
void lcd_send_byte( byte address, byte n );
void lcd_init();
void lcd_gotoxy( byte x, byte y);
void lcd_putc( char c);
void lcd_refresh();
unsigned char ow_reset(void);
unsigned char read_bit(void);
void write_bit(char bival);
unsigned char read_byte(void);
void write_byte(char val);

/* Doc mot byte tu LCD */
byte lcd_read_byte()
{
   byte read_byte;
   set_tris_B(0xFF);       // PORTB = input
   RW = 1;
   delay_cycles(1);
   E  = 1;
   delay_cycles(1);
   read_byte = lcd_data;
   E  = 0;
   set_tris_B(0x00);       // PORTB = output
   return(read_byte);
}

/* Goi 1byte den LCD */
void lcd_send_byte( byte address, byte n )
{
   RS = 0;
   while ( bit_test(lcd_read_byte(),7) ) ;
   RS = address;
   delay_cycles(1);
   RW = 0;
   delay_cycles(1);
   E  = 0;
   lcd_data = n;
   delay_cycles(1);
   E = 1;
   delay_us(2);
   E = 0;
}

/* Khoi tao ban dau cho LCD */
void lcd_init()
{
    byte const lcd_init_string[4] = {0x38, 0x0C, 1 , 6};
    byte i;
    set_tris_B(0x00);
    RS = 0;
    RW = 0;
    E  = 0;
    delay_ms(15);
    for(i=1;i<=3;++i)
       {
         lcd_data = 3;
         delay_cycles(1);
         E = 1;
         delay_us(2);
         E = 0;
         delay_ms(5);
       }
    lcd_data = 2;
    delay_cycles(1);
    E = 1;
    delay_us(2);
    E = 0;
    delay_ms(5);
    for(i=0;i<=3;++i)
      {
         lcd_send_byte(0,lcd_init_string[i]);
      }
}

/* Nhay den vi tri (x,y) tren LCD,nhay nham y se bao loi */
void lcd_gotoxy( byte x, byte y)
{
   byte address;
   switch(y)
      {
         case 1:  address=0;
                  address+=x-1;
                  lcd_send_byte(0,0x80|address);
                  break;
         case 2:  address=0x40;
                  address+=x-1;
                  lcd_send_byte(0,0x80|address);
                  break;
         default :lcd_init();
                  lcd_putc("ERROR Y POSITION");
                  while(true); // Dung tai day!
      }
}

/* Hien thi ki tu hoac chuoi ra LCD */
void lcd_putc( char c)
{
   lcd_send_byte(1,c);
}

/* Hien thi ki tu hoac chuoi ra LCD */
void lcd_refresh()
{
   lcd_send_byte(0,1);
   lcd_send_byte(0,6);
}

//reset

unsigned char ow_reset(void)
{
      unsigned char presence;
      set_tris_C(0);
      DQ = 0; //pull DQ line low
      delay_us(500); // leave it low for at least 480us
      DQ = 1;         // allow line to return high
      delay_us(40);  // wait for presence
      set_tris_C(0x01);
      presence = DQ; // get presence signal
      delay_us(150); // wait for end of timeslot
      return(presence); // presence signal returned
}                            //     0=presence, 1 = no part

unsigned char read_bit(void)
{
      set_tris_C(0);
      DQ = 0; // pull DQ low to start timeslot
      delay_us(1);
      set_tris_C(0x01); //release DQ
      delay_us(15);       
      return(DQ); // return value of DQ line
}

void write_bit(char bitval)
{
      set_tris_C(0);
      DQ = 0; // pull DQ low to start timeslot
      delay_us(15);
      if(bitval==1)   set_tris_C(0x01); //release DQ      
      delay_us(50); // hold value for remainder of timeslot
}

// READ_BYTE - reads a byte from the one-wire bus.
//
unsigned char read_byte(void)
{
      unsigned char i;
      unsigned char value = 0;
      for (i=0;i<8;i++)
      {
         if(read_bit()) value|=0x01<<i;     // reads byte in, one byte at a time and then
                                                   // shifts it left
         delay_us(150); // wait for rest of timeslot
      }
      return(value);
}

void write_byte(char val)
{
      unsigned char i;
      unsigned char temp;
      for (i=0; i<8; i++)             // writes byte, one bit at a time
      {
         temp = val>>i;           // shifts val right 'i' spaces
         temp &= 0x01;               // copy that bit to temp
         write_bit(temp);            // write bit in temp into
      }
      delay_us(100);
}
/* Ham main */
void main (void)
{ 
   unsigned char  x;     
   set_tris_B(0);    //PORTB = output
   set_tris_D(0);    //PORTD = output   
   
   ow_reset();
   write_byte(0xCC); // skip ROM
   write_byte(0x44); // Start conversion
   
   delay_us(100);
   ow_reset();
   write_byte(0xCC);   
   x = read_byte();   
   lcd_init();
   lcd_gotoxy(5,1);
   printf(lcd_putc,"temp = %u",x);
   lcd_gotoxy(4,2);
   lcd_putc("PICVIETNAM!");
  }
File Kèm Theo
File Type: rar ds1820.rar (15.8 KB, 217 lần tải)
notbadday vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 

Ðiều Chỉnh
Xếp Bài

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à 11:08 AM.


Đượ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