找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3573|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

PIC16F877A單片機采用IO模擬SPI進行主從機通信仿真代碼

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
#
ID:883568 發(fā)表于 2021-4-10 17:01 | 只看該作者 |只看大圖 回帖獎勵 |正序瀏覽 |閱讀模式
簡介,
主機采用IO口進行模擬SPI信號對從機進行數(shù)據(jù)通信。
PROTEUS原理圖,

仿真結(jié)果與波形,波形等,由于篇幅問題無法體現(xiàn)。
總結(jié),
源程序與PROTEUS文件都上傳了,歡迎下載。

從機單片機源程序如下:
  1. /*
  2. * File:   SLAVE-IO.c
  3. * Author: jiejiechen
  4. *
  5. * Created on 2021年4月9日, 上午10:54
  6. */
  7. #pragma config FOSC = XT        // Oscillator Selection bits (XT oscillator)
  8. #pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
  9. #pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)
  10. #pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
  11. #pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
  12. #pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
  13. #pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
  14. #pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)
  15. #include <xc.h>
  16. #define uint unsigned int
  17. #define uchar unsigned char
  18. #define SCK RC3        //SPI時鐘端
  19. #define SDI RC4
  20. #define SDO RC5        //SPI數(shù)據(jù)輸出端
  21. uint ERROR=0,flag=0x17;
  22. void init();
  23. void delayms(uint x);
  24. communication(uint data);
  25. communication_IO(uchar A);
  26. void main(void) {
  27.     uint temp1=0,temp2=0;
  28.      delayms(500);
  29.     init();
  30.     ERROR=0;
  31.     temp1=communication(0);//以寫“0”代讀,接收來至主機的數(shù)據(jù)。temp1=0xcc
  32.     RB0=1;
  33.     temp2=communication(0xe5);
  34.     if(temp1!=0)//判斷是否接收到數(shù)據(jù)
  35.         RB1=1;
  36.     if(temp1!=flag)
  37.         ERROR=1;
  38.     if(ERROR==1)
  39.         RB2=0;
  40.     else
  41.         RB2=1;
  42.     PORTD=temp1;
  43.     //PORTD=temp1;
  44.     //RB2=1;
  45.     while(1);
  46.     return;
  47. }
  48. void init()
  49. {
  50.     SSPSTAT=0X00;
  51.     SSPCON=0X34;
  52.     TRISC=0B00011000;
  53.     TRISD=0X00;
  54.     PORTD=0X00;
  55.     ADCON1=0B00000110;
  56.     TRISA5=1;
  57.     TRISB=0X00;
  58.     PORTB=0X00;
  59. }
  60. void delayms(uint x)
  61. {
  62.     uchar a=0,b=0;
  63.     for(a=x;a>0;a--)
  64.         for(b=110;b>0;b--)
  65.             ;
  66. }
  67. communication(uint data)
  68. {
  69.     uint BUF=0;
  70.     SSPBUF=data;
  71.     while(BF==0);
  72.     BUF=SSPBUF;//在講從機只設(shè)置為接收器時,主機發(fā)完數(shù)據(jù)后,不去讀數(shù)據(jù)也是可以的。
  73.     return(BUF);
  74. }
  75. /*
  76. communication_IO(uchar A)
  77. {
  78.     SCK=1;
  79.     uint BUF=0;
  80.     uchar temp=0,i=0;
  81.     temp=A;
  82.     for(i=0;i<8;i++)         //移動一個字節(jié)中的八位。
  83.     {
  84.         SCK=0;
  85.         NOP();
  86.         if((temp & 0x80)) //“探”最高位是否為“1”。
  87.         SDO=1;//為“0”時,直接拉低輸出數(shù)據(jù)線。
  88.         else
  89.             SDO=0;
  90.         NOP();
  91.         SCK=1;                 //上升沿發(fā)送數(shù)據(jù)
  92.         NOP();               //提供一定的脈寬,一個指令周期,1us
  93.         temp<<=1;         //數(shù)據(jù)左移一位
  94.         //注這里也可以采用讀函數(shù)中的寫法,先將數(shù)據(jù)確定好,然后,直接產(chǎn)生上升沿發(fā)出去,開始還是要拉低。
  95.     }
  96.     //SDO=0;
  97.     SCK=1;
  98.     for(i=0;i<10;i++)
  99.     {
  100.         NOP();
  101.     }
  102.     for(i=0;i<8;i++)
  103.     {
  104.         SCK=0;
  105.         NOP();
  106.         SCK=1;
  107.         NOP();//這個上升沿要先寫出來,如此數(shù)據(jù)才會發(fā)過來。
  108.         BUF<<=1;//一定要先,移位。不然,后移位會將正確的值擴大兩倍。
  109.         BUF|=SDI;
  110.         NOP();
  111.     }
  112.     return(BUF);
  113.     //delayms(1);
  114. }

  115. */

  116. …………限于本文篇幅 主機代碼請從51黑下載附件…………
復(fù)制代碼

所有資料51hei提供下載:
proteus+源程序.zip (40.55 KB, 下載次數(shù): 24)




評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表