Problema con la interfaz DS24B33 y PIC18f4550

0

He escrito un código simple para omitir las funciones de ROM y escribir 8 bytes de datos en la EEPROM DS24B33 (enlaces a EEPROM Overview & hoja de datos ). Lo conecté a una pantalla LCD para otro propósito, pero terminé usándolo con puntos de interrupción improvisados.

Mi código principal:

#include <xc.h>
#include <stdint.h>
#include "config.h"
#include "lcd.h"
#include "1wire.h"

int main(void) {
    uint8_t w_data[8]={0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38};
    uint8_t t0, t1, es, j, r_data[8], buffer[32];

    char m1[10] = "Point 1:";
    char m2[10] = "abcdefgh";

    OSCCON = 0x77;
    lcd_init();
    lcd_show(&m1, &m2, 0, 0);

    start:
    reset();

    write_byte(0xCC);   //Skip ROM
    write_byte(0x0F);   //Write scratchpad command
    write_byte(0x00);   //Write target address T0
    write_byte(0x00);   //Write target address T1
    for(j=0; j<32; j++){
        write_byte(w_data[j]);   //Write the complete scratchpad
    }
    t0 = read_byte();   // Getting 2 byte CRC but no checking is done
    t0 = read_byte();
    reset();

    write_byte(0xCC);   //Skip ROM
    write_byte(0xAA);   //Read scratchpad command
    t0 = read_byte();
    t1 = read_byte();
    es = read_byte();

    for(j=0; j<32; j++){
        buffer[j] = read_byte();
    }
    for(j=0; j<8; j++){             //Checking the data
        if(buffer[j]!= w_data[j]){
            char m3[] = "Point 2:";
            lcd_show(&m3, &buffer, 0, 0);
            while(1);
        }

    }
    reset();

    write_byte(0xCC);   //Skip ROM
    write_byte(0x55);   //Copy scratchpad command
    write_byte(t0);
    write_byte(t1);
    write_byte(es);
    dev_dir = 1;
    __delay_ms(7);      // Waiting for Tprog interval
    reset();

    write_byte(0xCC);   //Skip ROM
    write_byte(0xF0);   //Read memory command
    write_byte(0x00);   //Write target address T0
    write_byte(0x00);   //Write target address T1
    for(j=0; j<8; j++){
        r_data[j] = read_byte();
    }
    reset();

    for(j=0; j<8; j++){
        if(r_data[j] != w_data[j]){  // checking the data
            char m5[] = "Point 4:";
            lcd_show(&m5, &r_data, 0, 0);
            while(1);
        }
    }

    char m6[] = "Point 5:";
    lcd_show(&m6, &r_data, 0, 0);
    while(1);
    return 0;
}

El código de 1 cable que escribí:

#include <xc.h>
#include "1wire.h"

uint8_t reset(){
    /*Drive bus low, delay 480 ?s.
      Release bus, delay 70 ?s.
      Sample bus: 0 = device(s) present,
      1 = no device present
      Delay 410 ?s.*/
    reset:
    dev_dir = 0;
    dev_dout = 0;
    __delay_us(480);
    dev_dir = 1;
    __delay_us(70);
    if(dev_din != 0){
        __delay_us(410);
        goto reset;
    }
    __delay_us(410);
    return 1;    
}

void write_bit0(){
    /*Drive bus low, delay 60 ?s.
      Release bus, delay 10 ?s. */
    dev_dir = 0;
    dev_dout = 0;
    __delay_us(60);
    dev_dir = 1;
    __delay_us(10);
}

void write_bit1(){
    /*Drive bus low, delay 6 ?s.
      Release bus, delay 64 ?s. */
    dev_dir = 0;
    dev_dout = 0;
    __delay_us(6);
    dev_dir = 1;
    __delay_us(64);
}

uint8_t read_bit(){
    uint8_t x;
    /*Drive bus low, delay 6 ?s.
      Release bus, delay 9 ?s.
      Sample bus to read bit from slave.
      Delay 55 ?s. */
    dev_dir = 0;
    dev_dout = 0;
    __delay_us(6);
    dev_dir = 1;
    __delay_us(9);
    x = dev_din;
    __delay_us(55);
    return x;
}

void write_byte(uint8_t d){
    uint8_t i;
    for(i=0; i<8; i++){
        if((d&(1<<i)) == 0)
            write_bit0();
        else
            write_bit1();
    }
}

uint8_t read_byte(){
    uint8_t d=0, i;
    for(i=0; i<8; i++){
        d |= read_bit()<<i;
    }
    return d;
}

Y solo para completar mi código lcd.c:

#include <xc.h>
#include <stdint.h>
#include "config.h"
#include "lcd.h"

void lcd_init(){
    TRISD = 0x00;
    port =0;
    TRISEbits.RE0 = 0;
    rs = 0;
    TRISEbits.RE1 = 0;
    en = 0;
    __delay_ms(10);
    lcd_command(0x01);
    __delay_ms(50);
    lcd_command(0x38);
    __delay_ms(50);
    lcd_command(0x0C);
    __delay_ms(50);
}

void lcd_command(unsigned char x){
    port = x;
    rs = 0;
    en = 1;
    Nop();  Nop();  Nop();  Nop();
    Nop();  Nop();  Nop();  Nop();
    Nop();  Nop();  Nop();  Nop();
    Nop();  Nop();  Nop();  Nop();
    Nop();  Nop();   
    en = 0;
}

void lcd_putchar(unsigned char x){
    port = x;
    rs = 1;
    en = 1;
    Nop();  Nop();  Nop();  Nop();
    Nop();  Nop();  Nop();  Nop();
    Nop();  Nop();  Nop();  Nop();
    Nop();  Nop();  Nop();  Nop();
    Nop();  Nop();   
    en = 0;
    rs = 0;
}

void lcd_show(char *y1, char *y2, uint8_t o1, uint8_t o2){
    int i;
    lcd_command(0x80 + o1);
    __delay_ms(25);
    while(*y1 != '
#include <xc.h>
#include <stdint.h>
#include "config.h"
#include "lcd.h"
#include "1wire.h"

int main(void) {
    uint8_t w_data[8]={0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38};
    uint8_t t0, t1, es, j, r_data[8], buffer[32];

    char m1[10] = "Point 1:";
    char m2[10] = "abcdefgh";

    OSCCON = 0x77;
    lcd_init();
    lcd_show(&m1, &m2, 0, 0);

    start:
    reset();

    write_byte(0xCC);   //Skip ROM
    write_byte(0x0F);   //Write scratchpad command
    write_byte(0x00);   //Write target address T0
    write_byte(0x00);   //Write target address T1
    for(j=0; j<32; j++){
        write_byte(w_data[j]);   //Write the complete scratchpad
    }
    t0 = read_byte();   // Getting 2 byte CRC but no checking is done
    t0 = read_byte();
    reset();

    write_byte(0xCC);   //Skip ROM
    write_byte(0xAA);   //Read scratchpad command
    t0 = read_byte();
    t1 = read_byte();
    es = read_byte();

    for(j=0; j<32; j++){
        buffer[j] = read_byte();
    }
    for(j=0; j<8; j++){             //Checking the data
        if(buffer[j]!= w_data[j]){
            char m3[] = "Point 2:";
            lcd_show(&m3, &buffer, 0, 0);
            while(1);
        }

    }
    reset();

    write_byte(0xCC);   //Skip ROM
    write_byte(0x55);   //Copy scratchpad command
    write_byte(t0);
    write_byte(t1);
    write_byte(es);
    dev_dir = 1;
    __delay_ms(7);      // Waiting for Tprog interval
    reset();

    write_byte(0xCC);   //Skip ROM
    write_byte(0xF0);   //Read memory command
    write_byte(0x00);   //Write target address T0
    write_byte(0x00);   //Write target address T1
    for(j=0; j<8; j++){
        r_data[j] = read_byte();
    }
    reset();

    for(j=0; j<8; j++){
        if(r_data[j] != w_data[j]){  // checking the data
            char m5[] = "Point 4:";
            lcd_show(&m5, &r_data, 0, 0);
            while(1);
        }
    }

    char m6[] = "Point 5:";
    lcd_show(&m6, &r_data, 0, 0);
    while(1);
    return 0;
}
'){ lcd_putchar(*y1); __delay_ms(5); y1++; } lcd_command(0xC0 + o2); __delay_ms(25); while(*y2 != '
#include <xc.h>
#include "1wire.h"

uint8_t reset(){
    /*Drive bus low, delay 480 ?s.
      Release bus, delay 70 ?s.
      Sample bus: 0 = device(s) present,
      1 = no device present
      Delay 410 ?s.*/
    reset:
    dev_dir = 0;
    dev_dout = 0;
    __delay_us(480);
    dev_dir = 1;
    __delay_us(70);
    if(dev_din != 0){
        __delay_us(410);
        goto reset;
    }
    __delay_us(410);
    return 1;    
}

void write_bit0(){
    /*Drive bus low, delay 60 ?s.
      Release bus, delay 10 ?s. */
    dev_dir = 0;
    dev_dout = 0;
    __delay_us(60);
    dev_dir = 1;
    __delay_us(10);
}

void write_bit1(){
    /*Drive bus low, delay 6 ?s.
      Release bus, delay 64 ?s. */
    dev_dir = 0;
    dev_dout = 0;
    __delay_us(6);
    dev_dir = 1;
    __delay_us(64);
}

uint8_t read_bit(){
    uint8_t x;
    /*Drive bus low, delay 6 ?s.
      Release bus, delay 9 ?s.
      Sample bus to read bit from slave.
      Delay 55 ?s. */
    dev_dir = 0;
    dev_dout = 0;
    __delay_us(6);
    dev_dir = 1;
    __delay_us(9);
    x = dev_din;
    __delay_us(55);
    return x;
}

void write_byte(uint8_t d){
    uint8_t i;
    for(i=0; i<8; i++){
        if((d&(1<<i)) == 0)
            write_bit0();
        else
            write_bit1();
    }
}

uint8_t read_byte(){
    uint8_t d=0, i;
    for(i=0; i<8; i++){
        d |= read_bit()<<i;
    }
    return d;
}
'){ lcd_putchar(*y2); __delay_ms(5); y2++; } }

Soy capaz de simularlo correctamente en Proteus usando DS2433 pero las pruebas prácticas siempre fallan. Muestra el Punto 2 con el significado 'abcdefgh', los datos no se están leyendo en absoluto.

¿Cuáles podrían ser las trampas que debo revisar?

    
pregunta Harsha

0 respuestas

Lea otras preguntas en las etiquetas