Bit-bang SPI
2007-05-30 by Brian Gregory
Yahoo Groups archive
Index last updated: 2026-04-28 22:41 UTC
Thread
2007-05-30 by Brian Gregory
Can anyone point me to a bit-bang SPI example? I'm trying to talk to one of these: http://www.parallax.com/detail.asp?product_id=29123 Perhaps someone has an equivalent to SHIFTOUT and SHIFTIN in C? Thanks, Brian Gregory
2007-05-31 by David VanHorn
On 5/30/07, Brian Gregory <bgregory@g-webdesign.com> wrote: > Can anyone point me to a bit-bang SPI example? I'm trying to talk to one of > these: > http://www.parallax.com/detail.asp?product_id=29123 > > Perhaps someone has an equivalent to SHIFTOUT and SHIFTIN in C? > Haven't done it in C, but I've had a lot of success in ASM. It's pretty easy. I've done multiple devices, one on the hardware SPI at high speed, while talking to another one at lower speed in bit-bang mode.
2007-05-31 by Leon
----- Original Message -----
From: "Brian Gregory" <bgregory@g-webdesign.com> To: <AVR-Chat@yahoogroups.com> Sent: Wednesday, May 30, 2007 9:36 PM Subject: [AVR-Chat] Bit-bang SPI > Can anyone point me to a bit-bang SPI example? I'm trying to talk to one > of > these: > http://www.parallax.com/detail.asp?product_id=29123 > > Perhaps someone has an equivalent to SHIFTOUT and SHIFTIN in C? > Atmel has an app note with code. Leon -- Leon Heller Amateur radio call-sign G1HSM Yaesu FT-817ND and FT-857D transceivers Suzuki SV1000S motorcycle leon355@btinternet.com http://www.geocities.com/leon_heller
2007-05-31 by briangregory82
Wow! Thanks for everyone's help. Brian
2007-05-31 by Ralph Hilton
On Wed, 30 May 2007 16:36:56 -0400 you wrote:
>Can anyone point me to a bit-bang SPI example? I'm trying to talk to one of
>these:
>http://www.parallax.com/detail.asp?product_id=29123
>
>Perhaps someone has an equivalent to SHIFTOUT and SHIFTIN in C?
>
>Thanks,
>Brian Gregory
Here's code for bit banging an ADC chip showing how a shift in and out can be
done:
#define LTCSDI PORTD.2
#define LTCCS PORTD.3
#define LTCSDO PIND.4
#define LTCSCK PORTD.5
char i;
bit inbit;
bit msb;
bit datasign;
bit adcflag;
flash char LTCspeeds[10]={16,8,24,4,20,12,28,2,18,30}; //stored in reverse
order for easy out with >>
char speed=3, speedout;
char outbyte1, outbyte2, outbyte3, outbyte4;
unsigned long int rawdataval, mcdataval;
unsigned long int lastmcdataval;
void readbit(void)
{
LTCSCK=1;
delay_us(2);
inbit=LTCSDO;
delay_us(2);
LTCSCK=0;
}
void setsdi(void)
{
LTCSDI=speedout&1;
speedout>>=1;
}
void main(void)
{
sleep_enable();
LTCCS=1; //start conversion
idle(); //take a nap until timer 0 interrupt
while (1)
{
if (adcflag)
{
speedout=LTCspeeds[speed];
rawdataval=0;
LTCCS=0;
while (LTCSDO==1);
setsdi();
readbit(); //EOC
setsdi();
readbit(); //dummy
setsdi();
readbit(); //sign
datasign=inbit;
setsdi();
readbit(); //msb
msb=inbit;
setsdi();
for (i=0;i<23;i++)
{
readbit();
rawdataval<<=1;
rawdataval+=inbit;
}
LTCCS=1; //start next conversion
--
Ralph Hilton
http://www.ralphhilton.org
C-Meter: http://www.cmeter.org
FZAOINT http://www.fzaoint.net2007-05-31 by BobGardner@aol.com
> Can anyone point me to a bit-bang SPI example?
=============================================
#define DATHI() PORTB |= 0x01;
#define DATLO() PORTB &= ~0x01;
#define CLKHI() PORTB |= 0x02;
#define CLKLO() PORTB &= ~0x02;
void bb(char n){
unsigned char msk;
msk=0x80;
while(msk){
if(msk & n) DATHI(); else DATLO();
CLKHI();
CLKLO();
msk >>=1;
}
}
________________________________________________________________________
AOL now offers free email to everyone. Find out more about what's free from AOL at AOL.com.
[Non-text portions of this message have been removed]2007-06-01 by maria mastik
Hi
I have done such thing before.
My code is like bellow
the function "Source_transmit()" transsends 4 bytes in each call.
Mohammad.
code -->
#define source_SCK PORTC.2
#define source_MOSI PORTC.4
#define source_MISO PINC.3
#define source_RST PORTC.5
//========================================================================
unsigned char inbyte[4],outbyte[4],i;
//========================================================================
void source_transmit(void)
{
source_SCK=0;
inbyte[0]=inbyte[1]=inbyte[2]=inbyte[3]=0;
for(i=0;i<4;i++)
{
source_MOSI=(outbyte[i] & 0b10000000);delay_us
(10);source_SCK=1;delay_us(10);inbyte[i]<<=1;if (source_MISO) inbyte[i]
++;source_SCK=0;
source_MOSI=(outbyte[i] & 0b01000000);delay_us
(10);source_SCK=1;delay_us(10);inbyte[i]<<=1;if (source_MISO) inbyte[i]
++;source_SCK=0;
source_MOSI=(outbyte[i] & 0b00100000);delay_us
(10);source_SCK=1;delay_us(10);inbyte[i]<<=1;if (source_MISO) inbyte[i]
++;source_SCK=0;
source_MOSI=(outbyte[i] & 0b00010000);delay_us
(10);source_SCK=1;delay_us(10);inbyte[i]<<=1;if (source_MISO) inbyte[i]
++;source_SCK=0;
source_MOSI=(outbyte[i] & 0b00001000);delay_us
(10);source_SCK=1;delay_us(10);inbyte[i]<<=1;if (source_MISO) inbyte[i]
++;source_SCK=0;
source_MOSI=(outbyte[i] & 0b00000100);delay_us
(10);source_SCK=1;delay_us(10);inbyte[i]<<=1;if (source_MISO) inbyte[i]
++;source_SCK=0;
source_MOSI=(outbyte[i] & 0b00000010);delay_us
(10);source_SCK=1;delay_us(10);inbyte[i]<<=1;if (source_MISO) inbyte[i]
++;source_SCK=0;
source_MOSI=(outbyte[i] & 0b00000001);delay_us
(10);source_SCK=1;delay_us(10);inbyte[i]<<=1;if (source_MISO) inbyte[i]
++;source_SCK=0;
}
}
//===================================================================
void main(void)
{
unsigned char temp[2],a,b;
// Crystal Oscillator division factor: 1
CLKPR=0x80;
CLKPR=0x00;
// Input/Output Ports initialization
PORTB=0b00111100;DDRB=0b00000011;
PORTC=0b01001000;DDRC=0b00110100;
PORTD=0b00000010;DDRD=0b11101101;
ACSR=0x80;ADCSRB=0x00;
while (1)
{
outbyte[0]=12;outbyte[1]=98;outbyte[2]=5;outbyte[3]=34;
source_transmit();
temp1=inbyte[0];
temp2=inbyte[1];
a=inbyte[2];
b=inbyte[3];
};
}
David VanHorn <microbrix@gmail.com> wrote:
On 5/30/07, Brian Gregory <bgregory@g-webdesign.com> wrote:
> Can anyone point me to a bit-bang SPI example? I'm trying to talk to one of
> these:
> http://www.parallax.com/detail.asp?product_id=29123
>
> Perhaps someone has an equivalent to SHIFTOUT and SHIFTIN in C?
>
Haven't done it in C, but I've had a lot of success in ASM.
It's pretty easy. I've done multiple devices, one on the hardware SPI
at high speed, while talking to another one at lower speed in bit-bang
mode.
---------------------------------
Get the Yahoo! toolbar and be alerted to new email wherever you're surfing.
[Non-text portions of this message have been removed]2007-06-01 by Ralph Hilton
On Fri, 1 Jun 2007 08:31:28 -0700 (PDT) you wrote:
>Hi
> I have done such thing before.
> My code is like bellow
> the function "Source_transmit()" transsends 4 bytes in each call.
>
> Mohammad.
I'm glad I'm not responsible for maintaining that code! Try some spacing,
separate lines for separate statements and liberal use of comments.
Compact code might look clever but think of the guy who has to figure out what
it does!
> code -->
>
>
> #define source_SCK PORTC.2
>#define source_MOSI PORTC.4
>#define source_MISO PINC.3
>#define source_RST PORTC.5
>
>//========================================================================
> unsigned char inbyte[4],outbyte[4],i;
> //========================================================================
>
>void source_transmit(void)
>{
>source_SCK=0;
>inbyte[0]=inbyte[1]=inbyte[2]=inbyte[3]=0;
>
>for(i=0;i<4;i++)
> {
> source_MOSI=(outbyte[i] & 0b10000000);delay_us
> (10);source_SCK=1;delay_us(10);inbyte[i]<<=1;if (source_MISO) inbyte[i]
> ++;source_SCK=0;
> source_MOSI=(outbyte[i] & 0b01000000);delay_us
> (10);source_SCK=1;delay_us(10);inbyte[i]<<=1;if (source_MISO) inbyte[i]
> ++;source_SCK=0;
> source_MOSI=(outbyte[i] & 0b00100000);delay_us
> (10);source_SCK=1;delay_us(10);inbyte[i]<<=1;if (source_MISO) inbyte[i]
> ++;source_SCK=0;
> source_MOSI=(outbyte[i] & 0b00010000);delay_us
> (10);source_SCK=1;delay_us(10);inbyte[i]<<=1;if (source_MISO) inbyte[i]
> ++;source_SCK=0;
> source_MOSI=(outbyte[i] & 0b00001000);delay_us
> (10);source_SCK=1;delay_us(10);inbyte[i]<<=1;if (source_MISO) inbyte[i]
> ++;source_SCK=0;
> source_MOSI=(outbyte[i] & 0b00000100);delay_us
> (10);source_SCK=1;delay_us(10);inbyte[i]<<=1;if (source_MISO) inbyte[i]
> ++;source_SCK=0;
> source_MOSI=(outbyte[i] & 0b00000010);delay_us
> (10);source_SCK=1;delay_us(10);inbyte[i]<<=1;if (source_MISO) inbyte[i]
> ++;source_SCK=0;
> source_MOSI=(outbyte[i] & 0b00000001);delay_us
> (10);source_SCK=1;delay_us(10);inbyte[i]<<=1;if (source_MISO) inbyte[i]
> ++;source_SCK=0;
> }
>}
>//===================================================================
> void main(void)
>{
>unsigned char temp[2],a,b;
> // Crystal Oscillator division factor: 1
>CLKPR=0x80;
>CLKPR=0x00;
> // Input/Output Ports initialization
>PORTB=0b00111100;DDRB=0b00000011;
>PORTC=0b01001000;DDRC=0b00110100;
>PORTD=0b00000010;DDRD=0b11101101;
> ACSR=0x80;ADCSRB=0x00;
> while (1)
> {
>
> outbyte[0]=12;outbyte[1]=98;outbyte[2]=5;outbyte[3]=34;
> source_transmit();
>
> temp1=inbyte[0];
> temp2=inbyte[1];
> a=inbyte[2];
> b=inbyte[3];
> };
>}
--
Ralph Hilton
http://www.ralphhilton.org
C-Meter: http://www.cmeter.org
FZAOINT http://www.fzaoint.net