PA2脚输入方波,想在PA1脚输出反向的方波,PA2状态监测不对..
本帖最后由 any012 于 2016-6-4 16:16 编辑感觉挺简单的,可就是在PA1脚输出不了波形。
单独控制PA1脚有定时器控制反转,没问题。
应该能排除硬件故障的问题。
想不出原因在哪?求助,自个看不出问题来...#include "stm32f10x.h"
#include "stm32f10x.h"
#include "gpio.h"
int main(void)
{
GPIO_Config();
while(1)
{
if(PA2() == 1)
PA1(1);
else
PA1(0);
}
}
#include "gpio.h"
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
#ifndef __GPIO_H_
#define __GPIO_H_
#include "stm32f10x.h"
#define PA0(a) if(a)\
GPIO_SetBits(GPIOA, GPIO_Pin_0);\
else\
GPIO_ResetBits(GPIOA, GPIO_Pin_0)
#define PA1(a) if(a)\
GPIO_SetBits(GPIOA, GPIO_Pin_1);\
else\
GPIO_ResetBits(GPIOA, GPIO_Pin_1)
#define PC7(a) if(a)\
GPIO_SetBits(GPIOC, GPIO_Pin_7);\
else\
GPIO_ResetBits(GPIOC, GPIO_Pin_7)
#define PA2() GPIO_ReadInputDataBit(GPIOA, 2)
#define PC6() GPIO_ReadInputDataBit(GPIOC, 6)
void GPIO_Config(void);
#endif
本帖最后由 安 于 2016-6-7 09:14 编辑
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
这个要改成输入。或者用输入捕获功能进行处理。
本帖最后由 any012 于 2016-6-4 16:17 编辑
晕了,GPIO配置函数没有被调用...:L
调用了也不行,主楼已更新。
频率太高了。加个延时。 安 发表于 2016-6-4 16:47
频率太高了。加个延时。
主循环太短吗?
按我的想法,主程序不停的检测PA2的状态,然后根据检测时PA2的状态,将PA1拉高或拉低。 周一人多一些吧,希望有朋友能帮分析下。 PA2() == 1 改成 PA2() != Bit_RESET再试试 本帖最后由 any012 于 2016-6-6 10:41 编辑
dsjsjf 发表于 2016-6-6 10:30
PA2() == 1 改成 PA2() != Bit_RESET再试试改成这样了,不行。
int main(void)
{
GPIO_Config();
while(1)
{
if(GPIO_ReadInputDataBit(GPIOA, 1) != Bit_RESET)
PA1(0);
else
PA1(1);
}
}
这样也不行。
int main(void)
{
GPIO_Config();
while(1)
{
if(PA2() != Bit_RESET)
{
if(GPIO_ReadInputDataBit(GPIOA, 1) == Bit_RESET)
PA1(1);
}
else
{
if(GPIO_ReadInputDataBit(GPIOA, 1) != Bit_RESET)
PA1(0);
}
}
}
现在PA1引脚改成OD模式,输出外加个上拉电阻。
问题依旧。
页:
[1]
2