SPI bus driver (C language source program)

Publisher:keiss2018Latest update time:2013-01-07 Source: EDN china Keywords:SPI Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/*--------------------
〖Description〗The SPI bus driver source program defaults to a 11.0592Mhz crystal oscillator.
〖File〗93CXX.C ﹫2003/5/12
---------------------*/
/*General 93c06-93c86 series instructions
93c06=93c4693c56=93c6693c76=93c86*/
//dipx can be defined by yourself
#i nclude
#i nclude "intrins.h"
/*-----------------------------------------------------
SPI93cXX series timing function call (normal package)
Calling method: self-defined﹫2001/05/12
Function description: private function, encapsulate each interface definition
-----------------------------------------------------*/
/*#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 series timing function call (normal package)
Calling method: void high46(void) --- High 8-bit function call
void low46(void) --- Low 8-bit function call ﹫2001/05/12
Function description: Private function, SPI dedicated 93c46 normal package driver program
-----------------------------------------------------*/
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 series timing function call (special package)
Calling method: self-definition﹫2001/05/12
Function description: private function, special package interface definition
-----------------------------------------------------
#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 series timing function call (special package)
Calling method: void high46a(void) ---high 8-bit function call
void low46a(void) ---Low 8-bit function call﹫2001/05/12Function
description: Private function, SPI dedicated 93c46 special package driver
----------------------------------------------------------
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 series function call (example)
Calling method: bit write93c56_word(unsigned int address,unsigned int dat) ﹫2001/05/12
Function description: Private function, SPI dedicated
-----------------------------------------------------*/
void ewen46(void)
{
_nop_();
cs_93=1;
high46();
wd46(0x30);
cs_93=0;
}[page]

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 series function call (example)
Calling method: bit write93c57_word(unsigned int address,unsigned int dat ) ﹫2001/05/12
Function description: Private function, SPI only
----------------------------------- ------------------
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 series function call (example)
Calling method: bit write93c56_word(unsigned int address,unsigned int dat) ﹫2001/05/ 12Function
description: Private function, SPI only
----------------------------------------- ------------
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;
93c56_word(temp )
==dat) {
return( 0)
;
} e++ ; } return ( 1) ; } [page] /*----------------------------------------------------- SPI93c76 and SPI93c86 series function call ( example ) Calling method : bit write93c76_word ( unsigned int address , unsigned int dat) ﹫2001/05/12Function description: Private function, SPI dedicated ----------------------------------------------------- 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) ; 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 function call (example)
Calling method: main() ﹫2001/05/12
Function description: Private function, SPI dedicated
-----------------------------------------------------*/
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 ; 344); j[i]=read93c46_word(i); i=3; b=write93c46_word(i,0x1526); j [i]=read93c46_word(i); i= 4 ;












Keywords:SPI Reference address:SPI bus driver (C language source program)

Previous article:1-wire bus assembler
Next article:HD44780 read and write C51 program

Recommended ReadingLatest update time:2024-11-16 14:35

SPI bus driver (C language source program)
/*-------------------- 〖Description〗The SPI bus driver source program defaults to a 11.0592Mhz crystal oscillator. 〖File〗93CXX.C ﹫2003/5/12 ---------------------*/ /*General 93c06-93c86 series instructions 93c06=93c4693c56=93c6693c76=93c86*/ //dipx can be defined by yourself #i nclude A
[Microcontroller]
About Freescale S12 series MCU SPI communication MODRR configuration
MODRR is the module path selection register, which is used as the mapping pins of CAN0/4 and SPI0/1/2. For example, if CAN0 is selected as PM0/1 pin, configure MODRR_MODRR0&1=0. When MODRR_MODRR4=0, SPI0 selects PS4/5/6/7.
[Microcontroller]
About Freescale S12 series MCU SPI communication MODRR configuration
Basic introduction of SPI based on STM32
STM32---Summary of SPI (DMA) communication (library function operation) This article mainly introduces SPI in 7 items and will attach the test source code for reference at the end: 1. SPI communication protocol 2. SPI communication initialization (with STM32 as slave and LPC1114 as host) 3. SPI read and write function
[Microcontroller]
Basic introduction of SPI based on STM32
AVR MCU SPI Simple Application
Host Program #include "iom16v.h" #include "macros.h" #define uchar unsigned char #define uint unsigned int /*----------------------------------------- Function name: void delay(uchar Data ) Function: Delay function Parameters: Return value: None -----------------------------------------*/ void dela
[Microcontroller]
PIC Getting Started 3, SPI Communication and Serial Port Debugging Experiment
The MSSP module works in SPI master mode, which can be executed directly on the experimental board. program: //Suitable for 3EPIC experiment board, configure the MSSP module of PIC microcontroller to work in SPI master mode, //The received parallel data is sent out as serial data through the SD0 port through a para
[Microcontroller]
STM32 SPI usage and SPI initialization precautions
In the STM32F10x series chips, although the number of SPI synchronous serial ports is different, the initialization method is the same. When using SPI, we recall the use of GPIO and pay attention to 6 points. We will mainly explain one point here. According to the system's requirements for different functions, it will
[Microcontroller]
I2C bus learning ends, SPI bus learning begins
         I have been studying IIC bus for nearly a week, from the shallow to the deep, and I have gradually understood a lot of things. But I have never understood the bus timing of IIC bus protocol. I am confused about the transmission of data bits, knowing the fact but not the reason. I have checked a lot of informat
[Microcontroller]
I2C bus learning ends, SPI bus learning begins
MCU software SPI and hardware SPI read and write SD card, TF card test program
Software SPI and hardware SPI read and write SD card, TF card test program SD card module and SD card software SPI read and write SD card program test platform: STC89C516 and STC12C5A60S2 memory card size: 128MB and 2GB crystal oscillator: 12MHz hardware SPI read and write SD card program test platform: STC12C5A60S
[Microcontroller]
MCU software SPI and hardware SPI read and write SD card, TF card test program
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号