#include "stm32f10x.h" #include "stm32_eval.h" #include <stdio.h> volatile int flag; //板子已将PC12脚接到18B20 #define Set_B20() GPIO_SetBits(GPIOC, GPIO_Pin_12) //关上PC12 #define Reset_B20() GPIO_ResetBits(GPIOC, GPIO_Pin_12) //打开PC12 #define Read_B20() GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_12) //读PC12 unsigned char Error_Flag=0; unsigned char zf=0; /*延时函数 微秒*/ void delay_us(u32 n) { u8 j; while(n--) for(j=0;j<10;j++); } /*延时函数 毫秒*/ void delay_ms(u32 n) { while(n--) delay_us(1000); } void SysTick_Configuration(void) { /*==========此次作业的部分===========*/ if (SysTick_Config(48000)) //SysTick配置。48000/48ms=1ms /*===================================*/ { while (1); } NVIC_SetPriority(SysTick_IRQn, 0x0); //SysTick中断优先级 } GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; USART_ClockInitTypeDef USART_ClockInitStructure; void RCC_Configuration(void) { RCC_DeInit(); RCC_HSICmd(ENABLE); while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET); RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI); RCC_HSEConfig(RCC_HSE_OFF); RCC_LSEConfig(RCC_LSE_OFF); /*==========此次作业的部分===========*/ RCC_PLLConfig(RCC_PLLSource_HSI_Div2,RCC_PLLMul_6); // PLLMul_X就是X的倍数,即8*6=48HMz /*===================================*/ RCC_PLLCmd(ENABLE); while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); RCC_ADCCLKConfig(RCC_PCLK2_Div4); RCC_PCLK2Config(RCC_HCLK_Div1); RCC_PCLK1Config(RCC_HCLK_Div2); RCC_HCLKConfig(RCC_SYSCLK_Div1); RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); while(RCC_GetSYSCLKSource() != 0x08); // SystemInit(); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_ResetBits(GPIOD,GPIO_Pin_2); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_SetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); } void USART_int(long BaudRate) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); /* PA10 USART1_Rx */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = BaudRate;//?????? USART_InitStructure.USART_WordLength = USART_WordLength_8b;//???????8bit USART_InitStructure.USART_StopBits = USART_StopBits_1;//????1 USART_InitStructure.USART_Parity = USART_Parity_No;//???? USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//??????none USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//?????????? USART_ClockInitStructure.USART_Clock = USART_Clock_Disable; USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable; USART_ClockInit(USART1, &USART_ClockInitStructure); USART_Init(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); USART_Cmd(USART1, ENABLE); } void delay_18b20(u32 nus) //18B20时序要求严格精确,为18B20写的延时函数 { u16 i; //这个函数写的貌似是1us while(nus--) //nus每减一次,i减12次 for(i=12;i>0;i--); } void Init18B20(void) { u8 aa=0; u8 count =0; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //打开PC时钟 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //选中管脚12 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; //开漏输出 GPIO_Init(GPIOC, &GPIO_InitStructure); Set_B20() ; //拉高 delay_18b20(1); //一、复位:至少480us的低电平信号,当18b20接收到此信号后会回发芯片一个存在脉冲 Reset_B20(); //拉低 delay_18b20(480); //二、存在脉冲:将数据单总线拉高,接收存在脉冲,通信双方达成基本协议,双方开始数据通信 Set_B20(); //拉高 delay_18b20(480); count=0; aa=Read_B20(); //aa为读得的温度、 /*=====我觉得下面这几句用来统计18B20出现错误的次数,次数到99次的时候报错======*/ while(!aa && count<100) //如果温度值和count数都大于100时 { aa=Read_B20(); //再次读温度值 count++; //发生错误了,数值+1 } if(count>=99) //如果发生错误的次数大于99次,那么报错,标志位为1 Error_Flag=1; else Error_Flag=0; //报错次数未达到99次,正常工作 } //读一个字节 unsigned char Read18B20(void)//读18b20里的数据,由于是单数据总线,需要一位一位地读 { unsigned char i=0; unsigned char date=0; u8 tempp; for(i=8;i>0;i--)//循环8次 { Reset_B20(); //拉低读数 date>>=1; //右移一位 delay_18b20(1); Set_B20(); //拉高 delay_18b20(1); tempp=Read_B20(); // tempp为读得的温度 if(tempp) date|=0x80; delay_18b20(60); } return(date); } void Write18B20(unsigned char date) //写一个字节 { unsigned char i=0; for (i=8; i>0; i--) { Reset_B20(); delay_18b20(1); if(date & 0x01) { Set_B20(); } else { Reset_B20();} delay_18b20(60); date>>=1; Set_B20(); delay_18b20(1); } delay_18b20(15); } float Read_T()// { unsigned char TUp,TDown; unsigned char fTemp; u8 TT=0; float Temp = 0; Init18B20(); Write18B20(0xcc); // 跳过读取序列号 Write18B20(0x44); // Init18B20(); Write18B20(0xcc); // Write18B20(0xbe); // TDown = Read18B20(); //低8位 TUp = Read18B20(); //高8位 if(TUp>0x7f) { TDown=~TDown; TUp=~TUp+1; TUp/=8; zf=1; } else zf=0; fTemp=TDown&0x0f; TUp<<=4; TDown>>=4; TT=TUp|TDown; Temp=TT+(float)fTemp/16; return(Temp); } int main(void) { int i; unsigned char ID1[8]; //定义一个用于18b20id读取和存放的数组 RCC_Configuration(); USART_int(115200); SysTick_Configuration(); /*==========此次作业的部分===========*/ Init18B20 (); //初始化DS18B20 Write18B20(0x33); //写入“读取序列号”命令 delay_18b20 (15); for(i=0;i<8;i++) { ID1[i]=Read18B20(); //读取18b20发回的64位地址 } /*===================================*/ printf(" config done...\r\n"); delay_ms(1000); while(1) { /*==========此次作业的部分===========*/ if(flag == 300){ //300us读一次id printf("The id is:"); for(i=0;i<8;i++) { printf("%u",ID1[i]); if(i==7){printf("\r\n"); } } } /*===================================*/ if(flag == 500){ //500us读一次温度 printf("The Temperature is:%f\r\n",Read_T()); } } } #ifdef USE_FULL_ASSERT void assert_failed(uint8_t* file, uint32_t line) { while (1) { } } #endif #ifdef __GNUC__ #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) #else #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) #endif /* __GNUC__ */ PUTCHAR_PROTOTYPE { USART_SendData(EVAL_COM1, (uint8_t) ch); /* Loop until the end of transmission */ while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET) {} return ch; } #ifdef USE_FULL_ASSERT void assert_failed(uint8_t* file, uint32_t line) { while (1) { } } #endif