abang comey 0 Report post Posted August 28, 2008 (edited) aku ader project pasal temperature sensor...prob aku skang...aku ader dua sensor ni..ds18s20...pastu aku gna pic16f877 sebagai controller dan out put dier aku display kat lcd...so far yang aku bley wat ialah read and display je...masalah aku skang ni...camne aku nk tambah coding so that aku bley fullfilled kan condition kat bawh ni: 1. sensor 1 - sensor 2 => display result kat lcd2. jika result bg operasi tolak tu >50 degree buzzer akan on, led merah akan on3. jika result dier dalam lingkungan 30 ke 50, led kuning akan on4. jika bawah 30 degree led hijau akan onni coding yang aku ader buat mase ni n kena tambah la apa yng patut dan kalo ader silap coding ni bantulah aku untuk betulkan..aku baru lagi sentuh pic ni......aku da penin gler da ni...aku start pening bila nk assign pin..wargh!!!!#include < 16F877.H >#include < jonsinc.h >#fuses XT, NOPROTECT, NOPUT, NOWDT, NOBROWNOUT, NOLVP, NOCPD, NOWRT#define DS1820_DATA_IN_PIN PIN_C0#define DS1820_FET_CONTROL_PIN PIN_C1#define DS1820_SKIP_ROM 0xCC#define DS1820_READ_SCRATCHPAD 0xBE#define DS1820_CONVERT_T 0x44// LCD NYER#define LCD_D0 PIN_B0#define LCD_D1 PIN_B1#define LCD_D2 PIN_B2#define LCD_D3 PIN_B3#define LCD_EN PIN_B4#define LCD_RS PIN_B5#define LINE_1 0x00#define LINE_2 0x40#define CLEAR_DISP 0x01#define DEGREE_SYM 0xdf#use delay ( clock=6000000 )#use standard_io ( A )#use standard_io ( B )#use standard_io ( C )void ResetDS1820 ( void );void WriteDS1820 ( void );void ReadDS1820 ( void );void WaitForConversion ( void );void LCD_Init ( void );void LCD_SetPosition ( unsigned int cX );void LCD_PutChar ( unsigned int cX );void LCD_PutCmd ( unsigned int cX );void LCD_PulseEnable ( void );void LCD_SetData ( unsigned int cX );static char cShiftBit,cDataOut;static long iTemperature,iDataIn;void main ( void ) { delay_ms ( 200 ); LCD_Init(); LCD_SetPosition ( LINE_1 + 0 ); printf ( LCD_PutChar, "Temperature is" ); while ( TRUE ) { ResetDS1820(); cDataOut = DS1820_SKIP_ROM; WriteDS1820(); cDataOut = DS1820_CONVERT_T; WriteDS1820(); WaitForConversion(); ResetDS1820(); cDataOut = DS1820_SKIP_ROM; WriteDS1820(); cDataOut = DS1820_READ_SCRATCHPAD; WriteDS1820(); ReadDS1820(); iTemperature = iDataIn / 2; LCD_SetPosition ( LINE_2 + 5 ); printf ( LCD_PutChar, "%lu%cC %lu%cF ", iTemperature, DEGREE_SYM, ( ( 9 * iTemperature ) / 5 ) + 32, DEGREE_SYM ); } }void ResetDS1820 ( void ) { output_low ( DS1820_DATA_IN_PIN ); // low delay_us ( 480 ); // reset pulse width output_float ( DS1820_DATA_IN_PIN ); // high delay_us ( 480 ); // presence pulse width }void WriteDS1820 ( void ) // ~70uS per bit { for ( cShiftBit = 1; cShiftBit <= 8; ++cShiftBit ) { output_low ( DS1820_DATA_IN_PIN ); delay_us ( 5 ); output_bit ( DS1820_DATA_IN_PIN, shift_right ( &cDataOut, 1, 0 ) ); delay_us ( 60 ); output_float ( DS1820_DATA_IN_PIN ); delay_us ( 5 ); } //delay_us ( 200 ); }void ReadDS1820 ( void ) { iDataIn = 0; for ( cShiftBit = 1; cShiftBit <= 16; ++cShiftBit ) { output_low ( DS1820_DATA_IN_PIN ); delay_us ( 5 ); output_float ( DS1820_DATA_IN_PIN ); delay_us ( 5 ); shift_right ( &iDataIn, 2, input ( DS1820_DATA_IN_PIN ) ); // sample bit delay_us ( 55 ); } ResetDS1820(); }void WaitForConversion ( void ) { while ( TRUE ) { output_low ( DS1820_DATA_IN_PIN ); delay_us ( 5 ); output_float ( DS1820_DATA_IN_PIN ); delay_us ( 5 ); if ( input ( DS1820_DATA_IN_PIN ) == 1 ) { break; } delay_us ( 55 ); } }void LCD_Init ( void ) { LCD_SetData ( 0x00 ); delay_ms ( 200 ); output_low ( LCD_RS ); LCD_SetData ( 0x03 ); LCD_PulseEnable(); LCD_PulseEnable(); LCD_PulseEnable(); LCD_SetData ( 0x02 ); LCD_PulseEnable(); LCD_PutCmd ( 0x2C ); LCD_PutCmd ( 0x0C ); LCD_PutCmd ( 0x01 ); LCD_PutCmd ( 0x06 ); }void LCD_SetPosition ( unsigned int cX ) { LCD_SetData ( swap ( cX ) | 0x08 ); LCD_PulseEnable(); LCD_SetData ( swap ( cX ) ); LCD_PulseEnable(); }void LCD_PutChar ( unsigned int cX ) { // this subroutine works specifically for 4-bit Port A output_high ( LCD_RS ); LCD_SetData ( swap ( cX ) ); LCD_PulseEnable(); LCD_SetData ( swap ( cX ) ); LCD_PulseEnable(); output_low ( LCD_RS ); }void LCD_PutCmd ( unsigned int cX ) { // this subroutine works specifically for 4-bit Port A LCD_SetData ( swap ( cX ) ); LCD_PulseEnable(); LCD_SetData ( swap ( cX ) ); LCD_PulseEnable(); }void LCD_PulseEnable ( void ) { output_high ( LCD_EN ); delay_us ( 10 ); output_low ( LCD_EN ); delay_ms ( 5 ); }void LCD_SetData ( unsigned int cX ) { output_bit ( LCD_D0, cX & 0x01 ); output_bit ( LCD_D1, cX & 0x02 ); output_bit ( LCD_D2, cX & 0x04 ); output_bit ( LCD_D3, cX & 0x08 ); } Edited August 28, 2008 by abang comey Quote Share this post Link to post Share on other sites
troikasix 0 Report post Posted August 28, 2008 (edited) Benda camni pakai header file specific kepada hardware, baik post kat www.codeproject.com ataupun diorang nyer website. Aku pun takder experience dengan PIC apalagi nak dapatkan hardware dengan SDK.Lain kali gunakan 'code' dengan '/code' block untuk paparkan kod dengan lebih jelas. Edited August 28, 2008 by xeonn Quote Share this post Link to post Share on other sites