Voici un code que j'ai trouvé sur internet qui permet de lire un capteur de temperature DS18S20 via pic16f877a et afficher le résultat sur un lcd 2x16.
Donc le programme fonctionne mais j'arrive pas a interpreter quelques lignes dans ce programme. Merci d'avance
Voici les lignes que j'ai mis en rouge dans le code.
1- unsigned short RES_SHIFT = TEMP_RESOLUTION - 8
2-if (temp2write & 0x8000) , ici on fait un ET bit a bit voir datasheet page 6 , donc pour -0.5 degre DS indique 1111 1111 1111 1111 si on fait ET 0x8000
comme resultat on a 0000 0000 0000 0000. comment ce resultat est considéré comme négatif?
3-temp2write = ~temp2write + 1; on fait le complément a 2 , pourquoi ceci?
4- temp_whole = temp2write >> RES_SHIFT ; on décale a droite si j'ai bien compris, mais combien de fois?
Code : Tout sélectionner
// lecture de temperature par DS18S20 pic1f6877A
// LCD module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
// End LCD module connections
// Set TEMP_RESOLUTION to the corresponding resolution of used DS18x20 sensor:
// 18S20: 9 (default setting; can be 9,10,11,or 12)
// 18B20: 12
//Programmer: Syed Tahmid Mahbub
//Compiler: mikroC PRO for PIC v4.60
//Target PIC: PIC16F877A
// Set TEMP_RESOLUTION to the corresponding resolution of used DS18x20 sensor:
// 18S20: 9 (default setting; can be 9,10,11,or 12)
// 18B20: 12
unsigned short TEMP_RESOLUTION = 9;
unsigned temp;
char *text = "000.00";
int i;
int colona;
void Read_Temperature()
{
// Perform temperature reading
Ow_Reset(&PORTE, 2); // Onewire reset signal
Ow_Write(&PORTE, 2, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTE, 2, 0x44); // Issue command CONVERT_T
Delay_us(120);
Ow_Reset(&PORTE, 2);
Ow_Write(&PORTE, 2, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTE, 2, 0xBE); // Issue command READ_SCRATCHPAD
temp = Ow_Read(&PORTE, 2);
temp = (Ow_Read(&PORTE, 2) << 8) + temp;
}
void Display_Temperature()
{
[color=#FF0000]unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;[/color]
char temp_whole;
unsigned int temp2write;
unsigned int temp_fraction;
temp2write = temp;
// Check if temperature is negative
[color=#FF0000]if (temp2write & 0x8000)[/color]
{
text[0] = '-';
[color=#FF0000]temp2write = ~temp2write + 1;[/color]
}
// Extract temp_whole
[color=#FF0000]temp_whole = temp2write >> RES_SHIFT ;[/color]
// Convert temp_whole to characters
if (temp_whole/100)
text[0] = temp_whole/100 + 48;
text[1] = (temp_whole/10)%10 + 48; // Extract tens digit
text[2] = temp_whole%10 + 48; // Extract ones digit
text[3] = '.';
// Extract temp_fraction and convert it to unsigned int
temp_fraction = temp2write << (4-RES_SHIFT);
temp_fraction &= 0x000F;
temp_fraction *= 625;
// Convert temp_fraction to characters
text[4] = temp_fraction/1000 + 48; // Extract thousands digit
// Print temperature on LCD
Lcd_Out(1, 1, text);
// Print degree character and'C' for Celsius
Lcd_Chr_CP(0xDF); // 223 ASCII for degree symbol on my LCD
Lcd_Chr_CP('C');
}
void main()
{
CMCON |=7;
ADCON1 = 0x0D;
TRISE.B2 = 1; // Configure RE2 pin as input
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear LCD
Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off
// Main loop
do
{
Read_Temperature();
Display_Temperature();
Delay_ms(100);
} while (1);
} 


et Paul il en a plein