Bonjour à tous,
Je suis en train de réaliser un programme en SPI pour discuter avec un inclinomètre. Sur le site du constructeur j'ai trouvé un exemple de programmation pour les microcontroleurs ATMEL que je souhaite adapter au PIC. Il y a juste quelques lignes du programme donné en exemple que je ne comprend pas. Es-ce que quelqu'un peux me les expliquer merci.
Voici le code:
Code:inline uint16_t HW_SPI(uint8_t W_len, uint16_t W_data, uint8_t R_len) { uint16_t value; value = 0; #ifdef SCA_SCP_DEMO SPI_I2C_PORT &= ~(1 << SPI_I2C_SELECT); #endif HW_POUT(0,0); // pin 0 is reserved for CSB! switch(W_len) { case 0: break; case 8: // This loop writes 8 bit data to SPI SPDR = (uint8_t)(W_data); while(!(SPSR & (1<<SPIF))) ; value = SPDR; break; case 16: // This loop writes 16 bit data to SPI SPDR = (uint8_t)(W_data>>8); while(!(SPSR & (1<<SPIF))) ; value = SPDR << 8; SPDR = (uint8_t)(W_data); while(!(SPSR & (1<<SPIF))) ; value |= SPDR; break; default: SPI_DISABLE(); W_data <<= (16 - W_len); // This loop writes W_len bit long data to SPI while(W_len > 0) { SPI_PORT &= ~(1<<SPI_CLK); // Je ne comprend pas cette ligne if(W_data & 0x8000) // Je ne comprend pas cette ligne SPI_PORT |= (1<<SPI_MOSI); else SPI_PORT &= ~(1<<SPI_MOSI); W_data <<= 1; wait_1us(); SPI_PORT |= (1<<SPI_CLK); wait_1us(); value <<= 1; if(SPI_PORT & (1<<SPI_MISO)) value |= 0x0001; W_len--; } SPI_PORT &= ~(1<<SPI_CLK); SPI_ENABLE(); break; } while(R_len != 0) //This loop reads R_len bits from SPI { if((R_len % 8) == 0) //use standard HW SPI for 8 bit and 16 bit data { //This loop is used for VTI SCA100T 8 bit temperature data SPDR = 0x00; value <<= 8; R_len -= 8; while(!(SPSR & (1<<SPIF))) ; value |= SPDR; } else { SPI_DISABLE(); //if data length is not 8 or 16 bit then disable HW SPI while(R_len > 0)//This loop generates SPI clock signal to the SPI port { //and read SPI data bit by bit SPI_PORT &= ~(1<<SPI_CLK);//This loop is used for VTI SCA100T 11 bit measurement data. wait_1us(); //For measurement data R_len=11 wait_1us(); wait_1us(); //wait_1us routines generate 1 us delay, These 4 1us waits are needed to generate 50% duty cycle to SPI clock signal SPI_PORT |= (1<<SPI_CLK); //with these delays the SPI clock is high 3us and low 3us. wait_1us(); value <<= 1; if(SPI_PORT_IN & (1<<SPI_MISO)) value++; R_len--; } SPI_PORT &= ~(1<<SPI_CLK); SPI_ENABLE(); } } HW_POUT(0,1); // pin 0 is reserved for CSB! return value; }
-----