spi四種模式io模擬時(shí)序
#include "iom8535v.h"#define _CPOL 1#define _CPHA 0#define SCK_IO DDRA|=0X01#define MOSI_IO DDRA|=0X02#define MISO_IO DDRA=0XFB#define SSEL_IO DDRA|=0X08#define SCK_D(X) (X?(PORTA|=0X01):(PORTA=0XFE))#define MOSI_D(X) (X?(PORTA|=0X02):(PORTA=0XFD))#define SSEL_D(X) (X?(PORTA|=0X08):(PORTA=0XF7))#define MISO_I() (PINA0X04) void delay(){unsigned char m,n;for(n=0;n5;n++);for(m=0;m100;m++);}/************************************************端口方向配置 與輸出初始化************************************************/void SPI_Init(void){SCK_IO ; MOSI_IO ;MISO_IO ; SSEL_IO ;SSEL_D(1);MOSI_D(1);#if _CPOL==0SCK_D(0);#elseSCK_D(1);#endif}/**********************************************模式零 寫數(shù)據(jù)***********************************************/#if _CPOL==0_CPHA==0 //MODE 0 0 void SPI_Send_Dat(unsigned char dat){unsigned char n;for(n=0;n8;n++){SCK_D(0);if(dat0x80)MOSI_D(1);else MOSI_D(0);dat=1;SCK_D(1);}SCK_D(0);}/*********************************************模式零 讀數(shù)據(jù)*********************************************/unsigned char SPI_Receiver_Dat(void){unsigned char n ,dat,bit_t;for(n=0;n8;n++){SCK_D(0);dat=1;if(MISO_I())dat|=0x01;else dat=0xfe;SCK_D(1);}SCK_D(0);return dat;}#endif/**********************************************模式二 寫數(shù)據(jù)***********************************************/#if _CPOL==1_CPHA==0 //MODE 1 0void SPI_Send_Dat(unsigned char dat){unsigned char n;for(n=0;n8;n++){SCK_D(1);if(dat0x80)MOSI_D(1);else MOSI_D(0);dat=1;SCK_D(0);}SCK_D(1);}/*********************************************模式二 讀數(shù)據(jù)*********************************************/unsigned char SPI_Receiver_Dat(void){unsigned char n ,dat,bit_t;for(n=0;n8;n++){SCK_D(1);dat=1;if(MISO_I())dat|=0x01;else dat=0xfe;SCK_D(0);}SCK_D(1);return dat;}
51單片機(jī)相關(guān)文章:51單片機(jī)教程
評論