Make My Own SPI Driver

General discussion on mikroC PRO for AVR.
Post Reply
Author
Message
rrsquez
Posts: 117
Joined: 23 Feb 2011 21:35

Make My Own SPI Driver

#1 Post by rrsquez » 11 Nov 2015 20:28

Hello. I had trouble reading in a SPI port value using the SoftSPI commands, so I'm trying to write my own SPI driver. I don't see any examples on how to do this, but it seemed simple enough .... at first.

Every example I see shows how to use a library command like "button", or "keypad", but none of them show how to read in a single bit from a port. It seems trivial until something doesn't work right. If there is an example, please point it out.

I have included a stripped down version of my code. It only reads the ADC value from an LTC2400 chip and then displays it in an endless loop (2 seconds before reads).

I believe that I have properly configured the pin as an input (Port F, pin 7) using the correct data direction register.
I am using an oscilloscope to verify that ADC value is valid. I do see 32 bits clocking in response to my SCLK signal. I have slowed down things using a 50 uSecond delay. Regardless of the actual ADC voltage, I keep getting the same value back from my function. This tells me it's invalid.

I am getting values like "10", or "90" (decimal), but the SPI port value on the o-scope shows many more bits changing. When I change the voltage, I do see the higher order bits changing in response. This tells me the ADC is working, but my software is not.

The scheme in my code is that I set SCLK high after setting it low (which shifts in the data). During this HI time, there doesn't appear to be any transitions, so the data should be stable and valid.

I read in the single bit and test it. If it's HI, I add 1 to the existing value (starts with 0). This bit should always be zero to start with. If this is not the last bit, I shift left 1 bit. I repeat this process 31 times to acquire all 32 bits. This should be simple to do, but it's turning into a "project".

Would someone please take a look at my project? I am using an ATmega128A chip.

Thank you, Richard V

Post Reply

Return to “mikroC PRO for AVR General”