最近在用STM8做東西,看到論壇上關于STM8的例程少之又少,故發一個串口例程
串口1波特率9600,不斷發送數據,同時可響應接收數據,注釋較少。
開發環境為IAR,可直接燒錄生成的hex文件。
tu1.JPG (76.75 KB, 下載次數: 77)
下載附件
2019-3-6 18:42 上傳
tu2.JPG (74.89 KB, 下載次數: 77)
下載附件
2019-3-6 18:43 上傳
單片機源程序如下:
- /* Includes ------------------------------------------------------------------*/
- #include "stm8s.h"
- /* Private defines -----------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
- #define LED_GPIO_PORT (GPIOB)
- #define LED_GPIO_PINS (GPIO_PIN_5)
- void Delay(uint16_t nCount)
- {
- /* Decrement nCount value */
- while (nCount != 0)
- {
- nCount--;
- }
- }
- void Init_UART1(void)
- {
- UART1_DeInit();
- UART1_Init((u32)9600, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO, UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
-
- UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);
- enableInterrupts();
- UART1_Cmd(ENABLE);
- }
- void Send(uint16_t dat)
- {
- while(( UART1_GetFlagStatus(UART1_FLAG_TXE)==RESET));
-
- UART1_SendData9(dat);
-
- }
- void main(void)
- {
-
- uint8_t i=0;
- uint16_t j=0;
- CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
- Init_UART1();
-
- /* Infinite loop */
- while (1)
- {
- for(j=0;j<60000;j++);
- Send(i);
- //UART1_SendData8(0x01);
- i++;
- }
-
- }
- #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(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) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
復制代碼
所有資料51hei提供下載:
UARTX-RX.7z
(3.96 MB, 下載次數: 60)
2019-3-6 23:59 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|