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

📄 spi_mem.c

📁 pic单片机驱动mcp2510源代码
💻 C
字号:
/*
** Copyright (C)1999 KVASER AB, http://www.kvaser.com
** This code may be freely distrubuted and used if the source is indicated.
**
**  Includes all fundamental functions to communicate by the SPI-bus
**  Main function is for the CAN-controller MCP2510 and Memory 25LC640 
*/

// #define  PORTBIT(adr, bit) ((unsigned)(&adr)*8+(bit))

#include  <pic.h>
#include "..\inc\std.h"
#include "..\inc\spi.h"
#include "..\inc\spi_mem.h"

// ;***************25Cxxx command definitions
#define   WREN  6                 //write enable latch
#define   WRDI  4                 //reset the write enable latch
#define   RDSR  5                 //read status register
#define   WRSR  1                 //write status register
#define   READ  3                 //read data from memory
#define   WRITE 2                 //write data to memory

//    ; Bit defines within status register
#define   WIP   0                 //write in progress 
#define   WEL   1                 //write enable latch 
#define   BP0   2                 //block protection bit 
#define   BP1   3                 //block protection bit 


void SPI_mem_select (void)
{
    memCS = 0;
}

void SPI_mem_unselect (void)
{
    CKP = 0;
    memCS = 1;
    CKP = 1;
}

void SPI_mem_RD_address(uchar output)
{
    unsigned char store;
    memCS=0;              // Select the memory
    store = SPI_putch( READ );    // Write the command
    store = SPI_putch( 0x00 );    // Write high byte in address
    store = SPI_putch( output );    // Write low byte in address 
}

void SPI_mem_WR_address(uchar output)
{
    unsigned char store;
    memCS=0;              // Select the memory
    store = SPI_putch( WRITE );    // Write the command
    store = SPI_putch( 0x00 );    // Write high byte in address
    store = SPI_putch( output );    // Write low byte in address 
}


// ;***** Write Enable memory device  **************************************** 
void SPI_mem_WR_enable(void)
{
    uchar store;
    memCS=0;              // Select the memory
    store = SPI_putch( WREN );    // Write the command
    CKP = 0;
    memCS=1;              // deselect the memory to effectuate the command
    CKP = 1;
}

unsigned char SPI_mem_RD_status ( void )
{
    uchar store;
    memCS=0;              // Select the memory
    store =  SPI_putch( RDSR );   // Write the command
    store =  SPI_putch( RDSR );   // Write any byte to get a byte in return 
    CKP = 0;
    memCS=1;              // deselect the memory to effectuate the command
    CKP = 1;
    return store;        // Command done OK
} 

void SPI_mem_WR_status ( uchar outdata )
{
    uchar store;
    memCS=0;              // Select the memory
    store =  SPI_putch( WRSR );   // Write the command
    store =  SPI_putch( outdata );   // enable write by setting BP1 to 1 
    CKP = 0;
    memCS=1;
    CKP = 1;              // deselect the memory to effectuate the command
} 

uchar SPI_mem_busy ( uchar outdata )
{
    uchar store;
    do {
        memCS=0;              // Select the memory
        store = SPI_putch( RDSR );   // Write the command
        store = SPI_putch( 0x00 );   // Get status byte by dummy write 
        CKP = 0;
        memCS=1;               // deselect the memory to effectuate the command
        CKP = 1;
    } while ( (store & 0x01)== 0x01 ); 
    return store;        // Command done OK
} 



⌨️ 快捷键说明

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