اطلاعیه

Collapse
No announcement yet.

SPI_ خواندن اطلاعات slave از Master _ اتمل اتودیو

Collapse
X
 
  • فیلتر
  • زمان
  • Show
Clear All
new posts

    SPI_ خواندن اطلاعات slave از Master _ اتمل اتودیو

    سلام، من می خوام 2 تا میکرو رو به هم وصل کنم از Spi استفاده کردم، می تونم اطلاعات رو از master به slave بفرستم ولی نمی تونم داده ها رو از slave بخونم
    این هم کدهام:
    Master:
    کد:
    unsigned char SPI_WriteRead(unsigned char dataout)
    {
     unsigned char datain;
    
     
     // Start transmission (MOSI)
     SPDR = dataout;
    
     // Wait for transmission complete
     while(!(SPSR & (1<<SPIF)));
    
     // Get return Value;
     datain = SPDR;
    
     // Latch the Output using rising pulse to the RCK Pin
     SPI_PORT |= (1<<SPI_CS);
    
     _delay_us(1);       // Hold pulse for 1 micro second
    
     // Disable Latch
     SPI_PORT &= ~(1<<SPI_CS);
    
     // Return Serial In Value (MISO)
     return datain;
    }
    
    int main(void)
    {
     unsigned char cnt;
     unsigned char data;
     data='0';
     
     // Set the PORTD as Output:
     DDRD=0xFF;
     DDRC=0xff;	 
     // Initial the AVR ATMega168 SPI Peripheral
    
     // Set MOSI and SCK as output, others as input
     SPI_DDR = (1<<PORTB5)|(1<<PORTB7)|(1<<PORTB4);
    
     // Latch Disable (RCK Low)
     SPI_PORT &= ~(1<<SPI_CS);
    
     // Enable SPI, Master, set clock rate fck/2 (maximum)
     SPCR = (1<<SPE)|(1<<MSTR);
     SPSR = (1<<SPI2X);
    
     // Reset the 74HC595 register
     SPI_WriteRead(0);
    	int i;
     while(1) {
    	
    			 data=SPI_WriteRead(2);
    			_delay_ms(100);
    			
    	
    	 
    	 if(data==0x65){
    			 PORTC |=(1<<PORTC1);
    			 _delay_ms(300);	 
    	 }else{
    		 PORTC |=(1<<PORTC2);
    			 _delay_ms(300); 
    	 }
    	
    	 	 	
     }
    
     return 0;
    }
    Slave:
    کد:
    unsigned char SPI_WriteRead(unsigned char dataout)
    {
     unsigned char datain;
    	
    // Start transmission (MISO)
     SPDR = dataout; 
    _delay_us(10);
     // Wait for transmission complete
     while(!(SPSR & (1<<SPIF)));
    _delay_us(10);
     // Get return Value;
     //datain = SPDR;
    
    
     // Latch the Output using rising pulse to the RCK Pin
     SPI_PORT |= (1<<SPI_CS);
    
     _delay_us(1);       // Hold pulse for 1 micro second
    
     // Disable Latch
     SPI_PORT &= ~(1<<SPI_CS);
    
     // Return Serial In Value (MISO)
     return SPDR;
    }
    
    int main(void)
    {
     unsigned char cnt;
     unsigned int data;
     data=0;
     
     // Set the PORTD as Output:
     DDRD=0x00;
     PORTD=0xff;
     DDRC=0xff;
     PORTC=0x00;
    
     // Initial the AVR ATMega168 SPI Peripheral
    
     // Set MOSI and SCK as output, others as input
     SPI_DDR = (1<<PORTB3)|(1<<PORTB5)|(1<<PORTB2);
    
     // Latch Disable (RCK Low)
     SPI_PORT &= ~(1<<SPI_CS);
    
     // Enable SPI, slave, set clock rate fck/2 (maximum)
     SPCR = (1<<SPE);
     SPCR &= ~(1<<MSTR);
     SPSR = (1<<SPI2X);
    
     // Reset the 74HC595 register
     SPI_WriteRead(0);
    
     while(1) {
      
    	
    		 data=SPI_WriteRead(0x65);
    		 _delay_ms(100);
    		if(data==1){
    		 PORTC|=(1<<PORTC1);
    		 PORTC&=~(1<<PORTC2);
    		 _delay_ms(300);
    		}else{
    		 PORTC|=(1<<PORTC2);
    		 PORTC&=~(1<<PORTC1);
    		 _delay_ms(300);
    		}
    		
    		if(!(PIND &(1<<PIND0))){
    			data=SPI_WriteRead(0x65);
    			_delay_ms(100);			
    	  }
     }
    
     return 0;
    }

    #2
    پاسخ : SPI_ خواندن اطلاعات slave از Master _ اتمل اتودیو

    دوستان، کسی نمیدونهن مشکل از کجاست؟

    //**************
    دوستان دیگه زحمت نکشید مشکل حل شد.

    دیدگاه


      #3
      پاسخ : SPI_ خواندن اطلاعات slave از Master _ اتمل اتودیو

      خب جواب میدادین. شاید یکی دیگه هم مث من همین مشکل رو داشته باشه :sad:
      خدا نور است و من اشعه ای از او ...

      دیدگاه

      لطفا صبر کنید...
      X