Bit banging SPI

General discussion on mikroElektronika website & forums.
Post Reply
Author
Message
plawrence14
Posts: 1
Joined: 15 Nov 2019 14:47

Bit banging SPI

#1 Post by plawrence14 » 15 Nov 2019 14:54

HI
I am using a PIC18F45K22 and am trying to control a sx1278 radio module through the SPI I have tried bit banging as per the code below but dont seam to be getting any output I can see the states by slowing the programdown and watchingthe different pins state and i believe they are looping through bytes correctly. can anyone have a look and see if they can see a mistake?

Thanks paul

program bitBangSPI

' Declarations section
dim NCS as sbit at LATA.4 'set a4 as nss output
dim MCK as sbit at LATC.3 'set c3 as clk outout
dim MOSI as sbit at LATC.5 'set c5 as write outpur
dim MISO as sbit at PORTC.4 'set c4 as read input

dim bitcnt as byte ' used as count for sending a byte
dim address as byte ' used to hold register address to write too
dim payload as byte ' used to hold data to be sent




Sub procedure sendByte()
MOSI = 0 'set MOSI start at 0
NCS = 0 'NCS low to select sx1278
MCK =0
'first write address byte
for bitcnt = 0 to 7 'counts through all bits in byte
MCK = 0 'clock low
if address AND 0x80 then 'checks MSB against address for 1to decide what to write to mosi
MOSI = 1
else
MOSI = 0
end if
delay_ms (200)
MCK = 1 'clock high
delay_ms (200)
address = address <<1
next bitcnt


'now write data byte
for bitcnt = 0 to 7 'counts through all bits in byte
MCK = 0 'clock low
if payload AND 0x80 then 'checks MSB against payloadfor 1to decide what to write to mosi
MOSI = 1
else
MOSI = 0
end if
delay_ms (200) 'just used so I can watch the bits on dev board
MCK = 1 'clock high
delay_ms (200) 'just used so I can watch the bits on dev board
payload = payload <<1
next bitcnt 'loop to next bit in byte
MCK = 0 'clock low
NCS = 1 'NCS sent low unselecting sx1278
end sub

sub procedure Standby()
address = 0x01 'OpMode reg address

payload = 0x89 ' in standby mode
sendByte()

end sub

sub procedure FSTX()
address = 0x01 'OpMode reg address

payload = 0x8A ' in FSTX mode
sendByte()

end sub

sub procedure transmit()
address = 0x01 'OpMode reg address

payload = 0x8B ' in TX mode (used transmit as name of sub as TX reserved
sendByte()

end sub

main:
' Main program

OSCCON = 0x60 'set to 8 negs
PLLEN_Bit = true 'multiple of 4 so 32 megs
ANSELB = 0 ' set port b to digital
ANSELC = 0 ' set port c to digital
ANSELD = 0 ' set port d to digital
TRISA = 0x00
TRISD = 0X00 'SET AS OUT PUT
TRISC = 0x10 'set port C as output except pin 4 thats an input
TRISB0_bit =1 'sets RB0 pin as a input
TRISB1_bit =1 'sets RB1 pin as a input
TRISB2_bit =1 'sets RB2 pin as a input
TRISB3_bit =1 'sets RB3 pin as a input
NCS = 1
Standby() ' in standby mode



while true
if (BUTTON(PORTB, 0, 1, 1)) then ' check scondition of button attached to B0 run if pressed
LATD = 0xFF

FSTX()

address = 0x00 'fifo reg address


payload = 0x01 'data to be transmitted

sendByte()

transmit()

LATD = 0x00
end if
wend



end.

Post Reply

Return to “Website & Forums General Discussion”