Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: [AVR-Chat] Bit-bang SPI, I have a code!

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]

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.