بارك الله فيك أخي أحمد لقد قمت بتجريب هذا الكود ولكن للاسف لم ينجح مع 18F452 هذا هو الكود
sbit Data at LATA0_bit;
sbit DataDir at TRISA0_bit;
unsigned short TOUT = 0, CheckSum, i;
unsigned short T_Byte1, T_Byte2, RH_Byte1, RH_Byte2, tmp, HM;
void StartSignal(){
DataDir = 0; // Data port is output
Data = 0;
Delay_ms(25);
Data = 1;
Delay_us(30);
DataDir = 1; // Data port is input
}
unsigned short CheckResponse(){
TOUT = 0;
TMR2 = 0;
T2CON.TMR2ON = 1; // start timer
while(!Data && !TOUT);
if (TOUT) return 0;
else {
TMR2 = 0;
while(Data && !TOUT);
if (TOUT) return 0;
else {
T2CON.TMR2ON = 0;
return 1;
}
}
}
unsigned short ReadByte(){
unsigned short num = 0, t;
DataDir = 1;
for (i=0; i<8; i++){
while(!Data);
TMR2 = 0;
T2CON.TMR2ON = 1;
while(Data);
T2CON.TMR2ON = 0;
/*
For 10.0MHz clock, 1 machine cycle is 0.2 uS. So for 40 us, the
TMR2 value should be 200
*/
if(TMR2 > 200) num |= 1<<(7-i); // If time > 40us, Data is 1
}
return num;
}
void interrupt(){
if(PIR1.TMR2IF){
TOUT = 1;
T2CON.TMR2ON = 0; // stop timer
PIR1.TMR2IF = 0; // Clear TMR0 interrupt flag
}
}
void main() {
unsigned short check;
TRISB =0x06
LATB=0;
TRISA0_bit= 1;
TRISC 0
// configure pins as input
INTCON.GIE = 1; //Enable global interrupt
INTCON.PEIE = 1; //Enable peripheral interrupt
// Configure Timer2 module
PIE1.TMR2IE = 1; // Enable Timer2 interrupt
T2CON = 0; // Prescaler 1:1, and Timer2 is off initially
PIR1.TMR2IF =0; // Clear TMR INT Flag bit
TMR2 = 0;
LATB0_bit=1;
Delay_ms(300);
LATB0_bit=0;
do {
Delay_ms(1000);
StartSignal();
check = CheckResponse();
if (!check) {
LATB1_bit=1;
Delay_ms(300);
LATB1_bit=0;
}
else{ RH_Byte1 = ReadByte();
RH_Byte2 = ReadByte();
T_Byte1 = ReadByte();
T_Byte2 = ReadByte();
CheckSum = ReadByte();
LATB1_bit=1;
// Check for error in Data reception
if (CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) & 0xFF))
{
LATB0_bit=1;
Delay_ms(300);
LATB0_bit=0;
tmp= T_Byte1/10 +T_Byte1%10 +T_Byte2/10;
HM= RH_Byte1/10 + RH_Byte1%10 +RH_Byte2/10;
}
else{
LATB1_bit=1;
Delay_ms(300);
LATB1_bit=0;
}
}
}
}while(1);
}