Avatar billede force_master Praktikant
30. august 2015 - 14:26 Der er 1 kommentar

Forkert udlæst grader

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;
}
Avatar billede force_master Praktikant
30. august 2015 - 14:27 #1
#include <htc.h>            //include hitech C header file
#include "lcd.h"
#include <stdlib.h>
#include <stdio.h>

#define    _XTAL_FREQ            4000000

// 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)


#define SCS_BIT                0x01
#define INT_OSC_8MHZ         (0b01110000 | SCS_BIT)
#define INT_OSC_4MHZ         (0b01100000 | SCS_BIT)
#define INT_OSC_2MHZ         (0b01010000 | SCS_BIT)
#define INT_OSC_1MHZ         (0b01000000 | SCS_BIT)
#define INT_OSC_500KHZ         (0b00110000 | SCS_BIT)
#define INT_OSC_250KHZ         (0b00100000 | SCS_BIT)
#define INT_OSC_125KHZ         (0b00010000 | SCS_BIT)
#define INT_OSC_31KHZ         (0b00000000 | SCS_BIT)

#define Skip_ROM             0xCC
#define Convert_T             0x44
#define Read_scratchpad     0xBE

#define Port_18B20             RA2
#define Tx_18B20             TRISA2 = 0
#define Rx_18B20             TRISA2 = 1

#define WAIT1                1000
#define WAIT2                500

#define DS18B20_CONV_TIME    750
#define DS18B20_RESET_PULSE 480
#define DS18B20_WAIT_TIME     60
#define DS18B20_PULLUP_TIME 2

#define ONEWIRE_PRESENT     0
#define ONEWIRE_ABSENT         1

unsigned char TLow;
unsigned char vDisp[9];
unsigned char DP;


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)
{
   
    unsigned short tempL, tempH; //, fraction;
   
        init();
    lcd_init();
    __delay_ms(500);
    lcd_goto(0x00);
    lcd_puts("Try again");
    __delay_ms(2000);
    lcd_clear();
    vDisp[4] = '.';
    vDisp[6] = " ";
    vDisp[7] = "´";
    vDisp[8] = 'C';
    lcd_goto(0x00);
    lcd_puts("Temp:");

    while(1)     // create an infinite loop
    {       
        if (!reset() == ONEWIRE_PRESENT)
        {
         
            write(Skip_ROM);
            write(Convert_T);       
            __delay_ms(DS18B20_CONV_TIME);
           
            reset();
            write(Skip_ROM);       
            write(Read_scratchpad);   
           
            tempL = readL();            //read low temp value
         
            tempH = readH();            //read high temp value
         
          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);
       
           
       
       
           
           

       
        }
    }

   
}
Avatar billede Ny bruger Nybegynder

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.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester