久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1890|回復: 0
打印 上一主題 下一主題
收起左側

STM8藍牙串口程序 發送端

[復制鏈接]
跳轉到指定樓層
樓主
ID:413663 發表于 2018-10-25 20:57 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
藍牙串口程序

單片機源程序如下:
  1. /**
  2.   ******************************************************************************
  3.   * @file UART2_Printf\main.c
  4.   * @brief This file contains the main function for: retarget the C library printf
  5.   *        /scanf functions to the UART2 example.
  6.   * @author  MCD Application Team
  7.   * @version V2.0.0
  8.   * @date    25-February-2011
  9.   *******************************************************************************/

  10. /* Includes ------------------------------------------------------------------*/
  11. #include "nRF24L01.h"
  12. #include "stm8s.h"
  13. #include "stdio.h"
  14. /**
  15.   * @addtogroup UART2_Printf
  16.   * @{
  17.   */
  18. /* Private typedef -----------------------------------------------------------*/
  19. /* Private define ------------------------------------------------------------*/
  20. /* _IAR_ */
  21. #define PUTCHAR_PROTOTYPE int putchar (int c)
  22. #define GETCHAR_PROTOTYPE int getchar (void)
  23. /* _RAISONANCE_ */
  24. /* Private macro -------------------------------------------------------------*/
  25. /* Private variables ---------------------------------------------------------*/
  26. /* Private function prototypes -----------------------------------------------*/
  27. /* Private functions ---------------------------------------------------------*/
  28. /**
  29.   * @brief  Main program.
  30.   * @param  None
  31.   * @retval None
  32.   */
  33. void delayms(unsigned int time)
  34. {
  35.     unsigned int i;
  36.     while(time--)   
  37.     for(i=900;i>0;i--)
  38.     inerDelay_us(1);
  39. }

  40. void GPIO_Configuration(void)
  41. {
  42.     //無線模塊的IO口初始化
  43.     //MISO 讀取數據  浮動輸入,沒有外部中斷
  44.     GPIO_Init(NRF24L01_CE_PORT, (GPIO_Pin_TypeDef)(NRF24L01_CE_PIN ), GPIO_MODE_OUT_PP_HIGH_FAST);//ce
  45.     GPIO_Init(NRF24L01_CS_PORT, (GPIO_Pin_TypeDef)(NRF24L01_CS_PIN ), GPIO_MODE_OUT_PP_HIGH_FAST);//cs
  46.     GPIO_Init(NRF24L01_SCK_PORT, (GPIO_Pin_TypeDef)(NRF24L01_SCK_PIN ), GPIO_MODE_OUT_PP_HIGH_FAST);//sck
  47.     GPIO_Init(NRF24L01_MOSI_PORT, (GPIO_Pin_TypeDef)(NRF24L01_MOSI_PIN ), GPIO_MODE_OUT_PP_HIGH_FAST);//MOSI
  48.     GPIO_Init(NRF24L01_MISO_PORT, (GPIO_Pin_TypeDef)(NRF24L01_MISO_PIN), GPIO_MODE_IN_FL_NO_IT);//MISO
  49.     GPIO_Init(NRF24L01_IRQ_PORT, (GPIO_Pin_TypeDef)(NRF24L01_IRQ_PIN ), GPIO_MODE_IN_FL_NO_IT); //IRQ
  50.      // GPIO_Init(NRF24L01_MISO_PORT, (GPIO_Pin_TypeDef)(NRF24L01_MISO_PIN), GPIO_MODE_OUT_PP_HIGH_FAST);//MISO
  51.     //GPIO_Init(NRF24L01_IRQ_PORT, (GPIO_Pin_TypeDef)(NRF24L01_IRQ_PIN ), GPIO_MODE_OUT_PP_HIGH_FAST); //IRQ
  52.    // GPIO_Init(GPIOA, (GPIO_Pin_TypeDef)(GPIO_PIN_6), GPIO_MODE_OUT_PP_HIGH_FAST);//ce
  53.     GPIO_Init(GPIOD, (GPIO_Pin_TypeDef)(GPIO_PIN_3), GPIO_MODE_OUT_PP_HIGH_FAST);//ce
  54. }

  55. void Delay(uint16_t nCount)
  56. {
  57.   /* Decrement nCount value */
  58.   while (nCount != 0)
  59.   {
  60.     nCount--;
  61.   }
  62. }

  63. void init_io(void)
  64. {
  65.     delayms(2000);          //延時 100us
  66.     GPIO_WriteHigh(NRF24L01_CE_PORT, NRF24L01_CE_PIN);
  67.     GPIO_WriteHigh(NRF24L01_CS_PORT, NRF24L01_CS_PIN);
  68.     GPIO_WriteLow(NRF24L01_SCK_PORT, NRF24L01_SCK_PIN);
  69. }

  70. void main(void)
  71. {

  72.         uchar TxBuf[TX_PLOAD_WIDTH]={0x01,0x01,0x01,0x01,0x01};
  73.         char ans;
  74.         GPIO_Configuration();
  75.         SPI_DeInit();
  76.        /* Initialize SPI in Slave mode  */
  77.         SPI_Init(SPI_FIRSTBIT_MSB, SPI_BAUDRATEPRESCALER_2, SPI_MODE_MASTER, SPI_CLOCKPOLARITY_LOW,
  78.              SPI_CLOCKPHASE_1EDGE, SPI_DATADIRECTION_2LINES_FULLDUPLEX, SPI_NSS_SOFT,(uint8_t)0x07);  
  79.         /* Enable the SPI*/
  80.         SPI_Cmd(ENABLE);
  81.    
  82.         CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); //16MHz高速內部RC振蕩器(HSI)
  83.         init_io();
  84.         GPIO_WriteLow(GPIOD, GPIO_PIN_3);
  85.         GPIO_WriteHigh(GPIOA, GPIO_PIN_6);
  86.         
  87.         TxBuf[0]=0x01;
  88.        //  GPIO_WriteHigh(GPIOD, GPIO_PIN_3);
  89.         /* UART2 configuration ------------------------------------------------------*/
  90.          UART2_DeInit();
  91.         /* UART2 configured as follow:
  92.         - BaudRate = 115200 baud  
  93.         - Word Length = 8 Bits
  94.         - One Stop Bit
  95.         - No parity
  96.         - Receive and transmit enabled
  97.         - UART2 Clock disabled
  98.          */  
  99.         UART2_Init((uint32_t)9600, UART2_WORDLENGTH_8D, UART2_STOPBITS_1, UART2_PARITY_NO,
  100.                                   UART2_SYNCMODE_CLOCK_DISABLE, UART2_MODE_TXRX_ENABLE);
  101.       
  102.         /* 前頭檢測 ------------------------------------------------------*/
  103.         printf("\n\r正在檢測系統設備...\n\r");

  104.         delayms(500);
  105.         if(NRF24L01_Check()==0)
  106.         {
  107.            delayms(500);
  108.            printf("\n\r/********************************* \n\r");
  109.            printf("\n\r>>>歡迎使用韭菜門禁系統<<<\n\r");
  110.            printf("\n\r/********************************* \n\r");
  111.         }
  112.         else  printf("\n\r>>>請檢查設備是否安裝正確<<<\n\r");  
  113.          
  114.         printf("\n\r請輸入命令:\n\r");
  115.         printf("\n\r門禁>> 請輸入:1\n\r");
  116.         printf("\n\r門開>> 請輸入:2\n\r");
  117.         printf("\n\r報警>> 請輸入:3\n\r");
  118.        while(1)
  119.         {
  120.              ans = getchar();
  121.             if(ans=='1'|ans=='2'|ans=='3')
  122.               {
  123.                
  124.                 TxBuf[0]=ans-'0';
  125.                 printf("\n\r合法輸入\n\r");
  126.                 if(nRF24L01_TxPacket(TxBuf)==TX_OK)
  127.                  {         
  128.                   GPIO_WriteHigh(GPIOD, GPIO_PIN_0);
  129.                   GPIO_WriteHigh(GPIOD, GPIO_PIN_2);
  130.                   GPIO_WriteHigh(GPIOD, GPIO_PIN_3);
  131.                   Delay(0xFFFF);  //延時
  132.                   GPIO_WriteLow(GPIOD, GPIO_PIN_0);
  133.                   GPIO_WriteLow(GPIOD, GPIO_PIN_2);
  134.                   GPIO_WriteLow(GPIOD, GPIO_PIN_3);
  135.                   printf("\n 發送成功 \n\r");
  136.                   delayms(200);
  137.                   }
  138.                 else printf("\n\r發送失敗\n\r");
  139.          
  140.               }
  141.             else printf("\n\r請輸入正確的命令\n\r");
  142.         }
  143. }
  144.       
  145. /**
  146.   * @brief Retargets the C library printf function to the UART.
  147.   * @param c Character to send
  148.   * @retval char Character sent
  149.   */
  150. PUTCHAR_PROTOTYPE
  151. {
  152.   /* Write a character to the UART2 */
  153.   UART2_SendData8(c);
  154.   /* Loop until the end of transmission */
  155.   while (UART2_GetFlagStatus(UART2_FLAG_TXE) == RESET);

  156.   return (c);
  157. }

  158. /**
  159.   * @brief Retargets the C library scanf function to the USART.
  160.   * @param None
  161.   * @retval char Character to Read
  162.   */
  163. GETCHAR_PROTOTYPE
  164. {
  165. #ifdef _COSMIC_
  166.   char c = 0;
  167. #else
  168.   int c = 0;
  169. #endif
  170.   /* Loop until the Read data register flag is SET */
  171.   while (UART2_GetFlagStatus(UART2_FLAG_RXNE) == RESET);
  172.     c = UART2_ReceiveData8();
  173.   return (c);
  174. }

  175. #ifdef USE_FULL_ASSERT

  176. /**
  177.   * @brief  Reports the name of the source file and the source line number
  178.   *   where the assert_param error has occurred.
  179.   * @param file: pointer to the source file name
  180.   * @param line: assert_param error line source number
  181.   * @retval None
  182.   */
  183. void assert_failed(uint8_t* file, uint32_t line)
  184. {
  185.   /* User can add his own implementation to report the file name and line number,
  186.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  187.   /* Infinite loop */
  188.   while (1)
  189.   {
  190.   }
  191. }
  192. #endif

  193. /**
  194.   * @}
  195.   */

  196. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
復制代碼

所有資料51hei提供下載:
發送端.zip (12.54 KB, 下載次數: 12)


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 一级毛片视频 | 欧美一级片在线看 | 久久国产综合 | 国产精品久久久 | 国产精品永久免费 | 国产精品一级 | 国产资源一区二区三区 | 国产一区免费视频 | 日本视频一区二区 | 久久久人 | 国产精品毛片一区二区在线看 | 四虎永久在线精品免费一区二 | 在线不卡视频 | 亚洲免费精品 | 成人性生交大片 | 在线不卡视频 | a在线观看免费 | 国产精品中文在线 | 亚洲精品一区二区在线观看 | 日韩一级电影免费观看 | 亚洲精品国产区 | 国产精品久久久久久久久免费相片 | 日本黄色一级视频 | 日韩在线不卡 | 精品国产乱码久久久久久中文 | 五月天国产视频 | 91精品国产综合久久久动漫日韩 | 毛片一区二区三区 | 精品九九| 91精品欧美久久久久久久 | 免费在线a视频 | 99精品视频免费观看 | 久久久久久久久久久久久9999 | 免费一区 | 成人免费观看男女羞羞视频 | 国产日产精品一区二区三区四区 | 精品欧美一区二区中文字幕视频 | 欧美aa在线 | 久热国产精品 | 国产美女精品视频 | 在线观看欧美一区 |