再次更新驱动代码:
#include "fsl_iomuxc.h"
#include "fsl_gpio.h"
#include "fsl_lpspi.h"
#include "pad_config.h"
#include "bsp_lpspi.h"
static void LPSPI_GPIO_Init(void);
static void LPSPI_GPIO_Init(void)
{
IOMUXC_SetPinMux(
IOMUXC_GPIO_AD_B0_00_LPSPI3_SCK, /* GPIO_AD_B0_00 is configured as LPSPI3_SCK */
0U); /* Software Input On Field: Input Path is determined by functionality */
IOMUXC_SetPinMux(
IOMUXC_GPIO_AD_B0_01_LPSPI3_SDO, /* GPIO_AD_B0_01 is configured as LPSPI3_SDO */
0U); /* Software Input On Field: Input Path is determined by functionality */
IOMUXC_SetPinMux(
IOMUXC_GPIO_AD_B0_02_LPSPI3_SDI, /* GPIO_AD_B0_02 is configured as LPSPI3_SDI */
0U); /* Software Input On Field: Input Path is determined by functionality */
IOMUXC_SetPinMux(
IOMUXC_GPIO_AD_B0_03_LPSPI3_PCS0, /* GPIO_AD_B0_03 is configured as LPSPI3_PCS0 */
0U);
}
void LPSPI_Init(void)
{
//config Lpspi clock
CLOCK_SetMux(kCLOCK_LpspiMux, 3);
CLOCK_SetDiv(kCLOCK_LpspiDiv, 7U);
//initial Gpio config
LPSPI_GPIO_Init();
//initial lpspi config
lpspi_master_config_t config;
LPSPI_MasterGetDefaultConfig(&config);
//config lpspi cs pin
config.whichPcs=kLPSPI_Pcs0;
// config.cpol=kLPSPI_ClockPolarityActiveLow;
// 使用ADX112 直接使用此配置
config.cpha=kLPSPI_ClockPhaseSecondEdge;
LPSPI_MasterInit(LPSPI3, &config, LPSPI3_CLOCK_FREQ);
}
//写数据
void Lpspi_Write(uint8_t datasize,uint8_t *data)
{
lpspi_transfer_t spi_tranxfer;
spi_tranxfer.configFlags=kLPSPI_MasterPcs0|kLPSPI_MasterPcsContinuous | kLPSPI_MasterByteSwap;
spi_tranxfer.txData=data; //要发送的数据
spi_tranxfer.rxData=NULL;
spi_tranxfer.dataSize=datasize; //数据长度
LPSPI_MasterTransferBlocking(LPSPI3, &spi_tranxfer);
}
//读写数据
void Lpspi_Read(uint8_t datasize,uint8_t *data,uint8_t *Rxdata)
{
lpspi_transfer_t spi_tranxfer;
spi_tranxfer.configFlags=kLPSPI_MasterPcs0|kLPSPI_MasterPcsContinuous | kLPSPI_MasterByteSwap;
spi_tranxfer.txData=data; //要发送的数据
spi_tranxfer.rxData=Rxdata; //要接收的数据
spi_tranxfer.dataSize=datasize; //数据长度
LPSPI_MasterTransferBlocking(LPSPI3, &spi_tranxfer);
}
#ifndef __BSP_LPSPI_H
#define __BSP_LPSPI_H
#include "fsl_common.h"
#define LPSPI3_CLOCK_FREQ 6000000UL
void LPSPI_Init(void);
//xue
void Lpspi_Write(uint8_t datasize,uint8_t *data);
void Lpspi_Read(uint8_t datasize,uint8_t *data,uint8_t *Rxdata);
#endif /* __BSP_LPSPI_H */
可以直接驱动ADX112
/**
******************************************************************************
* [url=home.php?mod=space&uid=1307177]@File[/url] main.c
* [url=home.php?mod=space&uid=1315547]@author[/url] fire
* [url=home.php?mod=space&uid=252314]@version[/url] V1.0
* [url=home.php?mod=space&uid=311857]@date[/url] 2018-xx-xx
* [url=home.php?mod=space&uid=159083]@brief[/url] GPIO输入—按键中断检测
******************************************************************************
* [url=home.php?mod=space&uid=1020061]@attention[/url] *
* 实验平台:野火 i.MXRT1052开发板
* 论坛 :http://www.firebbs.cn
* 淘宝 :http://firestm32.taobao.com
*
******************************************************************************
*/
#include "board.h"
#include "fsl_debug_console.h"
#include "fsl_gpio.h"
#include "fsl_gpt.h"
#include "fsl_lpspi.h"
#include "fsl_iomuxc.h"
#include "pad_config.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "fsl_lpi2c.h"
#include "./bsp/nvic/bsp_nvic.h"
#include "./bsp/led/bsp_led.h"
#include "./bsp/key/bsp_key_it.h"
#include "ad5668.h"
#include "fsl_adc.h"
#include "rtthread.h"
#include "bsp_lpspi.h"
#include "bsp_lpi2c.h"
#include "AnalogysemiCSD.h"
// 添加adc的返回值用于打印
volatile uint16_t ADC_Value;
uint8_t datas[]={0x04,0x9A,0x04,0X9A};
uint8_t Rdatas[2];
/*******************************************************************kkokkkkkkkkkkk
* Prototypes
*******************************************************************/
/**
* @brief 延时一段时间
*/
void delay(uint32_t count);
/*******************************************************************
* Code
*******************************************************************/
/**
* @note 本函数在不同的优化模式下延时时间不同,
* 如flexspi_nor_debug和flexspi_nor_release版本的程序中,
* flexspi_nor_release版本的延时要短得多
*/
void delay(uint32_t count)
{
volatile uint32_t i = 0;
for (i = 0; i < count; ++i)
{
__asm("NOP"); /* 调用nop空指令 */
__asm("NOP"); /* 调用nop空指令 */
__asm("NOP"); /* 调用nop空指令 */
__asm("NOP"); /* 调用nop空指令 */
__asm("NOP"); /* 调用nop空指令 */
}
}
// 步骤2:新建ADC初始化函数
void RT1052_ADC_Init(void)
{
adc_config_t adcConfigStruct;
adc_channel_config_t adcChannelConfigStruct;
/*
* config->enableAsynchronousClockOutput = true;
* config->enableOverWrite = false;
* config->enableContinuousConversion = false;
* config->enableHighSpeed = false;
* config->enableLowPower = false;
* config->enableLongSample = false;
* config->referenceVoltageSource = kADC_ReferenceVoltageSourceVref;
* config->samplePeriodMode = kADC_SamplePeriod2or12Clocks;
* config->clockSource = kADC_ClockSourceAD;
* config->clockDriver = kADC_ClockDriver1;
* config->resolution = kADC_Resolution12Bit;
*/
ADC_GetDefaultConfig(&adcConfigStruct);
//最大resolution 12bit 只有外部参考
//adcConfigStruct.referenceVoltageSource=kADC_ReferenceVoltageSourceAlt0;
//开启连续转换模式
adcConfigStruct.enableContinuousConversion=true;
ADC_Init(ADC1, &adcConfigStruct);
//开启ADC中断
EnableIRQ(ADC1_IRQn);
//开启ADC1 自动校准
ADC_DoAutoCalibration(ADC1);
//关闭硬件触发
ADC_EnableHardwareTrigger(ADC1, false);
//读取通道0
adcChannelConfigStruct.channelNumber = 0;
adcChannelConfigStruct.enableInterruptOnConversionCompleted = true;
// Channe_group 0 用于软件或者硬件出发
ADC_SetChannelConfig(ADC1, 0u /*ADC_CHANNEL_GROUP*/, &adcChannelConfigStruct);
}
// ADC 中断入口函数
void ADC1_IRQHandler(void)
{
ADC_Value= ADC_GetChannelConversionValue(ADC1, 0U);
rt_kprintf("ADC_Value :%d\r\n",ADC_Value);
}
/**
* @brief 主函数
* @param 无
* @retval 无
*/
int main(void)
{
// uint16_t BusVoltage;
rt_kprintf("hello analogysemi \r\n");
User_GPIO_Init();
LPSPI_Init();
Lpspi_Read(4,datas,Rdatas);
while(1)
{
Lpspi_Read(4,datas,Rdatas);
rt_thread_mdelay(10);
Lpspi_Read(4,datas,Rdatas);
rt_thread_mdelay(10);
}
}
/*********************************************END OF FILE**********************/
ADX112 测温结果输出
|