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

共9条 1/1 1 跳转至

【1.11更新】37号STM32的学习进程

菜鸟
2015-11-09 18:10:53     打赏

我的作业

第一次作业:http://forum.eepw.com.cn/thread/277609/1#2(软件安装)

第二次作业:http://forum.eepw.com.cn/thread/277609/1#3(流水灯)

第三次作业:http://forum.eepw.com.cn/thread/277609/1#4(数码管显示0-99)

第四次作业:http://forum.eepw.com.cn/thread/277609/1#6(单号:串口控制led)

第五次作业:http://forum.eepw.com.cn/thread/277609/1#7(用一个定时器制作跑马灯)

第六次作业:http://forum.eepw.com.cn/thread/277609/1#8(ADC改变灯的颜色)

二选一题目:http://forum.eepw.com.cn/thread/277609/1#9(触摸DS18B02改变灯的颜色)



菜鸟
2015-11-09 18:21:52     打赏
2楼

MDK的安装

首先,先找到安装包的文件夹,双击红色圈里的mdk472_a.exe,开始安装

打开mdk472_a.exe,就出现下面的界面,点击next

勾上I agree to all the terms of the ……,然后再点击next

选择安装目录,把它放到E盘MDK文件夹下,再点击next

把First Name和E-mail填上

接下来就是等待安装了,装好后会跳出一个界面,点击next

框框里有两个勾不用选,点击finsh

装好后点击桌面的快捷方式打开软件,点击File/License Management就出现下面这个界面


接下来需要打开和谐文件和谐一下,然后把License Management界面右边的CID复制到和谐文件的CID里,然后Target选择的是ARM,Generate多点击几次,把里面的内容复制到License Management里New License ID Code(LIC)里,点击Add




最后就关闭这个软件再重新打开就可以用了




菜鸟
2015-11-09 18:30:48     打赏
3楼

流水灯:

#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 GPIO_INIT()    
{    
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOA, 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 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_Configuration();
GPIO_INIT(); 
  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);  
  
while(1)
{
  GPIO_ResetBits(GPIOC,GPIO_Pin_0);
  delay_ms(150);
  GPIO_SetBits(GPIOC,GPIO_Pin_0);
  delay_ms(150);
  GPIO_ResetBits(GPIOC,GPIO_Pin_1);
  delay_ms(150);
  GPIO_SetBits(GPIOC,GPIO_Pin_1);
  delay_ms(150);
  GPIO_ResetBits(GPIOC,GPIO_Pin_2);
  delay_ms(150);
  GPIO_SetBits(GPIOC,GPIO_Pin_2);
  delay_ms(150);
  GPIO_ResetBits(GPIOC,GPIO_Pin_3);
  delay_ms(150);
  GPIO_SetBits(GPIOC,GPIO_Pin_3);
  delay_ms(150);
  GPIO_ResetBits(GPIOC,GPIO_Pin_4);
  delay_ms(150);
  GPIO_SetBits(GPIOC,GPIO_Pin_4);
  delay_ms(150);
  GPIO_ResetBits(GPIOC,GPIO_Pin_5);
  delay_ms(150);
  GPIO_SetBits(GPIOC,GPIO_Pin_5);
  delay_ms(150);
  GPIO_ResetBits(GPIOC,GPIO_Pin_6);
  delay_ms(150);
  GPIO_SetBits(GPIOC,GPIO_Pin_6);
  delay_ms(150);
  GPIO_ResetBits(GPIOC,GPIO_Pin_7);
  delay_ms(150);
  GPIO_SetBits(GPIOC,GPIO_Pin_7);
  delay_ms(150);

}
}

没有拍视频,就只能传一张相片了


菜鸟
2015-11-13 13:54:57     打赏
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()     
{     
   //GPIO数码管初始化  
   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 j;  
    while(n--)  
    for(j=0;j<10;j++);  
}  
  
void  delay_ms(u32 n)  
{  
    while(n--)  
    delay_us(1000);  
}  
  void function(void)  
{  
    int a[10],i=0;//数组保存0-9数码管组合
    unsigned char flag=0; 
    a[0]=GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14;  
    a[1]=GPIO_Pin_9|GPIO_Pin_12;  
    a[2]=GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_13|GPIO_Pin_14;  
    a[3]=GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_14;  
    a[4]=GPIO_Pin_5|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12;  
    a[5]=GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_12|GPIO_Pin_14;  
    a[6]=GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14;  
    a[7]=GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_12;  
    a[8]=GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14;  
    a[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,i<=99;) 
                {  
                    if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8))  
                    {  
                        delay_ms(50);         
                        if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8)){i++;}  
                    }  
                    if(i>99){i=0;}  
                    GPIO_ResetBits(GPIOB,GPIO_Pin_15);//PB15 off  
                    GPIO_SetBits(GPIOB,GPIO_Pin_1);//PB1 on  
                    GPIO_ResetBits(GPIOB,a[i%10]); 
                    GPIO_SetBits(GPIOB,a[i%10]);    
                    GPIO_ResetBits(GPIOB,GPIO_Pin_1);//PB1 off  
                    GPIO_SetBits(GPIOB,GPIO_Pin_15);//PB15 on  
                    GPIO_ResetBits(GPIOB,a[i/10]); 
                    GPIO_SetBits(GPIOB,a[i/10]);   
                }  
            }  
        }  
    }  
}  

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)  
{   
    RCC_Configuration();  
    GPIO_INIT();  
    function();  
}  


菜鸟
2015-11-20 17:22:07     打赏
5楼
#include "stm32f10x.h"    
#include "stm32_eval.h"    
#include    "stdio.h"    
#include "math.h"    
#define buff_size  16;         
char rx_buff[], rx_buff_count=0;    
    
GPIO_InitTypeDef  GPIO_InitStructure;    
USART_InitTypeDef  USART_InitStructure;    
NVIC_InitTypeDef  NVIC_InitStructure;    
USART_ClockInitTypeDef  USART_ClockInitStructure;    
    
void RCC_Configuration(void)    
{    
    RCC_DeInit();    
    RCC_HSICmd(ENABLE);    
    while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);    
    RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);    
    RCC_HSEConfig(RCC_HSE_OFF);    
    RCC_LSEConfig(RCC_LSE_OFF);    
    RCC_PLLConfig(RCC_PLLSource_HSI_Div2,RCC_PLLMul_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 GPIO_INIT()    
{    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);//ʹÄÜPCʱÖÓ    
    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;//PC0-PC7    
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//ÍÆÍìÊä³ö    
    GPIO_Init(GPIOC, &GPIO_InitStructure);//³õʼ»¯PC    
}   
/*delay_us*/     
void delay_us(u32 n)    
{    
    u8 j;    
    while(n--)    
    for(j=0;j<10;j++);    
}    
/*delay_ms*/     
void  delay_ms(u32 n)    
{    
    while(n--)    
    delay_us(1000);    
}    
    

 void USART_SendStr(char *str)  
{    
   while((*str)!='\0') 
    {       
        USART_SendData(USART1,*str++);    
        while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);    
    }    
}       
void USART_int(long BaudRate)    
{    
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);//ʹÄÜPA USART1    
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;//TXλÓÚPA9     
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;    
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //ÍÆÍìÊä³ö    
   GPIO_Init(GPIOA, &GPIO_InitStructure);//TX³õʼ»¯ PA9     
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//RXλÓÚPA10     
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//Ðü¿ÕÊäÈë   
   GPIO_Init(GPIOA, &GPIO_InitStructure);//RX³õʼ»¯PA10     
  /* 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;//×Ö³¤8±ÈÌØ   
    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);//³õʼ»¯USART1ʱÖÓ   
    USART_Init(USART1, &USART_InitStructure);//³õʼ»¯USART1     
    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);//    
}    
        
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 func(char *S,char LEN)
{   
   
    unsigned int sum,i,j,k;    
    if((LEN!=6)&(LEN!=7)&(LEN!=8)&(LEN!=9))//data=1~999    
    {USART_SendStr("\r\n Erro input!!!\r\n");}// Èç¹ûÊäÈë¸ñʽ´íÎ󣬷¢ËÍÌáʾ   
    else     
    {    
         
        sum = translate(S);//   
        GPIO_SetBits(GPIOC,0x000000ff);//all LED off     
        for(k=0;k<3;k++)//  
        {    
            i=0x00000100;    
            for(j=1;j<=8;j++)//LED D8-D1    
            {    
                i>>=1;    
                GPIO_ResetBits(GPIOC,i);// µãÁÁËùÓеÄLEDµÆ  
                delay_ms(50);    
                GPIO_SetBits(GPIOC,i);//ϨÃðËùÓеÄLEDµÆ    
                delay_ms(sum);//  
            }    
        }    
    }    
}    
void input_ASK()  
{    
    char j;    
    func(rx_buff,rx_buff_count);    
    rx_buff_count=0;    
    for (j=0;j<rx_buff_count;j++)    
    {rx_buff[j]='\0';}//    
    USART_SendStr("\n>");    
}    
    
int main(void)    
{     
    RCC_Configuration();    
    GPIO_INIT();    
    USART_int(9600);    
    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);  
    delay_ms(200);    
    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); 
    USART_SendStr("SyStem booting......\r\n");     
    USART_SendStr("\n>");//     
    while(1)    
        {}    
}    
    
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-20 17:36:38     打赏
6楼
#include "stm32f10x.h"    
#include "stm32_eval.h"    
#include    "stdio.h"    
#include "math.h"    
#define buff_size  16;         
char rx_buff[], rx_buff_count=0;    
    
GPIO_InitTypeDef  GPIO_InitStructure;    
USART_InitTypeDef  USART_InitStructure;    
NVIC_InitTypeDef  NVIC_InitStructure;    
USART_ClockInitTypeDef  USART_ClockInitStructure;    
    
void RCC_Configuration(void)    
{    
    RCC_DeInit();    
    RCC_HSICmd(ENABLE);    
    while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);    
    RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);    
    RCC_HSEConfig(RCC_HSE_OFF);    
    RCC_LSEConfig(RCC_LSE_OFF);    
    RCC_PLLConfig(RCC_PLLSource_HSI_Div2,RCC_PLLMul_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 GPIO_INIT()    
{    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);//使能PC时钟   
    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;//PC0-PC7    
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出   
    GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化PC   
}   
/*delay_us*/     
void delay_us(u32 n)    
{    
    u8 j;    
    while(n--)    
    for(j=0;j<10;j++);    
}    
/*delay_ms*/     
void  delay_ms(u32 n)    
{    
    while(n--)    
    delay_us(1000);    
}    
    

 void USART_SendStr(char *str)  
{    
   while((*str)!='\0') 
    {       
        USART_SendData(USART1,*str++);    
        while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);    
    }    
}       
void USART_int(long BaudRate)    
{    
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);//使能PA USART1    
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;//TX位于PA9     
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;    
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽输出   
   GPIO_Init(GPIOA, &GPIO_InitStructure);//TX初始化 PA9     
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//RX位于PA10     
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//悬空输入   
   GPIO_Init(GPIOA, &GPIO_InitStructure);//RX初始化PA10     
  /* 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;//字长为8比特   
    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);//初始化USART1时钟 
    USART_Init(USART1, &USART_InitStructure);//初始化USART1     
    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);//    
}    
        
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 func(char *S,char LEN)
{   
   
    unsigned int sum,i,j,k;    
    if((LEN!=6)&(LEN!=7)&(LEN!=8)&(LEN!=9))//data=1~999    
    {USART_SendStr("\r\n Erro input!!!\r\n");}// 如果输入格式错误,发送提示  
    else     
    {    
         
        sum = translate(S);//   
        GPIO_SetBits(GPIOC,0x000000ff);//all LED off     
        for(k=0;k<3;k++)//  
        {    
            i=0x00000100;    
            for(j=1;j<=8;j++)//LED D8-D1    
            {    
                i>>=1;    
                GPIO_ResetBits(GPIOC,i);// 点亮所有的LED灯 
                delay_ms(50);    
                GPIO_SetBits(GPIOC,i);//熄灭所有的LED灯   
                delay_ms(sum);//  
            }    
        }    
    }    
}    
void input_ASK()  
{    
    char j;    
    func(rx_buff,rx_buff_count);    
    rx_buff_count=0;    
    for (j=0;j<rx_buff_count;j++)    
    {rx_buff[j]='\0';}  
    USART_SendStr("\n>");    
}    
    
int main(void)    
{     
    RCC_Configuration();    
    GPIO_INIT();    
    USART_int(9600);    
    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);  
    delay_ms(200);    
    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); 
    USART_SendStr("SyStem booting......\r\n");     
    USART_SendStr("\n>");    
    while(1)    
        {}    
}    
    
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-29 17:00:33     打赏
7楼
#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;
int count=0;

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) 
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update );
//	flag = ~flag;	
//	if(flag){
//	GPIO_SetBits(GPIOC,GPIO_Pin_1);}
//	else {GPIO_ResetBits(GPIOC,GPIO_Pin_1);}
/*flag++;
	if(flag == 50)
		{led = ~led;
		 flag = 0;}
	if(led){
	GPIO_SetBits(GPIOC,GPIO_Pin_1);}
	else {GPIO_ResetBits(GPIOC,GPIO_Pin_1);}*/
	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

/**
  * @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-11 20:25:31     打赏
8楼
#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-22 21:55:22     打赏
9楼
#include "stdio.h"  
#include "stm32f10x.h"  
int fputc(int ch,FILE *f);  
  
/********************RCC配置*********************/  
void RCC_Configuration(void)      
{  
    
    GPIO_InitTypeDef  GPIO_InitStructure;  
    ErrorStatus HSEStartUpStatus;  
    RCC_DeInit();          
    RCC_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输入时钟源8x9=72M  
        RCC_PLLCmd(ENABLE);//*打开PLL:            
        while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);//*等待PLL工作:  
        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);  
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);  
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;       //#管脚号  关闭蜂鸣器  
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;       //#输出速度   
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;        //#输入输出模式(推挽输出)     
    GPIO_Init(GPIOD, &GPIO_InitStructure);       //#初始化   
    GPIO_ResetBits(GPIOD,GPIO_Pin_2);  
      
}     
  
void delay_us(u32 n)        //延时函数     
{     
    u8 j;     
    while(n--)     
            for(j=0;j<10;j++);     
}      
void  delay_ms(u32 n)       //延时函数     
{     
   while(n--)     
   delay_us(1000);     
}  
  
  
  
/*****************************GPIO初始********************************/  
void GPIO_Inits(void)  
{   /*结构体初始化及开启外设时钟*/  
        GPIO_InitTypeDef  GPIO_InitStructure;  
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);  
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);  
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);  
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);  
        /*GPIOA--RGB初始化*/  
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;  
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;  
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;   
        GPIO_Init(GPIOA,&GPIO_InitStructure);  
        GPIO_SetBits(GPIOA,GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3)      
          
}  
  
/**************************DS18B20**********************/  
  
uint8_t DS18B20_Init(void)  
{  
    GPIO_InitTypeDef GPIO_InitStructure;  
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   
  GPIO_Init(GPIOC, &GPIO_InitStructure);  
    Set_B20();  
  
    DS18B20_Rst();  
    return DS18B20_Presence ();  
}  
  
static void DS18B20_Mode_IPU(void)  
{  
      GPIO_InitTypeDef GPIO_InitStructure;  
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;  
  GPIO_Init(GPIOC, &GPIO_InitStructure);  
}  
  
static void DS18B20_Mode_Out_PP(void)  
{  
    GPIO_InitTypeDef GPIO_InitStructure;  
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   
  GPIO_Init(GPIOC, &GPIO_InitStructure);  
}  
  
  
  
static void DS18B20_Rst(void)  
{  
    /* 主机设置为输出 */  
    DS18B20_Mode_Out_PP();  
    Reset_B20() ;  
    delay_us(700);   
    Set_B20()   ;  
    delay_us(15);  
}  
  
  
  
static uint8_t DS18B20_Presence(void)  
{  
    uint8_t pulse_time = 0;  
      
    /* 主机设置为上拉输入 */  
    DS18B20_Mode_IPU();  
      
   
    while( Read_B20() && pulse_time<100 )  
    {  
        pulse_time++;  
        delay_us(1);  
    }     
    /* 经过100us后,存在脉冲都还没有到来*/  
    if( pulse_time >=100 )  
        return 1;  
    else  
        pulse_time = 0;  
      
    /* 存在脉冲到来,且存在的时间不能超过240us */  
    while( !Read_B20() && pulse_time<240 )  
    {  
        pulse_time++;  
        delay_us(1);  
    }     
    if( pulse_time >=240 )  
        return 1;  
    else  
        return 0;  
}  
  
  
static uint8_t DS18B20_ReadBit(void)  
{  
    uint8_t dat;  
      
    /* 读0和读1的时间至少要大于60us */   
    DS18B20_Mode_Out_PP();  
    Reset_B20();  
    delay_us(10);  
    DS18B20_Mode_IPU();  
    //Delay_us(2);  
      
    if( Read_B20() == SET )  
        dat = 1;  
    else  
        dat = 0;  
       
    delay_us(45);  
      
    return dat;  
}  
  
  
static uint8_t DS18B20_ReadByte(void)  
{  
    uint8_t i, j, dat = 0;    
      
    for(i=0; i<8; i++)   
    {  
        j = DS18B20_ReadBit();        
        dat = (dat) | (j<<i);  
    }  
      
    return dat;  
}  
  
  
  
static void DS18B20_WriteByte(uint8_t dat)  
{  
    uint8_t i, testb;  
    DS18B20_Mode_Out_PP();  
      
    for( i=0; i<8; i++ )  
    {  
        testb = dat&0x01;  
        dat = dat>>1;       
        /* 写0和写1的时间至少要大于60us */  
        if (testb)  
        {             
            Reset_B20();  
            /* 1us < 这个延时 < 15us */  
            delay_us(8);  
              
            Set_B20();  
            delay_us(58);  
        }         
        else  
        {             
            Reset_B20();  
            /* 60us < Tx 0 < 120us */  
            delay_us(70);  
              
            Set_B20();            
            delay_us(2);  
        }  
    }  
}  
  
  
  
static void DS18B20_SkipRom ( void )  
{  
    DS18B20_Rst();       
      
    DS18B20_Presence();    
      
    DS18B20_WriteByte(0XCC);        /* 跳过 ROM */  
      
}  
  
  
static void DS18B20_MatchRom ( void )  
{  
    DS18B20_Rst();       
      
    DS18B20_Presence();    
      
    DS18B20_WriteByte(0X55);        /* 匹配 ROM */  
      
}  
  
float DS18B20_GetTemp_SkipRom ( void )  
{  
    uint8_t tpmsb, tplsb;  
    short s_tem;  
    float f_tem;  
      
      
    DS18B20_SkipRom ();  
    DS18B20_WriteByte(0X44);                /* 开始转换 */  
      
      
    DS18B20_SkipRom ();  
  DS18B20_WriteByte(0XBE);              /* 读温度值 */  
      
    tplsb = DS18B20_ReadByte();        
    tpmsb = DS18B20_ReadByte();   
      
      
    s_tem = tpmsb<<8;  
    s_tem = s_tem | tplsb;  
      
    if( s_tem < 0 )      /* 负温度 */  
        f_tem = (~s_tem+1) * 0.0625;      
    else  
        f_tem = s_tem * 0.0625;  
      
    return f_tem;     
      
      
}  
  
  
  
void DS18B20_ReadId ( uint8_t * ds18b20_id )  
{  
    uint8_t uc;  
      
      
    DS18B20_WriteByte(0x33);       //读取序列号  
      
    for ( uc = 0; uc < 8; uc ++ )  
      ds18b20_id [ uc ] = DS18B20_ReadByte();  
      
}  
  
  
float DS18B20_GetTemp_MatchRom ( uint8_t * ds18b20_id )  
{  
    uint8_t tpmsb, tplsb, i;  
    short s_tem;  
    float f_tem;  
      
      
    DS18B20_MatchRom ();            //匹配ROM  
      
  for(i=0;i<8;i++)  
        DS18B20_WriteByte ( ds18b20_id [ i ] );   
      
    DS18B20_WriteByte(0X44);                /* 开始转换 */  
  
      
    DS18B20_MatchRom ();            //匹配ROM  
      
    for(i=0;i<8;i++)  
        DS18B20_WriteByte ( ds18b20_id [ i ] );   
      
    DS18B20_WriteByte(0XBE);                /* 读温度值 */  
      
    tplsb = DS18B20_ReadByte();        
    tpmsb = DS18B20_ReadByte();   
      
      
    s_tem = tpmsb<<8;  
    s_tem = s_tem | tplsb;  
      
    if( s_tem < 0 )      /* 负温度 */  
        f_tem = (~s_tem+1) * 0.0625;      
    else  
        f_tem = s_tem * 0.0625;  
      
    return f_tem;     
      
      
}  
  
  
  
  
  
int main()  
{    
uint8_t uc, ucDs18b20Id [ 8 ];  
RCC_Configuration();  
GPIO_Inits();  
USART_int(9600);  
while( DS18B20_Init() )   
printf("\r\n no ds18b20 exit \r\n");  
  
printf("\r\n Yes ds18b20 \r\n");  
  
DS18B20_ReadId ( ucDs18b20Id  );           // 读取 DS18B20 的序列号  
  
printf("\r\nDS18B20的序列号是: 0x");  
  
for ( uc = 0; uc < 8; uc ++ )             // 打印 DS18B20 的序列号  
printf ( "%.2x", ucDs18b20Id [ uc ] );  
  
  
for(;;)  
{     
static unsigned int i=0;  
float a,b;  
if(i==10)  
{  
printf ( "\r\n第10秒:温度: %.1f\r\n", DS18B20_GetTemp_MatchRom ( ucDs18b20Id ) );       // 打印通过 DS18B20 序列号获取的温度值           
a=DS18B20_GetTemp_MatchRom ( ucDs18b20Id );  
delay_ms(1000);     /* 1s 读取一次温度值 */  
i++;  
}  
else{}  
if(i<10)  
{  
printf ( "\r\n温度: %.1f\r\n", DS18B20_GetTemp_MatchRom ( ucDs18b20Id ) );        // 打印通过 DS18B20 序列号获取的温度值           
delay_ms(1000);     /* 1s 读取一次温度值 */  
i++;  
}  
if(i>10)  
{  
printf ( "\r\n10秒后:温度: %.1f\r\n", DS18B20_GetTemp_MatchRom ( ucDs18b20Id ) );       // 打印通过 DS18B20 序列号获取的温度值           
b=DS18B20_GetTemp_MatchRom ( ucDs18b20Id );  
delay_ms(1000);     /* 1s 读取一次温度值 */  
i++;  
if((b-a)>0.9)  
{GPIO_ResetBits(GPIOA,GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);  
  
}  
else{}  
if((b-a)<0.9)  
{  
        GPIO_SetBits(GPIOA,GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);  
  
  
}  
else{}  
  
}  
else{}  
  
}     
}  
int fputc(int ch,FILE *f)  
{  
USART_SendData(USART1, (uint8_t) ch);  
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)  
{}  
return ch;  
}  

 


共9条 1/1 1 跳转至

回复

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