/** * @brief Retargets the C library printf function to the UART. * @param c Character to send * @retval char Character sent */ PUTCHAR_PROTOTYPE { /* Write a character to the UART2 */ UART2_SendData8(c); /* Loop until the end of transmission */ while (UART2_GetFlagStatus(UART2_FLAG_TXE) == RESET); return (c); }
/** * @brief Retargets the C library scanf function to the USART. * @param None * @retval char Character to Read */ GETCHAR_PROTOTYPE { #ifdef _COSMIC_ char c = 0; #else int c = 0; #endif /* Loop until the Read data register flag is SET */ while (UART2_GetFlagStatus(UART2_FLAG_RXNE) == RESET); c = UART2_ReceiveData8(); return (c); }
2.3 stm8s_it.c
这里实现的是接收中断的功能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include"stm8s_it.h" #include"uart.h"
/** * @brief UART2 RX interrupt routine. * @param None * @retval * None */ INTERRUPT_HANDLER(UART2_RX_IRQHandler, 21) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ UART2_ClearITPendingBit(UART2_IT_RXNE); buffer = UART2_ReceiveData8(); } #endif/* STM8S105*/
3 简单应用:通过蓝牙控制 LED
一个很简单的测试程序:在手机上输入指令,告诉单片机需要让哪个 LED 工作,并在手机上显示哪个 LED 正在工作的信息。如果指令错误,则显示错误信息。放出主函数代码:
/** * @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 */ voidassert_failed(u8* file, u32 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) */