您的位置 首页 FPGA

单片机IO口模仿SPI四种形式的程序

#includeiom8535v.h#define_CPOL1#define_CPHA0#defineSCK_IODDRA

#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()  (PINA&0X04)  void delay(){unsigned char m,n;for(n=0;n<5;n++);for(m=0;m<100;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}/形式零           写数据*/#if _CPOL==0&&_CPHA==0          //MODE   0  0   void SPI_Send_Dat(unsigned char dat){unsigned char n;for(n=0;n<8;n++){SCK_D(0);if(dat&0x80)MOSI_D(1);else MOSI_D(0);dat<<=1;SCK_D(1);}SCK_D(0);}/*形式零         读数据*/unsigned char SPI_Receiver_Dat(void){unsigned char n ,dat,bit_t;for(n=0;n<8;n++){SCK_D(0);dat<<=1;if(MISO_I())dat|=0x01;else dat&=0xfe;SCK_D(1);}SCK_D(0);return dat;}#endif/形式二           写数据*/#if _CPOL==1&&_CPHA==0           //MODE   1  0void SPI_Send_Dat(unsigned char dat){unsigned char n;for(n=0;n<8;n++){SCK_D(1);if(dat&0x80)MOSI_D(1);else MOSI_D(0);dat<<=1;SCK_D(0);}SCK_D(1);}/*形式二          读数据*/unsigned char SPI_Receiver_Dat(void){unsigned char n ,dat,bit_t;for(n=0;n<8;n++){SCK_D(1);dat<<=1;if(MISO_I())dat|=0x01;else dat&=0xfe;SCK_D(0);}SCK_D(1);return dat;}#endif/*形式一        写数据*/#if _CPOL==0&&_CPHA==1           //MODE  0  1void SPI_Send_Dat(unsigned char dat){unsigned char n;SCK_D(0);for(n=0;n<8;n++){SCK_D(1);if(dat&0x80)MOSI_D(1);else MOSI_D(0);dat<<=1;SCK_D(0);}}/*形式一       读数据*/unsigned char SPI_Receiver_Dat(void){unsigned char n ,dat,bit_t;for(n=0;n<8;n++){SCK_D(1);dat<<=1;if(MISO_I())dat|=0x01;else dat&=0xfe;SCK_D(0);}SCK_D(0);return dat;}#endif//////////////////////////////////////////////////////////////////////////////////////////////////////////////#if _CPOL==1&&_CPHA==1            //MODE  1  1void SPI_Send_Dat(unsigned char dat){unsigned char n;SCK_D(1);for(n=0;n<8;n++){SCK_D(0);if(dat&0x80)MOSI_D(1);else MOSI_D(0);dat<<=1;SCK_D(1);}}/形式三          读数据/unsigned char SPI_Receiver_Dat(void){unsigned char n ,dat,bit_t;SCK_D(0);for(n=0;n<8;n++){ SCK_D(0);dat<<=1;if(MISO_I())dat|=0x01;else dat&=0xfe;SCK_D(1);}SCK_D(1);return dat;}#endif/**/void main(){SPI_Init();DDRB = 0XFF;//#if _CPOL//SCK_D(0);//#endifwhile(1){//SSEL_D(0);//SPI_Send_Dat(0x01);//SPI_Send_Dat(0x31);//SSEL_D(1);SSEL_D(0);SPI_Send_Dat(0x81);PORTB =SPI_Receiver_Dat();SSEL_D(1);//delay();}}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/fangan/fpga/265440.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部