您好,欢迎光临本网站![请登录][注册会员]  
文件名称: STC-MCU-SPI演示
  所属分类: C
  开发工具:
  文件大小: 127kb
  下载次数: 0
  上传时间: 2013-01-05
  提 供 者: wangche********
 详细说明: /*------------------------------------------------------------------*/ /* --- STC MCU International Limited -------------------------------*/ /* --- STC 1T Series MCU SPI Demo(1 master and 1 slave) ------------*/ /* --- Mobile: (86)13922805190 -------------------------------------*/ /* --- Fax: 86-755-82944243 ----------------------------------------*/ /* --- Tel: 86-755-82948412 ----------------------------------------*/ /* --- Web: www.STCMCU.com -----------------------------------------*/ /* If you want to use the program or the program referenced in the */ /* article, please specify in which data and procedures from STC */ /*------------------------------------------------------------------*/ #include "reg51.h" //#define MASTER //define:master undefine:slave #define FOSC 18432000L #define BAUD (256 - FOSC / 32 / 115200) typedef unsigned char BYTE; typedef unsigned int WORD; typedef unsigned long DWORD; sfr AUXR = 0x8e; //Auxiliary register sfr SPSTAT = 0xcd; //SPI status register #define SPIF 0x80 //SPSTAT.7 #define WCOL 0x40 //SPSTAT.6 sfr SPCTL = 0xce; //SPI control register #define SSIG 0x80 //SPCTL.7 #define SPEN 0x40 //SPCTL.6 #define DORD 0x20 //SPCTL.5 #define MSTR 0x10 //SPCTL.4 #define CPOL 0x08 //SPCTL.3 #define CPHA 0x04 //SPCTL.2 #define SPDHH 0x00 //CPU_CLK/4 #define SPDH 0x01 //CPU_CLK/16 #define SPDL 0x02 //CPU_CLK/64 #define SPDLL 0x03 //CPU_CLK/128 sfr SPDAT = 0xcf; //SPI data register sbit SPISS = P1^3; //SPI slave select, connect to slave' SS(P1.4) pin void InitUart(); void InitSPI(); void SendUart(BYTE dat); //send data to PC BYTE RecvUart(); //receive data from PC BYTE SPISwap(BYTE dat); //swap SPI data between master and slave /////////////////////////////////////////////////////////// void main() { InitUart(); //initial UART InitSPI(); //initial SPI while (1) { #ifdef MASTER //for master (receive UART data from PC and send it to slave, in the meantime // receive SPI data from slave and send it to PC) SendUart(SPISwap(RecvUart())); #else //for salve (receive SPI data from master and ACC = SPISwap(ACC); // send previous SPI data to master) #endif } } /////////////////////////////////////////////////////////// void InitUart() { SCON = 0x5a; //set UART mode as 8-bit variable baudrate TMOD = 0x20; //timer1 as 8-bit auto reload mode AUXR = 0x40; //timer1 work at 1T mode TH1 = TL1 = BAUD; //115200 bps TR1 = 1; } /////////////////////////////////////////////////////////// void InitSPI() { SPDAT = 0; //initial SPI data SPSTAT = SPIF | WCOL; //clear SPI status #ifdef MASTER SPCTL = SPEN | MSTR; //master mode #else SPCTL = SPEN; //slave mode #endif } /////////////////////////////////////////////////////////// void SendUart(BYTE dat) { while (!TI); //wait pre-data sent TI = 0; //clear TI flag SBUF = dat; //send current data } /////////////////////////////////////////////////////////// BYTE RecvUart() { while (!RI); //wait receive complete RI = 0; //clear RI flag return SBUF; //return receive data } /////////////////////////////////////////////////////////// BYTE SPISwap(BYTE dat) { #ifdef MASTER SPISS = 0; //pull low slave SS #endif SPDAT = dat; //trigger SPI send while (!(SPSTAT & SPIF)); //wait send complete SPSTAT = SPIF | WCOL; //clear SPI status #ifdef MASTER SPISS = 1; //push high slave SS #endif return SPDAT; //return received SPI data } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: C代码
 输入关键字,在本站1000多万海量源码库中尽情搜索: