我们正在尝试在pic32和ADS1298之间建立SPI通信。问题是我们无法正确设置时钟。
我尝试过的是以下
#include <xc.h>
#include "SDlib16.h"
#pragma config FNOSC = FRCPLL // Oscillator selection
#pragma config POSCMOD = HS // Primary oscillator mode
#pragma config FPLLIDIV = DIV_2 // PLL input divider (8 -> 4)
#pragma config FPLLMUL = MUL_20 // PLL multiplier ( 4x20 = 80)
#pragma config FPLLODIV = DIV_1 // PLL output divider
#pragma config FPBDIV = DIV_2 // Peripheral bus clock divider 80/ 2= 40 mhz
#pragma config FSOSCEN = OFF // Secondary oscillator enable
#pragma config IESO=OFF
#pragma config FCKSM=CSDCMD
#pragma config OSCIOFNC=OFF
#pragma config FWDTEN = OFF // Watchdog timer enable
#pragma config WDTPS = PS4096 // Watchdog timer post-scaler
#pragma config FSRSSEL = PRIORITY_7 // SRS interrupt priority
/*
*
*/
void configpins(void)
{
DDPCONbits.JTAGEN = 0;//Turns off the JTAG function of all pins
AD1PCFG=0xFFFF;//Turns off the analog function of all pins
TRISBbits.TRISB8=0; //Makes RB8 an output (Chip select)
TRISBbits.TRISB12=0;//Makes RB12 an output(Hold)
TRISBbits.TRISB10=0;//Makes RB10 an output(Write protect)
}
void initialSPI(void)
{
IEC0CLR=0x03800000;//turns off SPI interrupts
SPI4CONbits.ON=0;//turns off SPI4 module
SPI4BUF=0;//Sets SPI4 buffer to 0
SPI4CONbits.ENHBUF=0;//Sets ENHBUF to zero
SPI4CONbits.CKP=0;//Sets clock phase bit to 0
SPI4BRG=0;//Sets the SPI clock to be at 20MHz(Logic analyzer cant sample high enough for 20MHz )
SPI4STATbits.SPIROV=0;//Sets overflow bit to zero
SPI4CONbits.MSTEN=1;//Sets microcontroller to master mode
SPI4CONbits.DISSDO=0;//Sets the module to control the SDO pin
SPI4CONbits.CKE=1;//Sets the clock edge bit to 1
SPI4CONbits.SMP=0;//Sets SMP bit to 0
SPI4CONbits.MODE16=0;//Turns off 16bit mode
SPI4CONbits.MODE32=0;//Turns off 32bit mode
SPI4CONbits.ON=1;// Turns on SPI4 module
}
unsigned char Transfer(unsigned char data)
{
SPI4STATbits.SPIRBF=0; //Clears the receives buffer bit
SPI4BUF = data;//Puts data into the buffer
SPI4STATbits.SPITBE=0;//Turns off buffer enable
while (!SPI4STATbits.SPIRBF);//While the Receive buffer flag is empty
return SPI4BUF; // read a byte
}
unsigned char SPITransaction(unsigned char data)//Simple one byte in and one out
{
PORTBbits.RB8=0;//clears the chip select
unsigned char CharReturned=Transfer(data);// Calls the transfer function to send byte to buffer
PORTBbits.RB8=1;//Sets the chip select
return CharReturned;//Returns the byte received from buffer
}
void ReadDevice(void)//Gets the Device ID
{
PORTBbits.RB8=0;//Clears the chip select
Transfer(0xAB);//Bytes to get the device id
Transfer(0x00);
Transfer(0x00);
Transfer(0x00);
Transfer(0x00);
Transfer(0x00);
PORTBbits.RB8=1;//Sets the Device ID
}
int main(int argc, char** argv)
{
configpins();
initialSPI();
PORTBbits.RB10=1;//Sets the Write protect to high
PORTBbits.RB12=1;//Sets Hold to high
PORTBbits.RB0=0;
while(1)// Forever loop
{
//unsigned char Result=SPITransaction('W');//Sends W to the buffer and gets back a byte
//ReadDevice();//Gets the Device ID
PORTBbits.RB8=0;//Clears the chip select
Transfer(0x08);//Bytes to get the device id
PORTBbits.RB8=1;//Sets the Device ID
}
return (EXIT_SUCCESS);
}
我不知道我在做错什么,当我将时钟连接到逻辑分析仪时,它看起来不像时钟,而是看起来完全是随机的东西,有人知道我在做什么?
答案 0 :(得分:0)
我认为您错过了将SCK(时钟)设置为输出的方法。