初级会员

- 积分
- 124
- 金钱
- 124
- 注册时间
- 2018-3-26
- 在线时间
- 288 小时
|
20金钱
- #include "sys.h"
- #include "usart.h"
- #include "led.h"
- #include "delay.h"
- #include "timer.h"
- #if SYSTEM_SUPPORT_UCOS
- #include "includes.h" //ucos ê1óÃ
- #endif
- #if 1
- #pragma import(__use_no_semihosting)
-
- struct __FILE
- {
- int handle;
- };
- FILE __stdout;
- _sys_exit(int x)
- {
- x = x;
- }
- int fputc(int ch, FILE *f)
- {
- while((USART2->SR&0X40)==0);
- USART2->DR = (u8) ch;
- return ch;
- }
- #endif
- #if EN_USART2_RX
- u8 USART_RX_BUF[USART_REC_LEN];
- u16 USART_RX_STA=0;
-
- void uart_init(u32 bound){
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- //USART1_RX PA.10
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- USART_InitStructure.USART_BaudRate = bound;
- 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(USART2, &USART_InitStructure);
- USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
- USART_Cmd(USART2, ENABLE);
- }
- void USART2_IRQHandler(void)//????????
- {
- u8 res;
- if(USART_GetITStatus(USART2,USART_IT_RXNE))
- {
- res= USART_ReceiveData(USART2);
- if('a'==res)
- {
- PWM_control(100);
-
- }
- }
- }
复制代码 我的操作是,电脑向单片机发送字符a,执行PWM_control(100);,但是现在没反应
|
最佳答案
查看完整内容[请看2#楼]
RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART3|RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);
用的串口3?
RCC_APB1Periph_USART3这里是APB1总线的, 你用RCC_APB2PeriphClockCmd()函数是用来使能APB2外设时钟的, 肯定有问题
|