Menu ▾ ▴

Re-DEFINE #DEFINE SPI_BAUD_RATE_REGISTER ?

5 days ago
5 days ago
  • Roger Jönsson

    Roger Jönsson - 5 days ago

    I want to select one of two SPI speeds at startup by reading a pin and changing SPI_BAUD_RATE_REGISTER to a suitable value.
    Can I change the SPI_BAUD_RATE_REGISTER figure after it has been defined in GC BASIC or is my workaround THE way to go?:
    I tried to do this at first, which failed:
    "Warning: SCRIPT/Constant: SPI_BAUD_RATE_REGISTER equates to 15 and cannot be reassigned to equate to = 62"

    Dim i2s_or_DACMAKEUP as Byte
    If PortA.6=1 then i2s_or_DACMAKEUP=1
    If PortA.6=0 then i2s_or_DACMAKEUP=0
    
    If i2s_or_DACMAKEUP=0 then
        #DEFINE HWSPIMode MasterFast 
        #DEFINE SPI_BAUD_RATE_REGISTER = 15 //2MHz 
        #DEFINE HWSPIClockMode  SPI_SS_0 + SPI_CPOL_0 + SPI_CPHA_0
        SPIMode (HWSPIMode,  HWSPIClockMode )
    End if
    
    If i2s_or_DACMAKEUP=1 then
        #DEFINE HWSPIMode MasterFast
        #DEFINE SPI_BAUD_RATE_REGISTER = 62
        #DEFINE HWSPIClockMode  SPI_SS_0 + SPI_CPOL_0 + SPI_CPHA_0
        SPIMode (HWSPIMode,  HWSPIClockMode )
    End if
    

    Is there a more GCbasicy way or is this the way to go? It works, but I'm still curious... :

    //...
        #DEFINE HWSPIMode   MasterFast
        #DEFINE SPI_BAUD_RATE_REGISTER = 15
        #DEFINE HWSPIClockMode  SPI_SS_0 + SPI_CPOL_0 + SPI_CPHA_0
        SPIMode (HWSPIMode,  HWSPIClockMode )
    
    If i2s_or_DACMAKEUP=0 then
    SPI1BAUD = 15
    End if
    
    If i2s_or_DACMAKEUP=1 then
    SPI1BAUD = 62
    End if
    

    Edit: It is running on an 18FxxQ84

     

    Last edit: Roger Jönsson 5 days ago
  • Anobium

    Anobium - 5 days ago

    Let me have a look. But, I some code that I can compile. Just the basic code.

    Thanks

     
  • Anobium

    Anobium - 5 days ago

    Sorry, I do not need the code.

    You method is perfect. It works well and will change the SPIBAUD as needed. Nice.

     

Log in to post a comment.