main.c:(主文件)
/* 功能:蓝牙转串口模块 作者:jdh 时间:2012-2-27 */ #include "public.h" static __IO uint32_t TimingDelay; //定义GPIO结构体 GPIO_InitTypeDef GPIO_InitStructure; /* Private function prototypes -----------------------------------------------*/ void Delay(__IO uint32_t nTime); //初始化内部晶振 static void RCC_Config(void) { //将外设 RCC寄存器重设为缺省值 RCC_DeInit(); //内部晶振使能 RCC_HSICmd(ENABLE); //使能外部晶振 //SystemInit(); //等待工作稳定 while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET); if(1) { FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); FLASH_SetLatency(FLASH_Latency_2); //高速时钟 RCC_HCLKConfig(RCC_SYSCLK_Div1); RCC_PCLK2Config(RCC_HCLK_Div1); RCC_PCLK1Config(RCC_HCLK_Div2); //设置 PLL 时钟源及倍频系数 RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_9); //使能或者失能 PLL,这个参数可以取:ENABLE或者DISABLE RCC_PLLCmd(ENABLE);//如果PLL被用于系统时钟,那么它不能被失能 //等待指定的 RCC 标志位设置成功 等待PLL初始化成功 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); //设置系统时钟(SYSCLK) 设置PLL为系统时钟源 RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //等待PLL成功用作于系统时钟的时钟源 // 0x00:HSI 作为系统时钟 // 0x04:HSE作为系统时钟 // 0x08:PLL作为系统时钟 while(RCC_GetSYSCLKSource() != 0x08); } } //设置串口波特率 void set_uart_baud(int num,int baud) { if (num == 1) { //更新串口波特率 USART_Cmd(USART1,DISABLE); USART_InitStructure.USART_BaudRate = baud; USART_Init(USART1,&USART_InitStructure); USART_Cmd(USART1, ENABLE); } if (num == 2) { //更新串口波特率 USART_Cmd(USART2,DISABLE); USART_InitStructure.USART_BaudRate = baud; USART_Init(USART2,&USART_InitStructure); USART_Cmd(USART2, ENABLE); } } //初始化 void init() { //定义中断结构体 NVIC_InitTypeDef NVIC_InitStructure; //初始化结构体 GPIO_StructInit(&GPIO_InitStructure); //初始化uart2的接收fifo init_fifo_stack(&fifo_uart2); //初始化uart1的接收fifo init_fifo_stack(&fifo_uart1); //中断NVIC���置:允许中断,设置优先级 NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn; //更新事件 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级0 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //响应优先级1 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //允许中断 NVIC_Init(&NVIC_InitStructure); //写入设置 //RCC_Config(); //打开串口对应的外设时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 , ENABLE); //初始化参数 USART_InitStructure.USART_BaudRate = DEFAULT_BAUD; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //初始化串口 USART_Init(USART1,&USART_InitStructure); //初始化参数 USART_InitStructure.USART_BaudRate = DEFAULT_BAUD; USART_Init(USART2,&USART_InitStructure); //TXE发送中断,TC传输完成中断,RXNE接收中断,PE奇偶错误中断,可以是多个 USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); //配置UART1中断 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //通道设置为串口1中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //中断占先等级0 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //中断响应优先级0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开中断 NVIC_Init(&NVIC_InitStructure); //初始化 //配置UART2中断 NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //通道设置为串口1中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //中断占先等级0 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //中断响应优先级0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开中断 NVIC_Init(&NVIC_InitStructure); //初始化 //启动串口 USART_Cmd(USART1, ENABLE); USART_Cmd(USART2, ENABLE); //设置IO口时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); //串口1的管脚初始化 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //管脚9 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //选择GPIO响应速度 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_Init(GPIOA, &GPIO_InitStructure); //TX初始化 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //管脚10 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //选择GPIO响应速度 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入 GPIO_Init(GPIOA, &GPIO_InitStructure); //RX初始化 //设置IO口时钟 //RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); //串口2的管脚初始化 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //管脚9 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //选择GPIO响应速度 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_Init(GPIOA, &GPIO_InitStructure); //TX初始化 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //管脚10 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //选择GPIO响应速度 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入 GPIO_Init(GPIOA, &GPIO_InitStructure); //RX初始化 /* Setup SysTick Timer for 1 msec interrupts */ if (SysTick_Config(SystemCoreClock / 1000)) { /* Capture error */ while (1); } //设置IO口时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //管脚9 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //选择GPIO响应速度 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 //初始化修改flash结构 init_edit_flash(&edit_flash); //打开需要操作页面的写保护 open_write_lock(); } init_blue(int baud) { //置高AT_EN脚 GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_SetBits(GPIOB,GPIO_Pin_0); send_bt_cmd("AT+INIT"); Delay(100); send_bt_cmd("AT+CMODE=1"); Delay(100); switch (baud) { case 4800: { send_bt_cmd("AT+UART=4800,0,0"); Delay(100); break; } case 9600: { send_bt_cmd("AT+UART=9600,0,0"); Delay(100); break; } case 19200: { send_bt_cmd("AT+UART=19200,0,0"); Delay(100); break; } case 38400: { send_bt_cmd("AT+UART=38400,0,0"); Delay(100); break; } case 57600: { send_bt_cmd("AT+UART=57600,0,0"); Delay(100); break; } case 115200: { send_bt_cmd("AT+UART=115200,0,0"); Delay(100); break; } default: { send_bt_cmd("AT+UART=9600,0,0"); Delay(100); break; } } //置低AT_EN脚 GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_ResetBits(GPIOB,GPIO_Pin_0); } int main(void) { struct _match_string_header match_string_header; struct _match_string_tail match_string_tail; unsigned char buffer[LEN_BUF]; unsigned char buffer1[LEN_BUF]; int len = 0; int i = 0; int flag = 0; int flag2 = 0; int flag3 = 0; int baud = 0; //初始化系统 init(); //初始化蓝牙 //读取flash中波特率 write_baud(&edit_flash,9600); baud = read_baud(&edit_flash); //读取有效 if (baud > 0) { set_uart_baud(1,baud); set_uart_baud(2,baud); } else { //设置默认波特率 set_uart_baud(1,DEFAULT_BAUD); set_uart_baud(2,DEFAULT_BAUD); } //设置默认波特率 Delay(10); init_blue(DEFAULT_BAUD); set_uart_baud(1,DEFAULT_BAUD); set_uart_baud(2,DEFAULT_BAUD); Delay(500); init_blue(DEFAULT_BAUD); set_uart_baud(1,DEFAULT_BAUD); set_uart_baud(2,DEFAULT_BAUD); //初始化匹配字符 init_match_string_header(&match_string_header,"AT+BAUD="); init_match_string_tail(&match_string_tail,"END",8); while (1) { //读取fifo_uart1中所有数据 len = read_all_fifo_stack(&fifo_uart1,buffer1); //处理 for (i = 0;i < len;i++) { USART_SendData(USART2,buffer1[i]); //发送数据 while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET){} //等待发送结束 } //读取fifo_uart2中所有数据 len = read_all_fifo_stack(&fifo_uart2,buffer); //处理 for (i = 0;i < len;i++) { if (flag == 0) { flag3 = match_string_header_state(&match_string_header,buffer[i]); if (flag3 == 1) { flag = 1; } } else { flag2 = match_string_tail_state(&match_string_tail,buffer[i]); if (flag2 > 0) { if (flag2 == 4800 || flag2 == 9600 || flag2 == 19200 || flag2 == 38400 || flag2 == 115200) { //重启蓝牙 init_blue(flag2); //更新串口波特率 set_uart_baud(1,flag2); //写入到flash write_baud(&edit_flash,flag2); //返回成功 send_bt_cmd("set baud successful\n"); flag = 0; continue; } else { send_bt_cmd("set baud fail\n"); } } } //转发数据 USART_SendData(USART1,buffer[i]); //发送数据 while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET){} //等待发送结束 } } } /** * @brief Inserts a delay time. * @param nTime: specifies the delay time length, in milliseconds. * @retval None */ void Delay(__IO uint32_t nTime) { TimingDelay = nTime; while(TimingDelay != 0); } /** * @brief Decrements the TimingDelay variable. * @param None * @retval None */ void TimingDelay_Decrement(void) { if (TimingDelay != 0x00) { TimingDelay--; } } #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给Android设备增加串口功能(4)
内容版权声明:除非注明,否则皆为本站原创文章。