|
本帖最后由 reliXQ 于 2018-11-29 16:59 编辑
SPI驱动MAX6675出现程序一直重启
程序代码如下:
//参考网址代码: nrf52832 SPI方式无操作系统与SD卡通信
//https://blog.csdn.net/shouwang1994/article/details/78774927
#include "max6675.h"
#include <string.h>
#include "nrf_drv_common.h"
#include "nrf_drv_spi.h"
#include "pca10040.h"
#include "app_error.h"
#include "nrf_gpio.h"
#include "nrf_delay.h"
#define SPI_INSTANCE 0 /**< SPI instance index. */
static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE); /**< SPI instance. */
uint8_t spi_tx[5];
uint8_t spi_rx[5];
static uint32_t delay_HZ=4;
static volatile bool spi_xfer_done; //SPI数据传输完成标志
/**
* @brief SPI user event handler.
* @param event
*/
void spi_event_handler(nrf_drv_spi_evt_t const * p_event)
{
spi_xfer_done = true; //将数据接收标志位设为已接收
}
//片选拉高
void CS_MAX6675_HIGH(uint8_t pin)
{
nrf_gpio_pin_set(pin); //输出高电平
}
void CS_MAX6675_LOW(uint8_t pin)
{
nrf_gpio_pin_clear(pin); //输出低电平
}
char max6675_spi_init(void)
{
uint32_t err_code;
const nrf_drv_spi_config_t spi_config =
{
.sck_pin = SPIM0_SCK_PIN,
.mosi_pin = SPIM0_MOSI_PIN,
.miso_pin = SPIM0_MISO_PIN,
.ss_pin = NRF_DRV_SPI_PIN_NOT_USED, //片选上层程序使能
.irq_priority = 6,
.orc = 0xFF,
.frequency = NRF_DRV_SPI_FREQ_4M,
.mode = NRF_DRV_SPI_MODE_0,
.bit_order = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST,
};
err_code = nrf_drv_spi_init(&spi,&spi_config,spi_event_handler);
APP_ERROR_CHECK(err_code);
nrf_gpio_cfg_output(SPIMO_SS_PIN_1); //片选
nrf_gpio_cfg_output(SPIMO_SS_PIN_2);
CS_MAX6675_HIGH(SPIMO_SS_PIN_1);
CS_MAX6675_HIGH(SPIMO_SS_PIN_2);
return 0;
}
uint8_t MAX6675_SPI_RW(uint8_t* rx_data,uint8_t rx_size,uint8_t* tx_data,uint8_t tx_size)
{
uint8_t retry=0;
uint32_t err_code;
/* 将接收标志位设为未接收*/
spi_xfer_done=false;
err_code = nrf_drv_spi_transfer(&spi,tx_data,tx_size,rx_data,rx_size); //我觉得是不是这里写的有问题 请大神看下
APP_ERROR_CHECK(err_code);
while(spi_xfer_done)
{
retry++;
if(retry>200) return false; //放死锁
}
spi_xfer_done=false;
/*加延时*/
if(delay_HZ == 4){
nrf_delay_us(4);
}
else if(delay_HZ == 250){
nrf_delay_us(250);
}
else if(delay_HZ == 125){
nrf_delay_us(125);
}
return true;
}
//void MAX6675_SPI_Close(void)
//{
// nrf_drv_spi_uninit(&spi);
// printf("dd\r\n");
//}
//读一个字节
static uint8_t* m_read_char(uint8_t pin, uint8_t reg)
{
CS_MAX6675_LOW(pin);
spi_tx[0]=0xAA;
spi_tx[1]= reg;
MAX6675_SPI_RW(spi_rx,2,spi_tx,2);
printf("spi_rx=%02X%02X\r\n",spi_rx[0],spi_rx[1]);
CS_MAX6675_HIGH(pin);
return spi_rx;
}
//定时器调用 入口函数//MAX6675读数据
char max6675_read_data(void)
{
unsigned long Temp=0;
m_read_char(SPIMO_SS_PIN_1,0xAA); //PARTID 寄存器地址
Temp = Temp+spi_rx[0];
Temp = (Temp<<8) + spi_rx[1];
Temp = Temp<<1;
Temp = Temp>>4;
Temp = Temp*1024/4096;
printf("Temp=%ld\r\n",Temp);
return 0;
}
|
|