如何解决从 pic24fv16k301
我想从 pic24fv16k301 微控制器读取 SPI 值。如果 1 或 2 从微控制器发送 LED 将打开,但当我发送它时什么也没有发生。我没有在图片上进行 SPI 编程的经验,所以我认为这将是一个好的开始。数据通过arduino发送。通过将其连接到另一个 arduino 来测试 arduino 代码,这似乎有效。
这是代码
#include <xc.h>
#include "Header_School_Project.h"
#include <stdlib.h>
#include <stdio.h>
#define FOSC (7370000ULL) //Allows to use the macros of the __delay_ms function in xc16
#define FCY (FOSC/2)
#include <libpic30.h>
#define _XTAL_FREQ 20000000
#define LED_HIGH PORTBbits.RB2
#define LED_LOW PORTAbits.RA4
#define HIGH_VALUE 1
#define LOW_VALUE 2
void SPI_Master_Init()
{
SPI1CON1bits.MSTEN = 1; //Set SPI to master
SPI1CON1bits.SMP = 0; //Input data is sampled at the middle of output
SPI1STATbits.SPIROV = 0; //No overflow is occuring
SPI1STATbits.SPIEN = 1; //Enables SCK,SDO,SDI as serial ports
//Configure clock polarity & phase
SPI1CON1bits.CKE = 0;
SPI1CON1bits.CKP = 0;
TRISBbits.TRISB13 = 0; //RB13 = sdo -> OUTPUT
TRISBbits.TRISB14 = 1; //RB14 = sdi -> INPUT
TRISBbits.TRISB12 = 0; //RB12 = sck -> OUTPUT
//
}
char SPI_read()
{
while(!IFS0bits.SPI1IF);
IFS0bits.SPI1IF=0;
return(SPI1BUF);
}
void main(void)
{
TRISBbits.TRISB2 = 0;
TRISAbits.TRISA4 = 0;
ANSA = 0; //sets to digital
ANSB = 0;
SPI_Master_Init(); //Initialize the SPI in Master Mode
char buffer;
//---------------------------------
while(1)
{
buffer = SPI_read();
if(buffer == HIGH_VALUE)
{
LED_HIGH = 1;
LED_LOW = 0;
}
if(buffer == LOW_VALUE)
{
LED_LOW = 1;
LED_HIGH = 0;
}
}
}
如果有人可以帮助在 pic24 上接收 SPI 数据,那将不胜感激。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。