• AVR Freaks

AnsweredHot!SPI flag not setting

Author
muralysunam
Senior Member
  • Total Posts : 134
  • Reward points : 0
  • Joined: 2012/11/15 11:28:19
  • Location: 0
  • Status: offline
2020/07/08 14:01:34 (permalink)
0

SPI flag not setting

Hi all,
 
I am trying to interface PIC18F4550/4520 with W5500 ethernet module.
 
I am using PIC18F4520 now. I am using the following code for SPI initalization as master.
 
#define spi_enable() SSPCON1bits.SSPEN = 1
#define spi_disable() SSPCON1bits.SSPEN = 0
#define spi_collision SSPCON1bits.WCOL
#define spi_interrupt_flag PIR1bits.SSPIF
#define spi_interrupt_enable PIE1bits.SSPIE
#define spi_in_use v_sts.b0 //high when a module uses SPI via this library.

void spi_master_init(uint8_t speed, uint8_t mode)
{
    spi_disable();
    spi_enable();
    ADC_SS = 1;
    w5500_SS = 1;
    //add other SPI devices here
    spi_in_use = 0;

    TRISCbits.RC5 = 0;//SDO
    TRISCbits.RC4 = 1;//SDI
    TRISCbits.RC4 = 0;//SCLK
    
    if(mode == 3)
    {
        SSPCON1bits.CKP = 1;
        SSPSTATbits.SMP = 1;
    }
    else if(mode == 2)
    {
        SSPCON1bits.CKP = 1;
        SSPSTATbits.SMP = 0;
    }
    else if(mode == 1)//this for w5500
    {
        SSPCON1bits.CKP = 0;
        SSPSTATbits.SMP = 1;
    }
    else
    {
        SSPCON1bits.CKP = 0;
        SSPSTATbits.SMP = 0;
    }
    SSPSTATbits.CKE = 0;
    SSPCON1 |= (0xF0|speed);
    spi_collision = 0;
    spi_interrupt_flag = 0;
    spi_interrupt_enable = 0;//interrupt not using
    
}

 
The above code is available in another header file (my own library like file).
Then I use the below showing code in main function
 
void main(void) 
{
    uCinit();
    blink();
    lcd_init();
    tmr0_init(tmr0_200ms);
    lcd_update_time();
    spi_master_init(0,1);
    //adc_init();
    tmr0_start();
    /*int abc = 0,ov = 0;
    while(1)//TIMER test
    {
        lcd_update_time();
        lcd_cursor(2,1);
        lcd_print_int(abc++);
        lcd_cursor(3,1);
        lcd_print_int(ov);
        if(tmr0_of == 1)
        {
           ov++;
           tmr0_of = 0;
        }
    }*/
    int flag=0,collision =0;
    spi_en(w5500_addr);
    while(1)
    {
        SSPBUF = 0x80;
        lcd_cursor(2,1);
        lcd_print_int(flag);
        lcd_cursor(3,1);
        lcd_print_int(collision);
        if(spi_interrupt_flag == 1)
        {
            flag++;
        }
        if(spi_collision == 1)
        {
            collision++;
        }
    }
}

 
The problem is, after loading SSPBUF with a value, it is not raising spi_interrupt_flag. I would like to ensure the data is sent before going to the next operation. Above shown main function is just to show the issue. Actually I am having a function to transmit the data and it waits for flag raise in order to exit from the function (which never happens). 
 
I know I am doing something wrong but I couldn't figure it out even after wasting 5-6 hours in this issue. I tried reading lot of pdf issued by microchip and datasheet also.
 
 
Thanks is advance for any replies.
#1
upand_at_them
Super Member
  • Total Posts : 585
  • Reward points : 0
  • Joined: 2005/05/16 07:02:38
  • Location: Pennsylvania
  • Status: online
Re: SPI flag not setting 2020/07/08 14:52:36 (permalink)
0
In my code I don't use interrupts and I don't use the interrupt flag.  This is my function to transmit a byte (in JAL).  I only reference the BF flag bit.
 
 
function SPI_Recv( byte in b ) return byte is
 SSPBUF = b
 while (SSPSTAT_BF == 0) loop end loop
 return SSPBUF
end function

post edited by upand_at_them - 2020/07/08 14:53:42
#2
ric
Super Member
  • Total Posts : 28009
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: SPI flag not setting 2020/07/08 15:03:41 (permalink) ☼ Best Answerby muralysunam 2020/07/08 19:52:48
0
You set the wrong TRIS bit for SCK
    TRISCbits.RC4 = 1;//SDI
    TRISCbits.RC4 = 0;//SCLK
should be RC3.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#3
Aussie Susan
Super Member
  • Total Posts : 3737
  • Reward points : 0
  • Joined: 2008/08/18 22:20:40
  • Location: Melbourne, Australia
  • Status: offline
Re: SPI flag not setting 2020/07/08 19:31:53 (permalink) ☄ Helpfulby muralysunam 2020/07/08 19:54:22
0
Also, not your problem but in 'spi_master_init()' what do you mean by "mode"?
The reason I ask is that there is a common usage of the term when applied to SPI interfaces that DOES NOT relate to the settings of CKP and CKE (and in particular to the settings you have used for CKP). To properly set up the Tx side of things you need to  set CKE as well.
SMP is used for the Rx side of the SPI module.
Susan
#4
muralysunam
Senior Member
  • Total Posts : 134
  • Reward points : 0
  • Joined: 2012/11/15 11:28:19
  • Location: 0
  • Status: offline
Re: SPI flag not setting 2020/07/08 19:47:37 (permalink)
0
upand_at_them
In my code I don't use interrupts and I don't use the interrupt flag.  This is my function to transmit a byte (in JAL).  I only reference the BF flag bit.
 
 
function SPI_Recv( byte in b ) return byte is
 SSPBUF = b
 while (SSPSTAT_BF == 0) loop end loop
 return SSPBUF
end function



Hi,
Thanks for the reply. I tried that way also. But it is not working in my case. I may be doing something too foolish.
post edited by muralysunam - 2020/07/08 19:53:29
#5
muralysunam
Senior Member
  • Total Posts : 134
  • Reward points : 0
  • Joined: 2012/11/15 11:28:19
  • Location: 0
  • Status: offline
Re: SPI flag not setting 2020/07/08 19:49:29 (permalink)
+2 (2)
ric
You set the wrong TRIS bit for SCK
   TRISCbits.RC4 = 1;//SDI
   TRISCbits.RC4 = 0;//SCLKshould be RC3. 

Thanks a lot. That saved my day.
#6
muralysunam
Senior Member
  • Total Posts : 134
  • Reward points : 0
  • Joined: 2012/11/15 11:28:19
  • Location: 0
  • Status: offline
Re: SPI flag not setting 2020/07/09 13:49:34 (permalink)
0
Aussie Susan
Also, not your problem but in 'spi_master_init()' what do you mean by "mode"?The reason I ask is that there is a common usage of the term when applied to SPI interfaces that DOES NOT relate to the settings of CKP and CKE (and in particular to the settings you have used for CKP). To properly set up the Tx side of things you need to  set CKE as well.SMP is used for the Rx side of the SPI module.Susan

Thanks. I will try to implement as per current convention.
#7
Jump to:
© 2020 APG vNext Commercial Version 4.5