This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

msp430g2302重启和按键中断问题

在使用430mcu时,发现了两个问题:

1.在使用随机数rand()中,使用了rand()&0xff,会造成重启,最后就改成rand()%256;问题原因还没找出来。

2.在使用按键中断时,配置了上边沿触发,但是测试发现只要触发了两次,应该是上下边沿都触发了,不知道为什么,已经去抖了,不应该是这个问题。

请大家帮忙分析下以上两个问题,谢谢!

  • 能否提供下代码?

  • 1.对于随机数代码主要是我要实现定时器,如下:

        CCR0 = (SYS_CLK_32k>>2)+(rand()%256);// 不会重启;

       CCR0 = (SYS_CLK_32k>>2)+(rand&0xFF);// 会重启,不是必然的;

    2.按键中断配置如下:

    gpio_int_enable(PORT1, (KEY0 | KEY1), INT_RISE_EDGE); //上升沿触发;看了文档,触发方式只能二选一,不知道我的为什么会触发两次?

    实现如下:

    void gpio_int_enable(uint8 port, uint8 pin, uint8 int_type)
    {
        if (port == PORT1)
        {
            P1IE |= pin;                            // KEY引脚中断使能
            
            if (int_type == INT_FALL_EDGE)            
                P1IES |= pin;                       // 下降沿中断
            else
                P1IES &= ~pin;                      // 上升沿中断
            
            P1IFG &= ~pin;                          // 清除中断标志
        }
        else if (port == PORT2)
        {
            P2IE |= pin;                            // KEY引脚中断使能
            
            if (int_type == INT_FALL_EDGE)            
                P2IES |= pin;                       // 下降沿中断
            else
                P2IES &= ~pin;                      // 上升沿中断
            P2IFG &= ~pin;                          // 清除中断标志
        }
    }