这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 高校专区 » 漓东e学堂 » 13号同学【作业这个事儿】

共13条 1/2 1 2 跳转至

13号同学【作业这个事儿】

菜鸟
2014-10-21 22:06:19     打赏

【作业这个事儿】



2#   【keil 软件安装破解过程】http://forum.eepw.com.cn/thread/262550/1/#2

3#   【数码管按键切换扫描】http://forum.eepw.com.cn/thread/262550/1/#3

6#   【串口读0-99数据,数码管显示数据读到的0-99数据】http://forum.eepw.com.cn/thread/262550/1/#6

8#   【LED熄灭】http://forum.eepw.com.cn/thread/262550/1/#8

9#   【rgb呼吸灯】http://forum.eepw.com.cn/thread/262550/1/#9

       【时钟滴答48MHz&&读取18b20  ID地址http://forum.eepw.com.cn/thread/262550/1/#10

       【 IIC 按键次数存储http://forum.eepw.com.cn/thread/262550/1/#11


菜鸟
2014-10-21 23:23:10     打赏
2楼


【Keil安装破解流程】




Step 1 

         安装程序跑起来~



step 2

     看不懂的协议   无条件agree



step   3

    

              选路径    安家落户





     step 4    进度条  走着~




step   5


           弱弱的没看懂 


  step 6

       终于到FINISH了~




   破解~



好啦~ 安装完成  可以好好的玩耍(?!)了= =


菜鸟
2014-10-30 23:59:58     打赏
3楼
首先坦白因为自己的板子有纰漏,所以代码啊什么的都抱了大神的大腿~  
虽然没有太多自己的原创,仅仅是一个模仿学习和理解的过程啦
/**  
  ******************************************************************************  
  * @file    GPIO/IOToggle/main.c   
  * @author  MCD Application Team  
  * @version V3.5.0  
  * @date    08-April-2011  
  * @brief   Main program body.  
  ******************************************************************************  
  * @attention  
  *  
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS  
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE  
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY  
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING  
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE  
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.  
  *  
  *  
 
    ? COPYRIGHT 2011 STMicroelectronics  
 
******************************************************************************  
  */    
  
/* Includes ------------------------------------------------------------------*/  
#include "stm32f10x.h"   
#include "stm32_eval.h"   
GPIO_InitTypeDef GPIO_InitStructure;   
  
void RCC_Configuration(void)   
{   
  RCC_DeInit();   
       
  RCC_HSICmd(ENABLE);   
  while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);   
     
  RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);   
     
  RCC_HSEConfig(RCC_HSE_OFF);   
  RCC_LSEConfig(RCC_LSE_OFF);   
  RCC_PLLConfig(RCC_PLLSource_HSI_Div2,RCC_PLLMul_9); //  72HMz   
  RCC_PLLCmd(ENABLE);   
  while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);   
  RCC_ADCCLKConfig(RCC_PCLK2_Div4);   
  RCC_PCLK2Config(RCC_HCLK_Div1);   
  RCC_PCLK1Config(RCC_HCLK_Div2);   
  RCC_HCLKConfig(RCC_SYSCLK_Div1);   
  RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);   
  while(RCC_GetSYSCLKSource() != 0x08);   
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);   
 GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG   
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;   
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;   
  GPIO_Init(GPIOD, &GPIO_InitStructure);   
    GPIO_ResetBits(GPIOD,GPIO_Pin_2);   
}   
  
void GPIO_INIT()//ÊäÈëÊä³öµÄ³õʼ»¯   
{   
  
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;   
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);   
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;   
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   
      GPIO_Init(GPIOC, &GPIO_InitStructure);   
       
  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_Mode = GPIO_Mode_Out_PP;   
  GPIO_Init(GPIOB, &GPIO_InitStructure);   
       
}   
  
void delay_us(u32 n)  //ÑÓʱ1us   
{   
    u8 j;   
    while(n--)   
    for(j=0;j<10;j++);   
}   
  
void  delay_ms(u32 n)     //ÑÓʱ1ms   
{   
    while(n--)   
    delay_us(1000);   
}   
    
//¶¨ÒåÒ»¸öÊý×飬ÓÃÀ´ÏÔʾÊý×Ö0µ½9   
 int table[10]={GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_14|GPIO_Pin_13|GPIO_Pin_8 ,GPIO_Pin_9|GPIO_Pin_12 ,GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_14|GPIO_Pin_13|GPIO_Pin_5,   
GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_14|GPIO_Pin_5,GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_8|GPIO_Pin_5,GPIO_Pin_7|GPIO_Pin_12|GPIO_Pin_14|GPIO_Pin_8|GPIO_Pin_5,   
GPIO_Pin_7|GPIO_Pin_12|GPIO_Pin_14|GPIO_Pin_13|GPIO_Pin_8|GPIO_Pin_5,GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_12,   
GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14,GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_14   
  
};      
  
void shumaguan(char temp) 
{   
    char gewei=0,shiwei=0;   
    shiwei=temp/10;   
    gewei=temp%10;
	if(temp<10)
  	{
             GPIO_SetBits(GPIOB,GPIO_Pin_1);   
              GPIO_ResetBits(GPIOB,table[gewei]);   
  
          delay_ms(5);   
                GPIO_ResetBits(GPIOB,GPIO_Pin_1);   
           GPIO_SetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);   
	    }
		 if(temp>=10)
				{
					
					
                    GPIO_SetBits(GPIOB,GPIO_Pin_15);   
              GPIO_ResetBits(GPIOB,table[shiwei]);   
          delay_ms(1);   
            GPIO_ResetBits(GPIOB,GPIO_Pin_15);   
           GPIO_SetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);   
               
             GPIO_SetBits(GPIOB,GPIO_Pin_1);   
              GPIO_ResetBits(GPIOB,table[gewei]);   
  
          delay_ms(1);   
                GPIO_ResetBits(GPIOB,GPIO_Pin_1);   
           GPIO_SetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);   
    
	   }
				
           if(temp>30)   
            {   
                    GPIO_SetBits(GPIOB,GPIO_Pin_15);   
              GPIO_ResetBits(GPIOB,table[shiwei]);   
          delay_ms(100);   
            GPIO_ResetBits(GPIOB,GPIO_Pin_15);   
           GPIO_SetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);   
               
             GPIO_SetBits(GPIOB,GPIO_Pin_1);   
              GPIO_ResetBits(GPIOB,table[gewei]);   
  
          delay_ms(150);   
                GPIO_ResetBits(GPIOB,GPIO_Pin_1);   
           GPIO_SetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);   
  
            }   
  
}   
  

  
       
  
int main(void)      
{         
        char a = 0, b = 0,count=0;   //¶¨ÒåÈý¸ö¼ÆÊýÆ÷¡£ 
      RCC_Configuration();       //µ÷Óú¯Êý   
         GPIO_INIT();      //µ÷Óú¯Êý   
      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_10)) //Åжϰ´¼üÊÇ·ñ°´Ï   
      {   
                    delay_ms(100);   
         if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_10))   
               {   
                                 count++;  //count×Ô¼Ó1£»   
              }   
                        }   
             if(count%2==1){      
                                                            if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8))     
                                                 {   delay_ms(80);      
                                                             if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8))   
                                                                             {a++;}   
                                                            }     
                                                                if(a>100)   
                                   {a=0;}  //a>100£¬aÖÃ0£»   
                                                        shumaguan(a);   
            }   
       
       if(count%2==0){   
           if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_9))   
         {delay_ms(80);      
          if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_9)){b++;}}     
                 if(b>100){b=0;}     
                 shumaguan(b);}   
      
       }      
       
        
     } 

 



高工
2014-10-31 00:03:30     打赏
4楼

这......

我该说什么呢


菜鸟
2014-11-27 14:15:42     打赏
5楼
上周课请假了>。<  缺了一次课忘记交作业 先把数码管显示数字的作业补上来  老师不要生气~

菜鸟
2014-11-27 14:16:52     打赏
6楼
    /**   
      * @brief  Main program.   
      * @param  None   
      * @retval None   
      */    
    #include "stm32f10x.h" //定义       
    #include "stm32_eval.h"//定义       
    #include    "stdio.h" //定义       
    #include "math.h" //定义       
    #define buff_size  16;//定义              
    char rx_buff[], rx_buff_count=0; // 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_GPIOB, ENABLE);//使能PC

时钟     
        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);//初始化PB     
       GPIO_SetBits(GPIOB, 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);     
    }        
           
    void USART_int(long BaudRate)        
    {   USART_InitTypeDef USART_InitStructure;          
        USART_ClockInitTypeDef USART_ClockInitStructure;          
         NVIC_InitTypeDef NVIC_InitStructure;       
         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;      
        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; //选择

USART1中断通道       
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 15;    

    
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;        
        NVIC_Init(&NVIC_InitStructure);//初始化中断       
    }        
           
    /*delay_us*/         
              
    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);           
    }         
    /*delay_ms*/        
    void USART_SendStr(char *str)      //发送字符串函数       
    {        
       while((*str)!='\0')             //发送完直至最后          
            {USART_SendData(USART1,*str++);        
            while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == 

RESET);        
            }         
    unsigned int my_atoi(char* pstr) //字符串        
    {           
          int s = 0;          
            while(*pstr != '\0')        //一直扫描到字符串        
            {        
                    if(*pstr >= '0' && *pstr <= '9')  //字符串中有数

字0-9        
                    {          
                            s = s * 10 + *pstr - '0';           
                    }          
                    pstr++;        
            }               
        return s;         
    }      
                
     void Num(int i)        
    {        
          switch(i)//点亮对应输入的数字        
        {        
            case 0 : GPIO_ResetBits(GPIOB,GPIO_Pin_7|GPIO_Pin_8|

GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);break;        
            case 1 : GPIO_ResetBits(GPIOB,GPIO_Pin_9|GPIO_Pin_12);    

                                          break;        
            case 2 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_7|

GPIO_Pin_9|GPIO_Pin_13|GPIO_Pin_14);            break;        
            case 3 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_7|

GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_14);            break;        
            case 4 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_8|

GPIO_Pin_9|GPIO_Pin_12);                        break;        
            case 5 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_7|

GPIO_Pin_8|GPIO_Pin_12|GPIO_Pin_14);            break;        
            case 6 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_7|

GPIO_Pin_8|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);break;        
            case 7 : GPIO_ResetBits(GPIOB,GPIO_Pin_7|GPIO_Pin_9|

GPIO_Pin_12);                                   break;        
            case 8 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_7|

GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);break;     

   
            case 9 : GPIO_ResetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_7|

GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_14); break;        
            }      
        }            
        void func(char *S,char LEN)          
    {      
        int z;     
       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)        
        {               
           z=my_atoi(S);        
               
            GPIO_SetBits(GPIOB,GPIO_Pin_1); //打开个位数码管        
            Num(z%10);                      //点亮各位数         
            delay_ms(5);                    //延时    
            GPIO_ResetBits(GPIOB,GPIO_Pin_1);//关闭个位数码管         

    
            GPIO_SetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|

GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);        
                        
            GPIO_SetBits(GPIOB,GPIO_Pin_15);//打开十位数码管        
            Num(z/10);          
            delay_ms(5);        
            GPIO_ResetBits(GPIOB,GPIO_Pin_15);//关闭十位          
            GPIO_SetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|

GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);        
                            
            }      
        }     
        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()          
    {                                   
            RCC_Configuration();        
          GPIO_INIT();         
            USART_int(9600);        
           // USART_SendStr("Please input like: data=\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();}          //执行input_ASK()    
        else       
        {        
        USART_SendData(USART1,USART_ReceiveData(USART1));      //将数

据回送至超级终端  
        rx_buff[rx_buff_count]= USART_ReceiveData(USART1);      

 


菜鸟
2014-11-27 14:18:52     打赏
7楼

作业待更新!【挖坑占楼】


请教大神和百度ing    脑子有点混总有一点小乱码- -~ 好忧伤


菜鸟
2014-11-27 23:56:22     打赏
8楼

【填坑来】    在向百度和学霸们请教了之后勉强完成了1个作业   

中断这个东西让我严重的怀疑了自己的智商啊救命T^T

    实验模块都被我弄疯了我会乱说么@x@



    #include "stm32f10x.h"  
    #include "stm32_eval.h"  
    #include <stdio.h>  
      
    EXTI_InitTypeDef   EXTI_InitStructure;    
    GPIO_InitTypeDef   GPIO_InitStructure;    
    NVIC_InitTypeDef   NVIC_InitStructure;    
    USART_InitTypeDef USART_InitStructure;    
    USART_ClockInitTypeDef USART_ClockInitStructure;    
        
    void EXTIkeyS1_Config(void);    
    void EXTIkeyS2_Config(void);    
    void EXTIkeyS3_Config(void);    
    void EXTIkeyS4_Config(void);    
    void RCC_Configuration(void)    
    {    
      RCC_DeInit();        
      RCC_HSICmd(ENABLE);    
      while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);      
      RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);      
      RCC_HSEConfig(RCC_HSE_OFF);    
      RCC_LSEConfig(RCC_LSE_OFF);    
      RCC_PLLConfig(RCC_PLLSource_HSI_Div2,RCC_PLLMul_9); //  72HMz    
      RCC_PLLCmd(ENABLE);    
      while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);    
      RCC_ADCCLKConfig(RCC_PCLK2_Div4);    
      RCC_PCLK2Config(RCC_HCLK_Div1);    
      RCC_PCLK1Config(RCC_HCLK_Div2);    
      RCC_HCLKConfig(RCC_SYSCLK_Div1);    
      RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);    
      while(RCC_GetSYSCLKSource() != 0x08);    
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);    
      GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG    
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);    
      GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG    
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;    
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;    
      GPIO_Init(GPIOD, &GPIO_InitStructure);    
        GPIO_ResetBits(GPIOD,GPIO_Pin_2);    
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);    
      GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG    
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;    
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;    
      GPIO_Init(GPIOC, &GPIO_InitStructure);    
        GPIO_ResetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);    
    }    
        
    void USART_int(long BaudRate)    
    {    
          RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);    
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;    
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;    
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;     
        GPIO_Init(GPIOA, &GPIO_InitStructure);    
        /* PA10 USART1_Rx  */    
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;    
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;    
        GPIO_Init(GPIOA, &GPIO_InitStructure);    
        
            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_4);    
            NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //    
            NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 15;    
            NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;    
            NVIC_Init(&NVIC_InitStructure);    
    }    
        
    int main(void)    
    {    
        RCC_Configuration();    
          
      EXTIkeyS1_Config();    
      EXTIkeyS2_Config();    
      EXTIkeyS3_Config();    
      EXTIkeyS4_Config();    
      USART_int(115200);    
        printf("Config done,waiting for interrupt......\r\n");              
      while (1)    
      {    
      }    
    }    
        
    void EXTIkeyS1_Config(void)    
    {    
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);//打开按键  输入模式,PC8 ,S1   
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;    
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;    
      GPIO_Init(GPIOC, &GPIO_InitStructure);    
        
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);//PC8链接到中断线8    
      GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource8);    
        
      EXTI_InitStructure.EXTI_Line = EXTI_Line8;//中断线的设置    
      EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;    
      EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//低电平触发    
      EXTI_InitStructure.EXTI_LineCmd = ENABLE;    
      EXTI_Init(&EXTI_InitStructure);    
        
      NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;//设置分组    
      NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;    
      NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;    
      NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;    
      NVIC_Init(&NVIC_InitStructure);    
    }    
        
    void EXTIkeyS2_Config(void)    
    {    
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);    
        
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;    
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;    
      GPIO_Init(GPIOC, &GPIO_InitStructure);    
        
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);    
      GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource9);    
        
      EXTI_InitStructure.EXTI_Line = EXTI_Line9;    
      EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;    
      EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;    
      EXTI_InitStructure.EXTI_LineCmd = ENABLE;    
      EXTI_Init(&EXTI_InitStructure);    
        
      NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;    
      NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;    
      NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;    
      NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;    
      NVIC_Init(&NVIC_InitStructure);    
    }    
        
    void EXTIkeyS3_Config(void)    
    {    
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);//打开按键  输入模式 ,S3  
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;    
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;    
      GPIO_Init(GPIOC, &GPIO_InitStructure);    
        
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);//PC10链接到中断线10    
      GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource10);    
        
      EXTI_InitStructure.EXTI_Line = EXTI_Line10;//中断线的设置    
      EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;    
      EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//低电平触发    
      EXTI_InitStructure.EXTI_LineCmd = ENABLE;    
      EXTI_Init(&EXTI_InitStructure);    
        
      NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;//设置分组    
      NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;    
      NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;    
      NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;    
      NVIC_Init(&NVIC_InitStructure);//初始化    
    }    
        
    void EXTIkeyS4_Config(void)    
    {    
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);//打开按键  输入模式  S4  
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;    
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;    
      GPIO_Init(GPIOC, &GPIO_InitStructure);    
        
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);//PC11链接到中断线11    
      GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource11);    
        
      EXTI_InitStructure.EXTI_Line = EXTI_Line11;//中断线的设置    
      EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;    
      EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//低电平触发    
      EXTI_InitStructure.EXTI_LineCmd = ENABLE;    
      EXTI_Init(&EXTI_InitStructure);    
        
      NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;   
      NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;    
      NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;    
      NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;    
      NVIC_Init(&NVIC_InitStructure);    
    }    
        
    void EXTI15_10_IRQHandler(void)    
    {    
      if(EXTI_GetITStatus(EXTI_Line10) != RESET)//判断中断线10上的中断是否发生    
      {    
         GPIO_SetBits(GPIOC,GPIO_Pin_2|GPIO_Pin_3);//熄灭PC2,3  
         printf("S3 interrupt ......\r\n");    
         EXTI_ClearITPendingBit(EXTI_Line10);//清除中断线10上的中断标志位    
      }    
            
          if(EXTI_GetITStatus(EXTI_Line11) != RESET)//判断中断线11上的中断是否发生    
      {    
         GPIO_SetBits(GPIOC,GPIO_Pin_4|GPIO_Pin_5);//熄灭PC4,5   
         printf("S4 interrupt ......\r\n");    
         EXTI_ClearITPendingBit(EXTI_Line11);//清除中断线11上的中断标志位    
      }    
    }    
        
    void EXTI9_5_IRQHandler(void)    
    {    
      if(EXTI_GetITStatus(EXTI_Line8) != RESET)    
      {    
         GPIO_SetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_1);    
         printf("S2 interrupt ......\r\n");    
         EXTI_ClearITPendingBit(EXTI_Line8); 
      }    
            
        if(EXTI_GetITStatus(EXTI_Line9) != RESET)    
      {    
         GPIO_SetBits(GPIOC,GPIO_Pin_2|GPIO_Pin_3);    
         printf("S1 interrupt ......hahaha!\r\n");    
         EXTI_ClearITPendingBit(EXTI_Line9);    
      }    
    }    
        
        
    #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     
        
    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    

 


菜鸟
2014-12-19 13:12:45     打赏
9楼
view plaincopy to clipboardprint?
    //库函数    
    #include "stm32f10x.h"    
    #include "stm32_eval.h"    
    #include     
    #define VREF 3.3    
    unsigned int temp0,temp1,temp2;       
    //延时函数     
    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_InitTypeDef   GPIO_InitStructure;    
    USART_InitTypeDef USART_InitStructure;    
    USART_ClockInitTypeDef USART_ClockInitStructure;    
        
    //配置时钟函数    
    void RCC_Configuration(void)    
    {    
      SystemInit();    
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);    
      GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG    
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);    
      GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG    
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;    
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;    
      GPIO_Init(GPIOD, &GPIO_InitStructure);    
      GPIO_ResetBits(GPIOD,GPIO_Pin_2);    
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);    
      GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG    
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;    
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;    
      GPIO_Init(GPIOC, &GPIO_InitStructure);    
      GPIO_SetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);    
      RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);    
    }     
        
    void USART_int(long BaudRate)    
    {    
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);//使能PA USART1    
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;//TX位于PA9      
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    
      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    
        
      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);    
    }    
        
    //    
    void PWM_Config()    
    { uint16_t PrescalerValue = 0;    
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;//定义结构体    
      TIM_OCInitTypeDef  TIM_OCInitStructure;    
        
      RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//打开TIM2,TIM2挂在AP1上    
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO  , ENABLE);//开启功能复用    
            
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;//所需的3个管脚,复用输出    
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;    
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    
      GPIO_Init(GPIOA, &GPIO_InitStructure);    
            TIM_Cmd(TIM2, ENABLE);    
        
      PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;//二、配置TIM2    
      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;//脉冲宽度调制模式1    
        
      TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//选择输出比较状态    
      TIM_OCInitStructure.TIM_Pulse = 0xFFFF;//IM_Pulse设置了待装入捕获比较寄存器的脉冲值,取值在0x0000-0xFFFF之间                                             
      TIM_OC2Init(TIM2, &TIM_OCInitStructure);    
            
      TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//(通道3)    
      TIM_OCInitStructure.TIM_Pulse = 0xFFFF;//0x0000最亮,0xFFFF熄灭    
      TIM_OC3Init(TIM2, &TIM_OCInitStructure);    
        
      TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//(通道4)    
      TIM_OCInitStructure.TIM_Pulse = 0xFFFF;    
      TIM_OC4Init(TIM2, &TIM_OCInitStructure);    
      TIM_ARRPreloadConfig(TIM2, ENABLE);//重新装载初始值    
    }    
      
    void colour(int z)//自定义的函数colour  
    {  
       switch(z)//分别点亮相应的颜色  
    {  
      case 0 : temp0=0,temp1=5000,temp2=0;  /*红色*/  break;  
      case 1 : temp0=0,temp1=5000,temp2=2500;/*橙色*/  break;  
      case 2 : temp0=0,temp1=5000,temp2=5000;/*黄色*/  break;  
      case 3 : temp0=0,temp1=0,temp2=5000;  /*绿色*/  break;  
      case 4 : temp0=2500,temp1=0,temp2=2500;/*青色*/  break;  
      case 5: temp0=5000,temp1=0,temp2=0;   /*蓝色*/  break;  
      case 6 : temp0=2500,temp1=2500,temp2=0;/*紫色*/  break;  
      default: printf(" error\n");    
      }  
    }  
      
    void PWM_TEST()    
    {      
      int i=0;  
    for(i=0;i<7;i++)  
    {  
       colour(i);  //temp1-红,temp2-绿,temp0-蓝  
       TIM_SetCompare2(TIM2,temp0);delay_ms(500); //设置TIM2捕获比较器2寄存器的值  
       TIM_SetCompare3(TIM2,temp1);delay_ms(500);//TIM——通道数(定时器,数值)   
       TIM_SetCompare4(TIM2,temp2);delay_ms(500);  
      }   
    }    
      
    int main(void)//主函数  
    {  
    RCC_Configuration();  
    USART_int(115200);  
      
    printf(" config done...\r\n");
    PWM_Config();//配置pwm  
    delay_ms(1000);//执行延时函数  
    while(1)  
    {  
      PWM_TEST();  
    }//当以上为真时,执行PWM_TEST();函数  
    }  
      
    #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      
    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  

 


菜鸟
2014-12-25 22:20:07     打赏
10楼
view plaincopy to clipboardprint?
#include "stm32_eval.h"    
#include <stdio.h>    
#define VREF 3.3    
EXTI_InitTypeDef   EXTI_InitStructure;      
GPIO_InitTypeDef   GPIO_InitStructure;      
NVIC_InitTypeDef   NVIC_InitStructure;      
USART_InitTypeDef USART_InitStructure;      
USART_ClockInitTypeDef USART_ClockInitStructure;    
unsigned char i=0;    
unsigned char count=0;      
    
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);      
}      
int Display[10]={ GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_14|GPIO_Pin_13|GPIO_Pin_8,      
                    GPIO_Pin_9|GPIO_Pin_12,      
                    GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_14|GPIO_Pin_13|GPIO_Pin_5,      
                  GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_14|GPIO_Pin_5,      
                    GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_8|GPIO_Pin_5,      
                    GPIO_Pin_7|GPIO_Pin_12|GPIO_Pin_14|GPIO_Pin_8|GPIO_Pin_5,      
                  GPIO_Pin_7|GPIO_Pin_12|GPIO_Pin_14|GPIO_Pin_13|GPIO_Pin_8|GPIO_Pin_5,      
                    GPIO_Pin_7|GPIO_Pin_9|GPIO_Pin_12,         
                  GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14,      
                    GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_14 };      
       
void Dis_number(unsigned char i)      
{      
    char one=0,ten=0;         
    one=i%10; ten=i/10;        
    GPIO_SetBits(GPIOB,GPIO_Pin_1);         
    GPIO_ResetBits(GPIOB,Display[one]);         
    delay_ms(10);         
    GPIO_ResetBits(GPIOB,GPIO_Pin_1);         
    GPIO_SetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);      
                     
    GPIO_SetBits(GPIOB,GPIO_Pin_15);         
    GPIO_ResetBits(GPIOB,Display[ten]);           
    delay_ms(10);         
    GPIO_ResetBits(GPIOB,GPIO_Pin_15);         
    GPIO_SetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);      
}      
    
    
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_50MHz;    
    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);    
    USART_InitStructure.USART_BaudRate = BaudRate;//??????    
    
  USART_InitStructure.USART_BaudRate = BaudRate;//usart传输波比特      
  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;//硬件流控制失能      
  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;//最后一位数据的时钟不从SCLK输出      
  USART_ClockInit(USART1, &USART_ClockInitStructure);//使能USART1时钟      
  USART_Init(USART1, &USART_InitStructure);//初始化USART1      
  USART_Cmd(USART1, ENABLE);//使能USART1外设      
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//使能中断      
  USART_Cmd(USART1, ENABLE);      
  /* 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);      
}     
    
void GPIO_INIT()      
{      
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);      
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;      
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;      
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;      
  GPIO_Init(GPIOB, &GPIO_InitStructure);      
          
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);      
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11;      
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;      
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;      
  GPIO_Init(GPIOC, &GPIO_InitStructure);      
}      
      
    
void Iic1_Init(void)    
{    
        GPIO_InitTypeDef GPIO_InitStructure;    
        I2C_InitTypeDef I2C_InitStructure;    
            
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);    
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);    
        //PB6-I2C2_SCL PB7-I2C2_SDA PB10-I2C2_SCL PB11-I2C2_SDA    
        /* Configure IO connected to IIC*********************/    
        GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_10 | GPIO_Pin_11;    
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;    
        GPIO_Init(GPIOB, &GPIO_InitStructure);    
            
        I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;//配置I2C模式    
        I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;//该参数只有在I2C工作在快速模式下有意义    
        I2C_InitStructure.I2C_OwnAddress1 = 0xA0;//设置第一个设备自身地址    
        I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;//使能应答    
        I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;//地址为7位    
        I2C_InitStructure.I2C_ClockSpeed = 400000;//时钟速率 400KHZ       
        I2C_Cmd(I2C2, ENABLE);    
        I2C_Init(I2C2, &I2C_InitStructure);    
        I2C_AcknowledgeConfig(I2C2, ENABLE);        
}    
    
    
void I2C2_WriteByte(unsigned char id,unsigned char write_address,unsigned char byte)    
{    
        while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY));    
        I2C_GenerateSTART(I2C2,ENABLE);    
        while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));    
        I2C_Send7bitAddress(I2C2,id,I2C_Direction_Transmitter);    
        while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));    
        I2C_SendData(I2C2, write_address);    
        while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));    
        I2C_SendData(I2C2, byte);    
        while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));    
        I2C_GenerateSTOP(I2C2, ENABLE);    
    
        do    
        {                    
                /* Send START condition */    
                I2C_GenerateSTART(I2C2, ENABLE);    
                /* Read I2C2 SR1 register */    
                /* Send EEPROM address for write */    
                I2C_Send7bitAddress(I2C2, 0xA0, I2C_Direction_Transmitter);    
        }while(!(I2C_ReadRegister(I2C2, I2C_Register_SR1) & 0x0002));    
    
        /* Clear AF flag */    
        I2C_ClearFlag(I2C2, I2C_FLAG_AF);    
        /* STOP condition */        
        I2C_GenerateSTOP(I2C2, ENABLE);      
}    
    
    
unsigned char I2C2_ReadByte(unsigned char  id, unsigned char read_address)    
{      
        unsigned char temp;             
        while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY)){}    
        I2C_GenerateSTART(I2C2, ENABLE);    
        while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));    
        I2C_Send7bitAddress(I2C2, id, I2C_Direction_Transmitter);    
        while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));    
        I2C_Cmd(I2C2, ENABLE);    
        I2C_SendData(I2C2, read_address);      
        while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));    
        I2C_GenerateSTART(I2C2, ENABLE);    
        while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));    
        I2C_Send7bitAddress(I2C2, id, I2C_Direction_Receiver);    
        while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));    
        I2C_AcknowledgeConfig(I2C2, DISABLE);    
        I2C_GenerateSTOP(I2C2, ENABLE);    
        while(!(I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED)));    
        temp = I2C_ReceiveData(I2C2);    
        I2C_AcknowledgeConfig(I2C2, ENABLE);    
        return temp;    
}    
    
void EXTIkeyS1_Config(void)      
{      
  /* Enable GPIOA clock */      
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);      
        
  /* Configure PA.00 pin as input floating */      
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;      
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;      
  GPIO_Init(GPIOA, &GPIO_InitStructure);      
      
  /* Enable AFIO clock */      
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);      
      
  /* Connect EXTI0 Line to PA.00 pin */      
  GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource8);      
      
  /* Configure EXTI0 line */      
  EXTI_InitStructure.EXTI_Line = EXTI_Line8;      
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;      
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;        
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;      
  EXTI_Init(&EXTI_InitStructure);      
      
  /* Enable and set EXTI0 Interrupt to the lowest priority */      
  NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;      
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;      
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;      
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;      
  NVIC_Init(&NVIC_InitStructure);      
}    
    
void EXTI9_5_IRQHandler(void)  //s1    
{       
    if(EXTI_GetITStatus(EXTI_Line8) != RESET)      
  {      
     i++;           
     I2C2_WriteByte(0xA0,0,i);        
     EXTI_ClearITPendingBit(EXTI_Line8);      
    }     
}    
    
int main(void)    
{    
  RCC_Configuration();    
  USART_int(115200);    
  GPIO_INIT();     
    Iic1_Init();    
    EXTIkeyS1_Config();     
    printf(" config done...\r\n");    
    delay_ms(1000);    
    while(1)      
    {      
     count=I2C2_ReadByte(0xA0,0);         
     Dis_number(count);      
    }      
}    
    
#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    
{    
  /* 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    
    
void assert_failed(uint8_t* file, uint32_t line)    
{     
  while (1)    
  {    
  }    
}    
    
#endif 

 


共13条 1/2 1 2 跳转至

回复

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