找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1657|回复: 1
收起左侧

热电偶 配置 STM32F103

[复制链接]
ID:730478 发表于 2020-4-16 13:38 | 显示全部楼层 |阅读模式
/******************************************

spi通讯模式的热电偶,采集温度通过OLED显示

******************************************/
#include "temper.h"
#include "delay.h"

#define         MAX6675_CS                         GPIO_Pin_12
#define         MAX6675_CSL()                GPIOB->BRR = MAX6675_CS;
#define         MAX6675_CSH()                GPIOB->BSRR = MAX6675_CS;
//                                                                                                                                                                  
//void SPI_MAX6675_Init(void)
//{
//        GPIO_InitTypeDef GPIO_InitStructure;
//        SPI_InitTypeDef  SPI_InitStructure;       
//       
//        /* 使能 SPI1 时钟 */                        
//        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB1Periph_SPI2, ENABLE);
//        RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
//        /* ---------通信I/O初始化----------------
//         * PA5-SPI1-SCK :MAX6675_SCK
//         * PA6-SPI1-MISO:MAX6675_SO
//         * PA7-SPI1-MOSI:MAX6675_SI         
//         */
//        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_13 | GPIO_Pin_14;
//        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
//        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;                        // 复用输出
//        GPIO_Init(GPIOB, &GPIO_InitStructure);

//        /* ---------控制I/O初始化----------------*/
//        /* PA4-SPI1-NSS:MAX6675_CS */                                                        // 片选
//        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
//        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
//        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;                // 推免输出
//        GPIO_Init(GPIOB, &GPIO_InitStructure);                                                  
//        GPIO_SetBits(GPIOB, GPIO_Pin_12);                                                // 先把片选拉高,真正用的时候再拉低
//

//        /* SPI1 配置 */
//        SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
//        SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
//        SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
//        SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
//        SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
//        SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
//        SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
//        SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
//        SPI_InitStructure.SPI_CRCPolynomial = 7;
//        SPI_Init(SPI2, &SPI_InitStructure);
//       
//       
//        /* 使能 SPI1  */
//        SPI_Cmd(SPI2, ENABLE);
//}


unsigned char MAX6675_ReadByte(void)
{
       
        /* Loop while DR register in not emplty */
        while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE) == RESET);
       
        /* Send byte through the SPI1 peripheral */
        SPI_I2S_SendData(SPI2, 0xff);
       
        /* Wait to receive a byte */
        while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
       
        /* Return the byte read from the SPI bus */
        return SPI_I2S_ReceiveData(SPI2);
}

unsigned int t,i;
unsigned char c;
unsigned char flag;
float temper=5;
float temper_point=0;
void temper_get(void)
{
    MAX6675_CSL();
                c = MAX6675_ReadByte();
                i = c;
                i = i<<8;
                c = MAX6675_ReadByte();
                MAX6675_CSH();
               
                i = i|((unsigned int)c);                        //i是读出来的原始数据
                flag = i&0x04;                                                    //flag保存了热电偶的连接状态
                t = i<<1;
                t = t>>4;
                temper = t*0.25;
          temper_point=(int)(temper*100)%100;
          delay_ms(200);
}


回复

使用道具 举报

ID:1 发表于 2020-4-19 15:13 | 显示全部楼层
本帖需要重新编辑补全电路原理图,源码,详细说明与图片即可获得100+黑币(帖子下方有编辑按钮)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表