//******************SPI 初始化函数************************
void SPI_Init(void)
{
int rData=0;
IEC1CLR=0x000000E0; // disable all interrupts SPI2TXRE/SPI2RXRE/SPI2EIE
SPI2CON = 0; // Stops and resets the SPI1.
rData=SPI2BUF; // clears the receive buffer
// IFS1CLR=0x03800000; // clear any existing event
IFS1bits.SPI2EIF=0;
IFS1bits.SPI2RXIF=0;
IFS1bits.SPI2TXIF=0;
IEC1SET=0x000000E0; // Enable RX, TX and Error interrupts
SPI2BRG=0x0; // 0x1F;85 // use FPB/172 clock frequency BRG=85; SCK=116.28kHz
SPI2STATCLR=0x40; // clear the Overflow
SPI2CON=0x04008660; // 0x82A0; //空闲时总线为高电平,
// CS=1;
}
//****************读写通信函数**************************
short comm(short data)
{
// if(SPI2STATbits.SPITBE==1)
{
SPI2BUF = data; //发送数据
return SPI2BUF;
}
}
void write(short addata)
{
CS=0; //使能片选信号
comm(addata); //使能模块地址位,使能DAC,Rx,Tx,PA模块
CS=1;
}
short read(short add) //写第二个寄存器返回第一个寄存器的数据
{
short rtemp;
CS=0;
rtemp=comm(add|0x8000);
CS=1;
return (rtemp);
}
void send_data(int data_DAC)
{
CS=0;
comm(data_DAC);
CS=1;
}
//******************031初始化函数***********************
void Init031(void)
{
/////////////发送////////////////////
write(0x0914);
write(0x030E);
write(0x0560);
write(0x0400);
write(0x0123);
write(0x023A);
}
//************************主函数**************************
short read0,read1,read2,read3,read4,read5,read9,readA,readB; //************//
void main(void)
{
AD1PCFG = 0xFFFF; //引脚为数字I/O
// TRISAbits.TRISA7 = 0; //RA7连接LED,设置为输出
TRISBbits.TRISB5 = 0; //SD引脚
TRISGbits.TRISG9 = 0; //SS2A 031片选设置为输出
TRISEbits.TRISE9 = 0;
TRISGbits.TRISG6 = 0; //SCK2A
DAC=0; //SPI为指令和数据寄存器模式
SD=0; //031正常运行模式
SPI_Init(); //调用SPI初始化函数
Init031();
read0=read(0x0100);
read1=read(0x0200);
read2=read(0x0300);
read3=read(0x0400);
read4=read(0x0500);
read5=read(0x0900);
read9=read(0x0A00);
readA=read(0x0B00);
readB=read(0x0100);
DAC=1; //SPI进入DAC寄存器模式
while(1)
{
send_data(0x7C00); //1.6V
send_data(0xFFC0); //3.2V
send_data(0x7C00); //1.6V
send_data(0x0000); //0V
}
}
int num=0;
void __ISR(_SPI_1_VECTOR, ipl3)__SPI2Interrupt(void)
{
num++;
IFS0CLR = 0x000000E0;
}
这是我的程序,用的是PIC32MX795F512单片机, |