⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spi_5463.c

📁 电能计量芯片CS5463的SPI口读写程序。
💻 C
字号:
#include <reg52.h>
#include <intrins.h>
#include "io.h"
#include "sys.h"
#include "spi_5463.h"

/**********************************************************************
**函数名称:send_byte(uchar temp)
**功    能:发送一个字节
**说    明:
**输入参数:temp 发送的数据
**返回参数:无
*********************************************************************/

void send_byte(uchar temp) small
{
	uchar i;

	SPI_CLK=0;
	for(i=0;i<8;i++)
	{
		if(temp&0x80)
		{
			SPI_DI=1;
		}
		else
		{
			SPI_DI=0;
		}
		SPI_CLK=1;
		temp<<=1;
		SPI_CLK=0;

	}
	SPI_DI=1;
}

/**********************************************************************
//函数名称:receive_byte() 
//功    能:接受一个字节
//说    明:
//输入参数:无
//返回参数:接收到的数据
**********************************************************************/

uchar receive_byte() small
{
	uchar i;
	uchar temp;

	temp=0;
	SPI_DI=1;
	for(i=0;i<8;i++)
	{
		temp<<=1;
		SPI_CLK=1;
		if(SPI_DO)
		{
			temp|=0x01;
		}
		SPI_CLK=0;
	}
	return temp;
}


/**********************************************************************
//函数名称:Read_From_5460(uchar *ps,uchar which)
//功    能:读一个寄存器3字节
//说    明:
//输入参数:ps 缓存地址; which 寄存器地址
//返回参数:无
**********************************************************************/

void Read_From_5460(uchar *ps,uchar which) small
{
	send_byte(which);
	ps[0]= receive_byte();
	ps[1]= receive_byte();
	ps[2]= receive_byte();
}


/**********************************************************************
//函数名称:Write_To_5460(uchar *ps)
//功    能:写一个寄存器3字节
//说    明:
//输入参数:ps 要发送的数据地址 ram
//返回参数:无
**********************************************************************/

void Write_To_5460(uchar *ps) small
{
	send_byte(ps[0]);
	send_byte(ps[1]);
	send_byte(ps[2]);
	send_byte(ps[3]);
}




/**********************************************************************
//函数名称:Write_To_5460_code(uchar *ps)
//功    能:写一个寄存器3字节
//说    明:
//输入参数:ps 要发送的数据地址 code
//返回参数:无
**********************************************************************/


void Write_To_5460_code(uchar code *ps) small
{
	send_byte(ps[0]);
	send_byte(ps[1]);
	send_byte(ps[2]);
	send_byte(ps[3]);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -