旧事重提:STM8L串口收发问题

2019-07-19 21:05发布

本帖最后由 紫气东升 于 2016-9-9 14:03 编辑

大神们给点力啊,串口收发始终没能成功,我的程序到底是哪里出问题?

#include <stm8l052c6.h>
#include <stdio.h>

/*********函数声明********/
void delay(unsigned int time);

/******USART初始化*********/
void USART1_Init(void)
{
   
    USART1_CR1=0x00;         //Set the M length, 8 data bits
    //USART1_CR2=0x2c;      //Enable to send and receive
    USART1_CR2=0x08;        //Transmit enable (USART1_CR2_TEN=0x08;)
    USART1_CR2=0x04;       //Receive enable(USART1_CR2_REN=0x04;)
    USART1_CR2=0x80;       //Send interrupt enable(USART1_CR2_TIEN=0x80;)
    USART1_CR2=0x20;      //Receive interrupt enable(USART1_CR2_RIEN=0x20;)
    USART1_CR3=0x00;      //1 stop bits
    USART1_BRR2=0x02;    //Set the baud rate to 9600
    USART1_BRR1=0x68;
}

void Uart_SendData(unsigned char  data)
{
  while(!(USART1_SR&0X80));     //To determine whether the sending data register is empty
  USART1_DR = data;             //Write data to the sending register
}

/*********IO初始化*********/
void IO_Init(void)
{
   PD_ODR=0x03;         //The initial light Quanmie
   PD_DDR=0x03;         //Set PD port for output mode
   PD_CR1=0x03;         //Set the PD port for push-pull output
   PD_CR2=0x00;         //Set PD port to low speed output
}

/*********主函数********/
int main(void)
{
   CLK_PCKENR1 =0x0f;                             
   CLK_CKDIVR=0x00;
   USART1_Init();          //Universal serial interface USART1 initialization
   IO_Init();                 //IO port initialization
     _asm("rim");          //Open total interrupt
   while(1)
   {
     PD_ODR=0x02;         //PD1 (red) light
     delay(65535);           //Delay
     PD_ODR=0x00;         //PD1 (red) out
     delay(65535);          //Delay
    }
}

/******中断服务程序********/
#pragma vector = USART_R_RXNE_vector
@far @interrupt void USART1_Rx_IRQHandler(void)
{
    unsigned char ch1;
        if(USART1_SR ==0x20)       //Whether or not to complete
            {
                ch1 = USART1_DR;
                Uart_SendData(ch1);
            }
     
}

/********延时函数**********/
void delay(unsigned int time)
{
   while(time--);
}

/****中断函数修改******/
/*    BASIC INTERRUPT VECTOR TABLE FOR STM8 devices
*    Copyright (c) 2007 STMicroelectronics
*/

typedef void @far (*interrupt_handler_t)(void);

struct interrupt_vector {
    unsigned char interrupt_instruction;
    interrupt_handler_t interrupt_handler;
};

@far @interrupt void NonHandledInterrupt (void)
{
    /* in order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction
    */
    return;
}

extern void _stext();     /* startup routine */
@far @interrupt void USART1_Rx_IRQHandler(void);
struct interrupt_vector const _vectab[] = {
    {0x82, (interrupt_handler_t)_stext}, /* reset */
    {0x82, NonHandledInterrupt}, /* trap  */
    {0x82, NonHandledInterrupt}, /* irq0  */
    {0x82, NonHandledInterrupt}, /* irq1  */
    {0x82, NonHandledInterrupt}, /* irq2  */
    {0x82, NonHandledInterrupt}, /* irq3  */
    {0x82, NonHandledInterrupt}, /* irq4  */
    {0x82, NonHandledInterrupt}, /* irq5  */
    {0x82, NonHandledInterrupt}, /* irq6  */
    {0x82, NonHandledInterrupt}, /* irq7  */
    {0x82, NonHandledInterrupt}, /* irq8  */
    {0x82, NonHandledInterrupt}, /* irq9  */
    {0x82, NonHandledInterrupt}, /* irq10 */
    {0x82, NonHandledInterrupt}, /* irq11 */
    {0x82, NonHandledInterrupt}, /* irq12 */
    {0x82, NonHandledInterrupt}, /* irq13 */
    {0x82, NonHandledInterrupt}, /* irq14 */
    {0x82, NonHandledInterrupt}, /* irq15 */
    {0x82, NonHandledInterrupt}, /* irq16 */
    {0x82, NonHandledInterrupt}, /* irq17 */
    {0x82, NonHandledInterrupt}, /* irq18 */
    {0x82, NonHandledInterrupt}, /* irq19 */
    {0x82, NonHandledInterrupt}, /* irq20 */
    {0x82, NonHandledInterrupt}, /* irq21 */
    {0x82, NonHandledInterrupt}, /* irq22 */
    {0x82, NonHandledInterrupt}, /* irq23 */
    {0x82, NonHandledInterrupt}, /* irq24 */
    {0x82, NonHandledInterrupt}, /* irq25 */
    {0x82, NonHandledInterrupt}, /* irq26 */
    {0x82, NonHandledInterrupt}, /* irq27 */
    {0x82, USART1_Rx_IRQHandler}, /* irq28 */
    {0x82, NonHandledInterrupt}, /* irq29 */
};


友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。