基本信息
源码名称:RF2401发码源程序
源码大小:0.04M
文件格式:.zip
开发语言:C/C++
更新时间:2018-01-15
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
/**
******************************************************************************
* File Name : main.c
* Description : Main program body
******************************************************************************
*
* COPYRIGHT(c) 2017 STMicroelectronics
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f0xx_hal.h"
#include "adc.h"
#include "dma.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
/* USER CODE BEGIN Includes */
#include "nRF24L01.h"
#include "user.h"
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
unsigned char tmp_buf[8];
char RxBufferPre[BUFFER_SIZE],RxBuffer[BUFFER_SIZE];
uint8_t rx_len,recv_end_flag;
uint8_t TIMER1,TIMER3,resflag,timtick,timtick_10ms;
volatile uint16_t get_usart_data;
//volatile uint32_t get_IDcode1;//,haveBAT2,calres;
volatile uint8_t i,temp8,Opcode,RF_CHANNAL;
unsigned char tmp_buf[8];//={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void Error_Handler(void);
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
int fputc(int ch,FILE *f)
{
uint8_t temp[1]={ch};
HAL_UART_Transmit(&huart1,temp,1,2);
}
////////////////////////////////////
uint8_t sendata(uint8_t Operadata,uint8_t RF_CHANNAL,uint16_t idcode) //Master send
{uint16_t i,temp; uint16_t preIDcode;uint8_t stat;
unsigned char senddata[TX_PLOAD_WIDTH]={0x55,0xAE,0x80,0xF0,0x0C,0xE0,0x26,0x80};
LED_OFF;
TX_Mode(RF_CHANNAL);
HAL_Delay(1);
senddata[2]=Operadata;
senddata[4]=idcode>>8;
senddata[5]=idcode;
senddata[6]=(senddata[2] senddata[4] senddata[5])>>8;
senddata[7]=(senddata[2] senddata[4] senddata[5]);
for(i=0;i<TX_PLOAD_WIDTH;i )
{
tmp_buf[i]=senddata[i];
}
NRF24L01_TxPacket(tmp_buf);
//HAL_UART_Transmit(&huart1, (uint8_t*)&stat,1,0x1F);
HAL_Delay(1);//must delay
//stat=0;
RX_Mode(RF_CHANNAL);
do{i ;
if(NRF24L01_RxPacket(tmp_buf)==0)//一旦接收到信息,则显示出来.
{////0x55,0xAE,0x80,0xF0,0x0C,0xE0,0x26,0x80
temp=0;
preIDcode=0;
temp|=tmp_buf[6];
temp<<=8;
temp|=tmp_buf[7];
preIDcode|=tmp_buf[4];
preIDcode<<=8;
preIDcode|=tmp_buf[5];
LED_ON;
if((temp==(tmp_buf[2] tmp_buf[4] tmp_buf[5]))&&(tmp_buf[1]==0xAE)&&(idcode==preIDcode))
return 0xAC; //return ok
else
return 0xAF; //return fail
}
}while(i<200);
i=0;
return 0xFF;
}
/* USER CODE END 0 */
int main(void)
{
/* USER CODE BEGIN 1 */
get_usart_data=0;
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_ADC_Init();
MX_TIM1_Init();
MX_TIM3_Init();
MX_TIM15_Init();
MX_TIM16_Init();
MX_TIM17_Init();
MX_USART1_UART_Init();
MX_TIM14_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
nRF24L01_Initial();
while(NRF24L01_Check())//检测不到24L01
{
HAL_UART_Transmit(&huart1, (uint8_t*)("24L01 Check Failed!\n"),21,0x1F);
HAL_Delay(1000);
}
HAL_UART_Transmit(&huart1, (uint8_t*)("24L01 Ready!"),12,0x1F);
HAL_UART_Receive_DMA(&huart1,(uint8_t*)&RxBufferPre,1);
//if(HAL_UART_Receive_DMA(&huart1, (uint8_t*)RxBufferPre,1)!= HAL_OK) Error_Handler();
HAL_UART_Transmit(&huart1, (uint8_t*)("YPZN_ST"),7,0x1F);
if(PB11==1)
{while (1)
{ //usart_datacheck();
do{i ;
HAL_Delay(50);
temp8=sendata(0x80,0x01,0x1233); //RF_CHANNAL setting by usart
if (temp8==0xAC)
break;
}while(i<5);
i=0;
HAL_UART_Transmit(&huart1, (uint8_t*)&temp8,1,0x1F);
HAL_Delay(500);
do{i ;
HAL_Delay(50);
temp8=sendata(0x80,0x20,0x1233); //RF_CHANNAL setting by usart
if (temp8==0xAC)
break;
}while(i<5);
i=0;
HAL_UART_Transmit(&huart1, (uint8_t*)&temp8,1,0x1F);
}
}
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
usart_datacheck();
if(get_usart_data!=0){
temp8=sendata(Opcode,RF_CHANNAL,get_usart_data); //RF_CHANNAL setting by usart
do{i ;
HAL_Delay(50);
temp8=sendata(Opcode,RF_CHANNAL,get_usart_data); //RF_CHANNAL setting by usart
if (temp8==0xAC)
break;
}while(i<5);
i=0;
HAL_UART_Transmit(&huart1, (uint8_t*)&temp8,1,0x1F);
get_usart_data=0;
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/** System Clock Configuration
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInit;
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSI14;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSI14State = RCC_HSI14_ON;
RCC_OscInitStruct.HSICalibrationValue = 16;
RCC_OscInitStruct.HSI14CalibrationValue = 16;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
/**Configure the Systick interrupt time
*/
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
/**Configure the Systick
*/
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @param None
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler */
/* User can add his own implementation to report the HAL error return state */
while(1)
{
}
/* USER CODE END Error_Handler */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/