15块钱的遥控器(T-65 X-WING)的改造利用( 二 )


串口
板子上有两个串口的四个测试点 , 分别为UART1和UART3,其中UART3连接到了板载的蓝牙模块 , UART1可以用于调试 。具体代码如下:

15块钱的遥控器(T-65 X-WING)的改造利用

文章插图
void Usart_Init(void){RCC_AHBPeriphClock_Enable( RCC_AHBPERIPH_GPIOA , ENABLE ); /* Enable USART1 APB clock */RCC_APB2PeriphClock_Enable( RCC_APB2PERIPH_USART1 , ENABLE );/* USART1 Pins configuration **************************************************/GPIO_DeInit( GPIOA );{/* Configure the GPIO ports */GPIO_InitPara GPIO_InitStructure;/* Connect pin to Periph */GPIO_PinAFConfig( GPIOA , GPIO_PINSOURCE9, GPIO_AF_1 );GPIO_PinAFConfig( GPIOA , GPIO_PINSOURCE10, GPIO_AF_1 ); /* Configure pins as AF pushpull */GPIO_InitStructure.GPIO_Pin= GPIO_PIN_9 | GPIO_PIN_10;GPIO_InitStructure.GPIO_Mode= GPIO_MODE_AF;GPIO_InitStructure.GPIO_Speed= GPIO_SPEED_50MHZ;GPIO_InitStructure.GPIO_OType= GPIO_OTYPE_PP;GPIO_InitStructure.GPIO_PuPd= GPIO_PUPD_NOPULL;GPIO_Init( GPIOA , &GPIO_InitStructure); }{ USART_InitPara USART_InitStructure;USART_DeInit( USART1 );USART_InitStructure.USART_BRR= 115200;USART_InitStructure.USART_WL= USART_WL_8B;USART_InitStructure.USART_STBits= USART_STBITS_1;USART_InitStructure.USART_Parity= USART_PARITY_RESET;USART_InitStructure.USART_HardwareFlowControl = USART_HARDWAREFLOWCONTROL_NONE;USART_InitStructure.USART_RxorTx= USART_RXORTX_RX | USART_RXORTX_TX;USART_Init(USART1, &USART_InitStructure);}/* USART enable */USART_Enable(USART1, ENABLE);}void Usart_SendByte(USART_TypeDef * pUSARTx,uint8_t data){USART_DataSend(pUSARTx,data);while(USART_GetBitState(pUSARTx,USART_FLAG_TBE)==RESET);}void Usart_SendString(USART_TypeDef * pUSART,char *str){unsigned int k=0;do{Usart_SendByte(USART1, *(str+k));k++;}while(*(str+k)!='\0');while(USART_GetBitState(pUSART,USART_FLAG_TC)==RESET);}int fputc(int ch,FILE *f)//重定向之后可以直接用printf函数打印{USART_DataSend(USART1,(uint8_t) ch);while(USART_GetBitState(USART1,USART_FLAG_TBE)==RESET);return(ch);}
无线传输SPI
板子上集成了一块 , 连接到了MCU的一个硬件SPI上淘宝上说和兼容 , 不过没有资料 , 利用不了 , 如果用的是硬件SPI的话可以推测出三个脚的定义 。为了连接,我用了CON9的部分针脚连接 , 这样不用焊接 , 较为方便 。
用PB13、PB14、PB15对应的硬件SPI,初始化代码如下 。
void SPI1_Init(void){/* Initialization GPIO and SPI */GPIO_InitPara GPIO_InitStructure;SPI_InitParaSPI_InitStructure;/* Enable Peripheral clock */RCC_AHBPeriphClock_Enable( RCC_AHBPERIPH_GPIOA | RCC_AHBPERIPH_GPIOB | RCC_AHBPERIPH_GPIOC , ENABLE);RCC_APB1PeriphClock_Enable(RCC_APB1PERIPH_SPI2 ,ENABLE);/* Configure SPI2 pins: */GPIO_PinAFConfig( GPIOB , GPIO_PINSOURCE13, GPIO_AF_0 );GPIO_PinAFConfig( GPIOB, GPIO_PINSOURCE14, GPIO_AF_0 );GPIO_PinAFConfig( GPIOB, GPIO_PINSOURCE15, GPIO_AF_0 );GPIO_InitStructure.GPIO_Pin = GPIO_PIN_13| GPIO_PIN_14 | GPIO_PIN_15;GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_10MHZ;GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF;GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP;GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_PULLUP ;GPIO_Init(GPIOB, &GPIO_InitStructure);NRF_CSN_HIGH(); /*配置SPI_NRF_SPI的CE引脚,和SPI_NRF_SPI的 CSN 引脚*/GPIO_InitStructure.GPIO_Pin = NRF_CSN_PIN;GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_10MHZ;GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP;GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT;GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_PULLUP ;GPIO_Init(NRF_CSN_GPIO_PORT, &GPIO_InitStructure);GPIO_InitStructure.GPIO_Pin = NRF_CE_PIN;GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_10MHZ;GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP;GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT;GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_PULLUP ;GPIO_Init(NRF_CE_GPIO_PORT, &GPIO_InitStructure);/* SPI2 configuration */SPI_InitStructure.SPI_TransType= SPI_TRANSTYPE_FULLDUPLEX;SPI_InitStructure.SPI_Mode = SPI_MODE_MASTER;;SPI_InitStructure.SPI_FrameFormat= SPI_FRAMEFORMAT_8BIT;;SPI_InitStructure.SPI_SCKPL = SPI_SCKPL_LOW ;SPI_InitStructure.SPI_SCKPH = SPI_SCKPH_1EDGE ;SPI_InitStructure.SPI_SWNSSEN= SPI_SWNSS_SOFT;SPI_InitStructure.SPI_PSC = SPI_PSC_8 ;SPI_InitStructure.SPI_FirstBit = SPI_FIRSTBIT_MSB;;SPI_InitStructure.SPI_CRCPOL= 7;SPI_Init(SPI2, &SPI_InitStructure);SPI_Enable(SPI2,ENABLE);}