RT1052 (7) 驱动SPI-DAC
<div class='showpostmsg'><h1>RT1052 (7) 驱动SPI-DAC</h1><p > 首先在Bsp文件夹新建DAC目录,如下所示,因为是基于第4篇的工程文件,所以直接从第4篇下载工程文件即可。</p>
<p align="center" > </p>
<p align="center" >图1:BSP文件夹</p>
<p align="center" > </p>
<p align="center" >图2:放入DAC驱动</p>
<p >Dac .c文件</p>
<table style="border-collapse:collapse; border:none">
<tbody>
<tr>
<td style="border-bottom:1px solid black; border-top:1px solid black; border-right:1px solid black; border-left:1px solid black" valign="top">
<p align="left" >#include "AD5668.h"</p>
<p align="left" >/*</p>
<p align="left" > Channel Selection Range 1~16</p>
<p align="left" > Value Range 0~65535</p>
<p align="left" >*/</p>
<p align="left" >void DAC_Write(uint8_t channel,uint16_t Value)</p>
<p align="left" >{</p>
<p align="left" > uint16_t Date,temp;</p>
<p align="left" > if(channel<=8)</p>
<p align="left" > {</p>
<p align="left" > DAC_CS1_L;</p>
<p align="left" > /* Select Channel */</p>
<p align="left" > Date=WriteAndUpDateCh;</p>
<p align="left" > Date<<=4;</p>
<p align="left" > Date|=channel-1;</p>
<p align="left" > Date<<=4;</p>
<p align="left" > temp=Value>>12;</p>
<p align="left" > Date|=temp;</p>
<p align="left" > Soft_SPI_Write(Date);</p>
<p align="left" > Date=Value<<4;</p>
<p align="left" > Soft_SPI_Write(Date);</p>
<p align="left" > DAC_CS1_H;</p>
<p align="left" > }</p>
<p align="left" > else</p>
<p align="left" > {</p>
<p align="left" > DAC_CS2_L;</p>
<p align="left" > /* Select Channel */</p>
<p align="left" > Date=WriteAndUpDateCh;</p>
<p align="left" > Date<<=4;</p>
<p align="left" > Date|=channel-9;</p>
<p align="left" > Date<<=4;</p>
<p align="left" > temp=Value>>12;</p>
<p align="left" > Date|=temp;</p>
<p align="left" > Soft_SPI_Write(Date);</p>
<p align="left" > Date=Value<<4;</p>
<p align="left" > Soft_SPI_Write(Date);</p>
<p align="left" > DAC_CS2_H;</p>
<p align="left" > }</p>
<p align="left" >}</p>
<p align="left" >/*</p>
<p align="left" > Initial DAC GPIO</p>
<p align="left" >*/</p>
<p align="left" >void DAC_Init(void)</p>
<p align="left" >{</p>
<p align="left" > GPIO_InitTypeDef GPIO_InitStruct;</p>
<p align="left" > __HAL_RCC_GPIOC_CLK_ENABLE();</p>
<p align="left" > GPIO_InitStruct.Pin = DAC_CLR_PIN;</p>
<p align="left" > GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;</p>
<p align="left" > GPIO_InitStruct.Pull = GPIO_PULLDOWN;</p>
<p align="left" > GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;</p>
<p align="left" > HAL_GPIO_Init(DAC_CLR_PORT,&GPIO_InitStruct);</p>
<p align="left" > DAC_CLR_L;</p>
<p align="left" > /* OPEN INTERANL REFERENCE */</p>
<p align="left" > DAC_CS1_L;</p>
<p align="left" > Soft_SPI_Write(0x08f0);</p>
<p align="left" > Soft_SPI_Write(0x0001);</p>
<p align="left" > DAC_CS1_H;</p>
<p align="left" > DAC_CS2_L;</p>
<p align="left" > Soft_SPI_Write(0x08f0);</p>
<p align="left" > Soft_SPI_Write(0x0001);</p>
<p align="left" > DAC_CS2_H;</p>
<p align="left" >}</p>
<p align="left" > </p>
<p > </p>
</td>
</tr>
</tbody>
</table>
<p >Dac.h文件</p>
<table style="border-collapse:collapse; border:none">
<tbody>
<tr>
<td style="border-bottom:1px solid black; border-top:1px solid black; border-right:1px solid black; border-left:1px solid black" valign="top">
<p align="left" >#ifndef __AD5668_H</p>
<p align="left" >#define __AD5668_H</p>
<p align="left" >#include "main.h"</p>
<p align="left" >#include "STM32_SPI.h"</p>
<p align="left" >/*</p>
<p align="left" > AD5668 COMMAND AND ADDRESS</p>
<p align="left" > MSB</p>
<p align="left" > 31 30 29 28 27 26 25 24</p>
<p align="left" > X X X X C3 C2 C1 C0</p>
<p align="left" > 23 22 21 20 19 -> 5</p>
<p align="left" > A3 A2 A1 A0 Data Msb-> DataLSB</p>
<p align="left" > 4 3 2 1</p>
<p align="left" > X X X X</p>
<p align="left" >*/</p>
<p align="left" >/*</p>
<p align="left" > COMMAND</p>
<p align="left" >*/</p>
<p align="left" >#define UpdateDacReg 0X01</p>
<p align="left" >#define WriteAndUpDateCh 0x03 </p>
<p align="left" >#define ResetDac 0x07</p>
<p align="left" >#define SetUpRef 0x80</p>
<p align="left" >/*</p>
<p align="left" > ADDRESS </p>
<p align="left" >*/</p>
<p align="left" >#define ChannelA 0X00</p>
<p align="left" >#define ChannelB 0X01</p>
<p align="left" >#define ChannelC 0X02</p>
<p align="left" >#define ChannelD 0X03</p>
<p align="left" >#define ChannelE 0X04</p>
<p align="left" >#define ChannelF 0X05</p>
<p align="left" >#define ChannelG 0X06</p>
<p align="left" >#define ChannelH 0X07</p>
<p align="left" >#define ChannelAll 0X0F</p>
<p align="left" >/*</p>
<p align="left" > Ctrl GPIO redefine</p>
<p align="left" >*/</p>
<p align="left" >#define DAC_CS1_L SF_NSS1_L</p>
<p align="left" >#define DAC_CS1_H SF_NSS1_H</p>
<p align="left" >#define DAC_CS2_L SF_NSS2_L</p>
<p align="left" >#define DAC_CS2_H SF_NSS2_H</p>
<p align="left" >#define DAC_CLR_PORT GPIOC</p>
<p align="left" >#define DAC_CLR_PIN GPIO_PIN_13</p>
<p align="left" >#define DAC_CLR_H HAL_GPIO_WritePin(DAC_CLR_PORT,DAC_CLR_PIN,GPIO_PIN_SET)</p>
<p align="left" >#define DAC_CLR_L HAL_GPIO_WritePin(DAC_CLR_PORT,DAC_CLR_PIN,GPIO_PIN_RESET)</p>
<p align="left" >/*</p>
<p align="left" > Channel Selection Range 1~8</p>
<p align="left" > Value Range 0~65535</p>
<p align="left" >*/</p>
<p align="left" >void DAC_Init(void);</p>
<p align="left" >void DAC_Write(uint8_t channel,uint16_t Value);</p>
<p align="left" >#endif</p>
<p align="left" > </p>
<p > </p>
</td>
</tr>
</tbody>
</table>
<p > 因为是拷贝之前STM32的驱动,所以还要进行一些修改,先不管。</p>
<p > </p>
<p align="center" >图3:在User里面添加驱动</p>
<p align="center" > </p>
<p align="center" >图4:添加头文件路径1</p>
<p align="center" > </p>
<p align="center" >图5:添加头文件路径2</p>
<p >因为是修改STM32的驱动,其本质没有啥差别,从<a href="https://bbs.eeworld.com.cn/thread-1225595-1-1.html" style="color:blue; text-decoration:underline" target="_blank">RT1052(2)工具生成代码</a> 那个章节直接copy代码过来就行。</p>
<p >Bsp_lpspi.c文件</p>
<table style="border-collapse:collapse; border:none">
<tbody>
<tr>
<td style="border-bottom:1px solid black; border-top:1px solid black; border-right:1px solid black; border-left:1px solid black" valign="top">
<p >#include "fsl_iomuxc.h"</p>
<p >#include "fsl_gpio.h"</p>
<p >#include "fsl_lpspi.h"</p>
<p >#include "pad_config.h"</p>
<p >#include "bsp_lpspi.h"</p>
<p > </p>
<p > </p>
<p >static void LPSPI_GPIO_Init(void);</p>
<p > </p>
<p >static void LPSPI_GPIO_Init(void)</p>
<p >{</p>
<p > IOMUXC_SetPinMux(</p>
<p > IOMUXC_GPIO_AD_B0_00_LPSPI3_SCK, /* GPIO_AD_B0_00 is configured as LPSPI3_SCK */</p>
<p > 0U); /* Software Input On Field: Input Path is determined by functionality */</p>
<p > IOMUXC_SetPinMux(</p>
<p > IOMUXC_GPIO_AD_B0_01_LPSPI3_SDO, /* GPIO_AD_B0_01 is configured as LPSPI3_SDO */</p>
<p > 0U); /* Software Input On Field: Input Path is determined by functionality */</p>
<p > IOMUXC_SetPinMux(</p>
<p > IOMUXC_GPIO_AD_B0_02_LPSPI3_SDI, /* GPIO_AD_B0_02 is configured as LPSPI3_SDI */</p>
<p > 0U); /* Software Input On Field: Input Path is determined by functionality */</p>
<p > IOMUXC_SetPinMux(</p>
<p > IOMUXC_GPIO_AD_B0_03_LPSPI3_PCS0, /* GPIO_AD_B0_03 is configured as LPSPI3_PCS0 */</p>
<p > 0U);</p>
<p >}</p>
<p >void LPSPI_Init(void)</p>
<p >{</p>
<p > //config Lpspi clock</p>
<p > CLOCK_SetMux(kCLOCK_LpspiMux, 3);</p>
<p > CLOCK_SetDiv(kCLOCK_LpspiDiv, 7U);</p>
<p > //initial Gpio config</p>
<p > LPSPI_GPIO_Init();</p>
<p > //initial lpspi config</p>
<p > lpspi_master_config_t config;</p>
<p > </p>
<p > LPSPI_MasterGetDefaultConfig(&config);</p>
<p > //config lpspi cs pin</p>
<p > config.whichPcs=kLPSPI_Pcs0;</p>
<p > </p>
<p > LPSPI_MasterInit(LPSPI3, &config, LPSPI3_CLOCK_FREQ);</p>
<p >}</p>
<p >void Lpspi_Write(uint8_t datasize,uint8_t *data)</p>
<p >{</p>
<p > lpspi_transfer_t spi_tranxfer;</p>
<p > spi_tranxfer.configFlags=kLPSPI_MasterPcs0|kLPSPI_MasterPcsContinuous | kLPSPI_MasterByteSwap;</p>
<p > spi_tranxfer.txData=data; //要发送的数据</p>
<p > spi_tranxfer.rxData=NULL;</p>
<p > spi_tranxfer.dataSize=datasize; //数据长度</p>
<p > LPSPI_MasterTransferBlocking(LPSPI3, &spi_tranxfer);</p>
<p >}</p>
</td>
</tr>
</tbody>
</table>
<p >Bsp_lpspi.h文件</p>
<table style="border-collapse:collapse; border:none">
<tbody>
<tr>
<td style="border-bottom:1px solid black; border-top:1px solid black; border-right:1px solid black; border-left:1px solid black" valign="top">
<p >#ifndef __BSP_LPSPI_H</p>
<p >#define __BSP_LPSPI_H</p>
<p > </p>
<p >#include "fsl_common.h"</p>
<p > </p>
<p > </p>
<p > </p>
<p >#define LPSPI3_CLOCK_FREQ 6000000UL</p>
<p > </p>
<p > </p>
<p >void LPSPI_Init(void);</p>
<p >void DAC_Write(uint8_t channel,uint16_t Value);</p>
<p > </p>
<p >#endif /* __BSP_LPSPI_H */</p>
<p > </p>
</td>
</tr>
</tbody>
</table>
<p >Github 地址:</p>
<p >https://github.com/xutong-analog/fantasy/tree/main/RT1052/lpspi</p>
<p >测试下驱动波形</p>
<p > </p>
<p align="center" >图6:lpspi驱动波形</p>
<p >再给AD5668套层壳</p>
<p >AD5668.C</p>
<table style="border-collapse:collapse; border:none">
<tbody>
<tr>
<td style="border-bottom:1px solid black; border-top:1px solid black; border-right:1px solid black; border-left:1px solid black" valign="top">
<p >#include "AD5668.h"</p>
<p >#include "board.h"</p>
<p >/*</p>
<p > Channel Selection Range 1~16</p>
<p > Value Range 0~65535</p>
<p >*/</p>
<p >void DAC_Write(uint8_t channel,uint16_t Value)</p>
<p >{</p>
<p > </p>
<p > uint8_t data;</p>
<p > data=WriteAndUpDateCh;</p>
<p > data=(Value>>12)|channel;</p>
<p > data=(Value>>4)&0x00ff;</p>
<p > data=(Value&0x000f)<<4;</p>
<p > </p>
<p > Lpspi_Write(4,data);</p>
<p > </p>
<p >}</p>
<p >/*</p>
<p > Initial DAC GPIO</p>
<p >*/</p>
<p >void DAC_Init(void)</p>
<p >{</p>
<p > </p>
<p > //DAC_CLR_L;</p>
<p > /* OPEN INTERANL REFERENCE */</p>
<p > uint8_t data={0x08,0xf0,0x00,0x01};</p>
<p > Lpspi_Write(4,data);</p>
<p > </p>
<p >}</p>
<p > </p>
</td>
</tr>
</tbody>
</table>
<p >AD5668.H</p>
<table style="border-collapse:collapse; border:none">
<tbody>
<tr>
<td style="border-bottom:1px solid black; border-top:1px solid black; border-right:1px solid black; border-left:1px solid black" valign="top">
<p >#ifndef __AD5668_H</p>
<p >#define __AD5668_H</p>
<p >#include "bsp_lpspi.h"</p>
<p >/*</p>
<p > AD5668 COMMAND AND ADDRESS</p>
<p > MSB</p>
<p > 31 30 29 28 27 26 25 24</p>
<p > X X X X C3 C2 C1 C0</p>
<p > 23 22 21 20 19 -> 5</p>
<p > A3 A2 A1 A0 Data Msb-> DataLSB</p>
<p > 4 3 2 1</p>
<p > X X X X</p>
<p >*/</p>
<p >/*</p>
<p > COMMAND</p>
<p >*/</p>
<p >#define UpdateDacReg 0X01</p>
<p >#define WriteAndUpDateCh 0x03 </p>
<p >#define ResetDac 0x07</p>
<p >#define SetUpRef 0x80</p>
<p >/*</p>
<p > ADDRESS </p>
<p >*/</p>
<p >#define ChannelA 0X00</p>
<p >#define ChannelB 0X10</p>
<p >#define ChannelC 0X20</p>
<p >#define ChannelD 0X30</p>
<p >#define ChannelE 0X40</p>
<p >#define ChannelF 0X50</p>
<p >#define ChannelG 0X60</p>
<p >#define ChannelH 0X70</p>
<p >#define ChannelAll 0XF0</p>
<p >/*</p>
<p > Ctrl GPIO redefine</p>
<p >*/</p>
<p >#define DAC_CS1_L SF_NSS1_L</p>
<p >#define DAC_CS1_H SF_NSS1_H</p>
<p > </p>
<p >#define DAC_CLR_PIN GPIO_PIN_13</p>
<p >#define DAC_CLR_H HAL_GPIO_WritePin(DAC_CLR_PORT,DAC_CLR_PIN,GPIO_PIN_SET)</p>
<p >#define DAC_CLR_L HAL_GPIO_WritePin(DAC_CLR_PORT,DAC_CLR_PIN,GPIO_PIN_RESET)</p>
<p > </p>
<p > </p>
<p > </p>
<p > </p>
<p >/*</p>
<p > Channel Selection Range 1~8</p>
<p > Value Range 0~65535</p>
<p >*/</p>
<p >void DAC_Init(void);</p>
<p >void DAC_Write(uint8_t channel,uint16_t Value);</p>
<p > </p>
<p >#endif</p>
</td>
</tr>
</tbody>
</table>
<p >掏出开发板</p>
<p > </p>
<p align="center" >图7:DAC开发板</p>
<p > </p>
<p align="center" >图8:DAC输出结果</p>
<p align="center" > </p>
<p align="center" >图9:测试环境</p>
<h2 >参考文档:</h2>
<p >Eeworld <a href="https://bbs.eeworld.com.cn/thread-1230425-1-1.html" style="color:blue; text-decoration:underline" target="_blank">10*10的洞洞板</a>—xutong</p>
<p >Eeworld <a href="https://bbs.eeworld.com.cn/thread-1225595-1-1.html" style="color:blue; text-decoration:underline" target="_blank">RT1052(2)工具生成代码</a> —xutong</p>
<p >Adi ad5668 datasheet</p>
<p > </p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> <div></div>
<p>https://github.com/xutong-analog/fantasy/tree/main/RT1052</p>
<p>github地址</p>
<p>更新驱动代码:</p>
<pre>
<code class="language-cpp">#include "AD5668.h"
#include "board.h"
#include "bsp_lpspi.h"
/*
Channel Selection Range 1~16
Value Range 0~65535
*/
void DAC_Write(uint8_t channel,uint16_t Value)
{
uint8_t data;
//command modify
data=WriteToReg;
//WriteAndUpDateCh
// command modify
data=(Value>>12)|channel;
data=(Value>>4)&0x00ff;
data=(Value&0x000f)<<4;
Lpspi_Write(4,data);
}
void DAC_Command_Write(uint8_t Command,uint8_t channel,uint16_t Value)
{
uint8_t data;
//command modify
data=Command;
//WriteAndUpDateCh
// command modify
data=(Value>>12)|channel;
data=(Value>>4)&0x00ff;
data=(Value&0x000f)<<4;
Lpspi_Write(4,data);
}
/*
Initial DAC GPIO
*/
void DAC_Init(void)
{
//DAC_CLR_L;
/* OPEN INTERANL REFERENCE */
uint8_t data={0x08,0xf0,0x00,0x01};
Lpspi_Write(4,data);
}
void DAC_Reset(void)
{
// no significance data
uint8_t data={0x00,0xf0,0x00,0x01};
//command modify
data=ResetDac;
Lpspi_Write(4,data);
}
void SoftLdac(void)
{
uint8_t data={0x06,0x00,0x00,0xFF};
Lpspi_Write(4,data);
}</code></pre>
<p> </p>
<pre>
<code class="language-cpp">#ifndef __AD5668_H
#define __AD5668_H
#include "bsp_lpspi.h"
/*
AD5668 COMMAND AND ADDRESS
MSB
31 30 29 2827 26 25 24
X XXXC3 C2 C1 C0
23 22 21 2019 -> 5
A3 A2 A1 A0Data Msb-> DataLSB
4 3 2 1
X X X X
*/
/*
COMMAND
*/
#defineWriteToReg 0X00
#defineUpdateDacReg 0X01
#defineWriteAndUpDateAll 0X02
#defineWriteAndUpDateCh 0x03
#definePowerDownAndPowerUpDac 0x04
#defineloadClearReg 0x05
#defineloadLdacReg 0x06
#define ResetDac 0x07
#define SetUpRef 0x80
/*
ADDRESS
*/
#defineChannelA 0X00
#defineChannelB 0X10
#defineChannelC 0X20
#defineChannelD 0X30
#defineChannelE 0X40
#defineChannelF 0X50
#defineChannelG 0X60
#defineChannelH 0X70
#defineChannelAll 0XF0
/*
Ctrl GPIO redefine
*/
#defineDAC_CS1_L SF_NSS1_L
#defineDAC_CS1_H SF_NSS1_H
#defineDAC_CLR_PIN GPIO_PIN_13
#define DAC_CLR_H HAL_GPIO_WritePin(DAC_CLR_PORT,DAC_CLR_PIN,GPIO_PIN_SET)
#define DAC_CLR_L HAL_GPIO_WritePin(DAC_CLR_PORT,DAC_CLR_PIN,GPIO_PIN_RESET)
/*
Channel Selection Range 1~8
Value Range 0~65535
*/
void DAC_Init(void);
void DAC_Write(uint8_t channel,uint16_t Value);
void DAC_Command_Write(uint8_t Command,uint8_t channel,uint16_t Value);
void DAC_Reset(void);
void SoftLdac(void);
#endif
</code></pre>
<p> </p>
<p>再次更新驱动代码:</p>
<pre>
<code>#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);
}
</code></pre>
<pre>
<code>#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 */
</code></pre>
<p>可以直接驱动ADX112</p>
<pre>
<code>/**
******************************************************************************
* @File main.c
* @author fire
* @version V1.0
* @date 2018-xx-xx
* @brief GPIO输入—按键中断检测
******************************************************************************
* @attention *
* 实验平台:野火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;
/*******************************************************************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_group0 用于软件或者硬件出发
ADC_SetChannelConfig(ADC1, 0u /*ADC_CHANNEL_GROUP*/, &adcChannelConfigStruct);
}
// ADC 中断入口函数
voidADC1_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**********************/
</code></pre>
<p>ADX112 测温结果输出</p>
<p> </p>
<p> </p>
<p> </p>
<pre>
<code class="language-cpp">/**
******************************************************************************
* @File main.c
* @author fire
* @version V1.0
* @date 2018-xx-xx
* @brief GPIO输入—按键中断检测
******************************************************************************
* @attention *
* 实验平台:野火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"
// 添加adc的返回值用于打印
volatile uint16_t ADC_Value;
/*******************************************************************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_group0 用于软件或者硬件出发
ADC_SetChannelConfig(ADC1, 0u /*ADC_CHANNEL_GROUP*/, &adcChannelConfigStruct);
}
// ADC 中断入口函数
voidADC1_IRQHandler(void)
{
ADC_Value= ADC_GetChannelConversionValue(ADC1, 0U);
rt_kprintf("ADC_Value :%d\r\n",ADC_Value);
}
/**
* @brief主函数
* @param无
* @retval 无
*/
int main(void)
{
rt_kprintf("hello analogysemi \r\n");
User_GPIO_Init();
LPSPI_Init();
// RT1052_ADC_Init();
// //读取通道0
// adc_channel_config_t adcChannelConfigStruct;
// adcChannelConfigStruct.channelNumber = 0;
// adcChannelConfigStruct.enableInterruptOnConversionCompleted = true;
DAC_Init();
rt_thread_mdelay(1);
while(1)
{
// RGB_RED_LED_ON;
//
DAC_Write(ChannelA,0x55aa);
rt_thread_mdelay(10);
DAC_Write(ChannelA,0xaa55);
rt_thread_mdelay(10);
// ADC_SetChannelConfig(ADC1, 0u /*ADC_CHANNEL_GROUP*/, &adcChannelConfigStruct);
// rt_thread_mdelay(500);
}
}
/*********************************************END OF FILE**********************/
</code></pre>
<p>主程序</p>
<p>这个波形看起来不太干净呢?</p>
freebsder 发表于 2023-4-21 23:19
这个波形看起来不太干净呢?
<p>线飞来飞去,接地没夹好</p>
xutong 发表于 2023-4-22 09:33
线飞来飞去,接地没夹好
<p>哈哈,好吧</p>
freebsder 发表于 2023-4-24 09:24
哈哈,好吧
<p><img height="48" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/congra.gif" width="48" />快速验下,快速搞下个东西</p>
<pre>
<code class="language-cpp">/*
analogysemi
xutong 2023/10/19
*/
#ifndef __BSP_LPSPI_H
#define __BSP_LPSPI_H
#include "stm32h7xx_hal.h"
#define SPIx SPI1
#define SPIx_CLK_ENABLE() __HAL_RCC_SPI1_CLK_ENABLE()
#define SPIx_SCK_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#define SPIx_MISO_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#define SPIx_MOSI_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#define SPIx_CS_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
/* Definition for SPIx Pins */
#define SPIx_SCK_PIN GPIO_PIN_3
#define SPIx_SCK_GPIO_PORT GPIOB
#define SPIx_SCK_AF GPIO_AF5_SPI1
#define SPIx_MISO_PIN GPIO_PIN_4
#define SPIx_MISO_GPIO_PORT GPIOB
#define SPIx_MISO_AF GPIO_AF5_SPI1
#define SPIx_MOSI_PIN GPIO_PIN_5
#define SPIx_MOSI_GPIO_PORT GPIOB
#define SPIx_MOSI_AF GPIO_AF5_SPI1
#define SPIx_CS_PIN GPIO_PIN_6
#define SPIx_CS_GPIO_PORT GPIOB
void LPSPI_Init(void);
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 */</code></pre>
<pre>
<code class="language-cpp">#include "bsp_lpspi.h"
SPI_HandleTypeDef SpiHandle;
void Spi_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空指令 */
}
}
void LPSPI_Init(void)
{
GPIO_InitTypeDefGPIO_InitStruct;
/* Enable SPI1 clock */
SPIx_CLK_ENABLE();
SPIx_SCK_GPIO_CLK_ENABLE();
SPIx_MISO_GPIO_CLK_ENABLE();
SPIx_MOSI_GPIO_CLK_ENABLE();
SPIx_CS_GPIO_CLK_ENABLE();
/* 初始化GPIO 配置*/
/*##-1- Configure peripheral GPIO ##########################################*/
/* SPI SCK GPIO pin configuration*/
GPIO_InitStruct.Pin = SPIx_SCK_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = SPIx_SCK_AF;
HAL_GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStruct);
/* SPI MISO GPIO pin configuration*/
GPIO_InitStruct.Pin = SPIx_MISO_PIN;
GPIO_InitStruct.Alternate = SPIx_MISO_AF;
HAL_GPIO_Init(SPIx_MISO_GPIO_PORT, &GPIO_InitStruct);
/* SPI MOSI GPIO pin configuration*/
GPIO_InitStruct.Pin = SPIx_MOSI_PIN;
GPIO_InitStruct.Alternate = SPIx_MOSI_AF;
HAL_GPIO_Init(SPIx_MOSI_GPIO_PORT, &GPIO_InitStruct);
/* SPI CS GPIO pin configuration*/
GPIO_InitStruct.Pin = SPIx_CS_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(SPIx_CS_GPIO_PORT, &GPIO_InitStruct);
/* 初始化SPI 配置*/
/*##-2- Configure the SPI peripheral #######################################*/
/* Set the SPI parameters */
SpiHandle.Instance = SPIx;
SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
SpiHandle.Init.Direction = SPI_DIRECTION_2LINES;
SpiHandle.Init.CLKPhase = SPI_PHASE_1EDGE;
SpiHandle.Init.CLKPolarity = SPI_POLARITY_HIGH;
SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT;
SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
SpiHandle.Init.TIMode = SPI_TIMODE_DISABLE;
SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
SpiHandle.Init.CRCPolynomial = 7;
SpiHandle.Init.NSS = SPI_NSS_SOFT;
SpiHandle.Init.Mode = SPI_MODE_MASTER;
//初始化SPI
HAL_SPI_Init(&SpiHandle);
//默认拉高CS引脚
HAL_GPIO_WritePin(SPIx_CS_GPIO_PORT,SPIx_CS_PIN,GPIO_PIN_SET);
}
void Lpspi_Write(uint8_t datasize,uint8_t *data)
{
//写数据
HAL_GPIO_WritePin(SPIx_CS_GPIO_PORT,SPIx_CS_PIN,GPIO_PIN_RESET);
HAL_SPI_Transmit(&SpiHandle,data, datasize, 5000);
Spi_delay(30);
HAL_GPIO_WritePin(SPIx_CS_GPIO_PORT,SPIx_CS_PIN,GPIO_PIN_SET);
}
void Lpspi_Read(uint8_t datasize,uint8_t *data,uint8_t *Rxdata)
{
//读写数据
HAL_GPIO_WritePin(SPIx_CS_GPIO_PORT,SPIx_CS_PIN,GPIO_PIN_RESET);
HAL_SPI_TransmitReceive(&SpiHandle,data,Rxdata, datasize, 5000);
Spi_delay(30);
HAL_GPIO_WritePin(SPIx_CS_GPIO_PORT,SPIx_CS_PIN,GPIO_PIN_SET);
}
</code></pre>
<p> </p>
<p>转换为STM32能用的头文件</p>
页:
[1]