这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 高校专区 » 漓东e学堂 » 【11.14更新】31号STM32初学习

共7条 1/1 1 跳转至

【11.14更新】31号STM32初学习

菜鸟
2015-10-23 10:50:03     打赏

作业一:Keil的安装:http://forum.eepw.com.cn/thread/277213/1#4

作业二:流水灯:http://forum.eepw.com.cn/thread/277213/1#2

作业三:0~99按键计数:http://forum.eepw.com.cn/thread/277213/1#3

作业四:单号uart控制LED流水灯转速:http://forum.eepw.com.cn/thread/277213/1#5

作业五:定时器控制led跑马灯:http://forum.eepw.com.cn/thread/277213/1#6






菜鸟
2015-11-09 13:09:37     打赏
2楼

作业二:流水灯

/**
  ******************************************************************************
  * @file    GPIO/IOToggle/main.c 
  * @author  MCD Application Team
  * @version V3.5.0
  * @date    08-April-2011
  * @brief   Main program body.
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "stm32_eval.h"

GPIO_InitTypeDef GPIO_InitStructure;

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_9); //  72HMz
  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);
 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);
}

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

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{  
 int i;      
 unsigned int a[8]={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_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
 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);

while(1)
 {
  for(i=0;i<=7;i++)  
  {   
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);     
   delay_ms(50);        
GPIO_ResetBits(GPIOC,a[i]);      
delay_ms(50);   
GPIO_SetBits(GPIOC,a[i]);        
delay_ms(50);   
  }      
 
}
}


菜鸟
2015-11-14 23:23:36     打赏
3楼

作业三:0~99按键计数



#include "stm32f10x.h"	

GPIO_InitTypeDef GPIO_InitStructure; // 声明一个结构体

void RCC_Configuration(void); 
void GPIO_INIT(void) ;
void Function(void) ;

int main(void)   
{    
    RCC_Configuration();   
    GPIO_INIT();
    Function();   
}    
      
void RCC_Configuration(void)	//复位所有的RCC外围设备寄存器
{   
		ErrorStatus HSEStartUpStatus;	//	设置错误标志量
	RCC_DeInit(); 						//*将外设寄存器RCC重设为初始值 
	RCC_HSEConfig(RCC_HSE_ON);//*打开外部高速时钟晶振HSE:
  HSEStartUpStatus = RCC_WaitForHSEStartUp();  //*等待外部高速时钟晶振工作: 
  if(HSEStartUpStatus==SUCCESS)
	{
		RCC_HCLKConfig(RCC_SYSCLK_Div1);//*AHB使用系统时钟,负责外部存储器的时钟 
		RCC_PCLK1Config(RCC_HCLK_Div2);//*APB1 负责DA,USB,SPI,I2C,CAN,串口2345,普通TIM
		RCC_PCLK2Config(RCC_HCLK_Div1);//*APB2负责AD,I/O,高级TIM,串口1 
		RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);//*设置PLL输入时钟源72M
		RCC_PLLCmd(ENABLE);//*打开PLL:          
		while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);//*等待PLL工作:
		RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//*设置系统时钟
		while(RCC_GetSYSCLKSource() != 0x08);//* 判断PLL是否是系统时钟:    
	}
	
	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//改变管脚的映射SWJ使能,禁用JTAG,选择用于事件输出的GPIO端口
	
	/*蜂鸣器*/
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE); //# 使能被重新映射到GPIOD时钟和使能复用时钟功能
	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);
} 

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 GPIO_INIT(void)   
{     
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOB, ENABLE);	
	
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 	//输出速度   
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;	
		GPIO_Init(GPIOC, &GPIO_InitStructure);	//初始化   
	 
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_1|GPIO_Pin_15|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14;//数码管
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;     
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
		GPIO_Init(GPIOB, &GPIO_InitStructure);	
}   
  

void Number(int a)  //数码管显示数字
{
    switch(a)  
			{  
        case 0 : GPIO_ResetBits(GPIOB,GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);break;  
        case 1 : GPIO_ResetBits(GPIOB,GPIO_Pin_9|GPIO_Pin_12);break;  
        case 2 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_13|GPIO_Pin_14);break;  
        case 3 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_14);break;  
        case 4 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12);break;  
        case 5 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_12|GPIO_Pin_14);break;  
        case 6 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);break;  
        case 7 : GPIO_ResetBits(GPIOB,GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_12);break;  
        case 8 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);break;  
        case 9 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_14); break;  
			}  
}  
  
   
void Function(void) 
{     
		int i=0,j=0;          //i被按下的次数,n延时变量  

		GPIO_SetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);     
		delay_ms(50); //关闭数码管    

		while(1)  
		{                 
				GPIO_SetBits(GPIOB,GPIO_Pin_1);         //打开个位数码管  
				Number(i);                                //用switch函数显示00  
				delay_ms(t);                            
				GPIO_ResetBits(GPIOB,GPIO_Pin_1);        //关闭个位       
				GPIO_SetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);  

				GPIO_SetBits(GPIOB,GPIO_Pin_15);           //打开十位  
				Number(j);    
				delay_ms(t);  
				GPIO_ResetBits(GPIOB,GPIO_Pin_15);      //关掉十位数码管     
				GPIO_SetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);  

				if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8))   //判断按键是s1否被按下  
				{     
					delay_ms(50);            //延时消抖  
					if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8))  //再次判断按键是s1否被按下    
					{   
						i++;                     //按键被按下,个位+1  
						if(i==10)     
						{  
							j++;               
							i=0;                 
						}
						if(j==10)
						{
							 j=0;
                                                 i=0;
						}
					}
				}   








 





菜鸟
2015-11-15 21:39:10     打赏
4楼

作业一:keil安装

猛戳这个程序:


然后出现这个玩意:


接下来的就不要理会那么多了,猛戳Next就对了,出现这个窗口:


剩下的就是等,等出现这玩意:


点击Finish,接下来就要用这个神器和谐Keil:


最后就Duang!!!能用到2020年了!



菜鸟
2015-11-15 21:43:26     打赏
5楼

作业四:单号uart控制LED流水灯转速

#include "stm32f10x.h"   		
#include    "stdio.h"

GPIO_InitTypeDef GPIO_InitStructure;		// 声明结构体
NVIC_InitTypeDef  NVIC_InitStructure;  
USART_InitTypeDef  USART_InitStructure;  
USART_ClockInitTypeDef  USART_ClockInitStructure;   
    
#define buff_size  16;      
char rx_buff[],rx_buff_count=0; 
 
void RCC_Configuration(void) ;
void GPIO_INIT(void)  ;
void USART_int(long BaudRate);
void USART_SendStr(char *str);
void  delay_ms(u32 n)  ;
void delay_us(u32 n); 



/****主函数 ****/
int main()    
{                             
    RCC_Configuration();  
    GPIO_INIT();  
    USART_int(9600);  
    GPIO_ResetBits(GPIOC,0xffff);//led全亮,提示程序开始  
    delay_ms(200);  
    GPIO_SetBits(GPIOC,0xffff);//led全灭  
    USART_SendStr("USART Led Speed\r\n");//   
    USART_SendStr("\n>");//   
    while(1);
}   
 

/****	配置RCC ****/
void RCC_Configuration(void)   
{     
    ErrorStatus HSEStartUpStatus;	//	设置错误标志量
	RCC_DeInit(); 						//*将外设寄存器RCC重设为初始值 
	RCC_HSEConfig(RCC_HSE_ON);//*打开外部高速时钟晶振HSE:
  HSEStartUpStatus = RCC_WaitForHSEStartUp();  //*等待外部高速时钟晶振工作: 
  if(HSEStartUpStatus==SUCCESS)
	{
		RCC_HCLKConfig(RCC_SYSCLK_Div1);//*AHB使用系统时钟,负责外部存储器的时钟 
		RCC_PCLK1Config(RCC_HCLK_Div2);//*APB1 负责DA,USB,SPI,I2C,CAN,串口2345,普通TIM
		RCC_PCLK2Config(RCC_HCLK_Div1);//*APB2负责AD,I/O,高级TIM,串口1 
		RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);//*设置PLL输入时钟源72M
		RCC_PLLCmd(ENABLE);//*打开PLL:          
		while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);//*等待PLL工作:
		RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//*设置系统时钟
		while(RCC_GetSYSCLKSource() != 0x08);//* 判断PLL是否是系统时钟:    
	}
	 GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
	
	/*蜂鸣器*/
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);        
    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);              
}   
  

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


/**** 初始化GPIO ****/
void GPIO_INIT(void)   
{    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);      
    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);    
}  


/**** USART传输的波特率 ****/ 
void USART_int(long BaudRate)   
{  
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);//使能PA和 usart1
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;               
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;  
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;          //复用推挽输出 PA9作为US1的TX端,打开复用,负责发送数据
		GPIO_Init(GPIOA, &GPIO_InitStructure);  
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;       					//RX位于PA10        
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;     //浮空输入,PA10作为US1的RX端,负责接收数据
 									//RX初始化PA10

		USART_InitStructure.USART_BaudRate = BaudRate;//波特率
		USART_InitStructure.USART_WordLength = USART_WordLength_8b;//USART_WordLength 在一个帧中传输8位数据(字节)
		USART_InitStructure.USART_StopBits = USART_StopBits_1;//停止位为一个字节
		USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验		
		USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无流控制 
		USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//收发模式
		USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;//时钟低电平活动,开启发送接受数据功能(usart 使能)     
		USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;      //SCLK输出时钟低电平
		USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;//时钟第二个边沿开始数据捕获      
		USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;//最后一位数据不从SCLK发出
		USART_ClockInit(USART1, &USART_ClockInitStructure);//初始化USART1时钟
		USART_Init(USART1, &USART_InitStructure);//初始化USART1
		USART_Cmd(USART1, ENABLE);//USART1使能
		USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//使能接收中断,在接受移位寄存器中有数据时产生
		USART_Cmd(USART1, ENABLE);

		//配置NVIC ,设置优先级分组,使用NVIC_Init对NVIC进行初始化
		NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);//选择中断分组4(先占优先级四位,从优先级0)
		NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //选择中断通道
		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢占优先级1-15
		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//响应优先级1-15
		NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能中断
		NVIC_Init(&NVIC_InitStructure); //初始化中断  
                    
}  




/**** usart发送数据 ****/
void USART_SendStr(char *str)   //USART发送数据   
{  
			while((*str)!='\0')             
        {
					USART_SendData(USART1,*str++);  //通过外设 USARTx 发送单个数据 
					while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);  //检查指定的 USART 标志位设置与否
        }  
}  
  

/**** 取字符串中的数字 ****/
unsigned int translate(char* pstr) 
{   
	  int s = 0;  
		while(*pstr != '\0')        //如果没扫到字符串的最后不停止
		{
				if(*pstr >= '0' && *pstr <= '9')  //如果字符串中存在0-9的数字
				{  
						s = s * 10 + *pstr - '0';   
				}  
				pstr++;
		}		
    return s; 
} 

/****  点亮led****/
void LED(char *S,char LEN)     //(字符串,字符串长度)
{ 
		  int m,i;
	    int a[8]={GPIO_Pin_0,GPIO_Pin_1,GPIO_Pin_2,GPIO_Pin_3,GPIO_Pin_4,GPIO_Pin_5,GPIO_Pin_6,GPIO_Pin_7};	
			m=translate(S);
    
			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);  
			delay_ms(100);			
			for(i=0;i<8;i++) 
			{
				GPIO_ResetBits(GPIOC,a[i]);    // 点亮LED
				delay_ms(m);
				GPIO_SetBits(GPIOC,a[i]);     //熄灭LED
				delay_ms(m);
			}
}


void input_ASK()
{  
    char j;  
    LED(rx_buff,rx_buff_count);
    rx_buff_count=0;  
    for (j=0;j<rx_buff_count;j++)  
    {
			rx_buff[j]='\0';
		}//判断读入信息是否结束,如果结束则准备下一次读入  
    USART_SendStr("\n>");  
} 


  /****接收中断****/
void USART1_IRQHandler(void)   
{   
		while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)  { }   //判断接收中断是否打开
    if(USART_ReceiveData(USART1)==0x0d)   
    {
			input_ASK();
		}  
    else  
    {  
			USART_SendData(USART1,USART_ReceiveData(USART1));  //将数据传回给电脑
			rx_buff[rx_buff_count]= USART_ReceiveData(USART1);  
      rx_buff_count++;  
    }  
		
		USART_ClearFlag(USART1, USART_FLAG_RXNE);  //清除中断接受完成标志位
  
}  

 



菜鸟
2016-01-18 14:42:46     打赏
6楼

作业五:定时器控制LED跑马灯

#include "stm32f10x.h"
#include "stm32_eval.h"
#include <stdio.h>
/** @addtogroup STM32F10x_StdPeriph_Examples
  * @{
  */

/** @addtogroup EXTI_Config
  * @{
  */ 

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
EXTI_InitTypeDef   EXTI_InitStructure;
GPIO_InitTypeDef   GPIO_InitStructure;
NVIC_InitTypeDef   NVIC_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
/* Private function prototypes -----------------------------------------------*/
void EXTIkeyS3_Config(void);
void EXTIkeyS4_Config(void);
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_9); //  72HMz
  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);
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_ResetBits(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);
}

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_10MHz;
    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);
  /* USARTx configured as follow:
        - BaudRate = 115200 baud  
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
  */
  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);
 
  /* Configure four bit for preemption priority */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
  /* Enable the USART1 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 15;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{RCC_Configuration();
  
  /* Configure PA.00 in interrupt mode */
  EXTIkeyS3_Config();

  /* Configure PB.09 or PG.08 in interrupt mode */
  EXTIkeyS4_Config();
USART_int(115200);
printf("Config done,waiting for interrupt......\r\n");
          
  while (1)
  {
  }
}

/**
  * @brief  Configure PA.00 in interrupt mode
  * @param  None
  * @retval None
  */
void EXTIkeyS3_Config(void)
{
  /* Enable GPIOA clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  
  /* Configure PA.00 pin as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Enable AFIO clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

  /* Connect EXTI0 Line to PA.00 pin */
  GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource10);

  /* Configure EXTI0 line */
  EXTI_InitStructure.EXTI_Line = EXTI_Line10;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;  
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);

  /* Enable and set EXTI0 Interrupt to the lowest priority */
  NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

/**
  * @brief  Configure PB.09 or PG.08 in interrupt mode
  * @param  None
  * @retval None
  */
void EXTIkeyS4_Config(void)
{
  /* Enable GPIOB clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

  /* Configure PB.09 pin as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

  /* Enable AFIO clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
  /* Connect EXTI9 Line to PB.09 pin */
  GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource11);

  /* Configure EXTI9 line */
  EXTI_InitStructure.EXTI_Line = EXTI_Line11;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);

  /* Enable and set EXTI9_5 Interrupt to the lowest priority */
  NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);
}

/******************************************************************************/
/*            STM32F10x Peripherals Interrupt Handlers                        */
/******************************************************************************/

/**
  * @brief  This function handles External lines 9 to 5 interrupt request.
  * @param  None
  * @retval None
  */
void EXTI15_10_IRQHandler(void)
{
  if(EXTI_GetITStatus(EXTI_Line10) != RESET)
  {
    /* Toggle LED2 */
     GPIO_SetBits(GPIOC,GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
printf("S4 interrupt ......\r\n");
    /* Clear the  EXTI line 9 pending bit */
    EXTI_ClearITPendingBit(EXTI_Line10);
  }
if(EXTI_GetITStatus(EXTI_Line11) != RESET)
  {
    /* Toggle LED1 */
     GPIO_SetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);
printf("S3 interrupt ......\r\n");
    /* Clear the  EXTI line 0 pending bit */
    EXTI_ClearITPendingBit(EXTI_Line11);
  }
}


#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}

#endif

/**
  * @}
  */ 

/**
  * @}
  */ 

#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
  


/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  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

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}

#endif

 


菜鸟
2016-01-19 11:28:21     打赏
7楼

触摸DS18B20,改变RGB

#include "stdio.h"
#include "stm32f10x.h"
int fputc(int ch,FILE *f);

/********************RCC配置*********************/
void RCC_Configuration(void)	
{
  
	GPIO_InitTypeDef  GPIO_InitStructure;
	ErrorStatus HSEStartUpStatus;	//	设置错误标志量
	RCC_DeInit(); 		//*将外设寄存器RCC重设为初始值 
	RCC_HSEConfig(RCC_HSE_ON);//*打开外部高速时钟晶振HSE:
  HSEStartUpStatus = RCC_WaitForHSEStartUp();  //*等待外部高速时钟晶振工作: 
  if(HSEStartUpStatus==SUCCESS)
	{
		RCC_HCLKConfig(RCC_SYSCLK_Div1);//*AHB使用系统时钟,负责外部存储器的时钟 
		RCC_PCLK1Config(RCC_HCLK_Div2);//*APB1 负责DA,USB,SPI,I2C,CAN,串口2345,普通TIM
		RCC_PCLK2Config(RCC_HCLK_Div1);//*APB2负责AD,I/O,高级TIM,串口1 
		RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);//*设置PLL输入时钟源8x9=72M
		RCC_PLLCmd(ENABLE);//*打开PLL:          
		while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);//*等待PLL工作:
		RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//*设置系统时钟
		while(RCC_GetSYSCLKSource() != 0x08);//* 判断PLL是否是系统时钟:    
	}
  														
	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//改变管脚的映射SWJ使能,禁用JTAG,选择用于事件输出的GPIO端口
	
	/**蜂鸣器*/	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE); //# 使能被重新映射到GPIOD时钟和使能复用时钟功能
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
	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);
	
}   

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



/*****************************GPIO初始********************************/
void GPIO_Inits(void)
{   /*结构体初始化及开启外设时钟*/
		GPIO_InitTypeDef  GPIO_InitStructure;
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
		/*GPIOA--RGB初始化*/
		GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
		GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
		GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; 
		GPIO_Init(GPIOA,&GPIO_InitStructure);
		GPIO_SetBits(GPIOA,GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3)	
		
}

/**************************DS18B20**********************/

uint8_t DS18B20_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
  GPIO_Init(GPIOC, &GPIO_InitStructure);
	Set_B20();

	DS18B20_Rst();
	return DS18B20_Presence ();
}

static void DS18B20_Mode_IPU(void)
{
 	  GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
}

static void DS18B20_Mode_Out_PP(void)
{
 	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
  GPIO_Init(GPIOC, &GPIO_InitStructure);
}



static void DS18B20_Rst(void)
{
	/* 主机设置为输出 */
	DS18B20_Mode_Out_PP();
	Reset_B20()	;
	/* 主机至少产生480us的低电平复位信号 */
	delay_us(700);
	/* 主机在产生复位信号后,需将总线拉高 */
	Set_B20()	;
	/*从机接收到主机的复位信号后,会在15~60us后给主机发一个存在脉冲*/
	delay_us(15);
}



static uint8_t DS18B20_Presence(void)
{
	uint8_t pulse_time = 0;
	
	/* 主机设置为上拉输入 */
	DS18B20_Mode_IPU();
	
	/* 等待存在脉冲的到来,存在脉冲为一个60~240us的低电平信号 
	 * 如果存在脉冲没有来则做超时处理,从机接收到主机的复位信号后,会在15~60us后给主机发一个存在脉冲
	 */
	while( Read_B20() && pulse_time<100 )
	{
		pulse_time++;
		delay_us(1);
	}	
	/* 经过100us后,存在脉冲都还没有到来*/
	if( pulse_time >=100 )
		return 1;
	else
		pulse_time = 0;
	
	/* 存在脉冲到来,且存在的时间不能超过240us */
	while( !Read_B20() && pulse_time<240 )
	{
		pulse_time++;
		delay_us(1);
	}	
	if( pulse_time >=240 )
		return 1;
	else
		return 0;
}


static uint8_t DS18B20_ReadBit(void)
{
	uint8_t dat;
	
	/* 读0和读1的时间至少要大于60us */	
	DS18B20_Mode_Out_PP();
	/* 读时间的起始:必须由主机产生 >1us <15us 的低电平信号 */
	Reset_B20();
	delay_us(10);
	
	/* 设置成输入,释放总线,由外部上拉电阻将总线拉高 */
	DS18B20_Mode_IPU();
	//Delay_us(2);
	
	if( Read_B20() == SET )
		dat = 1;
	else
		dat = 0;
	
	/* 这个延时参数请参考时序图 */
	delay_us(45);
	
	return dat;
}


static uint8_t DS18B20_ReadByte(void)
{
	uint8_t i, j, dat = 0;	
	
	for(i=0; i<8; i++) 
	{
		j = DS18B20_ReadBit();		
		dat = (dat) | (j<<i);
	}
	
	return dat;
}



static void DS18B20_WriteByte(uint8_t dat)
{
	uint8_t i, testb;
	DS18B20_Mode_Out_PP();
	
	for( i=0; i<8; i++ )
	{
		testb = dat&0x01;
		dat = dat>>1;		
		/* 写0和写1的时间至少要大于60us */
		if (testb)
		{			
			Reset_B20();
			/* 1us < 这个延时 < 15us */
			delay_us(8);
			
			Set_B20();
			delay_us(58);
		}		
		else
		{			
			Reset_B20();
			/* 60us < Tx 0 < 120us */
			delay_us(70);
			
			Set_B20();			
			/* 1us < Trec(恢复时间) < 无穷大*/
			delay_us(2);
		}
	}
}



static void DS18B20_SkipRom ( void )
{
	DS18B20_Rst();	   
	
	DS18B20_Presence();	 
	
	DS18B20_WriteByte(0XCC);		/* 跳过 ROM */
	
}


static void DS18B20_MatchRom ( void )
{
	DS18B20_Rst();	   
	
	DS18B20_Presence();	 
	
	DS18B20_WriteByte(0X55);		/* 匹配 ROM */
	
}

float DS18B20_GetTemp_SkipRom ( void )
{
	uint8_t tpmsb, tplsb;
	short s_tem;
	float f_tem;
	
	
	DS18B20_SkipRom ();
	DS18B20_WriteByte(0X44);				/* 开始转换 */
	
	
	DS18B20_SkipRom ();
  DS18B20_WriteByte(0XBE);				/* 读温度值 */
	
	tplsb = DS18B20_ReadByte();		 
	tpmsb = DS18B20_ReadByte(); 
	
	
	s_tem = tpmsb<<8;
	s_tem = s_tem | tplsb;
	
	if( s_tem < 0 )		/* 负温度 */
		f_tem = (~s_tem+1) * 0.0625;	
	else
		f_tem = s_tem * 0.0625;
	
	return f_tem; 	
	
	
}



void DS18B20_ReadId ( uint8_t * ds18b20_id )
{
	uint8_t uc;
	
	
	DS18B20_WriteByte(0x33);       //读取序列号
	
	for ( uc = 0; uc < 8; uc ++ )
	  ds18b20_id [ uc ] = DS18B20_ReadByte();
	
}


float DS18B20_GetTemp_MatchRom ( uint8_t * ds18b20_id )
{
	uint8_t tpmsb, tplsb, i;
	short s_tem;
	float f_tem;
	
	
	DS18B20_MatchRom ();            //匹配ROM
	
  for(i=0;i<8;i++)
		DS18B20_WriteByte ( ds18b20_id [ i ] );	
	
	DS18B20_WriteByte(0X44);				/* 开始转换 */

	
	DS18B20_MatchRom ();            //匹配ROM
	
	for(i=0;i<8;i++)
		DS18B20_WriteByte ( ds18b20_id [ i ] );	
	
	DS18B20_WriteByte(0XBE);				/* 读温度值 */
	
	tplsb = DS18B20_ReadByte();		 
	tpmsb = DS18B20_ReadByte(); 
	
	
	s_tem = tpmsb<<8;
	s_tem = s_tem | tplsb;
	
	if( s_tem < 0 )		/* 负温度 */
		f_tem = (~s_tem+1) * 0.0625;	
	else
		f_tem = s_tem * 0.0625;
	
	return f_tem; 	
	
	
}





int main()
{  
uint8_t uc, ucDs18b20Id [ 8 ];
RCC_Configuration();
GPIO_Inits();
USART_int(9600);
while( DS18B20_Init() )	
printf("\r\n no ds18b20 exit \r\n");

printf("\r\n Yes ds18b20 \r\n");

DS18B20_ReadId ( ucDs18b20Id  );           // 读取 DS18B20 的序列号

printf("\r\nDS18B20的序列号是: 0x");

for ( uc = 0; uc < 8; uc ++ )             // 打印 DS18B20 的序列号
printf ( "%.2x", ucDs18b20Id [ uc ] );


for(;;)
{	
static unsigned int i=0;
float a,b;
if(i==10)
{
printf ( "\r\n第10秒:温度: %.1f\r\n", DS18B20_GetTemp_MatchRom ( ucDs18b20Id ) );		// 打印通过 DS18B20 序列号获取的温度值		  
a=DS18B20_GetTemp_MatchRom ( ucDs18b20Id );
delay_ms(1000);		/* 1s 读取一次温度值 */
i++;
}
else{}
if(i<10)
{
printf ( "\r\n温度: %.1f\r\n", DS18B20_GetTemp_MatchRom ( ucDs18b20Id ) );		// 打印通过 DS18B20 序列号获取的温度值		  
delay_ms(1000);		/* 1s 读取一次温度值 */
i++;
}
if(i>10)
{
printf ( "\r\n10秒后:温度: %.1f\r\n", DS18B20_GetTemp_MatchRom ( ucDs18b20Id ) );		// 打印通过 DS18B20 序列号获取的温度值		  
b=DS18B20_GetTemp_MatchRom ( ucDs18b20Id );
delay_ms(1000);		/* 1s 读取一次温度值 */
i++;
if((b-a)>0.9)
{GPIO_ResetBits(GPIOA,GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);

}
else{}
if((b-a)<0.9)
{
		GPIO_SetBits(GPIOA,GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);


}
else{}

}
else{}

}	
}
int fputc(int ch,FILE *f)
{
USART_SendData(USART1, (uint8_t) ch);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}
return ch;
}

 


共7条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]