初级会员

- 积分
- 176
- 金钱
- 176
- 注册时间
- 2016-8-16
- 在线时间
- 77 小时
|

楼主 |
发表于 2016-8-17 12:10:28
|
显示全部楼层
这是主程序,有谁可以帮忙看一下咯,谢谢了~
#include "stm32f4xx.h"
#include "usart.h"
#define K_CS PAout(4)
void SPI_MAX6675_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//使能GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);//使能SPI1时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;//复用功能输出
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_SPI1); //SCK
GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_SPI1); //MISO
GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_SPI1); //MOSI
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //CS
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //普通输出
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化
GPIO_SetBits(GPIOA, GPIO_Pin_4);
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(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE); //使能SPI外设
}
unsigned char MAX6675_ReadByte(void)
{
/* Loop while DR register in not emplty */
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE) == RESET);
/* Send byte through the SPI1 peripheral */
SPI_I2S_SendData(SPI1, 0xff);
/* Wait to receive a byte */
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
/* Return the byte read from the SPI bus */
return SPI_I2S_ReceiveData(SPI1);
}
int main(void)
{
unsigned int t,i;
unsigned char c;
unsigned char flag;
float temprature;
SystemInit();
SPI_MAX6675_Init();
uart_init(115200);
while(1)
{
K_CS=0;
c = MAX6675_ReadByte();
i = c;
i = i<<8;
c = MAX6675_ReadByte();
K_CS=1;
i = i|((unsigned int)c);
flag = i&0x04;
t = i<<1;
t = t>>4;
temprature = t*0.25;
if(i!=0)
{
if(flag==0)
{
printf("原始数据是:%04X, 当前温度:%4.2f。\r\n",i,temprature);
}
else
{
printf("未检测到热电偶\r\n");
}
}
else
{
printf("max6675没有数据返回,请检测max6675.\r\n");
}
for(i=0;i<0x2fffff;i++);
}
}
|
|