这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 高校专区 » 漓东e学堂 » 【11.30更新】12号的作业进程

共8条 1/1 1 跳转至

【11.30更新】12号的作业进程

菜鸟
2015-11-30 11:36:42     打赏


菜鸟
2015-11-30 11:41:47     打赏
2楼

作业一


菜鸟
2015-11-30 11:44:15     打赏
3楼

作业二

/**
******************************************************************************
* @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)
{

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)
{
GPIO_ResetBits(GPIOC,GPIO_Pin_0);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_0);
GPIO_ResetBits(GPIOC,GPIO_Pin_1);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_1);
GPIO_ResetBits(GPIOC,GPIO_Pin_2);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_2);
GPIO_ResetBits(GPIOC,GPIO_Pin_3);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_3);
GPIO_ResetBits(GPIOC,GPIO_Pin_4);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_4);
GPIO_ResetBits(GPIOC,GPIO_Pin_5);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_5);
GPIO_ResetBits(GPIOC,GPIO_Pin_6);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_6);
GPIO_ResetBits(GPIOC,GPIO_Pin_7);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_7);
}
}


菜鸟
2015-11-30 11:46:43     打赏
4楼

作业三

#include "stm32f10x.h"
#include "stm32f10x_rcc.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)
{
ErrorStatus HSEStartUpStatus;
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus==SUCCESS)
{
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PCLK2Config(RCC_HCLK_Div1);
//RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource() != 0x08);
}

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

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;

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);
delay_ms(200);
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(200);
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))
{
delay_ms(100);
if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8))
{
i++;
if(i==10)
{
j++;
i=0;
}
if(j==10)
{
j=0;
i=0;
}
}
}

}}


菜鸟
2015-11-30 11:50:41     打赏
5楼

作业四

#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);
delay_ms(200);
GPIO_SetBits(GPIOC,0xffff);
USART_SendStr("USART Led Speed\r\n");
USART_SendStr("\n>");//
while(1);
}

void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus==SUCCESS)
{
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PCLK2Config(RCC_HCLK_Div1);
//RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);//*??PLL?????72M
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource() != 0x08);
}
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);
}

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

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;
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);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}

void USART_SendStr(char *str)
{
while((*str)!='\0')
{
USART_SendData(USART1,*str++);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
}

unsigned int translate(char* pstr)
{
int s = 0;
while(*pstr != '\0')
{
if(*pstr >= '0' && *pstr <= '9')
{
s = s * 10 + *pstr - '0';
}
pstr++;
}
return s;
}

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]);
delay_ms(m);
GPIO_SetBits(GPIOC,a[i]);
delay_ms(m);
}
}
void input_ASK()
{
char j;
LED(rx_buff,rx_buff_count);
rx_buff_count=0;
for (j=0;j<10;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-11-30 11:54:11     打赏
6楼

作业五


/**
******************************************************************************
* @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;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
unsigned int flag;
unsigned char led;
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);
*/
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);
/* 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);
}

void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;

/* Enable the TIM2 global Interrupt */
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 ---------------------------------------------------------*/

/**
* @brief Main program.
* @param None
* @retval None
*/
int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f10x_xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f10x.c file
*/

/* System Clocks Configuration */
RCC_Configuration();

NVIC_Configuration();
//USART_int(115200);
// printf("config done...\r\n");

/* Time base 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 );

/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE);

while (1){
//if(flag == 1)
// {printf("TIM2 interrupt......\r\n");
//flag = 0;
}
//else{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);}
//}
}

void TIM2_IRQHandler(void) //TIM3 ??
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //?? TIM3 ????????
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update ); //?? TIM3 ??????
// flag = ~flag;
// if(flag){
// GPIO_SetBits(GPIOC,GPIO_Pin_1);}
// else {GPIO_ResetBits(GPIOC,GPIO_Pin_1);}
flag++;
if(flag == 1)
{
GPIO_ResetBits(GPIOC,GPIO_Pin_0);
}
else if(flag == 2)
{
GPIO_SetBits(GPIOC,GPIO_Pin_0);}
else if(flag == 3)
{
GPIO_ResetBits(GPIOC,GPIO_Pin_1);}
else if(flag == 4)
{
GPIO_SetBits(GPIOC,GPIO_Pin_1);}
else if(flag == 5)
{
GPIO_ResetBits(GPIOC,GPIO_Pin_2);}
else if(flag == 6)
{
GPIO_SetBits(GPIOC,GPIO_Pin_2);}
else if(flag == 7)
{
GPIO_ResetBits(GPIOC,GPIO_Pin_3);}
else if(flag == 8)
{
GPIO_SetBits(GPIOC,GPIO_Pin_3);}
else if(flag == 9)
{
GPIO_ResetBits(GPIOC,GPIO_Pin_4);}
else if(flag == 10)
{
GPIO_SetBits(GPIOC,GPIO_Pin_4);}
else if(flag == 11)
{
GPIO_ResetBits(GPIOC,GPIO_Pin_5);}
else if(flag == 12)
{
GPIO_SetBits(GPIOC,GPIO_Pin_5);}
else if(flag == 13)
{
GPIO_ResetBits(GPIOC,GPIO_Pin_6);}
else if(flag == 14)
{
GPIO_SetBits(GPIOC,GPIO_Pin_6);}
else if(flag == 15)
{
GPIO_ResetBits(GPIOC,GPIO_Pin_7);}
else if(flag == 16)
{
GPIO_SetBits(GPIOC,GPIO_Pin_7);
flag = 0;
}

}
}


#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****/


菜鸟
2016-01-17 20:26:19     打赏
7楼
view plaincopy to clipboardprint?
  1. #include "stdio.h"//ÉèÖÃtarget£¬use microlib,include"stdio.h"£¬Öض¨Òå·¢ËͽÓÊÕº¯Êý²Å¿ÉÓÃprintf.  
  2. #include "delay.h"  
  3. #include "rcc.h"  
  4. #include "usart.h"  
  5. #include "timer.h"  
  6. #include "adc.h"  
  7. #include "stm32f10x_adc.h"  
  8.   
  9. int volt;  
  10. #define VREF 3.3   
  11. int fputc(int ch, FILE *f);  
  12. unsigned int temp0,temp1,temp2;   
  13. void PWM_TEST(void);    
  14. int main(void)    
  15. {     
  16.         float Volt=0.00;    
  17.       int ADValue = 0;    
  18. RCC_Configuration();   
  19.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);      
  20.         USART_int(9600);    
  21.     ADC_CONFIG();    
  22.     TIM2_PWM_Init(2400,0);   //²»·ÖƵ¡£PWMƵÂÊ=72000000/900=80Khz    
  23.     while(1)    
  24.     {    
  25.         ADValue = Get_Adc_Average(ADC_Channel_8,10); //PB0,¶ÔӦͨµÀ8  
  26.         Volt = VREF*ADValue/4095;    
  27.         /*voltµÄÖµÓÉVoltÖ±½Ó¾ö¶¨ voltÊÇVolt³ËÒÔ1000µÄÕûÐÍÖµ*/    
  28.                 volt=Volt*1000;  
  29.         printf("²É¼¯µ½µÄµçѹÊÇ:%f V \r\n",Volt);    
  30.         PWM_TEST();    
  31.         delay_ms(400);    
  32.     }    
  33. }   
  34. int fputc(int ch,FILE *f)  
  35. {  
  36.   USART_SendData(USART1, (uint8_t) ch);  
  37.   while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)  
  38.   {}  
  39.   return ch;  
  40. }  
  41. void PWM_TEST(void)    
  42. {  
  43.                                        
  44.         TIM_SetCompare2(TIM2,volt);     //ͨµÀ2   
  45.     }   
  46. void TIM2_PWM_Init(u16 arr,u16 psc)  
  47. {    
  48.     GPIO_InitTypeDef GPIO_InitStructure;  
  49.     TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;  
  50.     TIM_OCInitTypeDef  TIM_OCInitStructure;  
  51.       
  52.   
  53.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);    //ʹÄܶ¨Ê±Æ÷3ʱÖÓ  
  54.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);  //ʹÄÜGPIOÍâÉèºÍAFIO¸´Óù¦ÄÜÄ£¿éʱÖÓ  
  55.       
  56.     GPIO_PinRemapConfig(GPIO_PartialRemap2_TIM2, ENABLE); //Timer   
  57.    
  58.    //ÉèÖøÃÒý½ÅΪ¸´ÓÃÊä³ö¹¦ÄÜ,Êä³öTIM3 CH2µÄPWMÂö³å²¨ÐΠGPIOB.5  
  59.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //TIM_CH2  
  60.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //¸´ÓÃÍÆÍìÊä³ö  
  61.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  62.     GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIO  
  63.    
  64.    //³õʼ»¯TIM3  
  65.     TIM_TimeBaseStructure.TIM_Period = arr; //ÉèÖÃÔÚÏÂÒ»¸ö¸üÐÂʼþ×°Èë»î¶¯µÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ  
  66.     TIM_TimeBaseStructure.TIM_Prescaler =psc; //ÉèÖÃÓÃÀ´×÷ΪTIMxʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵ   
  67.     TIM_TimeBaseStructure.TIM_ClockDivision = 0; //ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim  
  68.     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIMÏòÉϼÆÊýģʽ  
  69.     TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //¸ù¾ÝTIM_TimeBaseInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î»  
  70.       
  71.     //³õʼ»¯TIM3 Channel2 PWMģʽ      
  72.     TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆģʽ2  
  73.     TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±È½ÏÊä³öʹÄÜ  
  74.     TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //Êä³ö¼«ÐÔ:TIMÊä³ö±È½Ï¼«ÐԸߠ 
  75.     TIM_OC2Init(TIM2, &TIM_OCInitStructure);  //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC2  
  76.   
  77.     TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);  //ʹÄÜTIM3ÔÚCCR2ÉϵÄԤװÔؼĴæÆ÷  
  78.    
  79.     TIM_Cmd(TIM2, ENABLE);  //ʹÄÜTIM3  
  80.       
  81.   
  82. }  
  83. void ADC_CONFIG(void)  
  84. {   
  85.     GPIO_InitTypeDef   GPIO_InitStructure;   
  86.     ADC_InitTypeDef ADC_InitStructure;   
  87.     ADC_DeInit(ADC1);    
  88.    
  89.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOB, ENABLE);    
  90.     RCC_ADCCLKConfig(RCC_PCLK2_Div6);   //ADC×î´óʱ¼ä²»Äܳ¬¹ý14M  
  91.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//ADCËùÔڶ˿ÚPB0    
  92.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//Ä£ÄâÊäÈëģʽ    
  93.   GPIO_Init(GPIOB, &GPIO_InitStructure);    
  94.     ADC_DeInit(ADC1);  //¸´Î»ADC1,½«ÍâÉè ADC1 µÄÈ«²¿¼Ä´æÆ÷ÖØÉèΪȱʡֵ  
  95.   
  96.   ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;  //ADC¶ÀÁ¢¹¤×÷ģʽ  
  97.   ADC_InitStructure.ADC_ScanConvMode = DISABLE;     //µ¥Í¨µÀ»ò¶àͨµÀɨÃè  
  98.   ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;  //Á¬Ðø»òµ¥´Î  
  99.   ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;  //ÊÇ·ñÍⲿ´¥·¢À´Æô¶¯  
  100.   ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//ADCÊý¾ÝÓÒ¶ÔÆë    
  101.   ADC_InitStructure.ADC_NbrOfChannel = 1;//ADCͨµÀÊýΪ1    
  102.   ADC_Init(ADC1, &ADC_InitStructure);//³õʼ»¯ADC1    
  103.      
  104.   ADC_Cmd(ADC1, ENABLE);    //ʹÄÜÖ¸¶¨µÄADC1  
  105.     ADC_ResetCalibration(ADC1); //ʹÄܸ´Î»Ð£×¼    
  106.     while(ADC_GetResetCalibrationStatus(ADC1)); //µÈ´ý¸´Î»Ð£×¼½áÊø  
  107.     ADC_StartCalibration(ADC1);  //¿ªÆôADУ׼  
  108.     while(ADC_GetCalibrationStatus(ADC1));   //µÈ´ýУ׼½áÊø  
  109. }    
  110.     
  111. int Get_ADC(u8 ch)  
  112. {    
  113.   ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_55Cycles5);//ADC1¹æÔò×éͨµÀ£º8 £¬×ª»»Ë³Ðò£¬²ÉÑùʱ¼ä    
  114.   ADC_SoftwareStartConvCmd(ADC1, ENABLE);//ʹÄÜADC1Èí¼þת»»¹¦ÄÜ    
  115.     while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//µÈ´ýת»»½áÊø  
  116.   return ADC_GetConversionValue(ADC1);  //·µ»Ø×î½üÒ»´ÎµÄADC1¹æÔò×éת»»½á¹û  
  117. }    
  118.   
  119. u16 Get_Adc_Average(u8 ch,u8 times)  
  120. {  
  121.     u32 temp_val=0;  
  122.     u8 t;  
  123.     for(t=0;t   

菜鸟
2016-02-29 11:27:33     打赏
8楼
view plaincopy to clipboardprint?


  1. #include "stdio.h"
  2. #include "stm32f10x.h"
  3. int fputc(int ch,FILE *f);
  4. /********************RCC配置*********************/
  5. void RCC_Configuration(void)
  6. {
  7. GPIO_InitTypeDef  GPIO_InitStructure;
  8. ErrorStatus HSEStartUpStatus;
  9. RCC_DeInit();
  10. RCC_HSEConfig(RCC_HSE_ON);
  11. HSEStartUpStatus = RCC_WaitForHSEStartUp();
  12. if(HSEStartUpStatus==SUCCESS)
  13. {
  14. RCC_HCLKConfig(RCC_SYSCLK_Div1);
  15. RCC_PCLK1Config(RCC_HCLK_Div2);
  16. RCC_PCLK2Config(RCC_HCLK_Div1);
  17. RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);//*设置PLL输入时钟源8x9=72M
  18. RCC_PLLCmd(ENABLE);//*打开PLL:
  19. while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);//*等待PLL工作:
  20. RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//*设置系统时钟
  21. while(RCC_GetSYSCLKSource() != 0x08);
  22. }
  23. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
  24. /**蜂鸣器*/
  25. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);
  26. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  27. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //#管脚号  关闭蜂鸣器
  28. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //#输出速度
  29. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //#输入输出模式(推挽输出)
  30. GPIO_Init(GPIOD, &GPIO_InitStructure); //#初始化
  31. GPIO_ResetBits(GPIOD,GPIO_Pin_2);
  32. }
  33. void delay_us(u32 n) //延时函数
  34. {
  35. u8 j;
  36. while(n--)
  37. for(j=0;j<10;j++);
  38. }
  39. void delay_ms(u32 n) //延时函数
  40. {
  41. while(n--)
  42. delay_us(1000);
  43. }
  44. /*****************************GPIO初始********************************/
  45. void GPIO_Inits(void)
  46. { /*结构体初始化及开启外设时钟*/
  47. GPIO_InitTypeDef  GPIO_InitStructure;
  48. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  49. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  50. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  51. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
  52. /*GPIOA--RGB初始化*/
  53. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  54. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
  55. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  56. GPIO_Init(GPIOA,&GPIO_InitStructure);
  57. GPIO_SetBits(GPIOA,GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3)
  58. }
  59. /**************************DS18B20**********************/
  60. uint8_t DS18B20_Init(void)
  61. {
  62. GPIO_InitTypeDef GPIO_InitStructure;
  63. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  64. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  65. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  66. GPIO_Init(GPIOC, &GPIO_InitStructure);
  67. Set_B20();
  68. DS18B20_Rst();
  69. return DS18B20_Presence ();
  70. }
  71. static void DS18B20_Mode_IPU(void)
  72. {
  73. GPIO_InitTypeDef GPIO_InitStructure;
  74. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  75. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  76. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  77. GPIO_Init(GPIOC, &GPIO_InitStructure);
  78. }
  79. static void DS18B20_Mode_Out_PP(void)
  80. {
  81. GPIO_InitTypeDef GPIO_InitStructure;
  82. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  83. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  84. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  85. GPIO_Init(GPIOC, &GPIO_InitStructure);
  86. }
  87. static void DS18B20_Rst(void)
  88. {
  89. /* 主机设置为输出 */
  90. DS18B20_Mode_Out_PP();
  91. Reset_B20() ;
  92. delay_us(700);
  93. Set_B20()   ;
  94. delay_us(15);
  95. }
  96. static uint8_t DS18B20_Presence(void)
  97. {
  98. uint8_t pulse_time = 0;
  99. /* 主机设置为上拉输入 */
  100. DS18B20_Mode_IPU();
  101. while( Read_B20() && pulse_time<100 )
  102. {
  103. pulse_time++;
  104. delay_us(1);
  105. }
  106. /* 经过100us后,存在脉冲都还没有到来*/
  107. if( pulse_time >=100 )
  108. return 1;
  109. else
  110. pulse_time = 0;
  111. /* 存在脉冲到来,且存在的时间不能超过240us */
  112. while( !Read_B20() && pulse_time<240 )
  113. {
  114. pulse_time++;
  115. delay_us(1);
  116. }
  117. if( pulse_time >=240 )
  118. return 1;
  119. else
  120. return 0;
  121. }
  122. static uint8_t DS18B20_ReadBit(void)
  123. {
  124. uint8_t dat;
  125. /* 读0和读1的时间至少要大于60us */
  126. DS18B20_Mode_Out_PP();
  127. Reset_B20();
  128. delay_us(10);
  129. DS18B20_Mode_IPU();
  130. //Delay_us(2);
  131. if( Read_B20() == SET )
  132. dat = 1;
  133. else
  134. dat = 0;
  135. delay_us(45);
  136. return dat;
  137. }
  138. static uint8_t DS18B20_ReadByte(void)
  139. {
  140. uint8_t i, j, dat = 0;
  141. for(i=0; i<8; i++)
  142. {
  143. j = DS18B20_ReadBit();
  144. dat = (dat) | (j<
  145. }
  146. return dat;
  147. }
  148. static void DS18B20_WriteByte(uint8_t dat)
  149. {
  150. uint8_t i, testb;
  151. DS18B20_Mode_Out_PP();
  152. for( i=0; i<8; i++ )
  153. {
  154. testb = dat&0x01;
  155. dat = dat>>1;
  156. /* 写0和写1的时间至少要大于60us */
  157. if (testb)
  158. {
  159. Reset_B20();
  160. /* 1us < 这个延时 < 15us */
  161. delay_us(8);
  162. Set_B20();
  163. delay_us(58);
  164. }
  165. else
  166. {
  167. Reset_B20();
  168. /* 60us < Tx 0 < 120us */
  169. delay_us(70);
  170. Set_B20();
  171. delay_us(2);
  172. }
  173. }
  174. }
  175. static void DS18B20_SkipRom ( void )
  176. {
  177. DS18B20_Rst();
  178. DS18B20_Presence();
  179. DS18B20_WriteByte(0XCC); /* 跳过 ROM */
  180. }
  181. static void DS18B20_MatchRom ( void )
  182. {
  183. DS18B20_Rst();
  184. DS18B20_Presence();
  185. DS18B20_WriteByte(0X55); /* 匹配 ROM */
  186. }
  187. float DS18B20_GetTemp_SkipRom ( void )
  188. {
  189. uint8_t tpmsb, tplsb;
  190. short s_tem;
  191. float f_tem;
  192. DS18B20_SkipRom ();
  193. DS18B20_WriteByte(0X44); /* 开始转换 */
  194. DS18B20_SkipRom ();
  195. DS18B20_WriteByte(0XBE); /* 读温度值 */
  196. tplsb = DS18B20_ReadByte();
  197. tpmsb = DS18B20_ReadByte();
  198. s_tem = tpmsb<<8;
  199. s_tem = s_tem | tplsb;
  200. if( s_tem < 0 ) /* 负温度 */
  201. f_tem = (~s_tem+1) * 0.0625;
  202. else
  203. f_tem = s_tem * 0.0625;
  204. return f_tem;
  205. }
  206. void DS18B20_ReadId ( uint8_t * ds18b20_id )
  207. {
  208. uint8_t uc;
  209. DS18B20_WriteByte(0x33); //读取序列号
  210. for ( uc = 0; uc < 8; uc ++ )
  211. ds18b20_id [ uc ] = DS18B20_ReadByte();
  212. }
  213. float DS18B20_GetTemp_MatchRom ( uint8_t * ds18b20_id )
  214. {
  215. uint8_t tpmsb, tplsb, i;
  216. short s_tem;
  217. float f_tem;
  218. DS18B20_MatchRom (); //匹配ROM
  219. for(i=0;i<8;i++)
  220. DS18B20_WriteByte ( ds18b20_id [ i ] );
  221. DS18B20_WriteByte(0X44); /* 开始转换 */
  222. DS18B20_MatchRom (); //匹配ROM
  223. for(i=0;i<8;i++)
  224. DS18B20_WriteByte ( ds18b20_id [ i ] );
  225. DS18B20_WriteByte(0XBE); /* 读温度值 */
  226. tplsb = DS18B20_ReadByte();
  227. tpmsb = DS18B20_ReadByte();
  228. s_tem = tpmsb<<8;
  229. s_tem = s_tem | tplsb;
  230. if( s_tem < 0 ) /* 负温度 */
  231. f_tem = (~s_tem+1) * 0.0625;
  232. else
  233. f_tem = s_tem * 0.0625;
  234. return f_tem;
  235. }
  236. int main()
  237. {
  238. uint8_t uc, ucDs18b20Id [ 8 ];
  239. RCC_Configuration();
  240. GPIO_Inits();
  241. USART_int(9600);
  242. while( DS18B20_Init() )
  243. printf("\r\n no ds18b20 exit \r\n");
  244. printf("\r\n Yes ds18b20 \r\n");
  245. DS18B20_ReadId ( ucDs18b20Id  ); // 读取 DS18B20 的序列号
  246. printf("\r\nDS18B20的序列号是: 0x");
  247. for ( uc = 0; uc < 8; uc ++ ) // 打印 DS18B20 的序列号
  248. printf ( "%.2x", ucDs18b20Id [ uc ] );
  249. for(;;)
  250. {
  251. static unsigned int i=0;
  252. float a,b;
  253. if(i==10)
  254. {
  255. printf ( "\r\n第10秒:温度: %.1f\r\n", DS18B20_GetTemp_MatchRom ( ucDs18b20Id ) ); // 打印通过 DS18B20 序列号获取的温度值
  256. a=DS18B20_GetTemp_MatchRom ( ucDs18b20Id );
  257. delay_ms(1000); /* 1s 读取一次温度值 */
  258. i++;
  259. }
  260. else{}
  261. if(i<10)
  262. {
  263. printf ( "\r\n温度: %.1f\r\n", DS18B20_GetTemp_MatchRom ( ucDs18b20Id ) ); // 打印通过 DS18B20 序列号获取的温度值
  264. delay_ms(1000); /* 1s 读取一次温度值 */
  265. i++;
  266. }
  267. if(i>10)
  268. {
  269. printf ( "\r\n10秒后:温度: %.1f\r\n", DS18B20_GetTemp_MatchRom ( ucDs18b20Id ) ); // 打印通过 DS18B20 序列号获取的温度值
  270. b=DS18B20_GetTemp_MatchRom ( ucDs18b20Id );
  271. delay_ms(1000); /* 1s 读取一次温度值 */
  272. i++;
  273. if((b-a)>0.9)
  274. {GPIO_ResetBits(GPIOA,GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);
  275. }
  276. else{}
  277. if((b-a)<0.9)
  278. {
  279. GPIO_SetBits(GPIOA,GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);
  280. }
  281. else{}
  282. }
  283. else{}
  284. }
  285. }
  286. int fputc(int ch,FILE *f)
  287. {
  288. USART_SendData(USART1, (uint8_t) ch);
  289. while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  290. {}
  291. return ch;

共8条 1/1 1 跳转至

回复

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