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

共10条 1/1 1 跳转至

【1月24日更新】19号STM32的学习

菜鸟
2015-11-15 19:28:24     打赏

嵌入式系统及原理作业

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

第二次作业流水灯:http://forum.eepw.com.cn/thread/277763/1#4

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

第四次作业串口输入0-99:http://forum.eepw.com.cn/thread/277763/1#7

第五次作业定时器控制流水灯:http://forum.eepw.com.cn/thread/277763/1#8

第六次作业记录开机次数(IIC上电计数)http://forum.eepw.com.cn/thread/277763/1#9

 触摸DS18B02使灯的颜色发生变化 :http://forum.eepw.com.cn/thread/277763/1#10



菜鸟
2015-11-15 19:33:44     打赏
2楼

首先,双击mdk472_a.exe

接着就会出现以下的界面

选择Next,之后选择可以安装目录了 ~

接着。。。

再然后就是漫长的安装过程 ~  ~

等啊等啊 ~~

Next Next  之后去掉以下的两个钩钩,就可以finsh啦

最后就是要和谐啦,打开安装好的软件,按照以下路径打开License

将里面的CID复制到和谐文件的对应位置,并在Target里选择ARM

多点几次Generate,将里面的内容复制到License Management里New License ID Code(LIC)里,然后点击Add LIC,就可以啦 ~


院士
2015-11-15 21:14:43     打赏
3楼
安装好工具就要开始干了啊

菜鸟
2015-11-15 21:34:45     打赏
4楼

依次点亮 D1-D8:

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

/*初始化GPIO*/
void GPIO_INIT() 
{ 
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); 
RCC_APB2PeriphClockCmd(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; //管脚最高输出速率为50M
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 
GPIO_Init(GPIOC, &GPIO_InitStructure); 
GPIO_Init(GPIOA, &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(); 
while(1){ 
GPIO_ResetBits(GPIOC,GPIO_Pin_0);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_0);

GPIO_ResetBits(GPIOC,GPIO_Pin_1);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_1);

GPIO_ResetBits(GPIOC,GPIO_Pin_2);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_2);

GPIO_ResetBits(GPIOC,GPIO_Pin_3);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_3);

GPIO_ResetBits(GPIOC,GPIO_Pin_4);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_4);

GPIO_ResetBits(GPIOC,GPIO_Pin_5);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_5);

GPIO_ResetBits(GPIOC,GPIO_Pin_6);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_6);

GPIO_ResetBits(GPIOC,GPIO_Pin_7);
delay_ms(100);
GPIO_SetBits(GPIOC,GPIO_Pin_7);
} 
} 

 


菜鸟
2015-11-15 22:15:57     打赏
5楼

数码管显示0-99:

#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;//管脚最高输出速率为50M     
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;     
  GPIO_Init(GPIOD, &GPIO_InitStructure);     
    GPIO_ResetBits(GPIOD,GPIO_Pin_2);     
}

    /*初始化GPIO*/ 
    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)     
{     
    u8 j;     
    while(n--)     
    for(j=0;j<10;j++);     
}     
/*毫秒延时函数*/    
void  delay_ms(u32 n)      
{     
    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;   //定义3个计数器   
      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++;    
              }     
                        }     
             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;}      
                                                        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);}     
        
       }        
         
          
     }       

 



院士
2015-11-15 22:36:56     打赏
6楼

楼主提供了一个实现数码管点亮的方式,非常值得称赞。

看的出来,是自己动脑筋之后的作业。

这段程序还是有提高的,楼主 加油~~


菜鸟
2015-11-22 21:44:36     打赏
7楼

第四次作业:串口输入0-99

每次做作业都好吃力,参阅了很多同学的代码

#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_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;//流控位

为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);//初始化中断       
}        
       
 /*微秒延时函数*/       
 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 USART_SendStr(char *str)      //发送字符串函数       
{        
   while((*str)!='\0')             //发送完直至最后          
        {USART_SendData(USART1,*str++);        
        while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);        
        }         
unsigned int translate(char* pstr) //字符串        
{           
      int s = 0;          
        while(*pstr != '\0')        //一直扫描到字符串        
        {        
                if(*pstr >= '0' && *pstr <= '9') //字符串中有数字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 m;     
   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)        
    {               
       m=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);        
        rx_buff_count++;        
    }        
  USART_ClearFlag(USART1, USART_FLAG_RXNE); //发送数据       
       
}  
}    

 


菜鸟
2015-11-29 22:28:49     打赏
8楼

第五次作业:定时器控制流水灯

#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;//字长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);  
  USART_Cmd(USART1, ENABLE);  
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);  
  USART_Cmd(USART1, ENABLE);  
}  

void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/* Private functions ---------------------------------------------------------*/

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

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);//初始化TIM2 
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE ); 

TIM_Cmd(TIM2, ENABLE);//使能

while(1){}
}

void TIM2_IRQHandler(void) 
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) 
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update ); 
	
count++;
GPIO_SetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|

GPIO_Pin_6|GPIO_Pin_7);
if(count==1) GPIO_ResetBits(GPIOC,GPIO_Pin_0);
if(count==2) GPIO_ResetBits(GPIOC,GPIO_Pin_1);
if(count==3) GPIO_ResetBits(GPIOC,GPIO_Pin_2);
if(count==4) GPIO_ResetBits(GPIOC,GPIO_Pin_3);
if(count==5) GPIO_ResetBits(GPIOC,GPIO_Pin_4);
if(count==6) GPIO_ResetBits(GPIOC,GPIO_Pin_5);
if(count==7) GPIO_ResetBits(GPIOC,GPIO_Pin_6);
if(count==8) GPIO_ResetBits(GPIOC,GPIO_Pin_7);
if(count==9) count=0;
}
}

 


菜鸟
2016-01-16 22:12:13     打赏
9楼

第六次作业:记录开机次数(IIC上电计数)

#include "stm32f10x.h"      
#include "stm32_eval.h"      
#include "STM32_I2C.h"    
#include <stdio.h>      
    
GPIO_InitTypeDef   GPIO_InitStructure;    
USART_InitTypeDef USART_InitStructure;    
USART_ClockInitTypeDef USART_ClockInitStructure;    
EXTI_InitTypeDef   EXTI_InitStructure;    
NVIC_InitTypeDef   NVIC_InitStructure;    
unsigned char i=0;    
void EXTIkeyS1_Config(void);    
    
/*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 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);    
  /* 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;    
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//接受和发送        
    USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;         
    USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;          
    USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;          
    USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;    
    USART_ClockInit(USART1, &USART_ClockInitStructure);    
  USART_Init(USART1, &USART_InitStructure);    
  USART_Cmd(USART1, ENABLE);    
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);    
  USART_Cmd(USART1, ENABLE);    
      /* Configure four bit for preemption priority */    
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);    
  /* Enable the USART1 Interrupt */    
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //    
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 15;    
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;    
  NVIC_Init(&NVIC_InitStructure);    
}    
    
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_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;//Tlov/Thigh=2  
        I2C_InitStructure.I2C_OwnAddress1 = 0xA0;//作为从机时的地址  
        I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;//使能I2C的应答  
        I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;//应答7位的地址  
        I2C_InitStructure.I2C_ClockSpeed = 400000;//I2C速率为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  
        I2C_GenerateSTART(I2C2,ENABLE);//产生起始信号  
        while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));//确定主机模式是否成功(检测EV5)EV5  
        I2C_Send7bitAddress(I2C2,id,I2C_Direction_Transmitter);//发送器件地址(写)  
        while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));//检测主机传输模式是否成功(检测EV6)EV6  
        I2C_SendData(I2C2, write_address);//通过外设I2C发送地址  
        while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));//检测主机发送的字节是否成功(检测EV8)EV8  
        I2C_SendData(I2C2, byte);//通过外设T2C发送数据  
        while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));//检测主机发送的字节是否成功(检测EV8)  
        I2C_GenerateSTOP(I2C2, ENABLE);//产生I2C2传输STOP条件    
        do    
        {                    
           /* Send START condition */    
           I2C_GenerateSTART(I2C2, ENABLE); //产生I2C2传输的START条件   
          /* 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);//清除I2Cx的应答错误标志位    
        /* STOP condition */        
        I2C_GenerateSTOP(I2C2, ENABLE);//产生I2C2传输START条件        
}    
    
unsigned char I2C2_ReadByte(unsigned char  id, unsigned char read_address)    
{      
     unsigned char temp;             
     while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY))//等待I2C空闲  
             {}    
     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)//S1   
{    
  /* Enable GPIOA clock */    
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);    
  /* Configure PA.00 pin as input floating */    
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;//S1    
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;    
  GPIO_Init(GPIOC, &GPIO_InitStructure);    
  /* Enable AFIO clock */    
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);    
  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 EXTI9_5_IRQHandler(void)    
{    
  if(EXTI_GetITStatus(EXTI_Line8) != RESET)    
  {    
        i=0;    
        I2C2_WriteByte(0xA0,0,0);    
        printf(" 按键清%d\r\n",i);    
    /* Clear the  EXTI line 8 pending bit */    
    EXTI_ClearITPendingBit(EXTI_Line8);    
  }    
}    
  
int main(void)    
{    
  RCC_Configuration();    
  EXTIkeyS1_Config();    
  USART_int(115200);    
  Iic1_Init();    
    printf(" config done...\r\n");    
    i = I2C2_ReadByte(0xA0,0);//  
    printf("write data to address:%d\r\n",i);    
    i++;    
    I2C2_WriteByte(0xA0,0,i);//   
    printf("Reading data from address:%d\r\n",i);    
    while(1)    
    {    
     delay_ms(2000);    
     printf(" 上电计数:%d\r\n",i);    
    }    
}    
    
#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-24 11:29:26     打赏
10楼

触摸DS18B02使灯的颜色发生变化

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

 


共10条 1/1 1 跳转至

回复

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