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

📄 cs5532.c

📁 一个基于C51开发的计价秤
💻 C
字号:
/*********************************************/
// File Name: CS5532.c
// Author: henny
// Date: 2006/3/1
/*********************************************/
#include <SST89x5xxRD2.h>
#include <intrins.h>
#include "Public.h"
#define	CS5532_CS P3_4
#define	RDYB P1_6

extern BYTE xdata RxdByte[15];

VOID SPI_init()
{
	P1=0xf0;
	SPCR=0x50;		//Enable SPI
}

BYTE SPI_RW(BYTE Data)
{
	BYTE temp;
	SPDR=Data;
	do
	{
		temp=SPSR&0x80;
	}while(temp!=0x80);
	SPSR&=0x7f;		//Clear SPIF(Tx complete interrupt flag)		
	return SPDR;
}

VOID Read_Register(BYTE command)
{
	BYTE data j;
	CS5532_CS=0;
	_nop_();
 	_nop_();
	SPI_RW(command);
	for(j=0;j<4;j++)
		RxdByte[j]=SPI_RW(0xfe);
	_nop_();
 	_nop_();
	CS5532_CS=1;
	_nop_();
 	_nop_();		
}

VOID Write_Register(BYTE command,BYTE top,BYTE high,BYTE mid,BYTE low)
{
	CS5532_CS=0;
 	_nop_();
 	_nop_();
	SPI_RW(command);
	SPI_RW(top);
	SPI_RW(high);
	SPI_RW(mid);
	SPI_RW(low);
	_nop_();
 	_nop_();
	CS5532_CS=1;
	_nop_();
 	_nop_();
}

VOID CS5532_init()
{
	SPI_init();
	//Perform Hardware Reset
 	Write_Register(0xff,0xff,0xff,0xff,0xff);
	Write_Register(0xff,0xff,0xff,0xff,0xff);
	Write_Register(0xff,0xff,0xff,0xff,0xff);
	Write_Register(0xff,0xff,0xff,0xff,0xfe);

	//Perform Software Reset
	Write_Register(0x03,0x20,0x00,0x00,0x00);
	
	//Voltage Reference select
	//Write Configuaration Registers Command,2.5<Vref<5V
	Write_Register(0x03,0x00,0x00,0x00,0x00);

 	//Write Chnnel Setup Register 1 Command
	//Select Chnnel 0,X=1
	Write_Register(0x05,0x02,0x40,0x02,0x40);
}

VOID Self_Offset_Calibration()
{
	CS5532_CS=0;		//Lower chip select		
	_nop_();
 	_nop_();
	SPI_RW(0x81);		//Self offset calibration command
	while(RDYB);		//Wait for SDO to drop(P1.6)
	_nop_();
 	_nop_();
	CS5532_CS=1;		//Raise chip select
	_nop_();
 	_nop_();		
}

VOID Self_Gain_Calibration()
{
	CS5532_CS=0;		//Lower chip select		
	_nop_();
 	_nop_();
	SPI_RW(0x82);		//Self offset calibration command
	while(RDYB);		//Wait for SDO to drop(P1.6)
	_nop_();
 	_nop_();
	CS5532_CS=1;		//Raise chip select
	_nop_();
 	_nop_();		
}

VOID Single_Convert()
{
	BYTE data j;
	CS5532_CS=0;		//Lower chip select		
	_nop_();
 	_nop_();
	SPI_RW(0x80);		//Shift single convert command
	while(RDYB);		//Wait for SDO to drop(P1.6)
	SPI_RW(0x00);		//Shift out zeros to release SDO
	for(j=0;j<3;j++)
		RxdByte[j]=SPI_RW(0xfe);
	 SPI_RW(0xfe);
	_nop_();
 	_nop_();
	CS5532_CS=1;		//Raise chip select
	_nop_();
 	_nop_();	
}

VOID Continuous_Convert_5_times()
{
	BYTE data i,j;
	BYTE temp;
	CS5532_CS=0;		//Lower chip select
	_nop_();
 	_nop_();
	SPI_RW(0xc0);		//Shift continuous convert command
	for(i=0;i<5;i++)	//Throw out 5 conversious-to flush residual filter
	{
		while(RDYB);	//Wait for SDO to drop(P1.6)
		SPI_RW(0x00);	//Shift out zeros to release SDO
		for(j=0;j<4;j++)
			temp=SPI_RW(0x00);	
		_nop_();
 		_nop_();
		CS5532_CS=1;
		_nop_();
 		_nop_();
		CS5532_CS=0;
	}
	for(i=0;i<5;i++)
	{
		while(RDYB);	//Wait for SDO to drop(P1.6)
		SPI_RW(0x00);	//Shift out zeros to clear SDO
		for(j=0;j<3;j++)
			RxdByte[3*i+j]=SPI_RW(0x00);
		SPI_RW(0x00);
		_nop_();
 		_nop_();
		CS5532_CS=1;
		_nop_();
 		_nop_();
		CS5532_CS=0;
	}
	while(RDYB);		//Wait for SDO to drop(P1.6)
	SPI_RW(0xff);		//Shift out ones to stop continuous convert
	for(j=0;j<4;j++)
		temp=SPI_RW(0x00);
	_nop_();
 	_nop_();
	CS5532_CS=1;		//Raise chip select
	_nop_();
 	_nop_();		
}

⌨️ 快捷键说明

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