0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

SPI总线驱动的C语言源代码详细概述

Wildesbeast 来源:单片机教程网 作者:单片机教程网 2020-09-26 11:36 次阅读

SPI总线驱动源程序 默认11.0592Mhz的晶振。

/*通用93c06-93c86系列使用说明

93c06=93c4693c56=93c6693c76=93c86*/

//dipx 可以自行定义

#i nclude 《AT89X52.H》

#i nclude “intrins.h”

/*-----------------------------------------------------

SPI93cXX系列时序函数调用(普通封装)

调用方式:自行定义﹫2001/05/12

函数说明:私有函数,封装各接口定义

-----------------------------------------------------*/

/*#define di_93 dip3

#define sk_93 dip2

#define cs_93 dip1

#define do_93 dip4

#define gnd_93 dip5

#define org_93 dip6*/

sbit cs_93=0xA3;

sbit sk_93=0xA4;

sbit di_93=0xA5;

sbit do_93=0xA6;

sbit org_93=0xA7;

/*

sbit spi_cs = 0xA3;

sbit spi_sk = 0xA4;

sbit spi_di = 0xA5;

sbit spi_do = 0xA6;

sbit spi_org = 0xA7;*/

/*-----------------------------------------------------

SPI93cXX系列时序函数调用(普通封装)

调用方式:void high46(void) ---高8位函数调用

void low46(void) ---低8位函数调用﹫2001/05/12

函数说明:私有函数,SPI专用93c46普通封装驱动程序

-----------------------------------------------------*/

void high46(void)

{

di_93=1;

sk_93=1;

_nop_();

sk_93=0;

_nop_();

}

void low46(void)

{

di_93=0;

sk_93=1;

_nop_();

sk_93=0;

_nop_();

}

void wd46(unsigned char dd)

{

unsigned char i;

for (i=0;i《8;i++)

{

if (dd》=0x80)

high46();

else

low46();

dd=dd《《1;

}

}

unsigned char rd46(void)

{

unsigned char i,dd;

do_93=1;

for (i=0;i《8;i++)

{

dd《《=1;

sk_93=1;

_nop_();

sk_93=0;

_nop_();

if (do_93)

dd|=1;

}

return(dd);

}

/*-----------------------------------------------------

SPI93cXX系列时序函数调用(特殊封装)

调用方式:自行定义﹫2001/05/12

函数说明:私有函数,特殊封装各接口定义

-----------------------------------------------------

#define di_93a dip5

#define sk_93a dip4

#define cs_93a dip3

#define do_93a dip6

#define gnd_93a dip7

#define vcc_93a out_vcc(2)

sbit cs_93a=P1^0;

sbit sk_93a=P1^1;

sbit di_93a=P1^2;

sbit do_93a=P1^3;

/*-----------------------------------------------------

SPI93cXX系列时序函数调用(特殊封装)

调用方式:void high46a(void) ---高8位函数调用

void low46a(void) ---低8位函数调用﹫2001/05/12

函数说明:私有函数,SPI专用93c46特殊封装驱动程序

-----------------------------------------------------

void high46a(void)

{

di_93a=1;

sk_93a=1;_nop_();

sk_93a=0;_nop_();

}

void low46a(void)

{

di_93a=0;

sk_93a=1;_nop_();

sk_93a=0;

_nop_();

}

void wd46a(unsigned char dd)

{

unsigned char i;

for (i=0;i《8;i++)

{

if (dd》=0x80) high46a();

else low46a();

dd=dd《《1;

}

}

unsigned char rd46a(void)

{

unsigned char i,dd;

do_93a=1;

for (i=0;i《8;i++)

{

dd《《=1;

sk_93a=1;_nop_();

sk_93a=0;_nop_();

if (do_93a) dd|=1;

}

return(dd);

}

/*-----------------------------------------------------

SPI93c46系列函数调用(举例)

调用方式:bit write93c56_word(unsigned int address,unsigned int dat) ﹫2001/05/12

函数说明:私有函数,SPI专用

-----------------------------------------------------*/

void ewen46(void)

{

_nop_();

cs_93=1;

high46();

wd46(0x30);

cs_93=0;

}

unsigned char eral_46()

{

data unsigned char temp1;

data unsigned int temp2;

cs_93=0;

sk_93=0;

//spi_org=1;

cs_93=1;

ewen46();

_nop_();

cs_93=1;

_nop_();

high46();

wd46(0x20);

wd46(0x20);

cs_93=0;

_nop_();

cs_93=1;

temp1=1;

temp2=50000;

while(!do_93)

{

temp2=temp2-1;

if(temp2==0)

{

temp1=0;

break;

}

}

cs_93=0;

return temp1;

}

unsigned int read93c46_word(unsigned char address)

{

unsigned int dat;

unsigned char dat0,dat1;

//gnd_93a=0;

//gnd_93=0;

cs_93=sk_93=0;

//org_93=1;

cs_93=1;_nop_();

//address=address《《1;

//address=address》》1;

address=address|0x80;

address=address|0x80;

high46();

wd46(address);

dat1=rd46();

dat0=rd46();

cs_93=0;

dat=dat1*256+dat0;

return(dat);

}

bit write93c46_word(unsigned char address,unsigned int dat)

{

unsigned char e,time,temp=address;

e=0;

while (e《3)

{

//gnd_93a=0;

//gnd_93=0;

cs_93=sk_93=0;

org_93=1;

cs_93=1;

ewen46();

_nop_();

cs_93=1;

_nop_();

high46();

address《《=1;//??

address|=0x80;

address》》=1;//??

address|=0x40;

wd46(address);

wd46(dat/256);

wd46(dat%256);

cs_93=0;

_nop_();

cs_93=1;

time=0;

do_93=1;

while (1)

{

if (do_93==1)

break;

if (time》20)

break;

time++;

}

cs_93=0;

if (read93c46_word(temp)==dat)

{

return(0);

}

e++;

}

return(1);

}

/*-----------------------------------------------------

SPI93c57系列函数调用(举例)

调用方式:bit write93c57_word(unsigned int address,unsigned int dat) ﹫2001/05/12

函数说明:私有函数,SPI专用

-----------------------------------------------------

void ewen57(void)

{

_nop_();

cs_93=1;

dip7=0;

high46();

low46();

wd46(0x60);

cs_93=0;

}

unsigned int read93c57_word(unsigned int address)

{

unsigned int dat;

unsigned char dat0,dat1;

gnd_93=0;

cs_93=sk_93=0;

org_93=1;

cs_93=1;

address=address》》1;

high46();

high46();

wd46(address);

dat1=rd46();

dat0=rd46();

cs_93=0;

dat=dat1*256+dat0;

return(dat);

}

bit write93c57_word(unsigned int address,unsigned int dat)

{

unsigned char e;

unsigned int temp=address;

e=0;

while (e《3)

{

gnd_93=0;

cs_93=sk_93=0;

org_93=1;

cs_93=1;

ewen57();

cs_93=1;

_nop_();

high46();

low46();

address》》=1;

address|=0x80;

wd46(address);

wd46(dat/256);

wd46(dat%256);

cs_93=0;

_nop_();

cs_93=1;

time=0;

do_93=1;

while (1)

{

if (do_93==1) break;

if (time》20) break;

}

cs_93=0;

if (read93c57_word(temp)==dat)

{

return(0);

}

e++;

}

return(1);

}

/*-----------------------------------------------------

SPI93c56系列函数调用(举例)

调用方式:bit write93c56_word(unsigned int address,unsigned int dat) ﹫2001/05/12

函数说明:私有函数,SPI专用

-----------------------------------------------------

void ewen56(void)

{

_nop_();

cs_93=1;

high46();

low46();

low46();

wd46(0xc0);

cs_93=0;

}

unsigned int read93c56_word(unsigned char address)

{

unsigned int dat;

unsigned char dat0,dat1;

gnd_93=0;

cs_93=sk_93=0;

org_93=1;

cs_93=1;

address=address》》1;

high46();

high46();

low46();

wd46(address);

dat1=rd46();

dat0=rd46();

cs_93=0;

dat=dat1*256+dat0;

return(dat);

}

bit write93c56_word(unsigned char address,unsigned int dat)

{

unsigned char e;

unsigned int temp=address;

e=0;

while (e《3)

{

gnd_93=0;

cs_93=sk_93=0;

org_93=1;

cs_93=1;

ewen56();

_nop_();

cs_93=1;

_nop_();

high46();

low46();

high46();

address》》=1;

wd46(address);

wd46(dat/256);

wd46(dat%256);

cs_93=0;

_nop_();

cs_93=1;

TH0=0;

time=0;

do_93=1;

while (1)

{

if (do_93==1) break;

if (time) break;

}

cs_93=0;

if (read93c56_word(temp)==dat)

{

return(0);

}

e++;

}

return(1);

}

/*-----------------------------------------------------

SPI93c76与SPI93c86系列函数调用(举例)

调用方式:bit write93c76_word(unsigned int address,unsigned int dat) ﹫2001/05/12

函数说明:私有函数,SPI专用

-----------------------------------------------------

void ewen76(void)

{

_nop_();

cs_93=1;

dip7=1;

high46();

low46();

low46();

high46();

high46();

wd46(0xff);

cs_93=0;

}

unsigned int read93c76_word(unsigned int address)

{

unsigned char dat0,dat1;

gnd_93=0;

cs_93=sk_93=0;

org_93=1;

cs_93=1;

address》》=1;

high46();

high46();

low46();

if((address&0x200)==0x200) high46();

else low46();

if ((address&0x100)==0x100) high46();

else low46();

wd46(address);

dat1=rd46();

dat0=rd46();

cs_93=0;

return(dat1*256|dat0);

}

bit write93c76_word(unsigned int address,unsigned int dat)

{

unsigned char e;

unsigned int temp=address;

e=0;

address》》=1;

while (e《3)

{

gnd_93=0;

cs_93=sk_93=0;

org_93=1;

cs_93=1;

ewen76();

_nop_();

cs_93=1;

high46();

low46();

high46();

if((address&0x200)==0x200) high46();

else low46();

if ((address&0x100)==0x100) high46();

else low46();

wd46(address);

wd46(dat/256);

wd46(dat%256);

cs_93=0;_nop_();cs_93=1;

time=0;do_93=1;

while (1)

{

if (do_93==1) break;

if (time》10) break;

}

cs_93=0;

e++;

}

return(1);

}

/*-----------------------------------------------------

主函数调用(举例)

调用方式:main() ﹫2001/05/12

函数说明:私有函数,SPI专用

-----------------------------------------------------*/

main()

{

bit b;

unsigned int i;

unsigned int j[32];

eral_46();

for(i=0;i《32;i++)

j[i]=read93c46_word(i);

for(i=0;i《32;i++)

write93c46_word(i,0x0910);

i=0;

b=write93c46_word(i,0x1213);

j[i]=read93c46_word(i);

i=1;

b=write93c46_word(i,0x1122);

j[i]=read93c46_word(i);

i=2;

b=write93c46_word(i,0x1344);

j[i]=read93c46_word(i);

i=3;

b=write93c46_word(i,0x1526);

j[i]=read93c46_word(i);

i=4;

b=write93c46_word(i,0xccde);

j[i]=read93c46_word(i);

P2_7=0;

}

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • SPI
    SPI
    +关注

    关注

    17

    文章

    1614

    浏览量

    89586
  • C语言
    +关注

    关注

    180

    文章

    7528

    浏览量

    128411
  • 源代码
    +关注

    关注

    94

    文章

    2926

    浏览量

    66057
收藏 人收藏

    评论

    相关推荐

    分享C语言的快速傅里叶变换源代码

    分享C语言的快速傅里叶变换源代码,本人是新手,希望大家多多指点,调错误
    发表于 05-07 19:17

    AVR单片机SPI通讯实例程序 (C语言源代码)

    AVR单片机SPI通讯实例程序 (C语言源代码) 一个SPI的实例,通过SPI实现两机通讯, 采用中断方式实现双全工通讯。本例用两MEGA
    发表于 04-23 14:10 6825次阅读

    SPI总线的学习――93C46的应用

    SPI总线的学习――93C46的应用源代码
    发表于 05-20 16:08 48次下载

    LPC2124SPI源代码

    LPC2124SPI源代码分享,下来看看
    发表于 06-07 14:13 14次下载

    LPC2378_SPI源代码

    单片机 LPC2378的 外设SPI源代码
    发表于 07-06 15:14 1次下载

    LPC2368_SPI源代码

    LPC2368_SPI源代码,有需要的下来看看
    发表于 08-15 17:55 11次下载

    STM32F4串行外设接口(SPI)的源代码详细资料免费下载

    本文档的主要内容详细介绍的是STM32F4串行外设接口(SPI)的源代码详细资料免费下载。
    发表于 08-31 15:53 50次下载
    STM32F4串行外设接口(<b class='flag-5'>SPI</b>)的<b class='flag-5'>源代码</b>的<b class='flag-5'>详细</b>资料免费下载

    调速电机C语言源代码免费下载

    本文档的主要内容详细介绍的是调速电机C语言源代码免费下载。
    发表于 04-03 17:21 18次下载
    调速电机C<b class='flag-5'>语言</b><b class='flag-5'>源代码</b>免费下载

    CAN总线源代码的C语言资料免费下载

    本文档的主要内容详细介绍的是CAN总线源代码的C语言资料免费下载。
    发表于 06-13 08:00 10次下载

    带有N的SPI总线详细资料概述

    本文档概述SPI(串行外围接口)总线,该总线通常用于集成电路或传感器之间的通信。本教程讨论了总线的底层基础,包括数据传输、仲裁,以及寻址。
    发表于 03-10 08:00 0次下载
    带有N的<b class='flag-5'>SPI</b><b class='flag-5'>总线</b><b class='flag-5'>详细</b>资料<b class='flag-5'>概述</b>

    使用STM32F4的SPI驱动HLW8112芯片源代码和工程文件免费下载

    本文档的主要内容详细介绍的是使用STM32F4的SPI驱动HLW8112芯片源代码和工程文件免费下载。
    发表于 04-10 08:00 190次下载
    使用STM32F4的<b class='flag-5'>SPI</b><b class='flag-5'>驱动</b>HLW8112芯片<b class='flag-5'>源代码</b>和工程文件免费下载

    延时函数的C语言源代码免费下载

    本文档的主要内容详细介绍的是延时函数的C语言源代码免费下载。
    发表于 09-14 08:00 2次下载
    延时函数的C<b class='flag-5'>语言</b><b class='flag-5'>源代码</b>免费下载

    CC2540驱动的C语言源代码

    CC2540驱动的C语言源代码
    发表于 03-10 11:13 5次下载
    CC2540<b class='flag-5'>驱动</b>的C<b class='flag-5'>语言</b><b class='flag-5'>源代码</b>

    178个经典c语言源代码

    178个经典c语言源代码分享
    发表于 11-17 16:57 170次下载

    基于STM32单片机SPI使用库文件设计源代码

    基于STM32单片机SPI使用库文件设计源代码
    发表于 04-26 14:26 4次下载