跟着教程学STM8,代码明明跟他们的一样,却总是有问题,求教!
- #include<iostm8s103k3.h>
- #define uint unsigned int
- #define uchr unsigned char
- void UART1_Init(uint baudart)
- {
- uint baud;
- baud=16000000/baudart;
- UART1_BRR2 = (uchr)((baud & 0xf000)>>8) | (uchr)(baud & 0x000f);
- UART1_BRR1 = (uchr)((baud & 0x0ff0)>>4);
- UART1_CR1_bit.UART0 = 0; //使能UART中断。
- UART1_CR2_bit.RIEN = 1; //使能接收中断使能
- UART1_CR2_bit.REN = 1; //使能接收
- UART1_CR2_bit.TEN = 1; //使能发送
- }
- void UART1_SendData(uchr data)
- {
- while(!(UART1_SR_bit.TXE));
- UART1_DR = data;
- }
-
- int main( void )
- {
- asm("sim");
- CLK_CKDIVR = 0X00;
- UART1_Init(9600);
- asm("rim");
- while(1);
- }
-
- #pragma vector = 20
- __interrupt void UART1_TX_ISP()
- {
- uchr ch;
- UART1_SR_bit.RXNE = 0;
- ch = UART1_DR;
- UART1_SendData(ch);
- }
复制代码
|