中级会员
 
- 积分
- 256
- 金钱
- 256
- 注册时间
- 2018-8-8
- 在线时间
- 50 小时
|
10金钱
本帖最后由 走投无路的卡卡 于 2019-11-20 14:46 编辑
- ——————————————main.c——————————————
- #include "sys.h"
- #include "delay.h"
- #include "led.h"
- #include "key.h"
- #include "uart.h"
- int main(void)
- {
- HAL_Init();
- Stm32_Clock_Init(360,25,2,8);
- delay_init(180);
- led_init();
- key_init();
- uart_init(115200);
-
- UART_HandleTypeDef uart_handler;
- u16 size;
-
- while(1)
- {
- if(uart_sta&0x8000)
- {
- size=(uart_sta|0x3fff);
- printf("Äú·¢Ë͵ÄÏûϢΪ£º");
- HAL_UART_Transmit(&uart_handler,(u8 *)data,size,1000);
- while(__HAL_UART_GET_FLAG(&uart_handler,UART_FLAG_TC)!=SET);
- uart_sta=0;
- }
-
- }
- }
- ————————————————————————————————uart.c————————————————————————
- #include "uart.h"
- #if 1
- #pragma import(__use_no_semihosting)
- struct __FILE
- {
- int handle;
- };
- FILE __stdout;
- void _sys_exit(int x)
- {
- x = x;
- }
- int fputc(int ch, FILE *f)
- {
- while((USART1->SR&0X40)==0);
- USART1->DR = (u8) ch;
- return ch;
- }
- #endif
- UART_HandleTypeDef uart_handler;
- GPIO_InitTypeDef gpio_initure;
- u16 uart_sta=0;
- u8 data[length];
- u8 rxbuff[1];
-
- void uart_init(u32 rate)
- {
- uart_handler.Instance=USART1;
- uart_handler.Init.BaudRate=rate;
- uart_handler.Init.Mode=UART_MODE_TX_RX;
- uart_handler.Init.StopBits=UART_STOPBITS_1;
- uart_handler.Init.WordLength=UART_WORDLENGTH_8B;
- uart_handler.Init.Parity=UART_PARITY_NONE;
- uart_handler.Init.HwFlowCtl=UART_HWCONTROL_NONE;
-
- HAL_UART_Init(&uart_handler);
- HAL_UART_Receive_IT(&uart_handler,(u8 *)rxbuff,1);
- __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
- //__HAL_UART_ENABLE_IT(&uart_handler,UART_IT_RXNE);
- }
- void HAL_UART_MspInit(UART_HandleTypeDef *huart)
- {
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_USART1_CLK_ENABLE();
-
- gpio_initure.Pin=GPIO_PIN_9|GPIO_PIN_10;
- gpio_initure.Mode=GPIO_MODE_AF_PP;
- gpio_initure.Pull=GPIO_PULLUP;
- gpio_initure.Speed=GPIO_SPEED_FREQ_HIGH;
- gpio_initure.Alternate=GPIO_AF7_USART1;
-
- HAL_GPIO_Init(GPIOA,&gpio_initure);
-
- HAL_NVIC_EnableIRQ(USART1_IRQn);
- HAL_NVIC_SetPriority(USART1_IRQn,3,3);
-
- }
- void UART_IRQ(void)
- {
- HAL_UART_IRQHandler(&uart_handler);
- HAL_UART_Receive_IT(&uart_handler,(u8 *)rxbuff,1);
- }
- void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
- {
- if((uart_sta&0x8000)==0)
- {
- if(uart_sta&0x4000) //receive 0x0d
- {
- if(rxbuff[0]==0x0a) uart_sta|=0x8000;
- else uart_sta=0; //error
- }
- else
- {
- if(rxbuff[0]==0x0d) uart_sta|=0x4000;
- else
- {
- data[uart_sta&0x3fff]=rxbuff[0];
- uart_sta++;
- }
- }
-
-
- }
- }
复制代码
————————————————————————————————————问题描述——————————————————————————————
代码无bug,跟原子哥的代码对比也相差不大,但是在串口调试助手上发送数据时,得不到接收数据,求大神帮我看看我代码的问题出在哪。在线等,谢谢了。。。。
|
|