当前位置:首页 > 单片机 > 单片机
[导读]使用固件版本:STM8S_StdPeriph_Lib_V2.1.0.zip主程序如下:void main(void){CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);UART1_Init((uint32_t)9600,UART1_WORDLENGTH_8D,UART1_STOPBITS_1,UART1_PARITY_NO, UA

使用固件版本:STM8S_StdPeriph_Lib_V2.1.0.zip

主程序如下:
void main(void)

{

CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);

UART1_Init((uint32_t)9600,UART1_WORDLENGTH_8D,UART1_STOPBITS_1,UART1_PARITY_NO, UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);

/* Enable the UART Receive interrupt: this interrupt is generated when the UART receive data register is not empty */

UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);

/* Enable the UART Transmit complete interrupt: this interrupt is generated when the UART transmit Shift Register is empty */

UART1_ITConfig(UART1_IT_TXE, ENABLE);

/* Enable UART */

UART1_Cmd(ENABLE);

/* Enable general interrupts */

enableInterrupts();

while (1)

{

}

}

中断程序如下:

#define TX_BUFFER_SIZE (countof(TxBuffer) - 1)

#define countof(a) (sizeof(a) / sizeof(*(a)))

uint8_t TxBuffer[] = "UART1-communication using Interrupt modelsim_tyc@163.comn";

__IO uint8_t TxCounter = 0;


INTERRUPT_HANDLER(UART1_TX_IRQHandler, 17)

{

UART1_SendData8(TxBuffer[TxCounter++]);

if (TxCounter == TX_BUFFER_SIZE)

UART1_ITConfig(UART1_IT_TXE, DISABLE);

}

INTERRUPT_HANDLER(UART1_RX_IRQHandler, 18)

{

uint8_t temp;

/* Read one byte from the receive data register and send it back */

temp = UART1_ReceiveData8();

UART1_SendData8(temp);

}


本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭