Using USI-SPI on Attiny 4313 to receive data from Attiny24A

Discussions on the software and hardware for Atmel's STK standard.

Moderator: phalanx

Post Reply
firstoption
Posts: 2
Joined: Tue Aug 19, 2014 7:34 am

Using USI-SPI on Attiny 4313 to receive data from Attiny24A

Post by firstoption » Tue Aug 19, 2014 7:40 am

Dear Sir/Ma,

Please I need your Support.I am not too sure if this Kind of configuration is possible.I am trying to use the Usi-Spi of Attiny4313(as Master)to receive DATA from Attiny24A(Slave).I am using the GPIO PINs of Attiny24A to send the DATA to the Master.I have already tried this configuration on a breadboard but instead of 4 LEDs ON and 4 LEDs OFF,all my Output Leds were blinking then after sometime all LEDs went OFF except one LED.Below are my lines of code and Pin Configuration.Both Master and Slave are runing @1MHz System clock.

PIN CONFIGURATION


ATTINY4313-------------------ATTINY24A

PB5/DATA_IN-----------------DATA_OUT/PB1

PB6/DATA_OUT----------------DATA_IN/PA1

PB7/USCK--------------------CLK_IN/PA7



Code:



//USI-SPI MASTER CODE

Code: Select all


 #include <avr/io.h> 
 #include <avr/delay.h> 
 //#include <avr/iotn2313.h> 

  #define CTRL_PORT   DDRB 
  #define DATA_PORT   PORTB 
  #define CLK_PIN     PORTB7 
  #define DI_PIN      PORTB5 
  #define DO_PIN      PORTB6 

  //USI-SPI master 

  void init() 
  { 
     //Configure outputs 
     CTRL_PORT |= ( 1<<DO_PIN) | (1<< CLK_PIN); 
  } 

  unsigned char Transmit(unsigned char data) 
  { 
     //Load data 
     USIDR = data; 
       
     //Clear the USI counter overflow flag 
     USISR = (1<< USIOIF); 

     do 
     { 
     USICR = (1<< USIWM0) | (1<< USICLK) | (1<< USICS1) | (1<< USITC); 
     } while((USISR & (1<< USIOIF)) == 0); 
       
     return USIDR; 
  } 

  int main(void) 
  { 
      DDRD |= 0xFF; //Led Output 
      unsigned char  dummy_value =20; 
      
      init(); 
   
     while(1) 
     
      {  
         
       
     
          PORTD =  Transmit(dummy_value); 
     
          } 
         
  } 





//USI-SPI SLAVE CODE


Code:

Code: Select all


 #include <avr/io.h> 
 #include <avr/delay.h> 
 #include <avr/interrupt.h> 


  ClearClock() 
 { 
    PORTA &= ~(1 << PINA7);  // Clear 
 } 

 void SetClock() 
 { 
    PORTA |= (1 << PINA7);   // Set 
 } 

 void ToggleClock() 
 { 
    SetClock(); 

    ClearClock(); 
 } 


 void SetData() 
 { 
    PORTB |= (1 << PINB1);   // Clear 
 } 

 void ClearData() 
 { 
    PORTB &= ~(1 << PINB1);  // Set 
 } 

 void send_8Bit(unsigned int data10) 

 { 
    int i=7; 
    do 
    { 
       if (((data10 & (1<<i))>>i) == 1) 
       SetData(); 
       else 
       ClearData(); 
       ToggleClock(); 
       i=i-1; 
    } 
    while (i >= 0); 
 } 

 int main(void) 
 { 
    unsigned int  data; 
    DDRA |=0b01111111; 
    DDRB |=(1<<PINB1); 
    data =0b01010101; 
     
     
 while(1) 
      { 
        
      send_8Bit(data); 
     
     } 

  } 





NOTE:The USART and I2C on both Attiny4313 and Attiny24A are already in use.I am using USART on Attiny4313 to send DATA to the PC and the I2C on Attiny24A to read Temperature from a Temperature-Sensor.So I am somehow left with USI-SPI and GPIO PINs for communication between the Master and the slave.Sir I would be very grateful if you could put me through on how to adjust these codes in order to get the desired output

Thank you for the Support.Best regards.

Osanyinpeju.

User avatar
Ross Robotics
Support Volunteer
Posts: 2007
Joined: Thu Jun 07, 2012 9:33 pm
Contact:

Re: Using USI-SPI on Attiny 4313 to receive data from Attin

Post by Ross Robotics » Tue Aug 19, 2014 2:03 pm

You can have multiple SPI devices communicating on the same bus.. They need different addresses individually..
------------------------------------------------------------------------------------------------------------------------------------------------------------
//codlink
Available for circuit and PCB design, just PM me!

n1ist
Support Volunteer
Posts: 1009
Joined: Wed Mar 22, 2006 11:02 am

Re: Using USI-SPI on Attiny 4313 to receive data from Attin

Post by n1ist » Wed Aug 20, 2014 6:01 am

codlink wrote:You can have multiple SPI devices communicating on the same bus.. They need different addresses individually..
SPI uses chip selects; I2C uses addresses...

I don't see a chip select between the two chips; how are you synchronizing the transfer? Also, while I haven't used USI for SPI, the bigger chips all need /SS tied correctly or the SPI hardware will abort.

/mike

User avatar
Ross Robotics
Support Volunteer
Posts: 2007
Joined: Thu Jun 07, 2012 9:33 pm
Contact:

Re: Using USI-SPI on Attiny 4313 to receive data from Attin

Post by Ross Robotics » Wed Aug 20, 2014 3:44 pm

lol, I meant I2C.. damn..
------------------------------------------------------------------------------------------------------------------------------------------------------------
//codlink
Available for circuit and PCB design, just PM me!

Post Reply