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

📄 spi_mcp.c

📁 pic单片机工程
💻 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_mcp.h"


// Selects the MCP2510.
// Note: mcpCS is set to zero also by SPI_mcp_RD_address(),
// SPI_mcp_WR_address() and SPI_mcp_RD_status().
void SPI_mcp_select (void)
{
    mcpCS = 0;
}

// To avoid a bug, SCK must be low when CS is raising. But thing doesn't work
// if CKP remains low, so we set it to 1 as soon as CS is 1.
void SPI_mcp_unselect (void)
{
    CKP = 0;
    mcpCS = 1;
    CKP = 1;
}

void SPI_mcp_reset ( void )
{
    SPI_mcp_select();
    SPI_putch(RESET);
    SPI_mcp_unselect();
}

void SPI_mcp_RD_address(uchar output)
{
    uchar store;
    mcpCS=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_mcp_WR_address(uchar output)
{
    uchar store;
    mcpCS=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 
}


uchar SPI_mcp_RD_status ( void )
{
    uchar store;
    mcpCS=0;              // Select the memory
    store =  SPI_putch( RD_STAT );   // Write the command
    store =  SPI_putch( RD_STAT );   // 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_mcp_write_bits( uchar MCPaddr, uchar data, uchar mask )
{
    SPI_mcp_select ();
    SPI_putch( BIT_MOD );
    SPI_putch( MCPaddr );
    SPI_putch( mask  );
    SPI_putch( data );
    SPI_mcp_unselect ();
}

⌨️ 快捷键说明

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