/******************************************
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);
}
|