笔记四:usart控制led  ; usart 控制数码管(数码管视频不好拍,上代码)
	控制led速度:
	
/****USART控制LED转速***/
#include "stm32f10x.h"   																																
#include    "stdio.h"
GPIO_InitTypeDef GPIO_InitStructure;		// 声明结构体
NVIC_InitTypeDef  NVIC_InitStructure;  
USART_InitTypeDef  USART_InitStructure;  
USART_ClockInitTypeDef  USART_ClockInitStructure;   
    
#define buff_size  16;      
char rx_buff[],rx_buff_count=0; 
 
void RCC_Configuration(void) ;
void GPIO_INIT(void)  ;
void USART_int(long BaudRate);
void USART_SendStr(char *str);
void  delay_ms(u32 n)  ;
void delay_us(u32 n); 
/****主函数 ****/
int main()    
{                             
    RCC_Configuration();  
    GPIO_INIT();  
    USART_int(9600);  
    GPIO_ResetBits(GPIOC,0xffff);//led全亮,提示程序开始  
    delay_ms(200);  
    GPIO_SetBits(GPIOC,0xffff);//led全灭  
    USART_SendStr("USART Led Speed\r\n");//   
    USART_SendStr("\n>");//   
    while(1);
}   
 
/****	配置RCC ****/
void RCC_Configuration(void)   
{     
    ErrorStatus HSEStartUpStatus;	//	设置错误标志量
	RCC_DeInit(); 						//*将外设寄存器RCC重设为初始值 
	RCC_HSEConfig(RCC_HSE_ON);//*打开外部高速时钟晶振HSE:
  HSEStartUpStatus = RCC_WaitForHSEStartUp();  //*等待外部高速时钟晶振工作: 
  if(HSEStartUpStatus==SUCCESS)
	{
		RCC_HCLKConfig(RCC_SYSCLK_Div1);//*AHB使用系统时钟,负责外部存储器的时钟 
		RCC_PCLK1Config(RCC_HCLK_Div2);//*APB1 负责DA,USB,SPI,I2C,CAN,串口2345,普通TIM
		RCC_PCLK2Config(RCC_HCLK_Div1);//*APB2负责AD,I/O,高级TIM,串口1 
		RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);//*设置PLL输入时钟源72M
		RCC_PLLCmd(ENABLE);//*打开PLL:          
		while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);//*等待PLL工作:
		RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//*设置系统时钟
		while(RCC_GetSYSCLKSource() != 0x08);//* 判断PLL是否是系统时钟:    
	}
	 GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
	
	/*蜂鸣器*/
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);        
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;     
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;     
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;     
    GPIO_Init(GPIOD, &GPIO_InitStructure);     
    GPIO_ResetBits(GPIOD,GPIO_Pin_2);              
}   
  
/****延时 ****/
void delay_us(u32 n)     
{     
    u8 j;     
    while(n--)     
    for(j=0;j<10;j++);     
}     
void  delay_ms(u32 n)     
{     
   while(n--)     
    delay_us(1000);     
} 
/**** 初始化GPIO ****/
void GPIO_INIT(void)   
{    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);      
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;     
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;         
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;               
    GPIO_Init(GPIOC, &GPIO_InitStructure);    
}  
/**** USART传输的波特率 ****/ 
void USART_int(long BaudRate)   
{  
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);//使能PA和 usart1
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;               
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;  
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;          //复用推挽输出 PA9作为US1的TX端,打开复用,负责发送数据
		GPIO_Init(GPIOA, &GPIO_InitStructure);  
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;       					//RX位于PA10        
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;     //浮空输入,PA10作为US1的RX端,负责接收数据
 									//RX初始化PA10
		USART_InitStructure.USART_BaudRate = BaudRate;//波特率
		USART_InitStructure.USART_WordLength = USART_WordLength_8b;//USART_WordLength 在一个帧中传输8位数据(字节)
		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 使能)     
		USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;      //SCLK输出时钟低电平
		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);
		//配置NVIC ,设置优先级分组,使用NVIC_Init对NVIC进行初始化
		NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);//选择中断分组4(先占优先级四位,从优先级0)
		NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //选择中断通道
		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢占优先级1-15
		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//响应优先级1-15
		NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能中断
		NVIC_Init(&NVIC_InitStructure); //初始化中断  
                    
}  
/**** usart发送数据 ****/
void USART_SendStr(char *str)   //USART发送数据   
{  
			while((*str)!='\0')             
        {
					USART_SendData(USART1,*str++);  //通过外设 USARTx 发送单个数据 
					while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);  //检查指定的 USART 标志位设置与否
        }  
}  
  
/**** 取字符串中的数字 ****/
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; 
} 
/****  点亮led****/
void LED(char *S,char LEN)     //(字符串,字符串长度)
{ 
		  int m,i;
	    int a[8]={GPIO_Pin_0,GPIO_Pin_1,GPIO_Pin_2,GPIO_Pin_3,GPIO_Pin_4,GPIO_Pin_5,GPIO_Pin_6,GPIO_Pin_7};	
			m=translate(S);
    
			GPIO_SetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);  
			delay_ms(100);			
			for(i=0;i<8;i++) 
			{
				GPIO_ResetBits(GPIOC,a[i]);    // 点亮LED
				delay_ms(m);
				GPIO_SetBits(GPIOC,a[i]);     //熄灭LED
				delay_ms(m);
			}
}
void input_ASK()
{  
    char j;  
    LED(rx_buff,rx_buff_count);
    rx_buff_count=0;  
    for (j=0;j<rx_buff_count;j++)  
    {
			rx_buff[j]='\0';
		}//判断读入信息是否结束,如果结束则准备下一次读入  
    USART_SendStr("\n>");  
} 
  /****接收中断****/
void USART1_IRQHandler(void)   
{   
		while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)  { }   //判断接收中断是否打开
    if(USART_ReceiveData(USART1)==0x0d)   
    {
			input_ASK();
		}  
    else  
    {  
			USART_SendData(USART1,USART_ReceiveData(USART1));  //将数据传回给电脑
			rx_buff[rx_buff_count]= USART_ReceiveData(USART1);  
      rx_buff_count++;  
    }  
		
		USART_ClearFlag(USART1, USART_FLAG_RXNE);  //清除中断接受完成标志位
  
}  
	
		 
	
	
	
	
	usart控制数码管显示:
	main.c
	
int main(void)   
{    
    RCC_Configuration();   
    GPIO_Inits(); 
		USART_int(9600);
		USART_SendStr("Config done\r\n");  //USART发送数据   
		while(1);  
}
	
	
	usart.c
	
#include "usart.h"
#include "stm32f10x.h"
#define buff_size  16;      
char rx_buff[],rx_buff_count=0; 
/* USART传输的波特率 */ 
void USART_int(long BaudRate)   
{  
		/*结构体类型声明  初始化*/
		GPIO_InitTypeDef GPIO_InitStructure;		
		NVIC_InitTypeDef  NVIC_InitStructure;  
		USART_InitTypeDef  USART_InitStructure;  
		USART_ClockInitTypeDef  USART_ClockInitStructure;
		/*发送接收管脚的配置*/
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);//使能PA和 usart1
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;               
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;  
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;          //复用推挽输出 PA9作为US1的TX端,打开复用,负责发送数据
		GPIO_Init(GPIOA, &GPIO_InitStructure);  
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;       					//RX位于PA10        
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;     //浮空输入,PA10作为US1的RX端,负责接收数据
		GPIO_Init(GPIOA, &GPIO_InitStructure); 
		/*USART的配置*/
		USART_InitStructure.USART_BaudRate = BaudRate;
		USART_InitStructure.USART_WordLength = USART_WordLength_8b;//USART_WordLength 在一个帧中传输8位数据(字节)
		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 使能)     
		USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;      //SCLK输出时钟低电平
		USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;//时钟第二个边沿开始数据捕获      
		USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;//最后一位数据不从SCLK发出
		/*初始化USART1时钟*/
		USART_ClockInit(USART1, &USART_ClockInitStructure);
		/*初始化USART1*/
		USART_Init(USART1, &USART_InitStructure);
		/*使能接收中断,在接受移位寄存器中有数据时产生*/
		USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
		/*USART1使能*/
		USART_Cmd(USART1, ENABLE);
		//配置NVIC ,设置优先级分组,使用NVIC_Init对NVIC进行初始化
		NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);//选择中断分组4(先占优先级四位,从优先级0)
		NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //选择中断通道
		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢占优先级1-15
		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//响应优先级1-15
		NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能中断
		NVIC_Init(&NVIC_InitStructure); //初始化中断                    
}  
/* usart发送数据 */
void USART_SendStr(char *str)   //USART发送数据   
{  
			while((*str)!='\0')             
        {
					USART_SendData(USART1,*str++);  //通过外设 USARTx 发送单个数据 
					while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);  //检查指定的 USART 标志位设置与否
        }  
}  
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 input_ASK() { char j; Function(rx_buff,rx_buff_count); rx_buff_count=0; for (j=0;j");  
} 
  /****接收中断****/
void USART1_IRQHandler(void)   
{   
		while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET){}   //判断接收中断是否打开
    if(USART_ReceiveData(USART1)==0x0d)   
    {
			input_ASK();
		}  
    else  
    {  
			USART_SendData(USART1,USART_ReceiveData(USART1));  //将数据传回给电脑
			rx_buff[rx_buff_count]= USART_ReceiveData(USART1);  
      rx_buff_count++;  
    }  
		
		USART_ClearFlag(USART1, USART_FLAG_RXNE);  //清除中断接受完成标志位
  
}  
	
	
	func.c
	
#include "func.h"
#include "stm32f10x.h"
int m,i,j;
void Function(char *S,char LEN) 
{  
m=translate(S);
i=m%10;
j=(m/10)%10;     
delay_ms(100);  
GPIO_SetBits(GPIOB,GPIO_Pin_1);         //打开个位数码管  
Number(i);                                //用switch函数显示00  
delay_ms(6);
GPIO_ResetBits(GPIOB,GPIO_Pin_1);        //关闭个位 	
GPIO_SetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);  
GPIO_SetBits(GPIOB,GPIO_Pin_15);           //打开十位  
Number(j);    
delay_ms(6);      
GPIO_ResetBits(GPIOB,GPIO_Pin_15);      //关掉十位数码管
delay_ms(100);  		
//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); 
}