使用背景
首先感谢华大半导体产商的辛苦努力 ,发布了一款高性价比的MCU-HC32F460 系列,它是基于 ARM Cortex-M4 32-bit RISC CPU,最高工作频率200MHz 的高性能 MCU。Cortex-M4 内核集成了浮点运算单元FPU和 DSP,实现单精度浮点算术运算,支持所有 ARM 单精度数据处理指令和数据类型,支持完整 DSP 指令集。
使用用途
场景
测量高温加热模块的温度
热电偶其特点主要如下:
1、装配简单,更换方便;
2、压簧式感温元件,抗震性能好;
3、测量精度高;
4、测量范围大(-200℃~1300℃,特殊情况下-270℃~2800℃),目前我使用的最高只能到600°;
5、热响应时间快;
具体代码实现
#include "hc32f46x.h"
#include "bsp_spi.h"
#include "hc32f46x_spi.h"
#include "hc32_ddl.h"
/* SPI_SCK Port/Pin definition */
#define SPI_SCK_PORT (PortB)
#define SPI_SCK_PIN (Pin13)
#define SPI_SCK_FUNC (Func_Spi3_Sck)/* SPI_NSS Port/Pin definition */
#define SPI_NSS_PORT (PortB)
#define SPI_NSS_PIN (Pin12)
#define SPI_NSS_HIGH() (PORT_SetBits(SPI_NSS_PORT, SPI_NSS_PIN))
#define SPI_NSS_LOW() (PORT_ResetBits(SPI_NSS_PORT, SPI_NSS_PIN))/* SPI_MOSI Port/Pin definition */
#define SPI_MOSI_PORT (PortE)
#define SPI_MOSI_PIN (Pin15)
#define SPI_MOSI_FUNC (Func_Spi3_Mosi)/* SPI_MISO Port/Pin definition */
#define SPI_MISO_PORT (PortB)
#define SPI_MISO_PIN (Pin14)
#define SPI_MISO_FUNC (Func_Spi3_Miso)#define SPI_UNIT (M4_SPI3)
#define SPI_UNIT_CLOCK (PWC_FCG1_PERIPH_SPI3)static void Spi_Config(void)
{stc_spi_init_t stcSpiInit;/* configuration structure initialization */MEM_ZERO_STRUCT(stcSpiInit);/* Configuration peripheral clock */PWC_Fcg1PeriphClockCmd(SPI_UNIT_CLOCK, Enable);/* Configuration SPI pin */PORT_SetFunc(SPI_SCK_PORT, SPI_SCK_PIN, SPI_SCK_FUNC, Disable);PORT_SetFunc(SPI_MOSI_PORT, SPI_MOSI_PIN, SPI_MOSI_FUNC, Disable);PORT_SetFunc(SPI_MISO_PORT, SPI_MISO_PIN, SPI_MISO_FUNC, Disable);/* Configuration SPI structure */stcSpiInit.enClkDiv = SpiClkDiv256;stcSpiInit.enFrameNumber = SpiFrameNumber1;stcSpiInit.enDataLength = SpiDataLengthBit8;stcSpiInit.enFirstBitPosition = SpiFirstBitPositionMSB;stcSpiInit.enSckPolarity = SpiSckIdleLevelLow;stcSpiInit.enSckPhase = SpiSckOddSampleEvenChange;stcSpiInit.enReadBufferObject = SpiReadReceiverBuffer;stcSpiInit.enWorkMode = SpiWorkMode4Line;stcSpiInit.enTransMode = SpiTransFullDuplex;stcSpiInit.enCommAutoSuspendEn = Disable;stcSpiInit.enModeFaultErrorDetectEn = Disable;stcSpiInit.enParitySelfDetectEn = Disable;stcSpiInit.enParityEn = Disable;stcSpiInit.enParity = SpiParityEven;stcSpiInit.enMasterSlaveMode = SpiModeMaster;stcSpiInit.stcDelayConfig.enSsSetupDelayOption = SpiSsSetupDelayCustomValue;stcSpiInit.stcDelayConfig.enSsSetupDelayTime = SpiSsSetupDelaySck1;stcSpiInit.stcDelayConfig.enSsHoldDelayOption = SpiSsHoldDelayCustomValue;stcSpiInit.stcDelayConfig.enSsHoldDelayTime = SpiSsHoldDelaySck1;stcSpiInit.stcDelayConfig.enSsIntervalTimeOption = SpiSsIntervalCustomValue;stcSpiInit.stcDelayConfig.enSsIntervalTime = SpiSsIntervalSck6PlusPck2;SPI_Init(SPI_UNIT, &stcSpiInit);SPI_Cmd(SPI_UNIT, Enable);
}/*
* Init hoten0 Configure
*/
void spi_init(void)
{stc_port_init_t stcPortInit;/* configure structure initialization */MEM_ZERO_STRUCT(stcPortInit);/* Flash NSS */stcPortInit.enPinMode = Pin_Mode_Out;PORT_Init(SPI_NSS_PORT, SPI_NSS_PIN, &stcPortInit);SPI_NSS_HIGH();Spi_Config();
}/*********************************************************************************** \brief SPI flash write byte function**** \param [in] u8Data SPI write data to flash**** \retval uint8_t SPI receive data from flash********************************************************************************/
static uint8_t SpiFlash_WriteReadByte(uint8_t u8Data)
{uint8_t u8Byte;/* Wait tx buffer empty */while (Reset == SPI_GetFlag(SPI_UNIT, SpiFlagSendBufferEmpty)){}/* Send data */SPI_SendData8(SPI_UNIT, u8Data);/* Wait rx buffer full */while (Reset == SPI_GetFlag(SPI_UNIT, SpiFlagReceiveBufferFull)){}/* Receive data */u8Byte = SPI_ReceiveData8(SPI_UNIT);return u8Byte;
}/*
* Read hoten0 temperature value
*/
float SpiTemp_Read(void)
{int data = 0xff;uint16_t adValue = 0;float temperature = 0.0;SPI_NSS_LOW();adValue = SpiFlash_WriteReadByte(data);while (Reset == SPI_GetFlag(SPI_UNIT, SpiFlagSpiIdle)){}adValue = adValue<<8 | SpiFlash_WriteReadByte(data); if (adValue & 0X04) {adValue = 0; //未检测到热电偶}else {adValue<<=1; //去掉D15位adValue>>=4; //去掉D0、1、2位}temperature = adValue*1023.75/4095; SPI_NSS_HIGH();return temperature;
}