匯流排驅動,雖然是模擬的,但是也包含進了內核。
inc:標頭檔案部分
sim_i2c.h
IIC匯流排的標頭檔案。
#ifndef __SIM_I2C_H
#define __SIM_I2C_H
//-----------標頭檔案-----------//
#include "include_drives.h"
#if STC89C52RC
#include <reg52.h>
#include "delay.h"
#define I2c_Delay //I2c時序延時宏定義,STC89C52速度
//慢所以直接宏定義一個空的延時
//-----------IIC的介面設定-----------//
sbit SCL = P2^4;
sbit SDA = P2^3;
//-----------IIC的時序函數-----------//
void I2c_Start(void); //起始信號
void I2c_Stop(void); //停止信號
unsigned char I2c_SendByte(unsigned char dat); //發送一個位元組
unsigned char I2c_ReadByte(void); //讀一個位元組
#endif
#endif
sim_spi.h
SPI匯流排的標頭檔案。
#ifndef __SIM_SPI_H
#define __SIM_SPI_H
#include "include_drives.h"
#if STC89C52RC
//---包含標頭檔案---//
#include <reg52.h>
#include <intrins.h>
//---重定義關鍵詞---//
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
#ifndef ulong
#define ulong unsigned long
#endif
//---定義SPI使用的IO口---//
sbit DOUT = P2^3; //輸出
sbit CLK = P2^4; //時鐘
sbit DIN = P3^7; //輸入
sbit CS = P2^0; //片選
#define SPI_Delay //SPI時序延時宏定義,STC89C52速度
//慢所以直接宏定義一個空的延時
//-----------SPI的實現函數-----------//
void SPI_Start(void); //SPI初始化
uint SPI_Read(void); //SPI讀
void SPI_Write(uchar dat); //SPI寫
#endif
#endif
src:原始碼部分
sim_i2c.c
IIC匯流排原始碼。
#include "sim_i2c.h"
#if STC89C52RC
/*******************************************************************************
* 函 數 名 : I2cStart
* 輸 入 : 空
* 輸 出 : 空
* 函數功能 : 起始信號
* 詳細描述 : 在SCL時鐘信號在高電平期間SDA信號產生一個下降沿
*******************************************************************************/
void I2c_Start(void)
{
SDA = 1;
I2c_Delay;
SCL = 1;
I2c_Delay; //建立時間是SDA保持時間>4.7us
SDA = 0;
I2c_Delay; //保持時間是>4us
SCL = 0;
I2c_Delay;
}
/*******************************************************************************
* 函 數 名 : I2cStop
* 輸 入 : 空
* 輸 出 : 空
* 函數功能 : 終止信號
* 詳細描述 : 在SCL時鐘信號高電平期間SDA信號產生一個上升沿,IIC的停止函數
*******************************************************************************/
void I2c_Stop(void)
{
SDA = 0;
I2c_Delay;
SCL = 1;
I2c_Delay; //建立時間大於4.7us
SDA = 1;
I2c_Delay;
}
/*******************************************************************************
* 函 數 名 : I2cSendByte
* 輸 入 : num
* 輸 出 : 0或1。發送成功返回1,發送失敗返回0
* 函數功能 : 通過I2C發送一個位元組。在SCL時鐘信號高電平期間,保持發送信
號SDA保持穩定
* 詳細描述 : 發送完一個位元組SCL=0,SDA=1
*******************************************************************************/
unsigned char I2c_SendByte(unsigned char dat)
{
unsigned char a,b; //最大255,一個機器週期爲1us,最大延時255us。
for(a = 0;a < 8;a ++) //要發送8位元,從最高位開始
{
SDA = dat >> 7; //起始信號之後SCL=0,所以可以直接改變SDA信號
dat = dat << 1;
I2c_Delay;
SCL = 1;
I2c_Delay; //建立時間>4.7us
SCL = 0;
I2c_Delay; //時間大於4us
}
SDA = 1;
I2c_Delay;
SCL = 1;
while(SDA) //等待應答,也就是等待從裝置把SDA拉低
{
b ++;
if(b > 200) //如果超過2000us沒有應答發送失敗,或者爲非應答,表示接收結束
{
SCL = 0;
I2c_Delay;
return 0;
}
}
SCL = 0;
I2c_Delay;
return 1;
}
/*******************************************************************************
* 函 數 名 : I2c_ReadByte
* 輸 入 : 空
* 輸 出 : unsigned char dat
* 函數功能 : IIC讀位元組函數
* 詳細描述 : 使用I2c讀取一個位元組,且返回值爲讀出的數據,爲unsigned char
*******************************************************************************/
unsigned char I2c_ReadByte(void)
{
unsigned char a,dat=0;
SDA = 1; //起始和發送一個位元組之後SCL都是0
I2c_Delay;
for(a = 0;a < 8;a ++)//接收8個位元組
{
SCL = 1;
I2c_Delay;
dat <<= 1;
dat |= SDA;
I2c_Delay;
SCL = 0;
I2c_Delay;
}
return dat;
}
#endif
sim_spi.c
SPI匯流排原始碼。
#include "sim_spi.h"
#if STC89C52RC
/*******************************************************************************
* 函 數 名 : SPI_Start
* 輸 入 : 空
* 輸 出 : 空
* 函數功能 : SPI初始化
* 詳細描述 : 初始化SPI
*******************************************************************************/
void SPI_Start(void)
{
CLK = 0;
CS = 1;
DIN = 1;
CLK = 1;
CS = 0;
}
/*******************************************************************************
* 函 數 名 : SPI_Write
* 輸 入 : dat 寫入的數據
* 輸 出 : 空
* 函數功能 : 使用SPI寫入數據
* 詳細描述 : 使用SPI寫入數據
*******************************************************************************/
void SPI_Write(uchar dat)
{
uchar i;
CLK = 0;
for(i = 0;i < 8;i ++)
{
DOUT = dat >> 7; //放置最高位
dat <<= 1;
CLK = 0; //上升沿放置數據
SPI_Delay;
CLK = 1;
SPI_Delay;
}
}
/*******************************************************************************
* 函 數 名 : SPI_Read
* 輸 入 : 空
* 輸 出 : dat 讀取到的數據
* 函數功能 : 使用SPI讀取數據
* 詳細描述 : 使用SPI讀取數據
*******************************************************************************/
uint SPI_Read(void)
{
uchar i,dat = 0;
CLK = 0;
for(i = 0;i < 8;i ++) //接收12位元數據 這個地方接受數據12位元爲非常規
{
dat <<= 1;
CLK = 0;
SPI_Delay;
CLK = 1;
SPI_Delay;
dat |= DIN;
}
return dat;
}
#endif
碼雲下載:https://gitee.com/LWTDZ/Core8051.git
店鋪地址:https://item.taobao.com/item.htm?spm=a1z10.3-c-s.w4002-22129052348.16.6b222269wKu2dH&id=615384296160
QQ羣(STM32嵌入式Linux&微控制器):950822175