Nguyên văn bởi queduong
Xem bài viết
![Big Grin](http://dientuvietnam.net/forums/images/smilies/biggrin.gif)
#include "stm8s.h" #include "stdio.h" #include "stm8s_uart2.h" #include "stm8s_clk.h" //#define PUTCHAR_PROTOTYPE char putchar (char c) //#define GETCHAR_PROTOTYPE char getchar (void) void main(void) { char ans; char c; GPIO_DeInit(GPIOD); // Initialise pin 0 of port D. GPIO_Init(GPIOD, GPIO_PIN_0, GPIO_MODE_OUT_PP_LOW_FAST); GPIO_WriteHigh(GPIOD, GPIO_PIN_0); /*High speed internal clock prescaler: 1*/ CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); UART2_DeInit(); /* UART1 configuration ------------------------------------------------------*/ /* UART1 configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - One Stop Bit - No parity - Receive and transmit enabled - UART1 Clock disabled */ UART2_Init((uint32_t)115200, UART2_WORDLENGTH_8D, UART2_STOPBITS_1, UART2_PARITY_NO, UART2_SYNCMODE_CLOCK_DISABLE, UART2_MODE_TXRX_ENABLE); UART2_Cmd(ENABLE); UART2_ITConfig(UART2_IT_RXNE_OR, ENABLE); enableInterrupts(); while (1) { /* while (UART2_GetFlagStatus(UART2_FLAG_RXNE) == RESET); c = UART2_ReceiveData8(); UART2_SendData8(c+1); // Loop until the end of transmission while (UART2_GetFlagStatus(UART2_FLAG_TXE) == RESET); */ } } #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
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. */ uint8_t temp; /* Read one byte from the receive data register and send it back */ temp = (UART2_ReceiveData8() & 0x7F); UART2_SendData8(temp+2); while (UART2_GetFlagStatus(UART2_FLAG_TXE) == RESET); if(temp=='a') { GPIO_WriteHigh(GPIOD, GPIO_PIN_0); } if(temp=='b') { GPIO_WriteLow(GPIOD, GPIO_PIN_0); } }
#include "stm8s.h" #include "stdio.h" #include "stm8s_uart2.h" #include "stm8s_clk.h" //#define PUTCHAR_PROTOTYPE char putchar (char c) //#define GETCHAR_PROTOTYPE char getchar (void) void main(void) { char ans; char c; GPIO_DeInit(GPIOD); // Initialise pin 0 of port D. GPIO_Init(GPIOD, GPIO_PIN_0, GPIO_MODE_OUT_PP_LOW_FAST); GPIO_WriteHigh(GPIOD, GPIO_PIN_0); /*High speed internal clock prescaler: 1*/ CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); UART2_DeInit(); /* UART1 configuration ------------------------------------------------------*/ /* UART1 configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - One Stop Bit - No parity - Receive and transmit enabled - UART1 Clock disabled */ UART2_Init((uint32_t)115200, UART2_WORDLENGTH_8D, UART2_STOPBITS_1, UART2_PARITY_NO, UART2_SYNCMODE_CLOCK_DISABLE, UART2_MODE_TXRX_ENABLE); UART2_Cmd(ENABLE); UART2_ITConfig(UART2_IT_RXNE_OR, ENABLE); enableInterrupts(); while (1) { /* while (UART2_GetFlagStatus(UART2_FLAG_RXNE) == RESET); c = UART2_ReceiveData8(); UART2_SendData8(c+1); // Loop until the end of transmission while (UART2_GetFlagStatus(UART2_FLAG_TXE) == RESET); */ } } #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
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. */ uint8_t temp; /* Read one byte from the receive data register and send it back */ temp = (UART2_ReceiveData8() & 0x7F); UART2_SendData8(temp+2); while (UART2_GetFlagStatus(UART2_FLAG_TXE) == RESET); if(temp=='a') { GPIO_WriteHigh(GPIOD, GPIO_PIN_0); } if(temp=='b') { GPIO_WriteLow(GPIOD, GPIO_PIN_0); } }
#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
Comment