Board logo

标题: PIC单片机C与汇编混合编程之模拟SPI接口发送数据 [打印本页]

作者: 苹果也疯狂    时间: 2014-3-6 21:58     标题: PIC单片机C与汇编混合编程之模拟SPI接口发送数据

#include <htc.h>
#define _XTAL_FREQ 4000000

__IDLOC(1234);
__CONFIG(OSC_IntRC & WDT_OFF & CP_OFF & MCLRE_ON);


volatile bit CLK        @ ((unsigned)&GPIO*8)+0;
volatile bit DAT        @ ((unsigned)&GPIO*8)+1;
volatile bit RCK        @ ((unsigned)&GPIO*8)+2;


#define bDAT 1
#define bRCK 2
#define bCLK 0

unsigned char buf;
unsigned char temp;


/*************************
时序CLK的脉宽>=10us
发送顺序:MSB->LSB
*************************/
void SPI_SendByte(void)
{

#asm

MOVLW
8

MOVWF
_temp
Clock:

NOP

NOP

NOP

NOP

NOP

bcf
_GPIO,bCLK
//1us

btfss
_buf, 7
//1us

GOTO
ClearBit
//1/2us
SetBit:

NOP


bsf
_GPIO, bDAT
//第5us,DAT=1

GOTO
SetupOver
//5+2= 7us
ClearBit:

bcf
_GPIO, bDAT
//1+1+1+2+1 = 5us

NOP

NOP
//7us
SetupOver:

NOP

NOP

NOP
//10us

bsf
_GPIO, bCLK

RLF
_buf, f

decfsz
_temp, f

GOTO
Clock

NOP

NOP

NOP

NOP

NOP

NOP

NOP

bcf
_GPIO, bCLK

NOP

NOP

NOP

NOP

NOP

NOP

NOP

NOP

NOP

#endasm
}


int main(void)
{

TRISGPIO = 0xf8;
//1111 1000

OPTION = 0x82;
//Time0时钟分频比为1:8

CLK = 0;

RCK = 0;

DAT = 0;



while(1)


{


RCK = 1;

buf = 0x81;

SPI_SendByte();


buf = 0x55;

SPI_SendByte();


buf = 0x06;

SPI_SendByte();


buf = 0x06;

SPI_SendByte();


buf = 0x6a;

SPI_SendByte();


buf = 0x6d;
//5

SPI_SendByte();


buf = 0x6f; //9

SPI_SendByte();


buf = 0x55;

SPI_SendByte();


RCK = 0;

__delay_us(50);

}

return 0;
}




欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0