SPI problem (PIC16F887)

Rasprava o PIC mikrokontrolerima, PIC projekti i drugo vezano za PIC-eve...

Moderators: pedja089, stojke369, [eDo], trax

Post Reply
BigL
Posts: 1
Joined: 13-11-2014, 19:09

SPI problem (PIC16F887)

Post by BigL »

Pozdrav ekipa,

Gnjavi me problem s čitanjem podataka preko SPI komunikacije. Sve radi super kad pošaljem podatke na uredaj (nRF24l01), samo kad mi on vrati ja uvijek čitam 0. Provjerio sam SDI pin na PIC-u s osciloskopom i vidim da dolaze bitovi sve kako spada. Neznam više šta uradit pa eto ako ima ko kakav prijedlog.
Kod je za mikroC PRO for PIC:
SPI.h

Code: Select all

sbit ChipSelect at PORTC.B0;
sbit CEpin at PORTC.B1;
unsigned short int temp;

void SPI_Init()
{
     SSPCON.SSPEN = 0;

     TRISC.RC5 = 0;             //data out
     TRISC.RC4 = 1;             //data in
     TRISC.RC3 = 0;             //clock
     TRISC.RC1 = 0;             //CEpin
     TRISC.RC0 = 0;             //ChipSelect

     SSPCON = 0x02;             // SPI Master mode, clock = Fosc/64
     SSPSTAT= 0x40;
     SSPCON.SSPEN = 1;          //enable SPI
     ChipSelect = 1;
     CEpin = 0;
}

short int SPI_write (short int addr)
{
      SSPBUF = addr;           // send data to the BUFFER
      while(!SSPSTAT.BF && !PIR1.SSPIF);      // wait for the tramsit/recieve to finish
                              // has to be SSPSTAT.BF not just BF!! (for some reason)
      temp = SSPBUF;           // clear the BF flag
      PIR1.SSPIF = 0;
      return temp;
}

short int SPI_read()
{
      SSPBUF = 0xFF;           // send a dummy byte
      while(!SSPSTAT.BF);
      //PORTA = SSPBUF;
      temp = SSPBUF;
      return temp;
}
sam program:

Code: Select all

unsigned short int dat = 0, tmp = 0;
// LCD module connections

sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

#include <SPI.h>

void nRF_Config()
{
     ChipSelect = 0;
     SPI_write(0x20);                   // write CONFIG
     SPI_write(0x03);                   // reciever
     ChipSelect = 1;
     asm nop;
     ChipSelect = 0;
     SPI_write(0x2A);                   // write receiver address
     SPI_write(0xF0);                   // for pipe 0
     SPI_write(0xF0);                   // address: 0xF0F0F0F0F0
     SPI_write(0xF0);
     SPI_write(0xF0);
     SPI_write(0xF0);
     ChipSelect = 1;
}

char txt[7];

void main() {
ANSEL = 0x00;
ANSELH = 0x00;

TRISA = 0;                          //LED-ica

PORTA.RA1 = 1;

SPI_Init();

nRF_Config();
//CEpin = 1;                              //receive mode

Lcd_Init();                        // Initialize LCD

Lcd_Cmd(_LCD_CLEAR);               // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
Lcd_Out(1,2,"hello");
  
delay_ms(1000);
while(1)
{
      ChipSelect = 0;
      dat = SPI_write(0xFF);                   //read status
      if(dat == 0) PORTA.RA1 = 0;
      else PORTA.RA1 = 1;
      Lcd_Chr(2,2, dat+48);
      ChipSelect = 1;
      asm nop;
}
}
Post Reply