蓝牙串口程序
单片机源程序如下:
- /**
- ******************************************************************************
- * @file UART2_Printf\main.c
- * @brief This file contains the main function for: retarget the C library printf
- * /scanf functions to the UART2 example.
- * @author MCD Application Team
- * @version V2.0.0
- * @date 25-February-2011
- *******************************************************************************/
- /* Includes ------------------------------------------------------------------*/
- #include "nRF24L01.h"
- #include "stm8s.h"
- #include "stdio.h"
- /**
- * @addtogroup UART2_Printf
- * @{
- */
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* _IAR_ */
- #define PUTCHAR_PROTOTYPE int putchar (int c)
- #define GETCHAR_PROTOTYPE int getchar (void)
- /* _RAISONANCE_ */
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
- /**
- * @brief Main program.
- * @param None
- * @retval None
- */
- void delayms(unsigned int time)
- {
- unsigned int i;
- while(time--)
- for(i=900;i>0;i--)
- inerDelay_us(1);
- }
- void GPIO_Configuration(void)
- {
- //无线模块的IO口初始化
- //MISO 读取数据 浮动输入,没有外部中断
- GPIO_Init(NRF24L01_CE_PORT, (GPIO_Pin_TypeDef)(NRF24L01_CE_PIN ), GPIO_MODE_OUT_PP_HIGH_FAST);//ce
- GPIO_Init(NRF24L01_CS_PORT, (GPIO_Pin_TypeDef)(NRF24L01_CS_PIN ), GPIO_MODE_OUT_PP_HIGH_FAST);//cs
- GPIO_Init(NRF24L01_SCK_PORT, (GPIO_Pin_TypeDef)(NRF24L01_SCK_PIN ), GPIO_MODE_OUT_PP_HIGH_FAST);//sck
- GPIO_Init(NRF24L01_MOSI_PORT, (GPIO_Pin_TypeDef)(NRF24L01_MOSI_PIN ), GPIO_MODE_OUT_PP_HIGH_FAST);//MOSI
- GPIO_Init(NRF24L01_MISO_PORT, (GPIO_Pin_TypeDef)(NRF24L01_MISO_PIN), GPIO_MODE_IN_FL_NO_IT);//MISO
- GPIO_Init(NRF24L01_IRQ_PORT, (GPIO_Pin_TypeDef)(NRF24L01_IRQ_PIN ), GPIO_MODE_IN_FL_NO_IT); //IRQ
- // GPIO_Init(NRF24L01_MISO_PORT, (GPIO_Pin_TypeDef)(NRF24L01_MISO_PIN), GPIO_MODE_OUT_PP_HIGH_FAST);//MISO
- //GPIO_Init(NRF24L01_IRQ_PORT, (GPIO_Pin_TypeDef)(NRF24L01_IRQ_PIN ), GPIO_MODE_OUT_PP_HIGH_FAST); //IRQ
- // GPIO_Init(GPIOA, (GPIO_Pin_TypeDef)(GPIO_PIN_6), GPIO_MODE_OUT_PP_HIGH_FAST);//ce
- GPIO_Init(GPIOD, (GPIO_Pin_TypeDef)(GPIO_PIN_3), GPIO_MODE_OUT_PP_HIGH_FAST);//ce
- }
- void Delay(uint16_t nCount)
- {
- /* Decrement nCount value */
- while (nCount != 0)
- {
- nCount--;
- }
- }
- void init_io(void)
- {
- delayms(2000); //延时 100us
- GPIO_WriteHigh(NRF24L01_CE_PORT, NRF24L01_CE_PIN);
- GPIO_WriteHigh(NRF24L01_CS_PORT, NRF24L01_CS_PIN);
- GPIO_WriteLow(NRF24L01_SCK_PORT, NRF24L01_SCK_PIN);
- }
- void main(void)
- {
- uchar TxBuf[TX_PLOAD_WIDTH]={0x01,0x01,0x01,0x01,0x01};
- char ans;
- GPIO_Configuration();
- SPI_DeInit();
- /* Initialize SPI in Slave mode */
- SPI_Init(SPI_FIRSTBIT_MSB, SPI_BAUDRATEPRESCALER_2, SPI_MODE_MASTER, SPI_CLOCKPOLARITY_LOW,
- SPI_CLOCKPHASE_1EDGE, SPI_DATADIRECTION_2LINES_FULLDUPLEX, SPI_NSS_SOFT,(uint8_t)0x07);
- /* Enable the SPI*/
- SPI_Cmd(ENABLE);
-
- CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); //16MHz高速内部RC振荡器(HSI)
- init_io();
- GPIO_WriteLow(GPIOD, GPIO_PIN_3);
- GPIO_WriteHigh(GPIOA, GPIO_PIN_6);
-
- TxBuf[0]=0x01;
- // GPIO_WriteHigh(GPIOD, GPIO_PIN_3);
- /* UART2 configuration ------------------------------------------------------*/
- UART2_DeInit();
- /* UART2 configured as follow:
- - BaudRate = 115200 baud
- - Word Length = 8 Bits
- - One Stop Bit
- - No parity
- - Receive and transmit enabled
- - UART2 Clock disabled
- */
- UART2_Init((uint32_t)9600, UART2_WORDLENGTH_8D, UART2_STOPBITS_1, UART2_PARITY_NO,
- UART2_SYNCMODE_CLOCK_DISABLE, UART2_MODE_TXRX_ENABLE);
-
- /* 前头检测 ------------------------------------------------------*/
- printf("\n\r正在检测系统设备...\n\r");
- delayms(500);
- if(NRF24L01_Check()==0)
- {
- delayms(500);
- printf("\n\r/********************************* \n\r");
- printf("\n\r>>>欢迎使用韭菜门禁系统<<<\n\r");
- printf("\n\r/********************************* \n\r");
- }
- else printf("\n\r>>>请检查设备是否安装正确<<<\n\r");
-
- printf("\n\r请输入命令:\n\r");
- printf("\n\r门禁>> 请输入:1\n\r");
- printf("\n\r门开>> 请输入:2\n\r");
- printf("\n\r报警>> 请输入:3\n\r");
- while(1)
- {
- ans = getchar();
- if(ans=='1'|ans=='2'|ans=='3')
- {
-
- TxBuf[0]=ans-'0';
- printf("\n\r合法输入\n\r");
- if(nRF24L01_TxPacket(TxBuf)==TX_OK)
- {
- GPIO_WriteHigh(GPIOD, GPIO_PIN_0);
- GPIO_WriteHigh(GPIOD, GPIO_PIN_2);
- GPIO_WriteHigh(GPIOD, GPIO_PIN_3);
- Delay(0xFFFF); //延时
- GPIO_WriteLow(GPIOD, GPIO_PIN_0);
- GPIO_WriteLow(GPIOD, GPIO_PIN_2);
- GPIO_WriteLow(GPIOD, GPIO_PIN_3);
- printf("\n 发送成功 \n\r");
- delayms(200);
- }
- else printf("\n\r发送失败\n\r");
-
- }
- else printf("\n\r请输入正确的命令\n\r");
- }
- }
-
- /**
- * @brief Retargets the C library printf function to the UART.
- * @param c Character to send
- * @retval char Character sent
- */
- PUTCHAR_PROTOTYPE
- {
- /* Write a character to the UART2 */
- UART2_SendData8(c);
- /* Loop until the end of transmission */
- while (UART2_GetFlagStatus(UART2_FLAG_TXE) == RESET);
- return (c);
- }
- /**
- * @brief Retargets the C library scanf function to the USART.
- * @param None
- * @retval char Character to Read
- */
- GETCHAR_PROTOTYPE
- {
- #ifdef _COSMIC_
- char c = 0;
- #else
- int c = 0;
- #endif
- /* Loop until the Read data register flag is SET */
- while (UART2_GetFlagStatus(UART2_FLAG_RXNE) == RESET);
- c = UART2_ReceiveData8();
- return (c);
- }
- #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 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) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
- /**
- * @}
- */
- /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
复制代码
所有资料51hei提供下载:
发送端.zip
(12.54 KB, 下载次数: 12)
|