https://blog.csdn.net/u012846795/article/details/123861413
1 /* CPOL = 0, CPHA = 0, MSB first */ 2 uint8_t SOFT_SPI_RW_MODE0( uint8_t write_dat ) 3 { 4 uint8_t i, read_dat; 5 for( i = 0; i < 8; i++ ) 6 { 7 if( write_dat & 0x80 ) 8 MOSI_H; 9 else 10 MOSI_L; 11 write_dat <<= 1; 12 delay_us(1); 13 SCK_H; 14 read_dat <<= 1; 15 if( MISO ) 16 read_dat++; 17 delay_us(1); 18 SCK_L; 19 __nop(); 20 } 21 22 return read_dat; 23 } 24 25 26 /* CPOL=0,CPHA=1, MSB first */ 27 uint8_t SOFT_SPI_RW_MODE1(uint8_t byte) 28 { 29 uint8_t i,Temp=0; 30 31 for(i=0;i<8;i++) // 循环8次 32 { 33 SCK_H; //拉高时钟 34 if(byte&0x80) 35 { 36 MOSI_H; //若最到位为高,则输出高 37 } 38 else 39 { 40 MOSI_L; //若最到位为低,则输出低 41 } 42 byte <<= 1; // 低一位移位到最高位 43 delay_us(1); 44 SCK_L; //拉低时钟 45 Temp <<= 1; //数据左移 46 47 if(MISO) 48 Temp++; //若从从机接收到高电平,数据自加一 49 delay_us(1); 50 51 } 52 return (Temp); //返回数据 53 } 54 55 /* CPOL=1,CPHA=0, MSB first */ 56 uint8_t SOFT_SPI_RW_MODE2(uint8_t byte) 57 { 58 uint8_t i,Temp=0; 59 60 for(i=0;i<8;i++) // 循环8次 61 { 62 if(byte&0x80) 63 { 64 MOSI_H; //若最到位为高,则输出高 65 } 66 else 67 { 68 MOSI_L; //若最到位为低,则输出低 69 } 70 byte <<= 1; // 低一位移位到最高位 71 delay_us(1); 72 SCK_L; //拉低时钟 73 Temp <<= 1; //数据左移 74 75 if(MISO) 76 Temp++; //若从从机接收到高电平,数据自加一 77 delay_us(1); 78 SCK_H; //拉高时钟 79 80 } 81 return (Temp); //返回数据 82 } 83 84 85 /* CPOL = 1, CPHA = 1, MSB first */ 86 uint8_t SOFT_SPI_RW_MODE3( uint8_t write_dat ) 87 { 88 uint8_t i, read_dat; 89 for( i = 0; i < 8; i++ ) 90 { 91 SCK_L; 92 if( write_dat & 0x80 ) 93 MOSI_H; 94 else 95 MOSI_L; 96 write_dat <<= 1; 97 delay_us(1); 98 SCK_H; 99 read_dat <<= 1; 100 if( MISO ) 101 read_dat++; 102 delay_us(1); 103 __nop(); 104 } 105 return read_dat; 106 }
标签:write,MOSI,uint8,模式,dat,SPI,模拟 From: https://www.cnblogs.com/better-day/p/17167819.html