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

📄 mem-87x.c

📁 Pic 93c46 spi read/write
💻 C
字号:
//
//							 Software License Agreement
//
// The software supplied herewith by Microchip Technology Incorporated 
// (the "Company") for its PICmicro?Microcontroller is intended and 
// supplied to you, the Company抯 customer, for use solely and 
// exclusively on Microchip PICmicro Microcontroller products. The 
// software is owned by the Company and/or its supplier, and is 
// protected under applicable copyright laws. All rights are reserved. 
//  Any use in violation of the foregoing restrictions may subject the 
// user to criminal sanctions under applicable laws, as well as to 
// civil liability for the breach of the terms and conditions of this 
// license.
//
// THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES, 
// WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED 
// TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
// PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, 
// IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR 
// CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
//
//
//*********************************************************************
//  Filename:   mem-87x.c
//*********************************************************************
//  Author:     Lucio Di Jasio
//  Company:    Microchip Technology
//  Revision:   Rev 1.00
//  Date:       08/11/00
//
//  Internal EEPROM routines for PIC16F87X 
// 
//  Compiled using HiTech PIC C compiler v.7.93
//  Compiled using CCS    PIC C compiler v. 2.535
//;*********************************************************************
#include "dechit.h"
#include "main.h"

bank1 byte dummy;
bank1 byte Cur_address;

void SPIinit()
{
	PIR1=0;		
	SSPCON=0x31;	//SSPEN=1;CKP=0 , FOSC/4 
	SSPSTAT=0x00;
	CS = 0;
}

byte SPI_OUTPUT ( byte spi_byte )
{
	SSPBUF = spi_byte;
	do {
		}while(!STAT_BF);
	return SSPBUF;
}

/*
void WRenable()
{
	CS = 1;
	SPI_OUTPUT(0x1);
	SPI_OUTPUT(0x30);
	CS  = 0;
}
*/

byte WRITE_93LC46B ( byte EE_address, word EE_data )
{
	byte b1,b2;
	GIE = 0;

	b1= EE_data >> 8;
	b2 = EE_data &0xff;

	CS = 1;	// Set chip select high
	dummy = SPI_OUTPUT ( 0x01 ); // start bit is bit 1,
	dummy = SPI_OUTPUT ( 0x30 ); // write enable,

	CS = 0; // Must set chip select low to start
			// internal programming cycle.
	asm("clrwdt");
	asm("nop");

	CS = 1;
	EE_address |= 0x40;
	dummy = SPI_OUTPUT ( 0x01 ); // start bit is bit 1,
	dummy = SPI_OUTPUT ( EE_address );    // Address location
	dummy = SPI_OUTPUT ( b1 );       // Data
	dummy = SPI_OUTPUT ( b2 );       // Data

	CS = 0; // Must set chip select low to start
			// internal programming cycle.
	asm("clrwdt");
	asm("nop");
	CS = 1;
	do{
		//timeout
	}while(SDI == 0);
	CS = 0;

	asm("clrwdt");
	asm("nop");

	CS = 1;
	dummy = SPI_OUTPUT ( 0x01 ); // start bit is bit 1,
	dummy = SPI_OUTPUT ( 0x0 );       // write disable

	CS = 0;
	return 1;

}

word READ_93LC46B(byte EE_address)
{
	word w;
	EE_address &=0x3f;
	Cur_address = EE_address;
	
	//GIE = 0 ;
	CS = 1;	// Set chip select high

	EE_address |= 0x80;
	dummy = SPI_OUTPUT ( 0x01 ); // start bit is bit 1,
	dummy = SPI_OUTPUT ( EE_address );    // Address location

	CKP = 0;
	w = SPI_OUTPUT(0x00);
	dummy = SPI_OUTPUT(0x00);
	w = (w <<8 )+dummy;

	CS = 0;
	CKP = 1;
	//GIE = 1 ;
	return w;
	
}

void RDword(word Ind)
{
	asm("clrwdt");
    Dato = READ_93LC46B( Ind);

    //Dato = EEPROM_READ( Ind);
    //Dato += (word) EEPROM_READ( Ind+1) <<8;
}

void RDnext()
{
    // continue reading
    //EEADR++;        // NOTE generate no carry
    //Dato = ((RD=1), EEDATA);
    //EEADR++;
    //Dato += ((RD=1), EEDATA)<<8;
	Cur_address ++;
    Dato = READ_93LC46B( Cur_address);

}

void WRword(word Ind)
{
	asm("clrwdt");
    WRITE_93LC46B( Ind, Dato); GIE = 1; // write and re-enable interrupt
    //WRITE_93LC46B( Ind+1, Dato>>8); GIE = 1; 
}

void Test_93lc46b()
{
	Dato = 0x1234;
	WRword(10);
	Dato = 0;
	RDword(10);
	Dato +=0;

}


⌨️ 快捷键说明

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