📄 io3w.c
字号:
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f320.h>
#include <stddef.h>
#include "typedefs.h"
//-----------------------------------------------------------------------------
// Defines
//-----------------------------------------------------------------------------
#define READ 1
#define WRITE 0
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
sbit RSTB = P1^3;
sbit SENB = P1^2;
sbit SCLK = P1^1;
sbit SDIO = P0^7;
//-----------------------------------------------------------------------------
// Function prototypes
//-----------------------------------------------------------------------------
void wait_ns(u16 ns);
void _nop_(void);
//-----------------------------------------------------------------------------
// Externals
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// This routine transfers 2 bytes of data between the host and si470x.
//
// Inputs:
// operation: Specifies read or write operation
// addr: Address for register read or write
// reg_array: source array for data to be written
// or
// destination array for data to be read
//
//-----------------------------------------------------------------------------
static void io3w_operation (u8 operation, u8 addr, u16 *reg_array)
{
i8 i;
u16 controlWord;
P0MDOUT |= 0x80; // Configure P0^7(SDIO) as push-pull
// issue the control word (8 bit chip address + R/W* bit)
if (operation == READ)
controlWord = 0x00E0 + addr; // A7-A5, R/W*, A4-A0
else
controlWord = 0x00C0 + addr; // // A7-A5, R/W*, A4-A0
SENB = 0;
for (i=8; i>=0; i--)
{
SCLK = 0;
SDIO = ((controlWord >> i) & 0x01);
wait_ns(25); // tLOW
SCLK = 1;
wait_ns(25); // tHIGH
}
// read or write data
if (operation == READ)
{
P0MDOUT &= ~(0x80); // Configure P0^7(SDIO) as open-drain
SDIO = 1; // Configure P0^7(SDIO) as a digital input
for (i=15; i>=0; i--)
{
SCLK = 0;
wait_ns(25); // tLOW
SCLK = 1;
wait_ns(25); // tCDV
reg_array[addr] = ((reg_array[addr] << 1) | SDIO);
}
}
if (operation == WRITE)
{
for (i=15; i>=0; i--)
{
SCLK = 0;
SDIO = ((reg_array[addr] >> i) & 0x01);
wait_ns(25); // tLOW
SCLK = 1;
wait_ns(25); // tHIGH
}
}
// end the transaction
SCLK = 0;
wait_ns(5); // tLOW - tS
SENB = 1;
wait_ns(20); // tS
SCLK = 1;
wait_ns(25); // tHIGH
SCLK = 0;
P0MDOUT |= 0x80; // Configure P0^7(SDIO) as push-pull
SDIO = 1;
}
//-----------------------------------------------------------------------------
// This routine reads a block of registers from the Si470x.
//
// Inputs:
// firstreg: First register to be read
// lastreg: Last register to be read
// reg_array: Destination array for data read
//
//-----------------------------------------------------------------------------
void io3w_read (u8 firstreg, u8 lastreg, u16 *reg_array)
{
u8 reg;
reg = firstreg;
lastreg = (lastreg+1)& 0xf;
do
{
io3w_operation(READ, reg, reg_array);
reg = (reg + 1) & 0xf;
} while (reg != lastreg);
}
//-----------------------------------------------------------------------------
// This routine writes a block of registers to the Si470x.
//
// Inputs:
// firstreg: First register to be written
// lastreg: Last register to be written
// reg_array: Source array for data to be written
//
//-----------------------------------------------------------------------------
void io3w_write (u8 firstreg, u8 lastreg, u16 *reg_array)
{
u8 reg;
reg = firstreg;
lastreg = (lastreg+1)& 0xf;
do
{
io3w_operation(WRITE, reg, reg_array);
reg = (reg + 1) & 0xf;
} while (reg != lastreg);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -