这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 高校专区 » 漓东e学堂 » STM32学习笔记及作业

共12条 1/2 1 2 跳转至

STM32学习笔记及作业

助工
2015-12-27 15:48:42     打赏

空气监测系统(千万别看):http://forum.eepw.com.cn/thread/283085/1

笔记一:工程的建立http://forum.eepw.com.cn/thread/278673/1#2

笔记二:GPIO的原理http://forum.eepw.com.cn/thread/278673/1#3

笔记三:按键消抖http://forum.eepw.com.cn/thread/278673/1#4

笔记四:Usart控制led,数码管http://forum.eepw.com.cn/thread/278673/1#5

笔记五:外部中断(中断优先级)http://forum.eepw.com.cn/thread/278673/1#6

笔记六:通用定时器http://forum.eepw.com.cn/thread/278673/1#7

笔记七:PWM&RGBhttp://forum.eepw.com.cn/thread/278673/1#8

笔记八:ADC&RGBhttp://forum.eepw.com.cn/thread/278673/1#9

笔记九:独立看门狗http://forum.eepw.com.cn/thread/278673/1#10



助工
2015-12-27 18:17:47     打赏
2楼

笔记一:

a.找个位置新建个文件夹命名Study

b.在Study该文件夹下,新建文件夹并命名为CORE,里面存放(core_cm3.c和startup_stm32f10x_hd)这里的startup_stm32f10x_hd是启动文件,''hd''指的是大容量芯片。

c.在Study该文件夹下,新建文件夹并命名为Fwlib,里面存放(inc和src文件夹)这里的inc和src文件夹是ST官方库里的头文件和c文件,需要去下载

d.在Study该文件夹下,新建文件夹OBJ(暂时不用放什么),在Study该文件夹下,新建文件夹并命名User,里面存放(main.c和stm32f10x_it.c和system_stm32f10x.c

e.打开keil,点击新建工程,在Study文件下命名,接着会弹出一个窗口,选择芯片型号--stm32f103ZE,点击OK,接着弹出来的窗口提示你是否添加文件,点击否。

f.右键点击左边的文件夹,选中manage components,在Groups这里输入CORE,接着添加Fwlib,User,在Files这里需要将c文件加入到文件夹中,在User下添加文件main.c和stm32f10x_it.c和system_stm32f10x.c在CORE下添加core_cm3.c和startup_stm32f10x-hd,在Fwlib下添加src里的c文件

g.点击keil界面里的魔术棒图标(Target options),Device窗口这里选中我们的芯片型号;Output窗口这里有个select选项,是用来存放过程文件的,这里点击select forder,选择OBJ文件夹并且底下的create hex和browse都打勾;Listing窗口也有个select forder,选择User文件夹即可;C/C++窗口的define这里输入STM32F10X_HD,USE_STDPERIPH_DRIVER  注意中间是个逗号不是句号,这个窗口下还有个include path,用来添加头文件,点击,一次添加CORE,USER,FWlib_inc

h.当我们还需要添加头文件或其他文件时,可以在Study下新建个文件夹,在keil界面中manage components里的Groups添加文件夹,files这里加入c文件,接着,在魔术棒图标(Target options)里的C/C++窗口添加头文件。

图片仅供参考








助工
2015-12-27 18:34:07     打赏
3楼

笔记二:GPIO原理

(rcc和gpio文件省略;复位后数码管显示00,按s1流水灯,s2RGB,s3数码管0加到99)


视频地址:http://player.youku.com/player.php/sid/XMTQyNjQ1NjgxNg==/v.swf

main.c文件

#include "rcc.h"
#include "delay.h"
#include "gpio.h"
#include "func.h"
int main(void) 
{ 
RCC_Configuration(); 
GPIO_Inits(); 
Function(); 
}
}

func.c文件

#include "func.h"
#include "stm32f10x.h"

void Function(void) 
{    int g=0,s=0;
signed int i;   
unsigned int c;   
static unsigned	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};  
static unsigned	int b[3]={GPIO_Pin_1,GPIO_Pin_2,GPIO_Pin_3}; 

while(1)   
{ 
GPIO_SetBits(GPIOB,GPIO_Pin_1);         //打开个位数码管  
Number(g);                              //用switch函数显示00  
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);           //打开十位  
Number(s);    
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);  
if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8))   //判断按键是s1否被按下 (消去前沿抖动)  
{     
delay_ms(50);            //延时去抖  
if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8))  //再次判断按键是s1否被按下 (再次确认是否消抖)   
{   
for(i=7;i>=0;i--)   //LED
{   
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);        
GPIO_ResetBits(GPIOC,a[i]);      
delay_ms(100);   
GPIO_SetBits(GPIOC,a[i]);        
delay_ms(100);   
}   
}  
}   		
if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_9))   //判断按键是s2否被按下 (消去前沿抖动)  
{     
delay_ms(50);            //延时去抖  
if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_9))  //再次判断按键是s2否被按下 (再次确认是否消抖)   
{   
for(c=0;c<3;c++) //RGB{ GPIO_SetBits(GPIOA,GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3); delay_ms(100); GPIO_ResetBits(GPIOA,b[c]); delay_ms(100); GPIO_SetBits(GPIOA,b[c]); delay_ms(100); } } } if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_10)) //判断按键是s3否被按下 (消去前沿抖动) { delay_ms(50); //延时去抖if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_10)) //再次判断按键是s3否被按下 (再次确认是否消抖){ g++; //按键被按下,个位+1 if(g==10) { s++; g=0; } if(s==10) { s=0;g=0; } } } } } 
										

助工
2015-12-29 11:34:22     打赏
4楼

笔记三:按键消抖
视频地址:http://player.youku.com/player.php/sid/XODkwMTk5MTQw/v.swf

/****按键****/

#include "stm32f10x.h"	

GPIO_InitTypeDef GPIO_InitStructure; // 声明一个结构体

void RCC_Configuration(void); 
void GPIO_INIT(void) ;
void Function(void) ;

int main(void)   
{    
    RCC_Configuration();   
    GPIO_INIT();
    Function();   
}    
      
void RCC_Configuration(void)	//复位所有的RCC外围设备寄存器
{   
		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);//改变管脚的映射SWJ使能,禁用JTAG,选择用于事件输出的GPIO端口
	
	/*蜂鸣器*/
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE); //# 使能被重新映射到GPIOD时钟和使能复用时钟功能
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; 		//#管脚号  关闭蜂鸣器
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 		//#输出速度 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;		//#推挽输出  
  GPIO_Init(GPIOD, &GPIO_InitStructure);  	 //#初始化 
	GPIO_ResetBits(GPIOD,GPIO_Pin_2);
} 

void delay_us(u32 n)		//延时函数   
{   
    u8 j;   
    while(n--)   
			for(j=0;j<10;j++);   
}    
void  delay_ms(u32 n)		//延时函数   
{   
   while(n--)   
   delay_us(1000);   
}
 
void GPIO_INIT(void)   
{     
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOB, 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);	//#初始化   
	 
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_1|GPIO_Pin_15|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14;//数码管
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;     
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
		GPIO_Init(GPIOB, &GPIO_InitStructure);	
}   
  

void Number(int a)  //显示数字
{
    switch(a)  
			{  
        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 Function(void) 
{     
		int b=2,c=0,t=5,g=0,s=0;          //i被按下的次数,n延时变量  
		int pin[2]={GPIO_Pin_8,GPIO_Pin_9};    //管脚切换 
		int num[2]={0,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);     
		delay_ms(50); //关闭数码管    

		while(1)  
		{                 
				GPIO_SetBits(GPIOB,GPIO_Pin_1);         //打开个位数码管  
				Number(g);                                //用switch函数显示00  
				delay_ms(t);                            
				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(s);    
				delay_ms(t);  
				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);  

				//如果s3被按下,i根据按下次数的奇偶来切换 
				if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_10))   //判断按键是s3否被按下 (消去前沿抖动)  
				{     
					delay_ms(50);            //延时去抖  
					if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_10))  //再次判断按键是s3否被按下 (再次确认是否消抖)   
					{   
						c++;  
						b=c%2;          //判断次数奇偶  
					}  
				}   

					//根据次数的奇偶来切换s1/s2    
				if(!GPIO_ReadInputDataBit(GPIOC,pin[b])&&b==num[b])   
				{     
						delay_ms(70);            //延时去抖  
						if(!GPIO_ReadInputDataBit(GPIOC,pin[b])&&b==num[b])   
						{    
								if(b==0)  {t=5;}             //按键次数为偶数
								if(b==1)  {t=30;}            //按键次数为奇数    
								g++;                     //按键被按下,个位+1  
								if(g==10)     
								{  
									s++;               
									g=0;                 
								}
								if(s==10)
								{
									 s=0;g=0;
								}
						}
				}           
		}  
}  








 


助工
2015-12-29 11:36:50     打赏
5楼

笔记四:usart控制led  ; usart 控制数码管(数码管视频不好拍,上代码)

控制led速度:


视频地址:http://player.youku.com/player.php/sid/XOTIzMjY2Njg0/v.swf

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








助工
2015-12-29 11:43:35     打赏
6楼

笔记五:外部中断(中断优先级)


视频地址:http://player.youku.com/player.php/sid/XMTMwNDQ5MjkxNg==/v.swf


/*外部中断熄灭LED*/

#include "stm32f10x.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 USART_SendStr(char *str) ;
void RCC_Configuration(void);
void USART_int(long BaudRate);
void NVIC_Config(void);

/***主函数***/
int main(void)
{
	RCC_Configuration();
  USART_int(9600);
	USART_SendStr("***************************\r\n");
	USART_SendStr("*                         *\r\n");
	USART_SendStr("*      STM32 F103RB       *\r\n"); 
	USART_SendStr("*   Ready for Interrupt   *\r\n"); 
	USART_SendStr("*                         *\r\n"); 
	USART_SendStr("***************************\r\n");
  EXTIkeyS1_Config();
  EXTIkeyS2_Config();
	EXTIkeyS3_Config();
  EXTIkeyS4_Config();
	NVIC_Config();
        
  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是否是系统时钟:    
	}
	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 NVIC_Config(void)
{
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//选择中断分组4
  NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;//选择中断通道使能(GPIOx5-9任何一个满足外部中断条件就可以触发),设置分组,响应优先级别0
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =1 ;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
	
  NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;//选择通道
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//0优先
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_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端,负责接收数据
 
		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)     
{  
			while((*str)!='\0')             
        {
					USART_SendData(USART1,*str++);  //通过外设 USARTx 发送单个数据 
					while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);  //检查发送完成与否
        }  
}

/***延时函数***/
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 EXTIkeyS1_Config(void) { GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_SetBits(GPIOC,GPIO_Pin_3); 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);//端口复用 GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource8);//PC8链接到中断线8 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); } void EXTIkeyS2_Config(void) { GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_SetBits(GPIOC,GPIO_Pin_2); 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); } void EXTIkeyS3_Config(void) { GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_SetBits(GPIOC,GPIO_Pin_1); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource10);//PC10链接到中断线10 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);//使用EXTI必须使能AFIO时钟 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); } void EXTIkeyS4_Config(void) { GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_SetBits(GPIOC,GPIO_Pin_0); 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); } /***自动进入中断服务函数***/ void EXTI9_5_IRQHandler(void) { if(EXTI_GetITStatus(EXTI_Line8) != RESET) { GPIO_ResetBits(GPIOC,GPIO_Pin_3); delay_ms(3000); GPIO_SetBits(GPIOC,GPIO_Pin_3);//熄灭PC6,7 USART_SendStr("S1 interrupt ......\r\n"); EXTI_ClearITPendingBit(EXTI_Line8); } if(EXTI_GetITStatus(EXTI_Line9) != RESET) { GPIO_ResetBits(GPIOC,GPIO_Pin_2); delay_ms(3000); GPIO_SetBits(GPIOC,GPIO_Pin_2);//熄灭PC6,7 USART_SendStr("S2 interrupt ......\r\n"); EXTI_ClearITPendingBit(EXTI_Line9); } } /***自动进入中断服务函数***/ void EXTI15_10_IRQHandler(void) { if(EXTI_GetITStatus(EXTI_Line10) != RESET)//检查指定的EXTI线路标志位设置与否 { GPIO_ResetBits(GPIOC,GPIO_Pin_1); delay_ms(3000); GPIO_SetBits(GPIOC,GPIO_Pin_1);//熄灭PC6,7 USART_SendStr("S3 interrupt ......\r\n"); EXTI_ClearITPendingBit(EXTI_Line10);//清除EXTI线路挂起位来退出中断 } if(EXTI_GetITStatus(EXTI_Line11) != RESET)//检查指定的EXTI线路标志位设置与否 { GPIO_ResetBits(GPIOC,GPIO_Pin_0); delay_ms(3000); GPIO_SetBits(GPIOC,GPIO_Pin_0);//熄灭PC2,3 USART_SendStr("S4 interrupt ......\r\n"); EXTI_ClearITPendingBit(EXTI_Line11);//清除EXTI线路挂起位来退出中断 } } 





助工
2015-12-29 11:45:12     打赏
7楼

笔记六:TIM2&TIM3


视频地址:http://player.youku.com/player.php/sid/XOTI0NjM4OTg0/v.swf

int main(void)  
{  
		RCC_Configuration();  
		NVIC_Configuration(); 
		GPIO_Configuration(); 	
		USART_int(115200);  
    TIM2_Configuration();  
    TIM3_Configuration();  
	
		TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);  //TIM_IT_Update为TIM中断源
		TIM_Cmd(TIM2, ENABLE);	
		TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);  
		TIM_Cmd(TIM3, ENABLE);	

    USART_SendStr(" TIM2 TIM3 Has Been Ready\r\n"); 
		while (1)  
		{   
			 GPIO_SetBits(GPIOC,GPIO_Pin_0);  
			 GPIO_SetBits(GPIOC,GPIO_Pin_4);  	
		}  
}  


/***RCC的配置(HSE,PLL,AHB,蜂鸣器和GOIOC,TIM使能) ***/

	//选择系统时钟,判断HSE时钟是否稳定,设置AHB预分频系数,设置APB2的预分频系数,设置APB1的预分频系数,设置PLL的时钟源,以及PLL的倍频系数,确定PLLCLK频率,使能PLL,等待PLL锁定
void RCC_Configuration(void)  																
{  
		ErrorStatus HSEStartUpStatus;//HSE是否起振(使能HSE)
		RCC_DeInit();
		RCC_HSEConfig(RCC_HSE_ON);	//选择系统时钟
		HSEStartUpStatus = RCC_WaitForHSEStartUp();

	  if(HSEStartUpStatus == SUCCESS)//判断HSE时钟是否稳定
		{														
			RCC_HCLKConfig(RCC_SYSCLK_Div1);  		//AHB使用系统时钟,负责外部存储器的时钟 72M
			RCC_PCLK2Config(RCC_HCLK_Div1); 		//APB2负责AD,I/O,高级TIM,串口1  
			RCC_PCLK1Config(RCC_HCLK_Div2);  		//APB1 负责DA,USB,SPI,I2C,CAN,串口2345,普通TIM
			RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9); 	//  设置PLL输入时钟源及倍频72M
			RCC_PLLCmd(ENABLE); 		
			while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET){}//*等待检测
			RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);				//*返回系统当前的时钟源 
			while(RCC_GetSYSCLKSource() != 0x08); 	 //0x08是当前时钟源 PLL  
}
			RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);  // 关闭蜂鸣器
			GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
			GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;  
			GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
			GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  //输入输出模式(推挽输出)   
			GPIO_Init(GPIOD, &GPIO_InitStructure);  
			GPIO_ResetBits(GPIOD,GPIO_Pin_2);  

			RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);  //打开外设时钟
			GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,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);  
			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);  
			RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);  //TIM2~7是使用的是APB1.TIM1|TIM8使用APB2
		
}  
  


/***GPIO的配置 **/
void GPIO_Configuration(void)
{  
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_0;;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
		GPIO_Init(GPIOC, &GPIO_InitStructure);  
} 


/***嵌套中断控制器的配置**/
void NVIC_Configuration(void)  
{  
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
  /* Enable the TIM2 global Interrupt */  
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;  
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
  NVIC_Init(&NVIC_InitStructure);  
	
   /* Enable the TIM3 global Interrupt */  
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;  
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
  NVIC_Init(&NVIC_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端,负责接收数据

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


/*** TIM2的配置 初始化 ***/
void TIM2_Configuration(void)//四,定时器配置  
{  			
	
		
		TIM_TimeBaseStructure.TIM_Period = 2000;//计19999个数就产生一次更新事件,((1+23999)/72M)*(1+19999)=6.6秒 */
		TIM_TimeBaseStructure.TIM_Prescaler = 36000;//预分频系数为23999,则周期=72MHZ/(23999+1)/2= 1500HZ(1000/1500)= 2/3ms中断溢出一次,预分频数取值范围为0~65535  
		
		TIM_TimeBaseStructure.TIM_ClockDivision = 0;
		TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式,从0开始向上  
		TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);//将配置应用到TIM3中  

}  
  

/*** TIM 3的配置 初始化 ***/
void TIM3_Configuration(void)//四,定时器配置  
{  	
	
	
		TIM_TimeBaseStructure.TIM_Period = 9999;				//计9999个数就产生一次更新事件,((1+35999)/72M)*(1+9999)=5秒 */
		TIM_TimeBaseStructure.TIM_Prescaler = 35999;		//预分频系数为23999,则周期=72MHZ/(35999+1)/2= 1000HZ(1000/1000)= 1s中断溢出一次
		TIM_TimeBaseStructure.TIM_ClockDivision = 0;//  
		TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式,从0开始向上  
		TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);//将配置应用到TIM3中  
			
}  



/* USART发送数据 */
void USART_SendStr(char *str)     
{  
		while((*str)!='\0')             
    {
				USART_SendData(USART1,*str++);  //通过外设 USARTx 发送单个数据 
				while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);  //检查发送完成与否
    }  
}
 



 /***中断 ***/
  
void TIM2_IRQHandler(void) 
{  
    if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)  //中断发生与否
    { 
       		 
        GPIO_ResetBits(GPIOC,GPIO_Pin_0); 
				USART_SendStr("TIM2 interrupt......\r\n"); 
			 TIM_ClearITPendingBit(TIM2, TIM_IT_Update ); 	
    }  
}  

void TIM3_IRQHandler(void)
{  	
    if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)   
    { 
          
        GPIO_ResetBits(GPIOC,GPIO_Pin_4);  
				USART_SendStr("TIM3 interrupt......\r\n"); 
			TIM_ClearITPendingBit(TIM3, TIM_IT_Update );
    }  
}  
  

 


助工
2015-12-29 11:50:45     打赏
8楼

笔记七:PWM&RGB
视频地址:http://player.youku.com/player.php/sid/XMTMwNDQ5NTM3Mg==/v.swf


int main(void)  
{  
		RCC_Configuration(); //RCC时钟配置
		PWM_Config();  //PWM配置
		while(1)
		{	
			PWM_TEST();  //占空比调节
		}  
}

/***RCC的配置(HSE,PLL,AHB,蜂鸣器和GOIOC,TIM使能) ***/
void RCC_Configuration(void)  																
{  
		ErrorStatus HSEStartUpStatus;//HSE是否起振(使能HSE)
		RCC_DeInit();
		RCC_HSEConfig(RCC_HSE_ON);	//选择系统时钟
		HSEStartUpStatus = RCC_WaitForHSEStartUp();
		if(HSEStartUpStatus == SUCCESS)//判断HSE时钟是否稳定
		{														
				RCC_HCLKConfig(RCC_SYSCLK_Div1);  		//AHB使用系统时钟,负责外部存储器的时钟 72M
				RCC_PCLK2Config(RCC_HCLK_Div1); 		//APB2负责AD,I/O,高级TIM,串口1  
				RCC_PCLK1Config(RCC_HCLK_Div2);  		//APB1 负责DA,USB,SPI,I2C,CAN,串口2345,普通TIM
				RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9); 	//  设置PLL输入时钟源及倍频72M
				RCC_PLLCmd(ENABLE); 		
				while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET){}//等待检测
				RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);				//返回系统当前的时钟源 
				while(RCC_GetSYSCLKSource() != 0x08); 	 //0x08是当前时钟源 PLL  
		}
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOA, ENABLE);  // 打开外设时钟
		GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//重映射
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;  //关蜂鸣器
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  //输入输出模式(推挽输出)   
		GPIO_Init(GPIOD, &GPIO_InitStructure);  
		GPIO_ResetBits(GPIOD,GPIO_Pin_2);  

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


void PWM_Config(void)   
{  
		uint16_t PrescalerValue = 0;  uint16_t PeriodValue = 0;
	
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);   //TIM2~7是使用的是APB1时钟.TIM1|TIM8使用APB2
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;//PWM&RGB所在位置 PA1 PA2 PA3  
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
		GPIO_Init(GPIOA, &GPIO_InitStructure);  
		TIM_Cmd(TIM2, ENABLE);  
		 /*
		PrescalerValue = (uint16_t) (72000000/ 36000000) - 1;  //定时器工作频率72M,计数频率36M,定时周期10K
		PeriodValue=(uint16_t) (36000000 / 10000) - 1;
	*/
		TIM_TimeBaseStructure.TIM_Prescaler = 0;  //时钟预分频,定时器工作频率72M,计数频率72M,定时周期1M
		TIM_TimeBaseStructure.TIM_Period = 2400;  
		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脉冲宽度调制模式1
		TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //TIM 输出比较极性高,输出不反向 

		TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  //PA1在通道2
		TIM_OCInitStructure.TIM_Pulse = 0xFFFF;  
		TIM_OC2Init(TIM2, &TIM_OCInitStructure);
 
		TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  // PA2在通道3
		TIM_OCInitStructure.TIM_Pulse =0xFFFF;  
		TIM_OC3Init(TIM2, &TIM_OCInitStructure);  

		TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  // PA3在通道4
		TIM_OCInitStructure.TIM_Pulse = 0xFFFF;  
		TIM_OC4Init(TIM2, &TIM_OCInitStructure);
		
		TIM_ARRPreloadConfig(TIM2, ENABLE);  //使能预装载寄存器
}    
/*调节各通道占空比*/
void PWM_TEST(void)  
{ 

	for(;(temp0>0)||(temp1<3590);temp0--,temp1++) { TIM_SetCompare2(TIM2,temp0); TIM_SetCompare3(TIM2,temp1); delay_ms(2); } for(;(temp2>0)||(temp0<3590);temp2--,temp0++) { TIM_SetCompare2(TIM2,temp0); TIM_SetCompare4(TIM2,temp2); delay_ms(2); } for(;(temp0>0)||(temp2<3590);temp0--,temp2++) { TIM_SetCompare3(TIM2,temp0); TIM_SetCompare4(TIM2,temp2); delay_ms(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); } 





助工
2015-12-29 12:45:20     打赏
9楼

笔记八:ADC


视频地址:http://player.youku.com/player.php/sid/XMTMwNDUwMDQ4OA==/v.swf


int main(void)  
{  
		float Volt=0.00;  
    int ADValue = 0;  
		RCC_Configuration();  
		USART_int(9600);  
    ADC_CONFIG();  
    Get_ADC();  
    PWM_Config();  
    while(1)  
    {  
        ADValue = Get_ADC();  
        Volt = VREF*ADValue/4095;  
        /*volt的值由Volt直接决定 volt是Volt乘以1000的整型值*/  
				USART_SendData(USART1,ADValue++);
        volt=Volt*1000;  
				printf("****************************\r\n");
        printf("* The ADC value is:%d   *\r\n",ADValue);  
        printf("* The Volt is:%f V  *\r\n",Volt);  
        printf("* The volt is:%d        *\r\n",volt); 
				printf("***************************\r\n");
        PWM_TEST();  
        delay_ms(500);  
    }  
} 
void RCC_Configuration(void)  																
{  
		ErrorStatus HSEStartUpStatus;//HSE是否起振(使能HSE)
		RCC_DeInit();
		RCC_HSEConfig(RCC_HSE_ON);	//选择系统时钟
		HSEStartUpStatus = RCC_WaitForHSEStartUp();
		if(HSEStartUpStatus == SUCCESS)//判断HSE时钟是否稳定
		{														
				RCC_HCLKConfig(RCC_SYSCLK_Div1);  		//AHB使用系统时钟,负责外部存储器的时钟 72M
				RCC_PCLK2Config(RCC_HCLK_Div1); 		//APB2负责AD,I/O,高级TIM,串口1  
				RCC_PCLK1Config(RCC_HCLK_Div2);  		//APB1 负责DA,USB,SPI,I2C,CAN,串口2345,普通TIM
				RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9); 	//  设置PLL输入时钟源及倍频72M
				RCC_PLLCmd(ENABLE); 		
				while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET){}//等待检测
				RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);				//返回系统当前的时钟源 
				while(RCC_GetSYSCLKSource() != 0x08); 	 //0x08是当前时钟源 PLL  
		}
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOA, ENABLE);  // 打开外设时钟
		GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//重映射
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;  //关蜂鸣器
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  //输入输出模式(推挽输出)   
		GPIO_Init(GPIOD, &GPIO_InitStructure);  
		GPIO_ResetBits(GPIOD,GPIO_Pin_2);  

		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;              
		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端,负责接收数据
 
		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);              
}  
void PWM_Config(void)   
{  
		//uint16_t PrescalerValue = 0;  uint16_t PeriodValue = 0;
	
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);   //TIM2~7是使用的是APB1时钟.TIM1|TIM8使用APB2
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;//PWM&RGB所在位置 PA1 PA2 PA3  
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
		GPIO_Init(GPIOA, &GPIO_InitStructure);  
		TIM_Cmd(TIM2, ENABLE);  
		 /*
		PrescalerValue = (uint16_t) (72000000/ 36000000) - 1;  //定时器工作频率72M,计数频率36M,定时周期10K
		PeriodValue=(uint16_t) (36000000 / 10000) - 1;
	*/
		TIM_TimeBaseStructure.TIM_Prescaler = 0;  //时钟预分频,定时器工作频率72M,计数频率72M,定时周期1M
		TIM_TimeBaseStructure.TIM_Period = 2400;  
		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脉冲宽度调制模式1
		TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //TIM 输出比较极性高,输出不反向 

		TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  //PA1在通道2
		TIM_OCInitStructure.TIM_Pulse = 0xFFFF;  
		TIM_OC2Init(TIM2, &TIM_OCInitStructure);
 
		TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  // PA2在通道3
		TIM_OCInitStructure.TIM_Pulse =0xFFFF;  
		TIM_OC3Init(TIM2, &TIM_OCInitStructure);  

		TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  // PA3在通道4
		TIM_OCInitStructure.TIM_Pulse = 0xFFFF;  
		TIM_OC4Init(TIM2, &TIM_OCInitStructure);
		
		TIM_ARRPreloadConfig(TIM2, ENABLE);  //使能预装载寄存器
}    

void ADC_CONFIG(void)
{  
	ADC_DeInit(ADC1);  
 
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOB, ENABLE);   
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//ADC所在端口PB0  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//模拟输入模式  
  GPIO_Init(GPIOB, &GPIO_InitStructure);  

  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;  //ADC独立工作模式
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;  	//单通道或多通道扫描
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;  //连续或单次
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;  //是否外部触发来启动
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//ADC数据右对齐  
  ADC_InitStructure.ADC_NbrOfChannel = 1;//ADC通道数为1  
  ADC_Init(ADC1, &ADC_InitStructure);//初始化ADC1  
   
  ADC_DMACmd(ADC1, ENABLE);  //Enable ADC1 DMA 
  ADC_Cmd(ADC1, ENABLE);   // Enable ADC1
}  
  
int Get_ADC(){  

  ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_55Cycles5);//ADC1规则组通道:8 ,转换顺序,采样时间  
 
  ADC_ResetCalibration(ADC1);//重置ADC1的校准寄存器  
  while(ADC_GetResetCalibrationStatus(ADC1));//获取重置校准寄存器的状态,确认校准完成
  ADC_StartCalibration(ADC1);//开始ADC1校准  
  while(ADC_GetCalibrationStatus(ADC1));//获取ADC1的校准寄存器状态,确认校准完成  
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);//使能ADC1软件转换功能  
	
  return ADC_GetConversionValue(ADC1);  //返回最近一次的ADC1规则组转换结果
}  
void PWM_TEST(void)  
{  
    unsigned int temp0=volt,temp1=0,temp2=volt;  
   for(;(temp0>0)||(temp10);temp0++,temp2--)//????  ????  
     {  
       TIM_SetCompare2(TIM2, temp0);//temp0:0~volt  
       TIM_SetCompare4(TIM2, temp2);//temp2:volt~0  
       delay_us(1000);  
   }  
   for(;(temp1>0)||(temp2




助工
2015-12-31 15:07:41     打赏
10楼

笔记九:独立看门狗

#include "rcc.h"
#include "nvic.h"
#include "wdg.h"
#include "gpio.h"
#include "delay.h"
 int main(void)
 {		
	  GPIO_Inits(); 
 		IWDG_Init(4,625);    //与分频数为4,重载值为625,溢出时间为1s	   
	  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);
	while(1)
	{
		if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8))
		{
			IWDG_Feed();//如果WK_UP按下,则喂狗
		}
		delay_ms(10);
	};	 
}
  

void IWDG_Init(u8 prer,u16 rlr) 
{	
 	IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);  //使可以对寄存器IWDG_PR和IWDG_RLR的写操作(也就是取消写保护)
	
	IWDG_SetPrescaler(prer);  //设置IWDG预分频值:设置IWDG预分频值为64
	
	IWDG_SetReload(rlr);  //设置IWDG重装载值
	
	IWDG_ReloadCounter();  //按照IWDG重装载寄存器的值重装载IWDG计数器
	
	IWDG_Enable();  //使能IWDG
}
//喂独立看门狗
void IWDG_Feed(void)
{   
 	IWDG_ReloadCounter();//reload			
							   
}

 


共12条 1/2 1 2 跳转至

回复

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