问题描述
我不明白为什么,尽管调用了函数并且适当地设置和清除了位,PWM 既不工作,按下/松开开关时 LED 也不闪烁。我在这个项目中使用了 Atmega 328 微控制器。 这些是以下使用的代码。
main.c
#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 工具进行模拟设置。这是我玩模拟后它达到的唯一状态。 Final state of the simulation tool,no change even if the switch is clicked. click on link to see image
我哪里出错了?请帮忙。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)