gpt4 book ai didi

c - SPI通讯,如何正确设置时钟

转载 作者:行者123 更新时间:2023-11-30 16:20:22 25 4
gpt4 key购买 nike

我们正在尝试在 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);
}

我不知道我做错了什么,当我将时钟连接到逻辑分析仪时,它看起来不像时钟,而是看起来完全随机的东西有人知道我做错了什么吗?

最佳答案

我认为您错过了将 SCK(时钟)设置为输出。

关于c - SPI通讯,如何正确设置时钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55327428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com