:: مهندس متواجد ::
تاريخ التسجيل: Oct 2005
المشاركات: 73
|
|
نشاط [ الطالب99 ]
قوة السمعة:0
|
|
13-04-2013, 10:30 AM
المشاركة 5
|
|
أخى الكريم
شكرا جزيلا لك
نعن يمكن ربط حساسين على هذه الدائرة بطريقة تقليدية عن طريق استخدام خط لمدخل آخر مع استخدام النسح واللصق كما يلى :
البرنامج :
كود:
/*
* Project name:
Digital Room Thermometer using PIC16F628
Onewire_Test (Interfacing the DS18B20 temperature sensor )
* Description:
After reset, PIC reads temperature from the sensor and prints it on the Lcd.
The display format of the temperature is"000.0°C " .
, the 18B20 temperature resolution is 12-bit (default) ( 0.065 °C . step)
* Test configuration:
MCU: PIC16F628
Oscillator: 4.0000 MHz
LCD 2x16
SW: mikroC PRO for PIC
*/
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
// Define Messages
char message0[] = "LCD Initialized"; // string constant
char message1[] = "Temp1="; //string constant
char message2[] = "Temp2="; //string constant
// String array to store temperature value to display
char *tempC = "000.0"; // pointer to string
// Variables to store temperature register values
unsigned int temp_whole, temp_fraction, temp_value; // 2-byte => 0 .. 65535
signed int tempinC; // 2-byte => -32768 .. 32767
unsigned short C_Neg=0, TempH, TempL; // 1-byte => 0 .. 255
void Display_Temperature1() {
// check if temperature is negative
if (temp_value & 0x8000) {
C_Neg = 1;
tempC[0] = '-';
// Negative temp values are stored in 2's complement form
temp_value = ~temp_value + 1;
}
else C_Neg = 0;
// Get temp_whole
temp_whole = temp_value >> 4 ; // 12 bit >>4 (fraction) => 8bit whole
if (temp_value & 0x0001){ // LSB is 0.0625C
temp_fraction = 5;
}
else temp_fraction = 0;
tempinC = temp_whole*10+temp_fraction;
// convert Temp to characters
if (!C_Neg) {
if (tempinC/1000)
// 48 is the decimal character code value for displaying 0 on LCD
tempC[0] = tempinC/1000 + 48;
else tempC[0] = ' '; // space
}
tempC[1] = (tempinC/100)%10 + 48; // Extract tens digit
tempC[2] = (tempinC/10)%10 + 48; // Extract ones digit
// convert temp_fraction to characters
tempC[4] = tempinC%10 + 48; // Extract tens digit
// print temperature on LCD
Lcd_Out(1, 7, tempC);
}
void Display_Temperature2() {
// check if temperature is negative
if (temp_value & 0x8000) {
C_Neg = 1;
tempC[0] = '-';
// Negative temp values are stored in 2's complement form
temp_value = ~temp_value + 1;
}
else C_Neg = 0;
// Get temp_whole
temp_whole = temp_value >> 4 ; // 12 bit >>4 (fraction) => 8bit whole
if (temp_value & 0x0001){ // LSB is 0.0625C
temp_fraction = 5;
}
else temp_fraction = 0;
tempinC = temp_whole*10+temp_fraction;
// convert Temp to characters
if (!C_Neg) {
if (tempinC/1000)
// 48 is the decimal character code value for displaying 0 on LCD
tempC[0] = tempinC/1000 + 48;
else tempC[0] = ' '; // space
}
tempC[1] = (tempinC/100)%10 + 48; // Extract tens digit
tempC[2] = (tempinC/10)%10 + 48; // Extract ones digit
// convert temp_fraction to characters
tempC[4] = tempinC%10 + 48; // Extract tens digit
// print temperature on LCD
Lcd_Out(2, 7, tempC);
}
void main() {
CMCON |= 7; // Set AN pins to Digital I/O;
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // CLEAR display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,message0);
Delay_ms(1000);
Lcd_Cmd(_LCD_CLEAR); // CLEAR display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,message1); // Write message1 in 1st row
Lcd_Out(2,1,message2); // Write message2 in 2nd row
// Print degree character "°C"
Lcd_Chr(1,11,223);
Lcd_Chr(2,11,223);
// different LCD displays have different char code for degree
// if you see greek alpha letter try typing 178 instead of 223
Lcd_Chr(1,12,'C');
Lcd_Chr(2,12,'C');
do {
//--- perform temperature reading
Ow_Reset(&PORTA, 0); // Onewire reset signal
Ow_Write(&PORTA, 0, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTA, 0, 0x44); // Issue command CONVERT_T
Delay_ms(600);
Ow_Reset(&PORTA, 0);
Ow_Write(&PORTA, 0, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTA, 0, 0xBE); // Issue command READ_SCRATCHPAD
// Read Byte 0 from Scratchpad
TempL = Ow_Read(&PORTA, 0); // Read => low byte of 2-byte temperature register
// Then read Byte 1 from Scratchpad
TempH = Ow_Read(&PORTA, 0); // Read => high byte of 2-byte temperature register
temp_value = (TempH<< 8)+ TempL ; // forming 2-byte value
//--- Format and display result on Lcd
Display_Temperature1();
Ow_Reset(&PORTA, 1); // Onewire reset signal
Ow_Write(&PORTA, 1, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTA, 1, 0x44); // Issue command CONVERT_T
Delay_ms(600);
Ow_Reset(&PORTA, 1);
Ow_Write(&PORTA, 1, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTA, 1, 0xBE); // Issue command READ_SCRATCHPAD
// Read Byte 0 from Scratchpad
TempL = Ow_Read(&PORTA, 1); // Read => low byte of 2-byte temperature register
// Then read Byte 1 from Scratchpad
TempH = Ow_Read(&PORTA, 1); // Read => high byte of 2-byte temperature register
temp_value = (TempH << 8)+ TempL ; // forming 2-byte value
//--- Format and display result on Lcd
Display_Temperature2();
} while(1);
}
النتائج :
لكن عمليا يمكن توصيل عدة حساسات على خط واحد One Wire باعتبار أن الميكروكونترولر هو الجهاز الرئيسى (الماستر) والحساسات توابع مع تعيين عنوان لكل حساس وهذا ما سوف نتناوله بالتفصيل إن شاء الله .
مع تمنياتى بدوام التوفيق
|
وانا بنتظارك اخي الكريم واسال الله ان يسدد خطاك ويفتح عليك وعلينا من ابواب علمه
وجزاك الله خير .....
|