|
功能如標(biāo)題已經(jīng)測(cè)試通過
單片機(jī)源程序如下:- /*********************************************************************************
- * 文件名 :main.c
- * 描述 :通過stm32的spi1讀取max6675的溫度值,并通過uart1發(fā)送出來
- *
- * 實(shí)驗(yàn)平臺(tái):STM32開發(fā)板
- * 庫版本 :ST3.0.0
- * 硬件連接: ------------------------------------
- * |PA6-SPI1-MISO:MAX6675-SO |
- * |PA7-SPI1-MOSI:MAX6675-SI |
- * |PA5-SPI1-SCK :MAX6675-SCK |
- * |PA4-SPI1-NSS :MAX6675-CS |
- * ------------------------------------
- **********************************************************************************/
- #include "stm32f10x.h"
- #include "usart1.h"
- #include "led.h"
- #include "oled.h"
- #include "hc05.h"
- #include "usart3.h"
- #include "string.h"
- #define MAX6675_CS GPIO_Pin_4
- #define MAX6675_CSL() GPIOA->BRR = MAX6675_CS;
- #define MAX6675_CSH() GPIOA->BSRR = MAX6675_CS;
- /*
- * 函數(shù)名:SPI1_Init
- * 描述 MAX6675 接口初始化
- * 輸入 :無
- * 輸出 :無
- * 返回 :無
- */
- void SPI_MAX6675_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- SPI_InitTypeDef SPI_InitStructure;
-
- /* 使能 SPI1 時(shí)鐘 */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE);
- /* ---------通信I/O初始化----------------
- * PA5-SPI1-SCK :MAX6675_SCK
- * PA6-SPI1-MISO:MAX6675_SO
- * PA7-SPI1-MOSI:MAX6675_SI
- */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 復(fù)用輸出
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /* ---------控制I/O初始化----------------*/
- /* PA4-SPI1-NSS:MAX6675_CS */ // 片選
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // 推免輸出
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_SetBits(GPIOA, GPIO_Pin_4); // 先把片選拉高,真正用的時(shí)候再拉低
-
- /* 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(SPI1, &SPI_InitStructure);
-
-
- /* 使能 SPI1 */
- SPI_Cmd(SPI1, ENABLE);
- }
- /*
- *
- *
- *
- */
- 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);
- }
- void HC05_Role_Show(void)
- {
- if(HC05_Get_Role()==1)printf("ROLE:Master"); //主機(jī)
- else printf("ROLE:Slave "); //從機(jī)
- }
- //顯示ATK-HC05模塊的連接狀態(tài)
- void HC05_Sta_Show(void)
- {
- if(HC05_LED)printf("STA:Connected "); //連接成功
- else printf("STA:Disconnect"); //未連接
- }
- /*
- * 函數(shù)名:main
- * 描述 :主函數(shù)
- * 輸入 :無
- * 輸出 :無
- */
- main (void)
- {
- unsigned int t,i;
- unsigned char c;
- unsigned char flag;
- float temprature;
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
-
- /* 配置系統(tǒng)時(shí)鐘為72M */
- SystemInit();
- LED_Init();
- /* MAX6675 SPI 接口初始化 */
- SPI_MAX6675_Init();
- USART1_Config();
- OLED_IIC_Init();
- LED0=LED1=0 ;
-
- //OLED_ShowNum(0,1,6666666,8,16);
- OLED_P8x16Str(0,0,"temp:");
-
- HC05_Init();
- // while(HC05_Init()) //初始化ATK-HC05模塊
- // {
- //OLED_P8x16Str(2,2,"xxxx");
- // }
- OLED_P8x16Str(3,3,"222");
- HC05_Role_Show();
- USART3_RX_STA=0;
- while(1)
- {
-
- MAX6675_CSL();
- c = MAX6675_ReadByte();
- i = c;
- i = i<<8;
- c = MAX6675_ReadByte();
- MAX6675_CSH();
-
- i = i|((unsigned int)c); //i是讀出來的原始數(shù)據(jù)
- flag = i&0x04; //flag保存了熱電偶的連接狀態(tài)
- t = i<<1;
- t = t>>4;
- temprature = t*0.25;
-
- if(i!=0) //max6675有數(shù)據(jù)返回
- {
- if(flag==0) //熱電偶已連接
- {
- printf("原始數(shù)據(jù)是:%04X, 當(dāng)前溫度是:%4.2f。\r\n",i,temprature);
- OLED_ShowNum(17,1,temprature,4,16);
- // if(temprature< 25) //溫度小于25°加熱,
- // {
- // LED0=1;// 加熱
- // LED1=0; // 關(guān)閉風(fēng)扇
- // }
- // else if (temprature >30) //溫度高于30都 散熱
- // {
- // LED1=1; //打開風(fēng)扇
- // LED0=0; //關(guān)閉加熱
- // }
- }
- else //熱電偶掉線
- {
- printf("未檢測(cè)到熱電偶,請(qǐng)檢查。\r\n");
- }
-
- }
- else //max6675沒有數(shù)據(jù)返回
- {
- printf("max6675沒有數(shù)據(jù)返回,請(qǐng)檢查max6675連接。\r\n");
- }
- for(i=0;i<0x2fffff;i++); //max6675的轉(zhuǎn)換時(shí)間是0.2秒左右,所以兩次轉(zhuǎn)換間隔不要太近
- }
- }
復(fù)制代碼
原理圖:無
仿真:無
程序:
程序.7z
(182.89 KB, 下載次數(shù): 47)
2023-8-11 14:26 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|