Hej, Ved måske godt det ikke er noget i kan svare på , men prøver alligevel.
Jeg har en temperatur føler tilkoblet en microchip. Men noget i kodningen går galt, for den er fejl med ca 60 grader. Alle data bliver læst ind i 2 bytes, og derfra skal graderne "bare" omsættes til normale tal. Det er nok det med bit til int som går galt.
Poster hele koden. unsigned char read() { char i,result = 0; Rx_18B20; // TRIS is input(1) for(i = 8; i > 0; i--) { Tx_18B20; // TRIS is output(0) Port_18B20 = 0; // genarate low pluse for 2us __delay_us(DS18B20_PULLUP_TIME); Rx_18B20; // TRIS is input(1) release the bus if(Port_18B20 != 0) { result |= 1<<i;//i } __delay_us(DS18B20_WAIT_TIME); // wait for recovery time } return result; }
// This configuration bits are for PIC16F690 using internal crystal
//__CONFIG(FCMEN_ON & IESO_OFF & BOREN_OFF & CPD_OFF & CP_OFF & MCLRE_OFF & PWRTE_ON & WDTE_OFF & FOSC_INTRCIO); #pragma config FOSC = INTRCIO // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN) #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register) #pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled) #pragma config MCLRE = OFF // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD) #pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled) #pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled) #pragma config BOREN = OFF // Brown-out Reset Selection bits (BOR disabled) #pragma config IESO = OFF // Internal External Switchover bit (Internal External Switchover mode is disabled) #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
void init (void) { ANSEL = 0; //Initialize A/D ports off ANSELH = 0; //Initialize A/D ports off CM1CON0 = 0; //Initialize comp 1 off CM2CON0 = 0; //Initialize comp 2 off PORTA = 0x00; // Clears PORTA PORTB = 0x00; // Clears PORTB PORTC = 0x00; // Clears PORTC TRISA = 0x00; // Sets PORTA to all output TRISB = 0x00; // Sets PORTB to all output TRISC = 0x00; // sets PORTC to all output. //TRISA1=1;
// We want 8MHz OSCCON = INT_OSC_8MHZ;
// Wait for clock to stabilise. __delay_ms(10); }
unsigned char reset() { Tx_18B20; // Tris = 0 (output) Port_18B20 = 0; // set pin# to low (0) __delay_us(DS18B20_RESET_PULSE); // 1 wire require time delay Rx_18B20; // Tris = 1 (input) __delay_us(DS18B20_WAIT_TIME); // 1 wire require time delay
if (Port_18B20 == 1) // if there is a presence pluse { __delay_us(DS18B20_RESET_PULSE);// Reset and return return ONEWIRE_PRESENT; // return 0 ( 1-wire is presence) }
__delay_us(DS18B20_RESET_PULSE); // Reset and return return ONEWIRE_ABSENT; // return 1 ( 1-wire is NOT presence) }
void write(char WRT) { char i,Cmd; Cmd = WRT; Rx_18B20; // set pin# to input (1) for(i = 0; i < 8; i++) { if((Cmd & (1<<i))!= 0) { // write 1 Tx_18B20; // set pin# to output (0) Port_18B20 = 0; // set pin# to low (0) __delay_us(DS18B20_PULLUP_TIME); // 1 wire require time delay Rx_18B20; // set pin# to input (release the bus) __delay_us(DS18B20_WAIT_TIME); // 1 wire require time delay } else { //write 0 Tx_18B20; // set pin# to output (0) Port_18B20 = 0; // set pin# to low (0) __delay_us(DS18B20_WAIT_TIME); // 1 wire require time delay Rx_18B20; // set pin# to input (release the bus) } } }
unsigned char readH() { char i,result = 0; Rx_18B20; // TRIS is input(1) for(i = 8; i > 0; i--) { Tx_18B20; // TRIS is output(0) Port_18B20 = 0; // genarate low pluse for 2us __delay_us(DS18B20_PULLUP_TIME); Rx_18B20; // TRIS is input(1) release the bus if(Port_18B20 != 0) { result |= 1<<i;//i } __delay_us(DS18B20_WAIT_TIME); // wait for recovery time } return result; } unsigned char readL() { char i,result = 0; Rx_18B20; // TRIS is input(1) for(i = 0; i < 8; i++) { Tx_18B20; // TRIS is output(0) Port_18B20 = 0; // genarate low pluse for 2us __delay_us(DS18B20_PULLUP_TIME); Rx_18B20; // TRIS is input(1) release the bus if(Port_18B20 != 0) { result |= 1<<i;//i } __delay_us(DS18B20_WAIT_TIME); // wait for recovery time } return result; }
/******************************************************************************* * MAIN FUNCTION * *******************************************************************************/ int main(void) {
DP = tempL & 0x01; // 'Check if Temperature is integer or fractional if (tempH){ //If reading is negative vDisp[0] = '-'; tempL = ((~tempL) + 1) >> 1; } else{ vDisp[0] = '+'; tempL = tempL >> 1; // 'Shift one position right (divide by 2) to get integer reading and get rid of decimal point } vDisp[1] = (tempL / 100) + 48; // 'Get hundreds and convert to ASCII vDisp[2] = ((tempL / 10) % 10) + 48; // 'Get tens and convert to ASCII vDisp[3] = (tempL % 10) + 48; // 'Get units and convert to ASCII if (DP){ // 'If reading is fractional, ie has 0.5 at end vDisp[5] = '5'; } else{ // 'If reading is a whole number vDisp[5] = '0'; } lcd_goto(0x08); lcd_putch(vDisp[0]); lcd_putch(vDisp[1]); lcd_putch(vDisp[2]); lcd_putch(vDisp[3]); lcd_putch(vDisp[4]); lcd_putch(vDisp[5]); lcd_putch(vDisp[6]); lcd_putch(vDisp[7]); lcd_putch(vDisp[8]); lcd_goto(0x40);
} }
}
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.