i am a beginner i am facing problem while using hardware spi. i need a software spi code for micro controller . please......... help me to get my project run properly,
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
i am a beginner i am facing problem while using hardware spi. i need a software spi code for micro controller . please......... help me to get my project run properly,
Biplab,
why not trying to get rid of the hardware issues? There are code examples showing the use of the hardware SPI module.
Anyway - a software SPI is like this:
#define DATA_OUT_PORT P1OUT
#define DATA_OUT_PIN 0x01
#define CLOCK_OUT_PORT P1OUT
#define CLOCK_OUT_PIN 0x02
void bitbang_data_out( uint8_t data );
void main( void )
{
...
CLOCK_OUT_PORT &= ~CLOCK_OUT_PIN;
...
bitbang_data_out( 0x21 );
...
}
void bitbang_data_out( uint8_t data )
{
uint8_t i;
for( i = 0; i < 8; i++ )
{
if( data & 0x80 )
{
DATA_OUT_PORT |= DATA_OUT_PIN;
}
else
{
DATA_OUT_PORT &= ~DATA_OUT_PIN;
}
CLOCK_OUT_PORT |= CLOCK_OUT_PIN;
CLOCK_OUT_PORT &= ~CLOCK_OUT_PIN;
data <<= 1;
}
}
Dennis
If you use the few lines I wrote for bitbanging the data out, then you don't have to think about the state of your SPI "bus". It is during the normal program flow, so it is busy as long as your program is currently executing this function. And you cannot adjust the clock speed in that case. It is the execution speed of the instructions. You could insert delays, but that would be the worst. The bitbang method isn't ideal when you could use an interrupt driven method instead. But for a few bytes it might be OK. If you want to send data to a graphic display that has hundreds or thousands of bytes, then you wouldn't be able do much else with your processor since it is busy with sending your data all the time.
You can easily send 32 bits of data, too. You can either split it into 4 bytes or you could adjust the function for that.
void bitbang_data_out( uint32_t data )
{
uint8_t i;
for( i = 0; i < 32; i++ )
{
if( data & 0x80000000 )
{
DATA_OUT_PORT |= DATA_OUT_PIN;
}
else
{
DATA_OUT_PORT &= ~DATA_OUT_PIN;
}
CLOCK_OUT_PORT |= CLOCK_OUT_PIN;
CLOCK_OUT_PORT &= ~CLOCK_OUT_PIN;
data <<= 1;
}
}
Dennis
i want to interface this spi with 74HC595 shift register ic. thus i required a synchronous clock having the same freq with which data is transmitted. how do i configure this with help of default clock of msp430f5510??
**Attention** This is a public forum