PIC16F684模拟SPI端口当改变SPI_CK口时SPI_MO端口被强制改变,如下图#define SPI_CS RC3
#define SPI_CS_DR TRISC3
#define SPI_CK RC2
#define SPI_CK_DR TRISC2
#define SPI_MI RC1
#define SPI_MI_DR TRISC1
#define SPI_MO RC0
#define SPI_MO_DR TRISC0
/*
* 初始化服务程序
*/
void init(void)
{
__CONFIG (0x3F44);//F4C);
OSCCON = 0b00010000; //8MHz
PORTA = 0b00000111; // 配置I/O
TRISA = 0b00011110; // 配置I/O状态寄存器
PORTC = 0b00000000; // 配置I/O
TRISC = 0b00000010; // 配置I/O状态寄存器
//ANSEL = 0b00000000; // 数字输入
OPTION_REG= 0b10001011; // 上拉,WDT 1:16 576ms
IOCA = 0b00011000; // 0,1上拉中断
WDA = 0b00000111; // 0,1,2上拉
VRCON = 0b10100001; // 参考电压为0.2v
CMCON0 = 0b10010100;
T1CON = 0b00000111; // T1(FOSC/4)1:8
PIE1 = 0b00000000; // T1 中断
INTCON = 0b10111000; // GPIO,INT中断
}
//SPI写入
u16 SPI_WriteReg(u8 addr, u16 da)
{
u8 i;
u16 reda;
reda=0;
SPI_CS = 0;
for(i = 0; i < 8; ++ i)
{
if(da & 0x80!=0)
SPI_MO = 1;
else
SPI_MO = 0;
SPI_CK = 1; //capturing at the down side.
addr = addr << 1; //There is no Delay here. determines the rate of SPI.
SPI_CK = 0;
}
for(i = 0; i < 16; ++ i)
{
if(da & 0x8000!=0)
SPI_MO = 1;
else
SPI_MO = 0;
SPI_CK = 1; //capturing at the down side.
da = da << 1; //There is no Delay here. determines the rate of SPI.
SPI_CK = 0;
reda |= SPI_MI;
}
SPI_CS = 1;
return reda;
}
|