STM8L串口收发

2019-03-23 18:01发布

本帖最后由 ziqidongsheng 于 2016-9-2 11:14 编辑

这是我用寄存器写的程序,但是实现不了功能,有哪位大神会的请指教,谢谢
#include <stm8l052c6.h>
#include <stdio.h>

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

/******USART初始化*********/
void USART1_Init(void)
{
    CLK_PCKENR1 =0x0f;
    USART1_CR1=0x00;          //设置M字长,8位数据位
    USART1_CR2=0x2c;          //使能发送、接收;
    USART1_CR3=0x00;         //1位停止位
    USART1_BRR2=0x03;       //设置波特率为9600
    USART1_BRR1=0x68;
}

void Uart_SendData(unsigned char  data)
{
  while(!(USART1_SR&0X80));       //判断发送数据寄存器是否为空
  USART1_DR = data;                  //向发送寄存器写入数据  
}

/*********IO初始化*********/
void IO_Init(void)
{
   PD_ODR=0x03;               //初始使小灯全灭
   PD_DDR=0x03;              //设置PD端口为输出模式
   PD_CR1=0x03;               //设置PD端口为推挽输出
   PD_CR2=0x00;               //设置PD端口为低速输出
}

/*********主函数********/
void main(void)
{
   _asm("sim");                  //关总中断
     USART1_Init();            //通用串行接口USART1初始化
   IO_Init();                       //GPIO端口初始化
   CLK_CKDIVR=0x00;     //系统HSI时钟1分频
     _asm("rim");               //开总中断
   while(1)
   {
     PD_ODR=0x02;         //PD1(红)亮
     delay(65535);            //延时1s
     PD_ODR=0x00;         //PD1(红)灭
     delay(65535);            //延时1s
    }
}

/******中断服务程序********/
#pragma vector = USART_R_RXNE_vector
@far @interrupt void USART1_Rx_IRQHandler(void)
{
    unsigned char ch1;
     if(USART1_SR =0x20)    //发送是否完成
      {
            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 */
};

想要实现的功能是这样的:上电后呼吸灯不断循环闪烁,当通过串口助手发送数据时进入中断,呼吸灯不再闪烁,同时串口助手也显示反馈回来的发送数据,收发完毕后又返回主函数(呼吸灯继续闪烁)。


友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
15条回答
ziqidongsheng
1楼-- · 2019-03-24 19:43
汤权 发表于 2016-9-2 12:50
这个是正确的语法,不会报错的,你这种写法是对的但是程序原来的意思错了,但是这不是影响接收不到数据的 ...

那应该怎么打开,我只知道这样设置,其他我不会,大神你教教我吧应该添加什么上去开中断
汤权
2楼-- · 2019-03-24 21:39
要自己看手册的这种东西,我帮你看一次,我也没弄过STM8其实。。。。如下图:
汤权
3楼-- · 2019-03-24 22:46
 精彩回答 2  元偷偷看……
ziqidongsheng
4楼-- · 2019-03-24 23:07
汤权 发表于 2016-9-2 12:50
这个是正确的语法,不会报错的,你这种写法是对的但是程序原来的意思错了,但是这不是影响接收不到数据的 ...

刚刚我看了,有串口中断。我这个USART1_CR2=0x2c;分开来写就是:
USART1_CR2=0x08;    //发送使能 (USART1_CR2_TEN=0x08;)
USART1_CR2=0x04;    //接收使能(USART1_CR2_REN=0x04;)
USART1_CR2=0x80;   //发送中断使能(USART1_CR2_TIEN=0x80;)
USART1_CR2=0x20;   //接收中断使能(USART1_CR2_RIEN=0x20;)
汤权
5楼-- · 2019-03-25 02:46
ziqidongsheng 发表于 2016-9-2 14:43
刚刚我看了,有串口中断。我这个USART1_CR2=0x2c;分开来写就是:
USART1_CR2=0x08;    //发送使能 (USA ...

不是的,你这样写USART1_CR2的值最终是0x20,而不是0x2c,C语言基础这是,改成:
USART1_CR2 |= 0x08;    //发送使能 (USART1_CR2_TEN=0x08;)
USART1_CR2 |= 0x04;    //接收使能(USART1_CR2_REN=0x04;)
USART1_CR2 |= 0x80;   //发送中断使能(USART1_CR2_TIEN=0x80;)
USART1_CR2 |= 0x20;   //接收中断使能(USART1_CR2_RIEN=0x20;)
汤权
6楼-- · 2019-03-25 07:58
 精彩回答 2  元偷偷看……

一周热门 更多>