main.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /************************************************************************************************/
  2. /**
  3. * @file main.c
  4. * @author MCU Ecosystem Development Team
  5. * @brief 该示例展示的SN模块用法。
  6. *
  7. *
  8. **************************************************************************************************
  9. * @attention
  10. * Copyright (c) CEC Huada Electronic Design Co.,Ltd. All rights reserved.
  11. *
  12. **************************************************************************************************
  13. */
  14. /*------------------------------------------includes--------------------------------------------*/
  15. #include "main.h"
  16. #include "common.h"
  17. #include "SN_GPIO.h"
  18. #include "SN_ADC.h"
  19. #include "SN_PWM.h"
  20. #include "SN_EXIT.h"
  21. #include "SN_TIM3_INIT.h"
  22. #include "SN_TIM1_INIT.h"
  23. #include "SN_UART.h"
  24. #include "SN_FLASH.h"
  25. #include "SN_RCC.h"
  26. #include "SN_SPI.h"
  27. #include "SN_DDQ.h"
  28. #include "i2c_bsp.h"
  29. #include "SN_STOP.h"
  30. #include <stdio.h>
  31. #include "myRadio.h"
  32. #include "pan_rf.h"
  33. /*------------------------------------------functions-------------------------------------------*/
  34. #define BOARD_PIN_MOTOR GPIO_PIN_6 //
  35. #define BOARD_PORT_MOTOR GPIOA //
  36. #define BOARD_GPIO_MOTOR BOARD_PORT_MOTOR, BOARD_PIN_MOTOR //
  37. uint16_t ADC_VAL = 0;
  38. float MCU_VDD = 0;
  39. void rfRx_callback(uint8_t status, rfRxPacket_ts packet)
  40. {
  41. switch (status)
  42. {
  43. case RX_STA_SECCESS:
  44. {
  45. printf("receive data:%s\r\n", packet.payload);
  46. RF_SetPreamLen(RF_PREAMBLE_DEFAULT);
  47. myRadio_receiver();
  48. motor_long();
  49. }
  50. break;
  51. case RX_STA_TIMEOUT:
  52. {
  53. RF_SetPreamLen(RF_PREAMBLE_DEFAULT);
  54. myRadio_receiver();
  55. }
  56. break;
  57. case RX_STA_PAYLOAD_ERROR:
  58. {
  59. RF_SetPreamLen(RF_PREAMBLE_DEFAULT);
  60. myRadio_receiver();
  61. }
  62. break;
  63. case TX_STA_SECCESS:
  64. {
  65. RF_SetPreamLen(RF_PREAMBLE_DEFAULT);
  66. myRadio_receiver();
  67. motor_short();
  68. }
  69. break;
  70. default:
  71. break;
  72. }
  73. }
  74. void motor_short(void)
  75. {
  76. SN_GPIO_PIN_write(BOARD_GPIO_MOTOR, 1);
  77. std_delayms(50);
  78. SN_GPIO_PIN_write(BOARD_GPIO_MOTOR, 0);
  79. }
  80. void motor_long(void)
  81. {
  82. SN_GPIO_PIN_write(BOARD_GPIO_MOTOR, 1);
  83. std_delayms(200);
  84. SN_GPIO_PIN_write(BOARD_GPIO_MOTOR, 0);
  85. }
  86. uint32_t sendTimeout = 0;
  87. int main(void)
  88. {
  89. SN_SYSCLK_set(SYSCLK_48MHZ); //设置系统时钟为48MHZ
  90. std_delay_init(); //初始化延时(该延时是使用滴答定时器实现的)
  91. std_delayms(500);
  92. SN_UART_init(UART1,921600, UART1_RX_PA2, UART1_TX_PB6); //初始化串口
  93. printf("uart init\r\n");
  94. myRadio_init(0, rfRx_callback);
  95. SN_GPIO_PIN_init(BOARD_GPIO_MOTOR , GPIO_MODE_OUTPUT ,GPIO_PULLUP ,GPIO_OUTPUT_PUSHPULL);
  96. motor_long();
  97. // myRadio_setFrequency(SET_RF_FREQ_HZ(rfBaseFreqList[deviceInfor.chipType], deviceInfor.rfChannel, deviceInfor.channelStep));
  98. // myRadio_setTxPower(rfTxPowerList[deviceInfor.txPower]);
  99. // myRadio_setRfParams(deviceInfor.rf_sf, deviceInfor.rf_bw, deviceInfor.rf_cr);
  100. // myRadio_setSyncWord(0x45);
  101. // myRadio_receiver();
  102. // std_delayms(500);
  103. // myRadio_transmitArray("hello world", 10);
  104. // SN_ADC_IN_init(ADC_CHANNEL_4_PA7); //初始化ADC
  105. // SN_PWM_TIM1_OUT_Complementary(TIM1_CH2N_PA0); //设置互补通道,注意要先设定互补通道后,才能初始化pwm输出
  106. // SN_PWM_TIM1_OUT_init(TIM1_CH2_PA1,1000); //设置定时器3通道1 和 通道2 输出占空比数是1000的方波,输出io分别是PA0 ,PA4
  107. // SN_UART_init(UART1,9600, UART1_RX_PA4 ,UART1_TX_PA3 ); //初始化串口
  108. printf("power on\r\n");
  109. while(1){
  110. std_delayms(1);
  111. // SN_ADC_start(); //此时编译的SN_ADC_star()是轮询处理
  112. // ADC_VAL = SN_ADC_Get_float(0); //PA7通道电压值
  113. // MCU_VDD = SN_ADC_MCU_VDD(); //获取MCU的当前vdd电压
  114. if (sendTimeout++ > 1000+rand()%100)
  115. {
  116. sendTimeout = 0;
  117. RF_SetPreamLen(50);
  118. myRadio_transmitArray("hello world", 10);
  119. printf("send rf data\r\n");
  120. }
  121. myRadio_process();
  122. }
  123. }