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 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

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.