首页 > 解决方案 > 端口不发送输出

问题描述

我不明白为什么,尽管调用了该函数并且尽管适当地设置和清除了这些位,但当按下/未按下开关时,PWM 既不工作也不 LED 闪烁。我正在为这个项目使用Atmega 328 微控制器。这些是使用的以下代码。

主程序

#include <avr/io.h>
#include "gpio.h"
#include "DoorLock_ver_2.h"
#include "adc.h"
#include "PWM.h"
int is_locked;
int main(void)
//copy this main() to proteus
{

    //unsigned int bit:1;
    // Insert code
      GPIOConfig(PD1,OUTPUT);
      GPIOConfig(PD2,OUTPUT);
      GPIOConfig(PD3, INPUT);
      GPIOConfig(PD4, INPUT);
      GPIOConfig(PD5, OUTPUT);
      GPIOConfig(PD6,OUTPUT);
      int s=0,r=0;
    while(1)
    {
        s=SpeedAssess();
        r=RemoteCtrl();
        if(s==1)
        {
            PWM(1);//JUST TESTING for locking
            is_locked=1;
        }
        else {
            if(r==1){
                PWM(1);//for locking
                AutomaticWindow(1);
                is_locked = 1;
            }
            else{
                //for unlocking
                PWM(0);
                AutomaticWindow(0);
                is_locked = 0;
            }
        }
    }
    return 0;
}

DoorLock_ver_2.c

#include<avr/io.h>
#include<util/delay.h>
#include "gpio.h"
#include "adc.h"
#include "PWM.h"
#include "DoorLock_ver_2.h"


short int SpeedAssess(){
    //check if speed is greater than 20kmph and sends signal to lock the door
    
    if (GPIOPinRead(PD4)){
        return 1;
    }
    else{
        return 0;
    }
}
short int RemoteCtrl(){
    //on pressing the button, lock the door
    if (GPIOPinRead(PD3)){
        //AutomaticWindow(0);
        return 0;
    }
    else {
        //AutomaticWindow(1);
        return 1;
    }
}
void AutomaticWindow(short int f){

    //closes window automatically when vehicle is locked(lock button is pressed on remote)
    if(f==1)
    {
        GPIOPinWrite(PD2,HIGH);
        GPIOPinWrite(PD1,HIGH);
    }
    else
    {
        GPIOPinWrite(PD2,LOW);
        GPIOPinWrite(PD1,LOW);

    }
}

我为 Atmega328 制作了一个 GPIO api,因为他们坚持让我们这样做,我无法使 API 对所有端口都有效,所以它特定于微控制器的端口 D

gpio.c

#ifndef GPIO_C_INCLUDED
#define GPIO_C_INCLUDED
#include "GPIO.h"
#include<stdint.h>
#include<avr/io.h>
#include<util/delay.h>
#define INPUT 0
#define OUTPUT 1
#define HIGH 1
#define LOW 0
int PIN,MODE,State;


/******************************************************************************
    * Name: GPIOConfig (pin, mode)
    * Description: Configures the mode of the pin as INPUT/PULLUP or OUTPUT
    * Arguments: pin and mode
    * Returns: None
    ******************************************************************************/
int GPIOConfig(PIN,MODE)
{
    if(MODE==OUTPUT)
    {
        SET_BIT(DDRD,PIN);  //output configuration
    }
    else if(MODE==INPUT)
    {
        CLR_BIT(DDRD,PIN);  //input configuration
        SET_BIT(PORTD,PIN); //pullup configuration
    }
}


/******************************************************************************
* Name:GPIOPinRead(pin)
* Description: The function returns the state (0 or 1) of the input pin to be read.
* Arguments: pin
* Returns: 0 or 1
******************************************************************************/
int GPIOPinRead(PIN)
{
    uint8_t PIN_READ=0x00; // 00000000
    PIN_READ=PIND;

    if (PIN_READ & (1<<PIN)) // (00000100)& (00000100) //Pooling
    {
        return LOW;
        //CLR_BIT(PORTD,PD7);// LED off change pd7 to pin //here change it to returning value
    }
    else
    {
        return HIGH;
        //SET_BIT(PORTD,PD7); // LED on change pd7 to pin //here change it to returning value
    }
}

/******************************************************************************
* Name:GPIOPinWrite(pin, state)
* Description:  The function is used to write LOW or HIGH state to GPIO pin.
* Arguments: pin and state
* Returns: none
******************************************************************************/
int GPIOPinWrite(PIN,State)
{
    if(State==HIGH)
    {
        SET_BIT(PORTD,PIN);
    }
    else if(State==LOW)
    {
        CLR_BIT(PORTD,PIN);
    }

}


/******************************************************************************
* Internal functions
******************************************************************************/
/******************************************************************************
* Name:
* Description:
******************************************************************************/
/******************************************************************************
*****************************************************************************
* End of File
******************************************************************************/


#endif // GPIO_C_INCLUDED

我也在使用 proteus 工具进行模拟设置。这是我玩模拟后它达到的唯一状态。 仿真工具的最终状态,即使点击开关也不会改变。点击链接查看图片

我哪里错了?请帮忙。

标签: cembeddedmicrocontrolleratmegaatmega32

解决方案


推荐阅读