⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 custom.c

📁 STM32驱动nrf24l01
💻 C
字号:
#include "stm32f10x_rcc.h"
#include "stm32f10x_flash.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_conf.h"
#include "stm32f10x_usart.h"

void delay_u(unsigned long n)		//延时 n us: n>=6,最小延时单位6us
{
	unsigned long j;
	while(n--)						//外部晶振:8M;PLL: 9; 8M * 9=72MHz
	{
		j=8;				  		//调整参数,保证延时的精度
		while(j--);
	}
}

void delay_m(unsigned long n)	 	//延时 n ms
{									
	while(n--)						//外部晶振:8M;PLL: 9; 8M * 9=72MHz
		delay_u(1100);				//1ms延时补偿
}

/*******时钟配置函数*******/
void RCC_Configuration(void)
{
	
	unsigned long HSEStartUpStatus;

	/*将外设 RCC 寄存器重设为默认值,即有关寄存器复位,但该函数不改动寄存器 
	  RCC_CR的HSITRIM[4:0] 位,也不重置寄存器 RCC_BDCR 和寄存器 RCC_CSR */
	RCC_DeInit();

	/*使能外部 HSE 高速晶振*/
	RCC_HSEConfig(RCC_HSE_ON);

	/*等待 HSE 高速晶振稳定,或者在超时的情况下退出*/
	HSEStartUpStatus = RCC_WaitForHSEStartUp();

	/*SUCCESS : HSE 晶振稳定且就绪,ERROR : HSE 晶振未就绪*/
	if(HSEStartUpStatus == SUCCESS)
	{
	   /*HCLK = SYSCLK 设置高速总线时钟=系统时钟*/
	   RCC_HCLKConfig(RCC_SYSCLK_Div1);

	   /*PCLK = HCKL 设置低速总线2时钟=高速总线时钟*/
	   RCC_PCLK2Config(RCC_HCLK_Div1);

	   /*PCLK1 = HCLK/2 设置低速总线1的时钟=高速总线时钟的二分频*/
	   RCC_PCLK1Config(RCC_HCLK_Div2);

	   /*设置 FLASH 存储器延时时钟周期数,2是针对高频时钟的,
	     FLASH_Latency_0 : 0 延时周期,FLASH_Latency_1 : 1延时周期
	     FLASH_Latency_2 : 2 延时周期*/
//	   FLASH_SetLatency(FLASH_Latency_2);

	   /*使能 flash 预取指令缓冲区.这两句跟 RCC 没有直接关系*/
//	   FLASH_perfetchBufferCmd(FLSAH_PerfetchBuffer_Enable);

	   /*Set PLL clock output to 72MHz using HSE(8MHz) as entry clock */
	   /*利用锁相环将 HSE 外部 8MHz 晶振9倍频到 72MHz */
	   RCC_PLLConfig(RCC_PLLSource_HSE_Div1 , RCC_PLLMul_9);

	   /*Enable PLL : 使能PLL锁相环*/
	   RCC_PLLCmd(ENABLE);

	   /*Wait till PLL is ready : 等待锁相环输出稳定*/
	   /*RCC_FLAG_HSIRDY : HSI 晶振就绪,RCC_FLAG_HSERDY : HSE 晶振就绪
	     RCC_FLAG_PLLRDY : PLL 就绪,RCC_FLAG_LSERDY : LSE 晶振就绪
		 RCC_FLAG_LSIRDY : LSI 晶振就绪,RCC_FLAG_PINRET : 引脚复位
		 RCC_FLAG_PORRST : POR/PDR 复位,RCC_FLAG_SFTRST : 软件复位
		 RCC_FLAG_IWDGRST : IWDG 复位,RCC_FLAG_WWDGRST : WWDG 复位
		 RCC_FLAG_LPWRRST : 低功耗复位*/
		while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
		
		/*Select PLL as system clock source : 将锁相环输出设置为系统时钟*/
		/*RCC_SYSCLKSource_HSI : 选择 HSI 作为系统时钟
		  RCC_SYSCLKSource_HSE : 选择 HSE 作为系统时钟
		  RCC_SYSCLKSource_PLLCLK : 选择 PLL 作为系统时钟*/
		RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
		
		/*等待PLL作为系统时钟标志位置位*/
		/*0x00 : HSI 作为系统时钟;0x04 : HSE 作为系统时钟
		  0x08 : PLL 作为系统时钟*/
		while(RCC_GetSYSCLKSource() != 0x08);
		
		/*ADC 时钟设为 12MHz,即系统时钟的6分频*/
//		RCC_ADCCLKConfig(RCC_PCLK2_Div6);				
	}

	/*Enable GPIOA~E and AFIO clock : 使能外围端口总线时钟*/
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
			               RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD |
			               RCC_APB2Periph_GPIOE | 
						   			 RCC_APB2Periph_AFIO , ENABLE);

	/*USART1 clock enable : USART1 时钟使能*/
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
 
	/*TIM1 clock enable : TIM1 时钟使能*/
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);

	/*TIM2 clock enable : TIM2 时钟使能*/
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);

	/*ADC1 clock enable : ADC1 时钟使能*/
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 , ENABLE);

	/*SPI1 clock enable : SPI1 时钟使能*/
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 , ENABLE);

	/*SPI2 clock enable : SPI2 时钟使能*/
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2 , ENABLE);
	
	/*FSMC clock enable : FSMC 时钟使能*/
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE); 
	
	/* Enable Key Button GPIO Port, GPIO_LED and AFIO clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
}


/***********定时器配置函数************/
void Time_Init(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
	TIM_DeInit(TIM2);
	TIM_TimeBaseStructure.TIM_Period = 35999;
	TIM_TimeBaseStructure.TIM_Prescaler = 1999;
	TIM_TimeBaseStructure.TIM_ClockDivision = 0x00;
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
	
	TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
	TIM_ClearFlag(TIM2,TIM_FLAG_Update);
	TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
	TIM_Cmd(TIM2,ENABLE);
}


/***********中断配置函数************/
void NVIC_Configuration(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;
/*-------------Timer2 IRQ Init-------------------------------*/
	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);
	

/*-------------Configure the NVIC Preemption Priority Bits---*/  
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  
/*-------------Enable the USART1 Interrupt-------------------*/
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

/*-------------Enable the EXTI Interrupt---------------------*/	
  /* Enable the EXTI1 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
	
		 
  /* Enable the EXTI9_5 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn  ;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  
}

/**********EXTI配置函数***********/
void EXTI_Configuration(void)
{
	EXTI_InitTypeDef EXTI_InitStructure;

	GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);
  EXTI_InitStructure.EXTI_Line = EXTI_Line1;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);	 

	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource6);
  /* Configure Key Button EXTI Line to generate an interrupt on falling edge */  
  EXTI_InitStructure.EXTI_Line = EXTI_Line6;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);
}


/**********USART配置函数**********/
void USART_Configuration(void)
{
	USART_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate = 9600;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	//USART_InitStructure.USART_Clock = USART_Clock_Disable;
	//USART_InitStructure.USART_CPOL = USART_CPOL_Low;
	//USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;
	//USART_InitStructure.USART_LastBit = USART_LastBit_Disable;
	USART_Init(USART1,&USART_InitStructure);
	USART_ClearFlag(USART1,USART_FLAG_TC);
	USART_Cmd(USART1,ENABLE);
	USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); 
}

void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

/*-------------LCD-RST-------------------------------------------------*/				     
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ; 	 
  GPIO_Init(GPIOE, &GPIO_InitStructure);  	
  
/*-------------SPI1 GPIO configuration---------------------------------*/  
  //PB10, PD9, PD10   CS,SI,CLK
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_7;				//PB7--CS
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_7 ;  	//PA5--SCK	PA7--MOSI
  GPIO_Init(GPIOA, &GPIO_InitStructure);
 
	/*SPI1 MISO	,	PENIRQ */
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6;	 //PA6--MISO
  GPIO_Init(GPIOA, &GPIO_InitStructure);	
	/****************************************************************************/
  /* Configure SPI2 pins: SCK, MISO and MOSI */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Configure PB.12 as Output push-pull, used as Flash Chip select */
	/*
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
	*/

/*------------SPI2 GPIO configuration---------------------------------------------*/
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;	
  GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	GPIO_SetBits(GPIOA,GPIO_Pin_14 | GPIO_Pin_15);
	GPIO_ResetBits(GPIOA,GPIO_Pin_13);
			
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;		 	//PB14 -- MISO
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  
	/*SPI2 GPIO Init  PB.13 -- SCK	PB.14 -- MISO  PB.15 -- MOSI*/																				 
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;	
  GPIO_Init(GPIOB, &GPIO_InitStructure);

	/*nrf24l01 IRQ Init  PA.1 -- IRQ*/
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
	GPIO_SetBits(GPIOA,GPIO_Pin_1);
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

	/*nrf23l01 CE Init  PA.2 -- CE*/ 
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
	GPIO_ResetBits(GPIOA,GPIO_Pin_2);

	/*nrf23l01 CSN Init  PA.3 -- CSN*/	
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
	GPIO_SetBits(GPIOA,GPIO_Pin_3);
  																				 


/*--FSMC GPIO Configuration ------------------------------------------------------*/  
 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5 |
                                GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_14 | 
                                GPIO_Pin_15;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOD, &GPIO_InitStructure);

  /* Set PE.07(D4), PE.08(D5), PE.09(D6), PE.10(D7), PE.11(D8), PE.12(D9), PE.13(D10),
     PE.14(D11), PE.15(D12) as alternate function push pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | 
                                GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | 
                                GPIO_Pin_15;
  GPIO_Init(GPIOE, &GPIO_InitStructure); 
  
  /* NE1 configuration */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; 
  GPIO_Init(GPIOD, &GPIO_InitStructure);
  
  /* RS */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 ; 
  GPIO_Init(GPIOD, &GPIO_InitStructure); 

  GPIO_SetBits(GPIOD, GPIO_Pin_7);			//CS=1 
  GPIO_ResetBits(GPIOE, GPIO_Pin_0);
  GPIO_ResetBits(GPIOE, GPIO_Pin_1);		//RESET=0
  GPIO_SetBits(GPIOD, GPIO_Pin_4);		    //RD=1
  GPIO_SetBits(GPIOD, GPIO_Pin_5);			//WR=1

  GPIO_SetBits(GPIOB, GPIO_Pin_6);			//PEn
  GPIO_ResetBits(GPIOD, GPIO_Pin_13);			//LIGHT
  GPIO_SetBits(GPIOC, GPIO_Pin_4);			//SPI CS1
  GPIO_SetBits(GPIOB, GPIO_Pin_12);			//SPI CS4
  GPIO_SetBits(GPIOA, GPIO_Pin_4);			//SPI NSS

	/*----------USART GPIO Configuration-------------------------*/
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure); 

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure); 
}


void SPI_Configuration(void) 
{ 					
	SPI_InitTypeDef  SPI_InitStructure;
	/*SPI2 configuration*/   
	SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;   
	SPI_InitStructure.SPI_Mode = SPI_Mode_Master;   
	SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;   
	SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;   
	SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;   
	SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;   
	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;   
	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; 
  SPI_InitStructure.SPI_CRCPolynomial = 7;  
	SPI_Init(SPI2, &SPI_InitStructure);       
	SPI_Cmd(SPI2, ENABLE); 
	
	/*SPI1 configuration*/   	
  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_Init(SPI1, &SPI_InitStructure);
  SPI_Cmd(SPI1, ENABLE); 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -