/ / the following samples: PA0 is connected to DO, PA1 is connected to CLK, PA2 is connected to CS pin. / / when using, please initialize the port and modify it according to different types of single-chip computers. #define CS_H GPIOA->ODR|=(1<<2); #define CS_L GPIOA->ODR&=~(1<<2); #define CLK_H GPIOA->ODR|=(1<<1); #define CLK_L GPIOA->ODR&=~(1<<1); void Delay_SPI(__IO uint32_t nCount) { for(; nCount != 0; nCount--); } int SPI_RW() { int i=0; int wdata=0; CS_H; Delay_SPI(0xF);//The routine is 72m clock. Please modify the delay according to the actual clock of single chip microcomputer. CS_L; Delay_SPI(0xF); for(i=0; i<16; i++) { CLK_H; Delay_SPI(0xF); if((GPIOA->IDR&0x00000001)>0)wdata|=1; //Please modify the pin according to the pin connected to DO wdata<<=1; CLK_L; Delay_SPI(0xF);// } return(wdata>>7); //Returns the absolute position value of the encoder }