CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Interface PIC16F877-DS1620

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
andyhug



Joined: 25 Jul 2008
Posts: 2

View user's profile Send private message Yahoo Messenger

Interface PIC16F877-DS1620
PostPosted: Fri Jul 25, 2008 9:09 am     Reply with quote

Can some one check my code, help me how to write TH, TL limit temperature register?
Code:
//*---------------------------------------------------------------------------------
//* writer           : Pham Thanh Cong
//* Date             : 18/06/2008
//* to date           : 22/07/2008
//* Hardware      : PIC16F877A, LCD 2x16[5x7] LMB162A, DS1620
//* Program       : use DS1620 and display temperature on LCD LMB162A
//*============================================*/

#include <16F877A.h>
#include <stdlib.h>
#include <string.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP  // set config for PIC
#use delay(clock=4000000)
#use fast_io(A)                  // use with comand set_tris
#use fast_io(B)
#use fast_io(D)
#byte   PORTB    = 0x06    // PortB address
#define LCD PORTB
#bit RA2         = 0x05.2
#bit RA1         = 0x05.1
#bit RA0         = 0x05.0
#define RS  RA0                // RS =0 comand,RST =1 Data
#define RW  RA1               // RW=1 read, RW =0 write
#define E   RA2                 // H-to-L enable LCD
#bit RD1         = 0x08.1     // data pin's DS1620
#bit RD2         = 0x08.2     // clock pin
#bit RD3         = 0x08.3     // reset pin
#define DQ     RD1            // =0 output, =1 input
#define CLK    RD2
#define RST    RD3
// Functions used in program
void LCD_init(void);
void comandwrt(void);
void datawrt(void);
void LCD_gotoxy(byte x, byte y);
void DS1620_InitTemp(void);
unsigned int8 DS1620_GetTemp(void);
void DS1620_Write(unsigned char Data);
unsigned int8 DS1620_Read(void);
void display(int8 temp);
void main(void);
// Global variable
const unsigned char dongchu[]="Temp:";      // global const
int i = 0, sign = 0;                                      //
//xxxxxxxxxxxxxxxxxxxxxxxx LCD Program xxxxxxxxxxxxxxxxxxxxxxx

void LCD_init()
{
   delay_ms(50);       // delay at least 30ms
   LCD = 0x38;         // 2line, dot matrix 5*8, 8 bits interface
   comandwrt();   
   delay_us(50);       // delay at least 39us
   LCD = 0x0C;         // display on, cusor off and blink off
   comandwrt();   
   delay_us(50);       // delay at least 39us
   LCD = 0x01;         // clear display
   comandwrt();   
   delay_ms(3);        // delay at least 1.53ms
   LCD = 0x06;         // increase cusor
   comandwrt();        // and blink off
   delay_ms(1);
}
// Function send comand to LCD
void comandwrt(void)
{
 RS = 0;
 RW = 0;
 E  = 1;
 E  = 0;
 delay_ms(1);
}
// Function send data to LCD
void datawrt(void)
{
 RS = 1;
 RW = 0;
 E  = 1;
 E  = 0;
 delay_ms(1);
}
// Function move pointer xy(colum,line) on LCD display
void LCD_gotoxy(int x, int y)
{  int address;
    switch(y)
      {
         case 1:  address=0;
                  address+=x-1;
                  LCD = (0x80 + address);    // line 1, colum1: 80h
                  break;
         case 2:  address=0;
                  address+=x-1;
                  LCD = (0xC0 + address);    // line 2, colum1: C0h
                  break;
         default : break;
      }
}
//xxxxxxxxxxxxxxxxxxxxxxx DS1620 Program xxxxxxxxxxxxxxxxxxxxx

void DS1620_InitTemp(void)
{
      RST = 1;                       // setup 3-wire mode.
      DS1620_Write(0x0C);    // enable write config
      DS1620_Write(0x00);    // clear config, setup continuous [spam]
      RST = 0;
      RST = 1;                      // setup 3-wire mode
      DS1620_Write(0xEE);    // start [spam]
      RST =0;
}

void DS1620_Write(unsigned char Data)
{
   unsigned char i;
   set_tris_D(0);             // Port D : ouput
   CLK = 1;                    // set clock pulse
   for (i=1; i<=8; ++i)
      {
       DQ = (Data & 1);   
       CLK = 0;
       CLK = 1;                // set clock
       Data = Data >> 1;  // shift Data
      }
}

unsigned int8 DS1620_Read(void)
{
   unsigned int8 Data, temp;
   set_tris_D(0x02);            // DQ = 1 input, others: ouput
   Data = 0;
   temp = 1;
   CLK = 1;
   for (i=1; i<=8; ++i)
   {
       CLK = 0;                     // CLK = 0: convert
       if (DQ == 1) Data += Temp;
       Temp = Temp << 1;       
       CLK = 1;
   }
   CLK = 0;
   if (DQ == 1) Sign = 1;      // get sign bit, the 9th bit
   else Sign = 0;
   CLK = 1;
   return(Data);
}

unsigned int8 DS1620_GetTemp(void)
{
   unsigned int8 Data;
   RST =1;                 
   DS1620_Write(0xAA);       // enable read temperature
   data = DS1620_Read();    // read temp
   RST =0;                          // end 3wire mode
   return(data);           
}

void display(int8 temp)
{
   int8 Data, leng;
   unsigned char string[3];
   if (sign == 1)                        // minus degree
   {
      LCD_gotoxy(5,2);
      comandwrt();
      LCD = '-';
      datawrt();
      Data = ~(temp - 1) / 2;
     
   }
   else  {  LCD_gotoxy(5,2);      // positive
            comandwrt();
            LCD = '+';
            datawrt();
            Data = temp / 2;
         }
   itoa(Data,10,string);
   leng = strlen(string);
   if (bit_test(temp,0))
   {
      for(i=0;i<leng;i++)
      {
         LCD = string[i];               // put temp
         datawrt();
      }
      LCD = '.';
      datawrt();
      LCD = '5';
      datawrt();
      LCD = ' ';
      datawrt();
      LCD = 'd';
      datawrt();
      LCD = 'o';
      datawrt();
      LCD = 'C';
      datawrt();
   }
   else
   {
      for(i=0;i<leng;i++)
      {
         LCD = string[i];
         datawrt();
      }
      LCD = '.';
      datawrt();
      LCD = '0';
      datawrt();
      LCD = ' ';
      datawrt();
      LCD = 'd';
      datawrt();
      LCD = 'o';
      datawrt();
      LCD = 'C';
      datawrt();
   }
}

//xxxxxxxxxxxxxxxxxxxxxxxx Main function xxxxxxxxxxxxxxxxxxxxxxx/
void main(void)
{
   int8 data;
   set_tris_B(0);                       // output
   set_tris_A(0);                       // output
   LCD_init();                           // set LCD mode
   delay_us(2);
   DS1620_InitTemp();
   delay_ms(2);
   LCD_gotoxy(5,1);                 // colum 5, line 1
   comandwrt();
   for (i=0;i<=8;i++)
   {
      LCD = dongchu[i];             // put line words to LCD
      datawrt();
   }
   while(1)
   {
      data  = DS1620_GetTemp();     // get data
      display(data);
   }
}
I use Proteus7.2 SP2 to simulation, CCSC 4.049 to compile. I tried to write TH, TL but when I download to chip it didn't worked! It only displayed temperature. Rolling Eyes
Can some one give me answer?
My email: [email protected]
Thanhks alot!
_________________
Khong co gi qui hon doc lap tu do!
xxxxxxxxx Ho Chi Minh xxxxxxxx
[email protected]
andyhug



Joined: 25 Jul 2008
Posts: 2

View user's profile Send private message Yahoo Messenger

Final I did it! Yahoo!
PostPosted: Fri Jul 25, 2008 12:32 pm     Reply with quote

//*--------------------------------------------------------------------------------
//* Tac gia : Pham Thanh Cong
//* Ngay viet : 18/06/2008
//* Ngay hoan thanh: 21/07/2008
//* Phan cung : PIC16F877, LCD 2x16[5x7]
//* Chuong trinh : do nhiet do dung DS1620 hien thi tren LCD LMB162A
//*============================================*/
// Khoi tien xu ly
#include <16F877A.h>
#include <stdlib.h>
#include <string.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP // dat cau hinh cho PIC
#use delay(clock=4000000)
#use fast_io(A) // su dung cung voi lenh set_tris
#use fast_io(B)
#use fast_io(D)
#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 LCD_init(void);
void comandwrt(void);
void datawrt(void);
void LCD_gotoxy(byte x, byte y);
void DS1620_InitTemp(void);
unsigned int8 DS1620_GetTemp(void);
void DS1620_Write(unsigned char Data);
void DS1620_WriteTHTL(unsigned char Data, int dau);
unsigned int8 DS1620_Read(void);
void display(int8 temp);
void main(void);
// Cac bien toan cuc
const unsigned char dongchu[]="Nhiet do:"; // Hang so toan cuc
int i = 0, Sign = 0; // Bien toan cuc
//xxxxxxxxxxxxxxxxxxxxxxx Program LCDxxxxxxxxxxxxxxxxxxxxxxxx
// Khoi tao cho LCD
void LCD_init()
{
delay_ms(50); // tre it nhat 30ms
LCD = 0x38; // Hai hang, ma tran diem 5*8, 8 bit interface
comandwrt();
delay_us(50); // tre it nhat 39us
LCD = 0x0C; // bat hien thi, tat con tro va tat nhap nhay
comandwrt();
delay_us(50); // tre it nhat 39us
LCD = 0x01; // xoa man hinh
comandwrt();
delay_ms(3); // tre it nhat 1.53ms
LCD = 0x06; // che do tang con tro sau khi viet 1 ki tu
comandwrt(); // va chu khong chay
delay_ms(1);
}
// Ham gui lenh ra LCD
void comandwrt(void)
{
RS = 0;
RW = 0;
E = 1;
E = 0;
delay_ms(1);
}
// Ham gui du lieu ra LCD
void datawrt(void)
{
RS = 1;
RW = 0;
E = 1;
E = 0;
delay_ms(1);
}
// Ham di chuyen con tro toi vi tri xy(cot,hang) tren man hinh LCD
void LCD_gotoxy(int x, int y)
{ int address;
switch(y)
{
case 1: address=0;
address+=x-1;
LCD = (0x80 + address); // dong 1, cot1: 80h
break;
case 2: address=0;
address+=x-1;
LCD = (0xC0 + address); // dong 2, cot1: C0h
break;
default : break;
}
}
//xxxxxxxxxxxxxxxxxxxxx Program DS1620 xxxxxxxxxxxxxxxxxxxxxxx
// Khoi tao DS1620
void DS1620_InitTemp(void)
{
RST = 1; // thiet lap che do giao tiep 3-wire.
DS1620_Write(0x0C); // cho phep ghi config
DS1620_Write(0x08);
RST = 0;
RST = 1; // thiet lap che do giao tiep 3-wire
DS1620_Write(0xEE); // bat dau chuyen doi nhiet do
RST = 0;
}

void DS1620_Write(unsigned char Data)
{
set_tris_D(0); // cong D la cong ra
CLK = 1; // tao clock
for (i=1; i<=8; ++i)
{
DQ = (Data & 1); // viet bit thap len DQ
CLK = 0;
CLK = 1; // tao clock
Data = Data >> 1; // dich fai Data di 1 bit
}
}

void DS1620_WriteTHTL(unsigned char Data, int dau)
{
set_tris_D(0); // cong D la cong ra
CLK = 1; // tao clock
for (i=1; i<=8; ++i)
{
DQ = (Data & 1); // viet bit thap len DQ
CLK = 0;
CLK = 1; // tao clock
Data = Data >> 1; // dich fai Data di 1 bit
}
CLK = 1;
if (dau == 1) DQ = 1; // Lay gia tri bit dau, gui not bit9 di
else DQ = 0;
CLK = 0;
}

unsigned int8 DS1620_Read(void)
{
unsigned int8 Data, temp;
set_tris_D(0x02); // Chan DQ = 1 : chan vao, con lai la chan ra
Data = 0;
temp = 1;
CLK = 1;
for (i=1; i<=8; ++i)
{
CLK = 0; // CLK = 0: convert
if (DQ == 1) Data += Temp; //neu DQ = 1, cong don Data
Temp = Temp << 1; // neu DQ = 0, dich trai temp 1bit de chen 0 vao
CLK = 1;
}
CLK = 0;
if (DQ == 1) Sign = 1;// Lay gia tri bit dau
else Sign = 0;
CLK = 1;
return(Data);
}

unsigned int8 DS1620_GetTemp(void)
{
unsigned int8 Data;
RST =1; // thiet lap che do giao tiep 3-wire
DS1620_Write(0xAA); // lenh cho phep doc noi dung thanh ghi nhiet
data = DS1620_Read(); // doc noi dung
RST =0; // ket thuc che do giao tiep 3-wire
return(data);
}

void display(int8 temp)
{
int8 Data, leng;
unsigned char string[3];
if (sign == 1) // Nhiet do am
{
LCD_gotoxy(5,2);
comandwrt();
LCD = '-'; // Hien thi dau -
datawrt();
Data = ~(temp - 1) / 2; // neu nhiet do am thi lay ma bu 2
}
else { LCD_gotoxy(5,2); // Nhiet do duong
comandwrt();
LCD = '+'; // Hien thi dau +
datawrt();
Data = temp / 2;
}
itoa(Data,10,string); // Chuyen doi Data thanh ma ASCII vao string
leng = strlen(string); // Lay chieu dai cua string
if (bit_test(temp,0)) // kiem tra bit0 =1 suy ra nhiet do .5 doC
{
for(i=0;i<leng;i++)
{
LCD = string[i]; // xuat nhiet do ra o dang ki tu so
datawrt();
}
LCD = '.'; // hien thi phan thap phan .5 do C
datawrt();
LCD = '5';
datawrt();
LCD = ' ';
datawrt();
LCD = 'd';
datawrt();
LCD = 'o';
datawrt();
LCD = 'C';
datawrt();
}
else // bit0 =0 suy ra nhiet do .0 do C
{
for(i=0;i<leng;i++)
{
LCD = string[i]; // xuat nhiet do ra o dang ki tu so
datawrt();
}
LCD = '.'; // hien thi phan thap phan .0 do C
datawrt();
LCD = '0';
datawrt();
LCD = ' ';
datawrt();
LCD = 'd';
datawrt();
LCD = 'o';
datawrt();
LCD = 'C';
datawrt();
}
}

//xxxxxxxxxxxxxxxxxxxxxxx Ham main xxxxxxxxxxxxxxxxxxxxxxxxx*/
void main(void)
{
int8 data;
set_tris_B(0); // cong ra
set_tris_A(0); // cong ra
LCD_init(); // dat che do hoat dong cho LCD
delay_us(2);
DS1620_InitTemp(); // dat che do hoat dong cho DS1620
delay_us(10);
LCD_gotoxy(5,1); // Vi tri cot 5, hang 1
comandwrt();
for (i=0;i<=8;i++)
{
LCD = dongchu[i]; // xuat chu ra LCD
datawrt();
}
RST = 1;
DS1620_Write(0x01); // write TH
DS1620_WriteTHTL(0x44,0); // TH = +34 do C
DS1620_Write(0x00); // infact must have this comand to 'run ok'
RST = 0;
while(1)
{
RST = 1; // infact must put this comand in to this loop to 'run ok'
DS1620_Write(0x02); // write TL
DS1620_WriteTHTL(0x40,0); // TL = +32 do C
DS1620_Write(0x00);
RST = 0;
data = DS1620_GetTemp(); // get data
display(data); // display data
}
}
_________________
Khong co gi qui hon doc lap tu do!
xxxxxxxxx Ho Chi Minh xxxxxxxx
[email protected]
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group