主函数
main()
{
USART_Configuration();
_asm("rim");
}
中断函数
@far @interrupt void USART_RX_IRQHandler (void)
{
UART_Recive();
return;
}
中断向量
{0x82, (interrupt_handler_t)USART_RX_IRQHandler}, /* irq18 */
串口函数
#include "uart.h"
#include <stdarg.h>
#include <stdio.h>
#include <iostm8s.h>
#define HSIClockFreq 16000000
#define BaudRate 115200
#define UART1_FLAG_TXE (uint8_t)0x80 /*!< Transmit Data Register Empty flag */
#define UART1_FLAG_RXNE (uint8_t)0x20 /*!< Read Data Register Not Empty flag */
void USART_Configuration(void)//串口初始化函数
{
unsigned int baud_div=0;
USART1_CR1 = (0<<4)|(0<<3)|(0<<2)|(0<<1)|(0<<0);
USART1_CR3 = (0<<6)|(0<<4)|(0<<3);
USART1_CR2 = (0<<7)|(0<<6)|(1<<5)|(0<<4)|(1<<3)|(1<<2);
USART1_CR5 = (0<<2)|(0<<1);
//设置波特率
baud_div =HSIClockFreq/BaudRate; //分频因子
USART1_BRR2 = baud_div & 0x0f;
USART1_BRR2 |= ((baud_div & 0xf000) >> 8);
USART1_BRR1 = ((baud_div & 0x0ff0) >> 4);
USART1_CR1 |= (0<<5); /*使能UART*/
}
void UART_send_byte(unsigned char byte) //发送1字节数据
{
USART1_DR=((unsigned char)byte);
/* Loop until the end of transmission */
while (!(USART1_SR & UART1_FLAG_TXE));
}
void UART_Send(unsigned char *Buffer, unsigned short Length)
{
uint16_t l=0;
for(;l<Length;l++)
UART_send_byte(Buffer[l]);
}
uint16_t i=0;
uint8_t UART1_Re_Buf[10];
unsigned char UART_Recive(void)
{
// while (!(USART1_SR & UART1_FLAG_RXNE));
if((USART1_SR & UART1_FLAG_RXNE))
{
if(i<5)
UART1_Re_Buf[i++]=((uint8_t)USART1_DR);
}
return 0;
}
|