实例介绍
【实例简介】
【实例截图】
【核心代码】
/**
******************************************************************************
* 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****/
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论