完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
#include
/* ----------------- APPLICAtiON GLOBALS ------------------ */ static OS_TCB AppTaskStartTCB; static CPU_STK AppTaskStartStk[APP_TASK_START_STK_SIZE]; static void AppTaskStart (void *p_arg); static void AppTaskCreate (void); static void AppObjCreate (void); //================================================================================================ OS_TCB RS232_command_StartTCB; CPU_STK RS232_command_Task_Stk[APP_TASK_START_STK_SIZE]; OS_SEM Uart_1_Char_Semp; OS_MUTEX Uart_1_Mutex; OS_SEM Uart_2_Char_Semp; OS_MUTEX Uart_2_Mutex; /* ********************************************************************************************************* * main() ********************************************************************************************************* */ int main(void) { OS_ERR err; BSP_IntDisAll(); /* Disable all interrupts. */ CPU_Init(); /* Initialize uC/CPU services. */ Frist_On_Machine(); OSInit(&err); /* Init OS. */ OSTaskCreate((OS_TCB *)&AppTaskStartTCB, /* Create the start task */ (CPU_CHAR *)"App Task Start", (OS_TASK_PTR ) AppTaskStart, (void *) 0, (OS_PRIO ) APP_TASK_START_PRIO, (CPU_STK *)&AppTaskStartStk[0], (CPU_STK )(APP_TASK_START_STK_SIZE / 10u), (CPU_STK_SIZE) APP_TASK_START_STK_SIZE, (OS_MSG_QTY ) 0, (OS_TICK ) 0, (void *) 0, (OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR), (OS_ERR *)&err); OSStart(&err); while (DEF_ON) { /* Should Never Get Here */ }; } /* ********************************************************************************************************* * STARTUP TASK ********************************************************************************************************* */ static void AppTaskStart (void *p_arg) { //CPU_TS p_ts; OS_ERR os_err; (void)p_arg; CPU_Init(); /* Init CPU name & int. dis. time measuring fncts. */ Mem_Init(); /* Initialize Memory managment */ BSP_CPU_TickInit(); /* Start Tick Initialization */ Frist_On_Machine(); AppObjCreate(); /* Create Application Kernel Objects */ AppTaskCreate(); /* Create Application Tasks */ OSTimeDlyHMSM(0, 0, 0, 50, OS_OPT_TIME_HMSM_STRICT, &os_err); //--------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------- while (DEF_ON) { // if(UART_1.RX_Count>0) // {USART_SendData(USART1, UART_1.RX_Buffer[UART_1.RX_Count]); // UART_1.RX_Count --; // while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET){} //发送数据寄存器空标志位 // } OSTimeDlyHMSM(0, 0, 0, 50, OS_OPT_TIME_HMSM_STRICT, &os_err); } } /* ********************************************************************************************************* * AppTaskCreate() * ********************************************************************************************************* */ static void AppTaskCreate (void) { OS_ERR err; OSTaskCreate((OS_TCB *)&RS232_command_StartTCB, // Create the start task (CPU_CHAR *)"RS232", (OS_TASK_PTR ) RS232_command, //任务名称 (void *) 0, (OS_PRIO ) APP_TASK_START_PRIO , //任务等级 (CPU_STK *)&RS232_command_Task_Stk[0], (CPU_STK )(APP_TASK_START_STK_SIZE / 10u), (CPU_STK_SIZE) APP_TASK_START_STK_SIZE, (OS_MSG_QTY ) 0, (OS_TICK ) 0, (void *) 0, (OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR), (OS_ERR *)&err); } static void AppObjCreate (void) { OS_ERR err; OSSemCreate ( &Uart_2_Char_Semp, (CPU_CHAR *)"Uart_2_Char", 0, &err ); OSSemCreate ( &Uart_1_Char_Semp, (CPU_CHAR *)"Uart_1_Char", 0, &err ); OSMutexCreate ( &Uart_2_Mutex, (CPU_CHAR *)"Uart_2_Mut", &err ); OSMutexCreate ( &Uart_1_Mutex, (CPU_CHAR *)"Uart_1_Mut", &err ); } //==================================================================================================================== #include uint8_t TX_Buffer[60]; char TX1_Buffer[128]; void RCC_Configuration(void); void GPIO_Configuration(void); void UART_1_Init(void); void UART_2_Init(void); void Frist_On_Machine(void); struct UART_STRUCT UART_2; struct UART_STRUCT UART_1; //===================================================================== //===================================================================== void Frist_On_Machine(void) { RCC_Configuration(); GPIO_Configuration(); UART_1_Init(); UART_2_Init(); } //===================================================================== //===================================================================== void RCC_Configuration(void) { // __IO uint32_t StartUpCounter = 0 RCC_DeInit(); //复位RCC RCC_HSEConfig(RCC_HSE_ON); //打开外部告诉晶振 while ((RCC_WaitForHSEStartUp() != SUCCESS)) // && (StartUpCounter != HSE_STARTUP_TIMEOUT) {}; //等待外部振荡器启动StartUpCounter++; // if(StartUpCounter == HSE_STARTUP_TIMEOUT) // { // RCC_HSICmd(ENABLE); // RCC_PLLConfig(RCC_PLLSource_HSE,8,120,2,5 ); //Configures the main PLL clock source, multiplication and division factors. // } // else // { RCC_PLLConfig(RCC_PLLSource_HSE,4,120,2,5 ); //Configures the main PLL clock source, multiplication and division factors. // } RCC_HCLKConfig(RCC_SYSCLK_Div1); // AHB clock = SYSCLK RCC_SYSCLK_Div1 RCC_PCLK1Config(RCC_HCLK_Div4); // APB1 clock = HCLK RCC_PCLK2Config(RCC_HCLK_Div2); // APB2 clock = HCLK RCC_HCLK_Div2 RCC_PLLCmd(ENABLE); //使能PLL while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) != SET){} //等待PLL稳定 FLASH_SetLatency(FLASH_Latency_7); // Flash延迟 4 Flash wait states FLASH_PrefetchBufferCmd (ENABLE); RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //设定系统时钟 while (RCC_GetSYSCLKSource() != RCC_CFGR_SWS_PLL){} // 使用PLL作为系统时钟 system clock //------------------------------------------------------------- RCC_HCLKConfig(RCC_SYSCLK_Div1); // AHB clock = SYSCLK RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_GPIOF | RCC_AHB1Periph_GPIOG | RCC_AHB1Periph_GPIOH | RCC_AHB1Periph_GPIOI | RCC_AHB1Periph_DMA1 | RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_CRC | RCC_AHB1Periph_BKPSRAM, ENABLE ); RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART2 | RCC_APB1Periph_USART3 | RCC_APB1Periph_CAN1| // | RCC_APB1Periph_UART4 | RCC_APB1Periph_UART5 RCC_APB1Periph_SPI2 | RCC_APB1Periph_TIM2| RCC_APB1Periph_TIM3, ENABLE ); RCC_APB2PeriphClockCmd( RCC_APB2Periph_TIM8 |RCC_APB2Periph_USART1 | RCC_APB2Periph_USART6 | RCC_APB2Periph_ADC1 |RCC_APB2Periph_TIM1| RCC_APB2Periph_ADC3 |RCC_APB2Periph_SDIO |RCC_APB2Periph_TIM11|RCC_APB2Periph_SPI1, ENABLE); } //IO口初始化====IO口初始化====IO口初始化====IO口初始化====IO口初始化====IO口初始化====IO口初始化==== //IO口初始化====IO口初始化====IO口初始化====IO口初始化====IO口初始化====IO口初始化====IO口初始化==== void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //通用输出模式 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; // 和单片机状态灯 GPIO_Init(GPIOG, &GPIO_InitStructure); //GPIO_SetBits(GPIOG,GPIO_Pin_0); } //UART1====//UART1====//UART1====//UART1====//UART1====//UART1====//UART1====//UART1====//UART1==== //UART1====//UART1====//UART1====//UART1====//UART1====//UART1====//UART1====//UART1====//UART1==== void UART_1_Init(void) { DMA_InitTypeDef DMA_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; //------------------------------------------------------------------------------------ NVIC_InitStructure.NVIC_IRQChannel = BSP_INT_ID_USART1; //UART1中断编号 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); //------------------------------------------------------------------------------------ USART_InitStructure.USART_BaudRate = 9600; 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; //------------------------------------------------------------------------------------ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; //开漏输出 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //没有上拉和下拉 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_Init(GPIOA, &GPIO_InitStructure); USART_Init(USART1, &USART_InitStructure); USART_ITConfig(USART1, USART_IT_TC, ENABLE); //USART_IT_TC: 发送完成 中断允许 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // 接收数据寄存器非空(有数据可读) 中断允许 USART_ITConfig(USART1, USART_IT_ERR, ENABLE); USART_ITConfig(USART1, USART_IT_ORE, ENABLE); USART_ITConfig(USART1, USART_IT_NE, ENABLE); USART_ITConfig(USART1, USART_IT_FE, ENABLE); USART_Cmd(USART1, ENABLE); //使能UART //=============================================================== DMA_DeInit (DMA2_Stream7); DMA_InitStructure.DMA_Channel = DMA_Channel_4; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART1->DR); DMA_InitStructure.DMA_BufferSize = (uint32_t)(UART_BUFER_SIZE); DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)(TX1_Buffer); DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream7, &DMA_InitStructure); DMA_ClearITPendingBit(DMA2_Stream7, DMA_IT_TCIF7); USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE); DMA_Cmd(DMA2_Stream7, ENABLE); //使能DMA } //================================================================================ #include //===================================================================================== uint16_t USART_RX_STA=0; uint8_t xxtt; void BSP_IntHandlerUSART1(void) { if(USART_GetITStatus(USART1, USART_IT_PE) == SET) { USART_ClearITPendingBit (USART1, USART_IT_PE); } if(USART_GetITStatus(USART1, USART_IT_TXE) == SET) { USART_ClearITPendingBit (USART1, USART_IT_TXE); } if(USART_GetITStatus(USART1, USART_IT_TC) == SET) //发送完成中断 { USART_ClearITPendingBit (USART1, USART_IT_TC); //清除中断标志 // Send_Glag = 0; // UART_1.RX_Count = 0; // UART_1.State = RX_START_x10; } if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET) { USART_ClearITPendingBit (USART1, USART_IT_RXNE); //清除中断标志 receive_ASCII(); } if(USART_GetITStatus(USART1, USART_IT_IDLE) == SET) { USART_ClearITPendingBit (USART1, USART_IT_IDLE); //清除中断标志 } if(USART_GetITStatus(USART1, USART_IT_LBD) == SET) { USART_ClearITPendingBit (USART1, USART_IT_LBD); xxtt = (char)(USART_ReceiveData(USART1)); } if(USART_GetITStatus(USART1, USART_IT_CTS) == SET) { USART_ClearITPendingBit (USART1, USART_IT_CTS); xxtt = (char)(USART_ReceiveData(USART1)); } if(USART_GetITStatus(USART1, USART_IT_ERR) == SET) { USART_ClearITPendingBit (USART1, USART_IT_ERR); xxtt = (char)(USART_ReceiveData(USART1)); } if(USART_GetITStatus(USART1, USART_IT_ORE) == SET) { USART_ClearITPendingBit (USART1, USART_IT_ORE); xxtt = (char)(USART_ReceiveData(USART1)); } if(USART_GetITStatus(USART1, USART_IT_NE) == SET) { USART_ClearITPendingBit (USART1, USART_IT_NE); xxtt = (char)(USART_ReceiveData(USART1)); } if(USART_GetITStatus(USART1, USART_IT_FE) == SET) //FE: 帧错误 { USART_ClearITPendingBit (USART1, USART_IT_FE); //清除中断标志 xxtt = (char)(USART_ReceiveData(USART1)); } } //================================================================= #include OS_TCB UART_2_RX_StartTCB; CPU_STK UART_2_RX_Task_Stk[APP_TASK_START_STK_SIZE]; OS_ERR err; CPU_TS p_ts; //============================================================================================================================ uint16_t uiCmdError; uint8_t Send_Flag; uint8_t RX_Check_Buffer[6]; uint8_t RX_Buffer[60]; void UART2_Send(uint8_t *ucFrameBuf, uint8_t ucFrameLen, uint16_t uiCmdError, uint8_t *parameter); //============================================================================================================================ void RS232_command (void *p_arg) { OS_ERR err; OSTimeDly(10,OS_OPT_TIME_DLY,&err); GPIO_SetBits(GPIOG,GPIO_Pin_0); OSTimeDly(500,OS_OPT_TIME_DLY,&err); while (1) { GPIO_ResetBits(GPIOG,GPIO_Pin_0); OSTimeDly(500,OS_OPT_TIME_DLY,&err); GPIO_SetBits(GPIOG,GPIO_Pin_0); OSTimeDly(500,OS_OPT_TIME_DLY,&err); } } //UART1====//UART1====//UART1====//UART1====//UART1====//UART1====//UART1====//UART1====//UART1==== //UART1====//UART1====//UART1====//UART1====//UART1====//UART1====//UART1====//UART1====//UART1==== void receive_ASCII(void) { uint16_t receive_word; receive_word = (uint16_t)(USART_ReceiveData(USART1)); UART_1.RX_Buffer[UART_1.RX_Count] = receive_word; USART_SendData(USART1,receive_word); while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET){} //发送数据寄存器空标志位 } |
|
相关推荐
4个回答
|
|
问题已经解决,是波特率问题,应该把时钟设置成实际使用的,不能用默认的。
|
|
|
|
接收的与发送的不一样
|
|
|
|
|
|
|
|
单片机收的也不对,我发的FF收到的是F8,发的22收到的是80
|
|
|
|
只有小组成员才能发言,加入小组>>
702 浏览 0 评论
1111 浏览 1 评论
2476 浏览 5 评论
2811 浏览 9 评论
移植了freeRTOS到STMf103之后显示没有定义的原因?
2654 浏览 6 评论
使用eim外接fpga可是端口一点反应都没有有没有大哥指点一下啊
658浏览 9评论
659浏览 7评论
请教大神怎样去解决iMX6Q在linux3.0.35内核上做AP失败的问题呢
788浏览 6评论
633浏览 5评论
677浏览 5评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-25 02:51 , Processed in 1.027349 second(s), Total 88, Slave 68 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号