In SPI,slave send wrong data from master

Discussion in 'Embedded Systems and Microcontrollers' started by franklin75, Jul 5, 2016.

  1. franklin75

    Thread Starter New Member

    Jun 11, 2016
    14
    1
    Hi everyone,i have a project about spi communication.My circuit have master pic,slave pic,lcd,lm35 .Lcd connect with master pic,lm35 connect with slave pic.Lm 35 is read using ADC in slave pic then slave pic send temprature from master pic and temprature is could read in lcd in master pic.My problem is slave send data and lcd work properly but temprature value everytime wrong and stable.i send code and circuit.
    ****master*****
    #include <spi_6_master.h>
    #define use_portb_lcd TRUE
    #include <lcd.c>

    void main(){
    float sicaklik,voltaj;
    int16 x;
    lcd_init();
    set_tris_a(0x01);
    set_tris_b(0x00);
    setup_spi(SPI_MASTER | SPI_L_TO_H| SPI_CLK_DIV_16);
    printf(lcd_putc,"\fsicaklik=");
    while(1) {
    spi_write(10);
    delay_us(100);
    x=spi_read();
    delay_us(100);
    voltaj=(0.0048828125*x)*1000;
    delay_ms(100);
    sicaklik=((voltaj/10)+2)/64;
    delay_ms(200);
    lcd_gotoxy(10,1);
    printf(lcd_putc,"%5.1f'C",sicaklik);
    delay_ms(50);
    }
    }
    ******slave********

    #include <spi_6_slave.h>
    #define use_portb_lcd TRUE
    #include <lcd.c>
    void main(){

    float voltaj,sicaklik;
    int y;
    int16 bilgi;
    lcd_init();
    set_tris_a(0x01);
    setup_adc(adc_clock_div_32);
    setup_adc_ports(AN0);
    set_adc_channel(0);
    delay_us(20);
    setup_spi(SPI_SLAVE | SPI_L_TO_H| SPI_CLK_DIV_16|SPI_SS_DISABLED);
    printf(lcd_putc,"\fSicaklik=");


    while(1) {
    y=spi_read();
    delay_us(100);
    bilgi=read_adc();
    voltaj=(0.0048828125*bilgi)*1000;
    sicaklik=((voltaj/10)+2)/64;
    lcd_gotoxy(10,1);
    printf(lcd_putc,"%5.1f'C",sicaklik);
    delay_ms(100);

    if(y==10){
    //spi_write(sicaklik);
    spi_write(bilgi);
    delay_us(100);
    output_toggle(pin_a1);
    delay_ms(200);
    }
    }
    }
     
Loading...