OpenVINOTM,给你看得见的未来!>>
电子产品世界 » 论坛首页 » 高校专区 » 漓东e学堂 » 【10.22更新】27号

共10条 1/1 1 跳转至

【10.22更新】27号

菜鸟
2015-10-22 23:15:45    评分

嵌入式系统作业一(Keil uVision5安装)http://forum.eepw.com.cn/thread/277210/1#2

嵌入式系统作业二D1~D8流水灯http://http://forum.eepw.com.cn/thread/277210/1#3

嵌入式系统作业三数码管显示0~99http://http://forum.eepw.com.cn/thread/277210/1#4

课堂作业S1,S2控制改为s3.s4.控制:http://http://forum.eepw.com.cn/thread/277210/1#5

嵌入式系统作业四:(Uart控制流水灯):http://forum.eepw.com.cn/thread/277210/1#6

计算开机次数作业六:http://forum.eepw.com.cn/thread/277210/1#7

作业五:用定时器控制LED:http://forum.eepw.com.cn/thread/277210/1#9

作业七:用DS18B20测量温度改变灯的颜色:http://forum.eepw.com.cn/thread/277210/1#10









关键词: 10.22     更新     27号    

菜鸟
2015-11-09 12:50:12    评分
2楼








菜鸟
2015-11-09 12:55:40    评分
3楼

2.从1灯亮到8,然后再循环

/**

  ******************************************************************************
  * @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)
{ unsigned char flag=0;

RCC_Configuration();

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  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_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)
{
GPIO_SetBits(GPIOC,GPIO_Pin_0);
GPIO_SetBits(GPIOC,GPIO_Pin_1);
GPIO_SetBits(GPIOC,GPIO_Pin_2);
GPIO_SetBits(GPIOC,GPIO_Pin_3);
GPIO_SetBits(GPIOC,GPIO_Pin_4);
GPIO_SetBits(GPIOC,GPIO_Pin_5);
GPIO_SetBits(GPIOC,GPIO_Pin_6);
GPIO_SetBits(GPIOC,GPIO_Pin_7);


 
 delay_ms(100);
    GPIO_ResetBits(GPIOC,GPIO_Pin_0);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_0);
delay_ms(100);
GPIO_ResetBits(GPIOC,GPIO_Pin_1);
delay_ms(100);
    GPIO_SetBits(GPIOC,GPIO_Pin_1);
delay_ms(100);
GPIO_ResetBits(GPIOC,GPIO_Pin_2);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_2);
delay_ms(100);
GPIO_ResetBits(GPIOC,GPIO_Pin_3);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_3);
delay_ms(100);
GPIO_ResetBits(GPIOC,GPIO_Pin_4);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_4);
delay_ms(100);
GPIO_ResetBits(GPIOC,GPIO_Pin_5);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_5);
delay_ms(100);
GPIO_ResetBits(GPIOC,GPIO_Pin_6);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_6);
delay_ms(100);
GPIO_ResetBits(GPIOC,GPIO_Pin_7);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_7);
delay_ms(100);

}
}

菜鸟
2015-11-15 16:02:40    评分
4楼

//按键计数:0~99

 #include "stm32f10x.h"  
#include "stm32_eval.h"  
#include "stdbool.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); 
  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 GPIO_INIT()     
{     
   
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);     
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|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_Pin_15;     
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;     
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;     
  GPIO_Init(GPIOB, &GPIO_InitStructure);     
         
  
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, 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);     
}     
  
void delay_us(u32 n)  
{  
    u8 i;  
    while(n--)  
    for(i=0;i<10;i++);  
}  
  
void  delay_ms(u32 n)  
{  
    while(n--)  
    delay_us(500);  
}  
  void function(void)  
{  
    int c[10],j=0;
    unsigned char flag=0; 
    c[0]=GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14;  
    c[1]=GPIO_Pin_9|GPIO_Pin_12;  
    c[2]=GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_13|GPIO_Pin_14;  
    c[3]=GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_14;  
    c[4]=GPIO_Pin_5|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12;  
    c[5]=GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_12|GPIO_Pin_14;  
    c[6]=GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14;  
    c[7]=GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_12;  
    c[8]=GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14;  
    c[9]=GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_14;  
    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);  
  
    while(1)  
    {  
        if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8))  
        {  
            delay_ms(50);  
            if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8))  
            {  
                flag=flag;
            for(;flag==flag,j<=99;) 
                {  
                    if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8))  
                    {  
                        delay_ms(50);         
                        if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8)){j++;}  
                    }  
                    if(j>99){j=0;}  
                    GPIO_ResetBits(GPIOB,GPIO_Pin_15); 
                    GPIO_SetBits(GPIOB,GPIO_Pin_1); 
                    GPIO_ResetBits(GPIOB,c[j%10]); 
                    GPIO_SetBits(GPIOB,c[j%10]);    
                    GPIO_ResetBits(GPIOB,GPIO_Pin_1);
                    GPIO_SetBits(GPIOB,GPIO_Pin_15);
                    GPIO_ResetBits(GPIOB,c[j/10]); 
                    GPIO_SetBits(GPIOB,c[j/10]);   
                }  
            }  
        }  
    }  
}  


int main(void)  
{   
    RCC_Configuration();  
    GPIO_INIT();  
    function();  
 }



菜鸟
2015-11-15 16:10:53    评分
5楼
/**
  ******************************************************************************
  * @file    EXTI/EXTI_Config/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"
#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

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

菜鸟
2015-11-15 22:48:38    评分
6楼
/**
  ******************************************************************************
  * @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"

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


菜鸟
2015-12-14 00:18:10    评分
7楼

//timer.c

//定时器 通道 输入捕获配置

TIM_ICInitTypeDef TIM5_ICInitStructure;

void TIM5_Cap_Init(u16 arr,u16 psc)

GPIO_InitTypeDef GPIO_InitStructure;

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); //①使能 TIM5 时钟

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //①使能 GPIOA 时钟

//初始化 GPIOA.0 

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //PA0 设置

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0 输入

GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化 GPIOA.0

GPIO_ResetBits(GPIOA,GPIO_Pin_0);  //PA0 下拉

//②初始化 TIM5 参数

TIM_TimeBaseStructure.TIM_Period = arr; //设定计数器自动重装值

TIM_TimeBaseStructure.TIM_Prescaler =psc; //预分频器

TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; // TDTS = Tck_tim

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM 向上计数模式

TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //初始化 TIMx

//③初始化 TIM5 输入捕获通道 1

TIM5_ICInitStructure.TIM_Channel = TIM_Channel_1; //选择输入端 IC1 映射到 TI1 

TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;  //上升沿捕获

TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到 TI1 

TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频

TIM5_ICInitStructure.TIM_ICFilter = 0x00; //IC1F=0000 配置输入滤波器 不滤波

TIM_ICInit(TIM5, &TIM5_ICInitStructure); //初始化 TIM5 输入捕获通道 1

//⑤初始化 NVIC 中断优先级分组

NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn; //TIM3 中断

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //先占优先级 

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //从优先级 

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ 通道被使能

NVIC_Init(&NVIC_InitStructure); //初始化 NVIC

TIM_ITConfig( TIM5,TIM_IT_Update|TIM_IT_CC1,ENABLE);//④允许更新中断捕获中断

TIM_Cmd(TIM5,ENABLE ); //⑥使能定时器 5

}

u8 TIM5CH1_CAPTURE_STA=0;  //输入捕获状态 

u16 TIM5CH1_CAPTURE_VAL; //输入捕获值

//⑤定时器 中断服务程序

void TIM5_IRQHandler(void)

{

if((TIM5CH1_CAPTURE_STA&0X80)==0)//还未成功捕获

if (TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET)

if(TIM5CH1_CAPTURE_STA&0X40) //已经捕获到高电平了

{

if((TIM5CH1_CAPTURE_STA&0X3F)==0X3F)//高电平太长了

{

TIM5CH1_CAPTURE_STA|=0X80; //标记成功捕获了一次

TIM5CH1_CAPTURE_VAL=0XFFFF;

}else TIM5CH1_CAPTURE_STA++;

   } 

}

if (TIM_GetITStatus(TIM5, TIM_IT_CC1) != RESET) //捕获 发生捕获事件

if(TIM5CH1_CAPTURE_STA&0X40) //捕获到一个下降沿

TIM5CH1_CAPTURE_STA|=0X80;  //标记成功捕获到一次上升沿

TIM5CH1_CAPTURE_VAL=TIM_GetCapture1(TIM5);

TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Rising); //设置为上升沿捕获

}else //还未开始,第一次捕获上升沿

{

TIM5CH1_CAPTURE_STA=0;  //清空

TIM5CH1_CAPTURE_VAL=0;

TIM_SetCounter(TIM5,0);

TIM5CH1_CAPTURE_STA|=0X40;  //标记捕获到了上升沿

TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Falling); //设置为下降沿捕获

   } 

  } 

 }

TIM_ClearITPendingBit(TIM5, TIM_IT_CC1|TIM_IT_Update); //清除中断标志位

}

//timer.h

extern u8 TIM5CH1_CAPTURE_STA;  //输入捕获状态 

extern u16  TIM5CH1_CAPTURE_VAL; //输入捕获值

int main(void)

{

u32 temp=0;

delay_init();  //延时函数初始化

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置 NVIC 中断分组 2

uart_init(115200); //串口初始化波特率为 115200

LED_Init();  //LED 端口初始化

TIM3_PWM_Init(899,0); //不分频。PWM 频率=72000/(899+1)=80Khz

TIM5_Cap_Init(0XFFFF,72-1);  //以 1Mhz 的频率计数

while(1)

{

delay_ms(10);

TIM_SetCompare2(TIM3,TIM_GetCapture2(TIM3)+1);

if(TIM_GetCapture2(TIM3)==300)

TIM_SetCompare2(TIM3,0);

if(TIM5CH1_CAPTURE_STA&0X80)//成功捕获到了一次上升沿

{

temp=TIM5CH1_CAPTURE_STA&0X3F;

temp*=65536;//溢出时间总和

temp+=TIM5CH1_CAPTURE_VAL;//得到总的高电平时间

printf("HIGH:%d us\r\n",temp); //打印总的高点平时间

TIM5CH1_CAPTURE_STA=0; //开启下一次捕获

       }

   }

 }

             

                                           











菜鸟
2016-01-07 08:35:37    评分
8楼

//ds18b20.c

//复位 DS18B20

void DS18B20_Rst(void) 

{

DS18B20_IO_OUT(); //SET PG11 OUTPUT

DS18B20_DQ_OUT=0; //拉低 DQ

delay_us(750); //拉低 750us

DS18B20_DQ_OUT=1; //DQ=1

delay_us(15); //15US

}

//等待 DS18B20 的回应

//返回 1:未检测到 DS18B20 的存在

//返回 0:存在

u8 DS18B20_Check(void)

{

u8 retry=0;

DS18B20_IO_IN();//SET PG11 INPUT 

while (DS18B20_DQ_IN&&retry<200) { retry++; delay_us(1); };

if(retry>=200)return 1;

else retry=0;

while (!DS18B20_DQ_IN&&retry<240) {retry++; delay_us(1); };

if(retry>=240)return 1;

return 0;

}

//从 DS18B20 读取一个位

//返回值:1/0

u8 DS18B20_Read_Bit(void)

{

u8 data;

DS18B20_IO_OUT();//SET PG11 OUTPUT

DS18B20_DQ_OUT=0;

delay_us(2);

DS18B20_DQ_OUT=1;

DS18B20_IO_IN();//SET PG11 INPUT

delay_us(12);

if(DS18B20_DQ_IN)data=1;

else data=0; 

delay_us(50);

return data;

}

//从 DS18B20 读取一个字节

//返回值:读到的数据

u8 DS18B20_Read_Byte(void)

{

u8 i,j,dat;

dat=0;

for (i=1;i<=8;i++)

{

j=DS18B20_Read_Bit();

dat=(j<<7)|(dat>>1);

return dat;

}

//写一个字节到 DS18B20

//dat:要写入的字节

void DS18B20_Write_Byte(u8 dat)

{

u8 j;

u8 testb;

DS18B20_IO_OUT();//SET PG11 OUTPUT;

for (j=1;j<=8;j++)

{

testb=dat&0x01;

dat=dat>>1;

if (testb)

{

DS18B20_DQ_OUT=0;// Write 1

delay_us(2);

DS18B20_DQ_OUT=1;

delay_us(60);

}

else

{

DS18B20_DQ_OUT=0;// Write 0

delay_us(60);

DS18B20_DQ_OUT=1;

delay_us(2);

}

}

}

//开始温度转换

void DS18B20_Start(void)

{

DS18B20_Rst(); 

DS18B20_Check();

DS18B20_Write_Byte(0xcc);// skip rom

DS18B20_Write_Byte(0x44);// convert

}

//初始化 DS18B20 的 IO 口 DQ 同时检测 DS 的存在

//返回 1:不存在

//返回 0:存在

u8 DS18B20_Init(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);//使能 GPIOG 时钟

//GPIOG9

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//50MHz

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉

GPIO_Init(GPIOG, &GPIO_InitStructure);//初始化

DS18B20_Rst();

return DS18B20_Check();

} //从 ds18b20 得到温度值

//精度:0.1C

//返回值:温度值 (-550~1250

short DS18B20_Get_Temp(void)

{

u8 temp;

u8 TL,TH;

short tem;

DS18B20_Start();// ds1820 start convert

DS18B20_Rst();

DS18B20_Check();

DS18B20_Write_Byte(0xcc);// skip rom

DS18B20_Write_Byte(0xbe);// convert

TL=DS18B20_Read_Byte(); // LSB

TH=DS18B20_Read_Byte(); // MSB

if(TH>7)

{

TH=~TH;

TL=~TL;

temp=0; //温度为负

}else temp=1; //温度为正 

tem=TH; //获得高八位

tem<<=8;

tem+=TL;  //获得底八位

tem=(double)tem*0.625; //转换

if(temp)return tem; //返回温度值

else return -tem;

}

//ds18b20.h

#ifndef__DS18B20_H

#define__DS18B20_H

#include"sys.h"

#include"sys.h"

//IO方向设置

#define DS18B20_IO_IN() {GPIOG->MODERA=~(3<<(9*2));GPIOG->MODERA=~(0<<(9*2))} //PG9输入模式

#define DS18B20_IO_OUT() {GPIOG->MODERA=~(3<<(9*2));GPIOG->MODERA=~(0<<(9*2))} //PG9输出模式

//IO操作函数

#define DS18B20_DQ_IN PGout(9) //数据端口 PG9

#define DS18B20_DQ_OUT PGout(9) //数据端口PG9

u8 DS18B20_Init(void);//初始化DS18B20

short DS18B20_Get_Temp(void);//获取温度

void DS18B20_Start(void);//开始温度转换

void DS18B20_Write_Byte(u8 dat);//写 入一个字节

u8 DS18B20_Read_Byte(void);/读出一个字节

u8 DS18B20_Read_Bit(void);//读出一个位

u8 DS18B20_Check(void);//检测是否存在DS18B20

void DS18B20_Rst(void);//复位DS18B20

#endif

//main函数

#include "sys.h"

#include "delay.h"

#include "usart.h"

#include "led.h"

#include "lcd.h"

#include "ds18b20.h"  

int main(void)

u8 t=0;     

short temperature;  

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2

delay_init(168);  //初始化延时函数

uart_init(115200); //初始化串口波特率为115200


LED_Init(); //初始化LED

  LCD_Init();

  POINT_COLOR=RED;//设置字体为红色 

LCD_ShowString(30,50,200,16,16,"Explorer STM32F4");

LCD_ShowString(30,70,200,16,16,"DS18B20 TEST");

LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK");

LCD_ShowString(30,110,200,16,16,"2014/5/7");

  while(DS18B20_Init()) //DS18B20初始化

{

LCD_ShowString(30,130,200,16,16,"DS18B20 Error");

delay_ms(200);

LCD_Fill(30,130,239,130+16,WHITE);

  delay_ms(200);

}   

LCD_ShowString(30,130,200,16,16,"DS18B20 OK");

POINT_COLOR=BLUE;//设置字体为蓝色 

  LCD_ShowString(30,150,200,16,16,"Temp:   . C");  

while(1)

{          

  if(t%10==0)//100ms读取一次

{   

temperature=DS18B20_Get_Temp();

if(temperature<0)

{

LCD_ShowChar(30+40,150,'-',16,0); //显示负号

temperature=-temperature; //转为正数

}else LCD_ShowChar(30+40,150,' ',16,0); //去掉负号

LCD_ShowNum(30+40+8,150,temperature/10,2,16); //显示正数部分     

    LCD_ShowNum(30+40+32,150,temperature%10,1,16); //显示小数部分     

}    

  delay_ms(10);

t++;

if(t==20)

{

t=0;

LED0=!LED0;

}

}

}


菜鸟
2016-01-16 00:02:25    评分
9楼

//作业五:

#include "stm32f10x.h"  

#include "stm32_eval.h"  
#include <stdio.h>  
 
EXTI_InitTypeDef   EXTI_InitStructure;  
GPIO_InitTypeDef   GPIO_InitStructure;  
NVIC_InitTypeDef   NVIC_InitStructure;  
USART_InitTypeDef USART_InitStructure;  
USART_ClockInitTypeDef USART_ClockInitStructure;  
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;  
TIM_OCInitTypeDef  TIM_OCInitStructure;  
int count=0;  
  
void RCC_Configuration(void)  
{
  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_10MHz;  
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;   
    GPIO_Init(GPIOA, &GPIO_InitStructure);  
   
    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; 
  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_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 NVIC_Configuration(void)  
{  
  NVIC_InitTypeDef NVIC_InitStructure;  
    
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;  
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;  
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
  NVIC_Init(&NVIC_InitStructure);  
}  
  
/* Private functions ---------------------------------------------------------*/  
  

int main(void)  
{  
      
         
  RCC_Configuration();  
  
  NVIC_Configuration();  

  TIM_TimeBaseStructure.TIM_Period = 36000;  
  TIM_TimeBaseStructure.TIM_Prescaler = 100;  
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;  
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);  
  TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE );   
    
  TIM_Cmd(TIM2, ENABLE);  
  
  while (1){  
   }    
  }  
  
void TIM2_IRQHandler(void) //TIM3  
{  
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)   
{  
TIM_ClearITPendingBit(TIM2, TIM_IT_Update );  

    count++;  
    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);  
        if(count==1)   
    GPIO_ResetBits(GPIOC,GPIO_Pin_0);  
    if(count==2)   
    GPIO_ResetBits(GPIOC,GPIO_Pin_1);  
    if(count==3)   
    GPIO_ResetBits(GPIOC,GPIO_Pin_2);  
    if(count==4)   
    GPIO_ResetBits(GPIOC,GPIO_Pin_3);  
    if(count==5)   
    GPIO_ResetBits(GPIOC,GPIO_Pin_4);  
    if(count==6)   
    GPIO_ResetBits(GPIOC,GPIO_Pin_5);  
    if(count==7)   
    GPIO_ResetBits(GPIOC,GPIO_Pin_6);  
    if(count==8)   
    GPIO_ResetBits(GPIOC,GPIO_Pin_7);  
    if(count==9)  
    count=0;  
}  
}  
  
  
#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);  
  
  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  
  
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 

菜鸟
2016-01-19 13:29:42    评分
10楼

//设计过程:复位,发SKIP ROM命令(0XCC),发开始转换命令(0X44),延时,复位,发送SKIP ROM命令(0XCC),发读存储器命令(0XBE),连续读出两个字节数据(即温度),改变灯的颜色。


//ds18b20.h

//IO方向设置

#define DS18B20_IO_IN(){GPIOG->CRH&=0XFFFF0FFF;GPIO->CRH|=8<<12;}

#define DS18B20_IO_OUT(){GPIOG->CRH&=0XFFFF0FFF;GPIO->CRH|=3<<12;}

//IO操作

#define    DS18B20_DQ_OUT PCout(12) //数据端口PA0

#define    DS18B20_DQ_IN PCin(12) //数据端口PAO


u8 DS18B20_Init(void);//初始化DS18B20

short DS18B20_Get_Temp(void);/获取温度

void DS18B20_Start(void);//开始温度转换

void DS18B20_Write_Byte(u8 dat);/写入一个字节

u8 DS18B20_Read_Byte(void);//读出一个字节

u8 DS18B20_Read_Bit(void);//读出一个字位

u8 DS18B20_Check(void);//检测是否存在DS18B20

void DS18B20_Rst(void);//复位DS18B20

#endif


//ds18b20.c

#include "ds18b20.h"

#include "delay.h"

//复位 DS18B20

void DS18B20_Rst(void)

{

DS18B20_IO_OUT(); //SET PA0 OUTPUT

DS18B20_DQ_OUT=0; //拉低 DQ

delay_us(750); //拉低 750us

DS18B20_DQ_OUT=1; //DQ=1

delay_us(15); //15US

}

//等待 DS18B20 的回应

//返回 1:未检测到 DS18B20 的存在

//返回 0:存在

u8 DS18B20_Check(void)

{

u8 retry=0;

DS18B20_IO_IN();//SET PA0 INPUT

while (DS18B20_DQ_IN&&retry<200)

{

retry++;

delay_us(1);

};

if(retry>=200)return 1;

else retry=0;

while (!DS18B20_DQ_IN&&retry<240)

{

retry++;

delay_us(1);

};

if(retry>=240)return 1;

return 0;

}

//DS18B20 读取一个位

//返回值:1/0

u8 DS18B20_Read_Bit(void) // read one bit

{

u8 data;

DS18B20_IO_OUT();//SET PA0 OUTPUT

DS18B20_DQ_OUT=0;

delay_us(2);

DS18B20_DQ_OUT=1;

DS18B20_IO_IN();//SET PA0 INPUT

delay_us(12);

if(DS18B20_DQ_IN)data=1;

else data=0;

delay_us(50);

return data;

}

//DS18B20 读取一个字节

//返回值:读到的数据

u8 DS18B20_Read_Byte(void) // read one byte

{

u8 i,j,dat;

dat=0;

for (i=1;i<=8;i++)

{

j=DS18B20_Read_Bit();

dat=(j<<7)|(dat>>1);

}

return dat;

}

//写一个字节到 DS18B20

//dat:要写入的字节

void DS18B20_Write_Byte(u8 dat)

{

u8 j;

u8 testb;

DS18B20_IO_OUT();//SET PA0 OUTPUT;

for (j=1;j<=8;j++)

{

testb=dat&0x01;

dat=dat>>1;

if (testb)

{ DS18B20_DQ_OUT=0;// Write 1

delay_us(2);

DS18B20_DQ_OUT=1;

delay_us(60);

}

else

{ DS18B20_DQ_OUT=0;// Write 0

delay_us(60);

DS18B20_DQ_OUT=1;

delay_us(2);

}

}

}

//开始温度转换

void DS18B20_Start(void)// ds1820 start convert

{

DS18B20_Rst();

DS18B20_Check();

DS18B20_Write_Byte(0xcc);// skip rom

DS18B20_Write_Byte(0x44);// convert

}

//初始化 DS18B20 IO DQ 同时检测 DS 的存在

//返回 1:不存在

//返回 0:存在

u8 DS18B20_Init(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG, ENABLE); //使能 PG 口时钟

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //PORTG.11 推挽输出

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOG, &GPIO_InitStructure); //初始化 GPIO

GPIO_SetBits(GPIOG,GPIO_Pin_11); //输出 1

DS18B20_Rst();

return DS18B20_Check();

} //ds18b20 得到温度值

//精度:0.1C

//返回值:温度值 (-550~1250

short DS18B20_Get_Temp(void)

{

u8 temp;

u8 TL,TH;

short tem;

DS18B20_Start (); // ds1820 start convert

DS18B20_Rst();

DS18B20_Check();

DS18B20_Write_Byte(0xcc);// skip rom

DS18B20_Write_Byte(0xbe);// convert

TL=DS18B20_Read_Byte(); // LSB

TH=DS18B20_Read_Byte(); // MSB

if(TH>7)

{

TH=~TH;

TL=~TL;

temp=0; //温度为负

}else temp=1; //温度为正

tem=TH; //获得高八位

tem<<=8;

tem+=TL; //获得底八位

tem=(float)tem*0.625; //转换

if(temp)return tem; //返回温度值

else return -tem;

}

//main函数

int main(void)

{

u8 t=0;

short temperature;

delay_init();  //延时函数初始化

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组 2

uart_init(115200); //串口初始化为 115200

LED_Init();  //初始化与 LED 连接的硬件接口

LCD_Init();  //初始化 LCD

POINT_COLOR=RED; //设置字体为红色

LCD_ShowString(30,50,200,16,16,"WarShip STM32");

LCD_ShowString(30,70,200,16,16,"DS18B20 TEST");

LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK");

LCD_ShowString(30,110,200,16,16,"2015/1/16");

while(DS18B20_Init()) //DS18B20 初始化

{

LCD_ShowString(30,130,200,16,16,"DS18B20 Error");

delay_ms(200);

LCD_Fill(30,130,239,130+16,WHITE);

delay_ms(200);

}

LCD_ShowString(30,130,200,16,16,"DS18B20 OK");

POINT_COLOR=BLUE;//设置字体为蓝色

LCD_ShowString(30,150,200,16,16,"Temp: . C");

while(1)

{

if(t%10==0) //100ms 读取一次

{

temperature=DS18B20_Get_Temp();

if(temperature<0)

{

LCD_ShowChar(30+40,150,'-',16,0);  //显示负号

temperature=-temperature;  //转为正数

}else LCD_ShowChar(30+40,150,' ',16,0); //去掉负号

LCD_ShowNum(30+40+8,150,temperature/10,2,16); //显示正数部分

LCD_ShowNum(30+40+32,150,temperature%10,1,16);  //显示小数部分

}

delay_ms(10);

t++;

if(t==20)

{

t=0;

LED0=!LED0;

}

}

}



共10条 1/1 1 跳转至

回复

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