这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 高校专区 » 漓东e学堂 » 【11.16】10号作业千辛万苦更新- -。

共14条 2/2 1 2 跳转至
菜鸟
2015-11-16 16:45:02     打赏
11楼

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


视频网址:http://v.youku.com/v_show/id_XMTQ0MTQzMjA4MA==.html

               http://v.youku.com/v_show/id_XMTQ0MTQzMjcxNg==.html


菜鸟
2015-11-16 17:21:33     打赏
12楼


/**
******************************************************************************
* @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.
*
*

© COPYRIGHT 2011 STMicroelectronics
******************************************************************************
*/


/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "stm32_eval.h"
#include
/** @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****/


视频网址:http://v.youku.com/v_show/id_XMTQ0MTQyNjk3Ng==.html


菜鸟
2016-01-19 11:59:35     打赏
13楼
view plaincopy to clipboardprint?
  1. #include "stm32f10x.h"    
  2. #include "stm32_eval.h"    
  3. #include <stdio.h>    
  4. #define VREF 3.3    
  5. GPIO_InitTypeDef   GPIO_InitStructure;    
  6. USART_InitTypeDef USART_InitStructure;    
  7. USART_ClockInitTypeDef USART_ClockInitStructure;    
  8. int volt;    
  9. unsigned int temp0,temp1,temp2;    
  10. void RCC_Configuration(void)    
  11. {/*  
  12.   RCC_DeInit();  
  13.       
  14.   RCC_HSICmd(ENABLE);  
  15.   while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);  
  16.     
  17.   RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);  
  18.     
  19.   RCC_HSEConfig(RCC_HSE_OFF);  
  20.   RCC_LSEConfig(RCC_LSE_OFF);  
  21.   RCC_PLLConfig(RCC_PLLSource_HSI_Div2,RCC_PLLMul_9); //  72HMz  
  22.   RCC_PLLCmd(ENABLE);  
  23.   while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);  
  24.   RCC_ADCCLKConfig(RCC_PCLK2_Div4);  
  25.   RCC_PCLK2Config(RCC_HCLK_Div1);  
  26.   RCC_PCLK1Config(RCC_HCLK_Div2);  
  27.   RCC_HCLKConfig(RCC_SYSCLK_Div1);  
  28.   RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);  
  29.   while(RCC_GetSYSCLKSource() != 0x08);  
  30. */    
  31.     SystemInit();    
  32.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);    
  33.  GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG    
  34. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);    
  35.  GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG    
  36.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;    
  37.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    
  38.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;    
  39.   GPIO_Init(GPIOD, &GPIO_InitStructure);    
  40.     GPIO_ResetBits(GPIOD,GPIO_Pin_2);    
  41.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);    
  42.  GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG    
  43.   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;    
  44.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    
  45.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;    
  46.   GPIO_Init(GPIOC, &GPIO_InitStructure);    
  47.     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);    
  48.       RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);    
  49. }     
  50.     
  51. void USART_int(long BaudRate)    
  52. {    
  53.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);    
  54.        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;    
  55.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;    
  56.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;     
  57.     GPIO_Init(GPIOA, &GPIO_InitStructure);    
  58.     /* PA10 USART1_Rx  */    
  59.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;    
  60.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;    
  61.     GPIO_Init(GPIOA, &GPIO_InitStructure);    
  62.   /* USARTx configured as follow:  
  63.         - BaudRate = 115200 baud    
  64.         - Word Length = 8 Bits  
  65.         - One Stop Bit  
  66.         - No parity  
  67.         - Hardware flow control disabled (RTS and CTS signals)  
  68.         - Receive and transmit enabled  
  69.   */    
  70.   USART_InitStructure.USART_BaudRate = BaudRate;   
  71.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;   
  72.   USART_InitStructure.USART_StopBits = USART_StopBits_1;    
  73.   USART_InitStructure.USART_Parity = USART_Parity_No;    
  74.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;    
  75.     USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;         
  76.     USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;          
  77.     USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;          
  78.     USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;    
  79.     USART_ClockInit(USART1, &USART_ClockInitStructure);    
  80.   USART_Init(USART1, &USART_InitStructure);    
  81.   USART_Cmd(USART1, ENABLE);    
  82.     USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);    
  83.  USART_Cmd(USART1, ENABLE);    
  84. }    
  85. void delay_us(u32 n)    
  86. {    
  87.     u8 j;    
  88.     while(n--)    
  89.     for(j=0;j<10;j++);    
  90. }    
  91. void  delay_ms(u32 n)    
  92. {    
  93.     while(n--)    
  94.     delay_us(1000);    
  95. }    
  96. void PWM_Config()    
  97. {    
  98.     uint16_t PrescalerValue = 0;    
  99.     TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;    
  100.   TIM_OCInitTypeDef  TIM_OCInitStructure;    
  101.     /* TIM2 clock enable */    
  102.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);    
  103.   /* GPIOA  enable */    
  104.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);    
  105.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;//PWM&RGB- PA1 PA2 PA3    
  106.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;    
  107.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    
  108.   GPIO_Init(GPIOA, &GPIO_InitStructure);    
  109.         TIM_Cmd(TIM2, ENABLE);    
  110.       /* Compute the prescaler value */    
  111.   PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;    
  112.   /* Time base configuration */    
  113.   TIM_TimeBaseStructure.TIM_Period = 0x07FF;    
  114.   TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;    
  115.   TIM_TimeBaseStructure.TIM_ClockDivision = 0;    
  116.   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;    
  117.   TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);    
  118.   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;    
  119.   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;    
  120.   /* PWM1 Mode configuration: Channel2 ,PA1在通道2*/    
  121.   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;    
  122.   TIM_OCInitStructure.TIM_Pulse = 0xFFFF;    
  123.   TIM_OC2Init(TIM2, &TIM_OCInitStructure);    
  124.     /* PWM1 Mode configuration: Channel3 PA2在通道3*/    
  125.   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;    
  126.   TIM_OCInitStructure.TIM_Pulse = 0xFFFF;    
  127.   TIM_OC3Init(TIM2, &TIM_OCInitStructure);    
  128.     /* PWM1 Mode configuration: Channel4 PA3在通道4*/    
  129.   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;    
  130.   TIM_OCInitStructure.TIM_Pulse = 0xFFFF;    
  131.   TIM_OC4Init(TIM2, &TIM_OCInitStructure);    
  132.   TIM_ARRPreloadConfig(TIM2, ENABLE);    
  133. }    
  134. void ADC_CONFIG(){    
  135.     ADC_InitTypeDef ADC_InitStructure;    
  136.     #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)    
  137.   /* ADCCLK = PCLK2/2 */    
  138.   RCC_ADCCLKConfig(RCC_PCLK2_Div2);     
  139. #else    
  140.   /* ADCCLK = PCLK2/4 */    
  141.   RCC_ADCCLKConfig(RCC_PCLK2_Div4);     
  142. #endif    
  143. ADC_DeInit(ADC1);    
  144.   /* Enable ADC1 and GPIOC clock */    
  145.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOB, ENABLE);    
  146.     /* Configure PB0 (ADC Channel14) as analog input -------------------------*/    
  147.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//ADC所在端口PB0    
  148.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//模拟输入模式    
  149.   GPIO_Init(GPIOB, &GPIO_InitStructure);    
  150.   /* ADC1 configuration ------------------------------------------------------*/    
  151.   ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;    
  152.   ADC_InitStructure.ADC_ScanConvMode = ENABLE;    
  153.   ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;    
  154.   ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;    
  155.   ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//ADC数据右对齐    
  156.   ADC_InitStructure.ADC_NbrOfChannel = 1;//ADC通道数为1    
  157.   ADC_Init(ADC1, &ADC_InitStructure);//初始化ADC1    
  158.   /* Enable ADC1 DMA */    
  159.   ADC_DMACmd(ADC1, ENABLE);    
  160.   /* Enable ADC1 */    
  161.   ADC_Cmd(ADC1, ENABLE);    
  162. }    
  163.     
  164. int Get_ADC(){    
  165.      /* ADC1 regular channel configuration */     
  166.   ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_55Cycles5);//通道:8 ,采样时间    
  167.     /* Enable ADC1 reset calibration register */       
  168.   ADC_ResetCalibration(ADC1);//重置ADC1的校准寄存器    
  169.   /* Check the end of ADC1 reset calibration register */    
  170.   while(ADC_GetResetCalibrationStatus(ADC1));//确认重置完毕    
  171.   /* Start ADC1 calibration */    
  172.   ADC_StartCalibration(ADC1);    
  173.   /* Check the end of ADC1 calibration */    
  174.   while(ADC_GetCalibrationStatus(ADC1));    
  175.   /* Start ADC1 Software Conversion */     
  176.   ADC_SoftwareStartConvCmd(ADC1, ENABLE);  
  177.     return ADC_GetConversionValue(ADC1);    
  178. }    
  179. void PWM_TEST()    
  180. {    
  181.      
  182.     unsigned int temp0=volt,temp1=0,temp2=volt;    
  183.     printf("PWM-RGB TEST......\r\n");    
  184.    for(;(temp0>0)||(temp1<volt);temp0--,temp1++)     
  185.    {    
  186.        TIM_SetCompare2(TIM2, temp0);//temp0:volt~0    
  187.      TIM_SetCompare3(TIM2, temp1);//temp1:0~volt    
  188.        delay_us(1000);    
  189.    }    
  190.      for(;(temp0<volt)||(temp2>0);temp0++,temp2--)    
  191.      {    
  192.        TIM_SetCompare2(TIM2, temp0);//temp0:0~volt    
  193.        TIM_SetCompare4(TIM2, temp2);//temp2:volt~0    
  194.        delay_us(1000);    
  195.    }    
  196.    for(;(temp1>0)||(temp2<volt);temp1--,temp2++)  
  197.    {    
  198.        TIM_SetCompare4(TIM2, temp2);//temp2:0~volt    
  199.        TIM_SetCompare3(TIM2, temp1);//temp1:volt~0    
  200.        delay_us(1000);    
  201.    }    
  202. }    
  203. int main(void)    
  204. {    
  205.   float Volt=0.00;    
  206.     int ADValue = 0;    
  207.   RCC_Configuration();    
  208.   USART_int(115200);    
  209.     ADC_CONFIG();    
  210.     Get_ADC();    
  211.     PWM_Config();    
  212.     delay_ms(1000);    
  213.     printf(" config done...\r\n");    
  214.     while(1)    
  215.     {    
  216.         ADValue = Get_ADC();    
  217.         Volt = VREF*ADValue/4095;    
  218.         volt=Volt*1000;    
  219.         printf("===============================\r\n");    
  220.         printf("The ADC value is:%d\r\n",ADValue);    
  221.         printf("The Volt is:%f V\r\n",Volt);    
  222.         printf("The volt is:%d \r\n",volt);    
  223.         PWM_TEST();    
  224.         delay_ms(500);    
  225.     }    
  226. }    
  227.     
  228. #ifdef  USE_FULL_ASSERT    
  229.     
  230. void assert_failed(uint8_t* file, uint32_t line)    
  231. {     
  232.   while (1)    
  233.   {    
  234.   }    
  235. }    
  236.     
  237. #endif    
  238.     
  239. #ifdef __GNUC__    
  240.   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)    
  241. #else    
  242.   #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)    
  243. #endif /* __GNUC__ */    
  244.       
  245. PUTCHAR_PROTOTYPE    
  246. {    
  247.     
  248.   USART_SendData(EVAL_COM1, (uint8_t) ch);    
  249.   while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)    
  250.   {}    
  251.     
  252.   return ch;    
  253. }    
  254.     
  255. #ifdef  USE_FULL_ASSERT    
  256.     
  257. void assert_failed(uint8_t* file, uint32_t line)    
  258. {     
  259.   while (1)    
  260.   {    
  261.   }    
  262. }    
  263. #endif    
#include "stm32f10x.h"  
#include "stm32_eval.h"  
#include <stdio.h>  
#define VREF 3.3  
GPIO_InitTypeDef   GPIO_InitStructure;  
USART_InitTypeDef USART_InitStructure;  
USART_ClockInitTypeDef USART_ClockInitStructure;  
int volt;  
unsigned int temp0,temp1,temp2;  
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; 
  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 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 PWM_Config()  
{  
    uint16_t PrescalerValue = 0;  
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;  
  TIM_OCInitTypeDef  TIM_OCInitStructure;  
    /* TIM2 clock enable */  
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);  
  /* GPIOA  enable */  
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);  
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;//PWM&RGB- PA1 PA2 PA3  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  GPIO_Init(GPIOA, &GPIO_InitStructure);  
        TIM_Cmd(TIM2, ENABLE);  
      /* Compute the prescaler value */  
  PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;  
  /* Time base configuration */  
  TIM_TimeBaseStructure.TIM_Period = 0x07FF;  
  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;  
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;  
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);  
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;  
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;  
  /* PWM1 Mode configuration: Channel2 ,PA1在通道2*/  
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  
  TIM_OCInitStructure.TIM_Pulse = 0xFFFF;  
  TIM_OC2Init(TIM2, &TIM_OCInitStructure);  
    /* PWM1 Mode configuration: Channel3 PA2在通道3*/  
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  
  TIM_OCInitStructure.TIM_Pulse = 0xFFFF;  
  TIM_OC3Init(TIM2, &TIM_OCInitStructure);  
    /* PWM1 Mode configuration: Channel4 PA3在通道4*/  
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  
  TIM_OCInitStructure.TIM_Pulse = 0xFFFF;  
  TIM_OC4Init(TIM2, &TIM_OCInitStructure);  
  TIM_ARRPreloadConfig(TIM2, ENABLE);  
}  
void ADC_CONFIG(){  
    ADC_InitTypeDef ADC_InitStructure;  
    #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)  
  /* ADCCLK = PCLK2/2 */  
  RCC_ADCCLKConfig(RCC_PCLK2_Div2);   
#else  
  /* ADCCLK = PCLK2/4 */  
  RCC_ADCCLKConfig(RCC_PCLK2_Div4);   
#endif  
ADC_DeInit(ADC1);  
  /* Enable ADC1 and GPIOC clock */  
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOB, ENABLE);  
    /* Configure PB0 (ADC Channel14) as analog input -------------------------*/  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//ADC所在端口PB0  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//模拟输入模式  
  GPIO_Init(GPIOB, &GPIO_InitStructure);  
  /* ADC1 configuration ------------------------------------------------------*/  
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;  
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;  
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;  
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;  
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//ADC数据右对齐  
  ADC_InitStructure.ADC_NbrOfChannel = 1;//ADC通道数为1  
  ADC_Init(ADC1, &ADC_InitStructure);//初始化ADC1  
  /* Enable ADC1 DMA */  
  ADC_DMACmd(ADC1, ENABLE);  
  /* Enable ADC1 */  
  ADC_Cmd(ADC1, ENABLE);  
}  
  
int Get_ADC(){  
     /* ADC1 regular channel configuration */   
  ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_55Cycles5);//通道:8 ,采样时间  
    /* Enable ADC1 reset calibration register */     
  ADC_ResetCalibration(ADC1);//重置ADC1的校准寄存器  
  /* Check the end of ADC1 reset calibration register */  
  while(ADC_GetResetCalibrationStatus(ADC1));//确认重置完毕  
  /* Start ADC1 calibration */  
  ADC_StartCalibration(ADC1);  
  /* Check the end of ADC1 calibration */  
  while(ADC_GetCalibrationStatus(ADC1));  
  /* Start ADC1 Software Conversion */   
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);
    return ADC_GetConversionValue(ADC1);  
}  
void PWM_TEST()  
{  
   
    unsigned int temp0=volt,temp1=0,temp2=volt;  
    printf("PWM-RGB TEST......\r\n");  
   for(;(temp0>0)||(temp1<volt);temp0--,temp1++)   
   {  
       TIM_SetCompare2(TIM2, temp0);//temp0:volt~0  
     TIM_SetCompare3(TIM2, temp1);//temp1:0~volt  
       delay_us(1000);  
   }  
     for(;(temp0<volt)||(temp2>0);temp0++,temp2--)  
     {  
       TIM_SetCompare2(TIM2, temp0);//temp0:0~volt  
       TIM_SetCompare4(TIM2, temp2);//temp2:volt~0  
       delay_us(1000);  
   }  
   for(;(temp1>0)||(temp2<volt);temp1--,temp2++)
   {  
       TIM_SetCompare4(TIM2, temp2);//temp2:0~volt  
       TIM_SetCompare3(TIM2, temp1);//temp1:volt~0  
       delay_us(1000);  
   }  
}  
int main(void)  
{  
  float Volt=0.00;  
    int ADValue = 0;  
  RCC_Configuration();  
  USART_int(115200);  
    ADC_CONFIG();  
    Get_ADC();  
    PWM_Config();  
    delay_ms(1000);  
    printf(" config done...\r\n");  
    while(1)  
    {  
        ADValue = Get_ADC();  
        Volt = VREF*ADValue/4095;  
        volt=Volt*1000;  
        printf("===============================\r\n");  
        printf("The ADC value is:%d\r\n",ADValue);  
        printf("The Volt is:%f V\r\n",Volt);  
        printf("The volt is:%d \r\n",volt);  
        PWM_TEST();  
        delay_ms(500);  
    }  
}  
  
#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  

 


菜鸟
2016-01-29 20:51:45     打赏
14楼

课堂上老师已经给了我们DS18B20使用的代码,即当用手触摸温度传感器,串口助手那里就可以读出当前温度,所以将这个代码与之前LED灯颜色改变的代码整合起来


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;
  292. }



共14条 2/2 1 2 跳转至

回复

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