SPI hardware communication issue


Closed Thread
Results 1 to 7 of 7

Hybrid View

  1. #1
    Join Date
    Apr 2012
    Location
    Leicester UK
    Posts
    21

    Default Re: SPI hardware communication issue

    My thanks goes to Richard for his help. But as a benefit to anybody else struggling with this, my corrections to my code are as follows:

    I can confirm that CLKOUT configuration line was the largest issue, as this was blocking the output of the SDO1 therefore the signal to the SN74HC595 was not being sent.

    I have taken Richard's advice to implement using the interrupt flag to check when the signal had been sent to the SN74HC595, though the my next step is to have this trigger as interrupt to control the latch while performing another routine.

    Finally changed the clock edge bit such that the signal is sent on the correct edge of the clock signal for the serial data output so the data arrives at the SN74HC595 correctly.

    This is my modified code based on my initial code:

    Code:
    #CONFIG
                 __config        _CONFIG1,    _CLKOUTEN_OFF
    #ENDCONFIG
    
    TxData var BYTE
    
    define OSC 4
    OSCCON = %01101010
    
    HC_Data var PORTA.4       'SDO
    HC_Clk var PORTC.0        'SCK
    HC_latch var PORTA.2      'Digital output
    
    LED_YELLOW var PORTA.1
    LED_GREEN var PORTC.3
    LED_RED var PORTA.0
    
    SSP1IF VAR PIR1.3         'SPI interrupt flag
    
    TRISA = 0                 'all digital outputs
    PORTA = 0                 'make all pins low
    ANSELA = 0  
    
    TRISC = %00000010         'all digital outputs except port 1
    PORTC = 0                 'make all pins low
    ANSELC = 0
    
    APFCON0 = %01000000          'set SPI pin alternative
    SSP1STAT = %01000000
    SSP1CON1 = %00100010
    
    'prove LED function
    LED_YEllow = 1
    LED_Green = 1
    LED_red = 1
    pause 200
    LED_YEllow = 0
    LED_Green = 0
    LED_red = 0
    
    Main:                                   'set up sequence of signals for output
    
                        LED_Yellow = 1
    
    TXDATA = %00000000
    Gosub Send_Data
    TxData = %10101010
    Gosub Send_Data
    TxData = %01010101
    Gosub Send_Data
    TXDATA = %00000000
    Gosub Send_Data
    TxData = %10000000
    Gosub Send_Data
    TxData = %01000000
    Gosub Send_Data
    TxData = %00100000
    Gosub Send_Data
    
                        LED_Yellow = 0
                                                                 
                        LED_gREEN = 1
    
    TxData = %00010000
    Gosub Send_Data
    TxData = %00001000
    Gosub Send_Data
    TxData = %00000100
    Gosub Send_Data
    TXDATA = %00000010
    Gosub Send_Data
    TXDATA = %00000001
    Gosub Send_Data
    TXDATA = %11111111
    Gosub Send_Data
    
                        LED_GREEN = 0
    
    goto main
    end
    
    Send_Data :
                       toggle LED_RED
    SSP1IF = 0
    SSP1BUF = TXDATA
    while ! SSP1IF :wend      ;WAIT TILL TX COMPLETES
    
    HC_Latch=1
    'could need a delay here on high processor speeds or no operation commands
    HC_Latch=0
    
    pause 500
    
    return

  2. #2
    Join Date
    Aug 2011
    Posts
    457

    Default Re: SPI hardware communication issue

    though the my next step is to have this trigger as interrupt to control the latch while performing another routine
    It might be a lot less complicated to just increase the osc speed from 4MHz and clock the SPI faster. Running at 32MHz with SPICLK=8MHz it takes <2us to send out a byte... hardly worth the bother. At the speed you're running now it takes longer than that just to branch to an interrupt.

    I'd also ditch the PAUSE in the Send_Data routine.

Similar Threads

  1. Using SPI hardware
    By harryweb in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 13th November 2013, 19:19
  2. Hardware SPI ?
    By Dick Ivers in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 27th October 2013, 06:42
  3. SPI on a pic without hardware SPI?
    By modifyit in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 26th April 2006, 13:29
  4. how can i use I2C and SPI hardware
    By micro in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 22nd December 2005, 15:33
  5. Hardware SPI
    By Ron Marcus in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 7th June 2005, 14:23

Members who have read this thread : 0

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts