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.

急!我用TM4C123GH6PM遇到很棘手的问题,为什么我同时初始化PA、PB口引脚输出,且都输出高电平,但输出结果不对,有的口为1,有的为0???而单独使用一个口(如PA口)时,就可以?我贴上程序,请教各位大神帮帮忙,谢谢!!!这是个简单的程序来验证的

Other Parts Discussed in Thread: TM4C123GH6PM

#include <stdint.h>
#include <inc\tm4c123gh6pm.h>


int main(void)
{
//将AB端口的总线形式设置为AHB总线形式
SYSCTL_GPIOHBCTL_R = SYSCTL_GPIOHBCTL_PORTA;
SYSCTL_GPIOHBCTL_R = SYSCTL_GPIOHBCTL_PORTB;
//启动AB端口时钟
SYSCTL_RCGCGPIO_R = SYSCTL_RCGCGPIO_R0 ;
SYSCTL_RCGCGPIO_R = SYSCTL_RCGCGPIO_R1 ;
//设置引脚为输出
//GPIO的某些寄存器位是直接与端口管脚号对应,因此没有掩码之类的寄存器
GPIO_PORTA_AHB_DIR_R = 0x000000FF;
GPIO_PORTB_AHB_DIR_R = 0x000000FF;
//激活引脚的数字输入输出功能
GPIO_PORTA_AHB_DEN_R = 0x000000FF;
GPIO_PORTB_AHB_DEN_R = 0x000000FF;
while(1)
{
GPIO_PORTA_AHB_DATA_R = 0x000000FF;
GPIO_PORTB_AHB_DATA_R = 0x000000FF;

}
}

  • PA0和PA1的默认的GPIOPCTL是1,其他的都是0.所以会有上述问题。

    如果想使用GPIO,还是用库函数吧,简单:

    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);

    GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_0, GPIO_PIN_0);

    GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1, GPIO_PIN_1);

    想看详细的寄存器操作,看库函数的源码吧。

  • 急!!大神,我还想问个问题,如何用按键控制步进电机正反转,我自己编了条程序,还请大神帮忙看看为什么按键控制没用,非常感谢!!!

    #ifndef PART_TM4C123GH6PM
    #define PART_TM4C123GH6PM
    #endif

    #include <stdbool.h>
    #include <stdint.h>
    #include <stdio.h>
    #include <inc\tm4c123gh6pm.h>
    #include "driverlib/ssi.h"
    #include "driverlib/rom.h"
    #include "inc/hw_memmap.h"
    #include "driverlib/gpio.h"
    #include "driverlib/sysctl.h"

    #define data GPIO_PORTB_AHB_DATA_R

    unsigned int i,j,k;
    unsigned int N=512;
    unsigned char eight_pos[8]={0x06,0x07,0x03,0x0b,0x09,0x0d,0x0c,0x0e}; /*八拍驱动正转*/
    unsigned char eight_rev[8]={0x0e,0x0c,0x0d,0x09,0x0b,0x03,0x07,0x06}; /*八拍驱动正转*/
    void delay(unsigned int z);
    void m_eight_pos();
    void m_eight_rev();


    int main(void)
    {

    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);

    GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1|GPIO_PIN_2 |GPIO_PIN_3 );

    GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, GPIO_PIN_2 | GPIO_PIN_3);


    while(1)
    {

    if((GPIOPinRead(GPIO_PORTB_BASE, GPIO_PIN_2 ))==0)
    {
    m_eight_pos(); //八拍驱动正转360度
    }
    // delay(200);

    else if((GPIOPinRead(GPIO_PORTB_BASE, GPIO_PIN_3))==0)
    {
    m_eight_rev(); //八拍驱动反转360度
    }
    // delay(200);

    }

    }
    void delay(unsigned int z) /*延时z毫秒*/
    {
    unsigned int x,y;
    for(x=z;x>0;x--)
    for(y=110;y>0;y--) ;
    }
    /******************************八拍驱动正转 (N*22.5/32)*************************************/
    void m_eight_pos()
    {
    for(k=0;k<N;k++)
    {

    for(i=0;i<8;i++) //八拍一个脉冲转子转动5.625/2度,八拍共 22.5度

    {
    GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_0 | GPIO_PIN_1|GPIO_PIN_2 |GPIO_PIN_3,(eight_pos[i]&0x01)|(eight_pos[i]&0x02)|(eight_pos[i]&0x04)|(eight_pos[i]&0x08));

    delay(20);
    }


    }
    }
    /******************************八拍驱动反转 (N*22.5/32)*************************************/
    void m_eight_rev()
    {
    for(k=0;k<N;k++)
    {

    for(i=0;i<8;i++) //八拍一个脉冲转子转动5.625/2度,八拍共22.5度

    {
    GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_0 | GPIO_PIN_1|GPIO_PIN_2 |GPIO_PIN_3,(eight_rev[i]&0x01)|(eight_rev[i]&0x02)|(eight_rev[i]&0x04)|(eight_rev[i]&0x08));
    delay(20);
    }
    }
    }