你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

PA2脚输入方波,想在PA1脚输出反向的方波,PA2状态监测不对..

[复制链接]
any012 提问时间:2016-6-4 16:07 /
悬赏1ST金币已解决
本帖最后由 any012 于 2016-6-4 16:16 编辑

感觉挺简单的,可就是在PA1脚输出不了波形。
单独控制PA1脚有定时器控制反转,没问题。
应该能排除硬件故障的问题。
想不出原因在哪?求助,自个看不出问题来...#include "stm32f10x.h"

  1. #include "stm32f10x.h"
  2. #include "gpio.h"

  3. int main(void)
  4. {
  5.         GPIO_Config();
  6.         while(1)
  7.         {
  8.                 if(PA2() == 1)
  9.                         PA1(1);
  10.                 else
  11.                         PA1(0);
  12.         }
  13. }
复制代码
  1. #include "gpio.h"

  2. void GPIO_Config(void)
  3. {
  4.         GPIO_InitTypeDef GPIO_InitStructure;
  5.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC, ENABLE);
  6.        
  7.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  8.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
  9.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  10.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  11.        
  12.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  13.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  14.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  15.        
  16.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  17.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  18.         GPIO_Init(GPIOC, &GPIO_InitStructure);
  19.        
  20.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  21.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  22.         GPIO_Init(GPIOC, &GPIO_InitStructure);
  23. }
复制代码
  1. #ifndef __GPIO_H_
  2. #define __GPIO_H_

  3. #include "stm32f10x.h"

  4. #define PA0(a)          if(a)\
  5.                                                         GPIO_SetBits(GPIOA, GPIO_Pin_0);\
  6.                                                 else\
  7.                                                         GPIO_ResetBits(GPIOA, GPIO_Pin_0)

  8. #define PA1(a)          if(a)\
  9.                                                         GPIO_SetBits(GPIOA, GPIO_Pin_1);\
  10.                                                 else\
  11.                                                         GPIO_ResetBits(GPIOA, GPIO_Pin_1)

  12. #define PC7(a)          if(a)\
  13.                                                         GPIO_SetBits(GPIOC, GPIO_Pin_7);\
  14.                                                 else\
  15.                                                         GPIO_ResetBits(GPIOC, GPIO_Pin_7)

  16. #define PA2()                GPIO_ReadInputDataBit(GPIOA, 2)

  17. #define PC6()                GPIO_ReadInputDataBit(GPIOC, 6)

  18. void GPIO_Config(void);
  19.        
  20. #endif
复制代码
PA2-PA1.zip (1.09 MB, 下载次数: 1)

最佳答案

查看完整内容

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 这个要改成输入。或者用输入捕获功能进行处理。
收藏 评论13 发布时间:2016-6-4 16:07

举报

13个回答
回答时间:2016-6-4 16:07:26
本帖最后由 安 于 2016-6-7 09:14 编辑

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

这个要改成输入。或者用输入捕获功能进行处理。

评分

参与人数 1ST金币 +2 收起 理由
zero99 + 2 赞一个!

查看全部评分

any012 回答时间:2016-6-4 16:08:19
本帖最后由 any012 于 2016-6-4 16:17 编辑

晕了,GPIO配置函数没有被调用...

调用了也不行,主楼已更新。
回答时间:2016-6-4 16:47:47
频率太高了。加个延时。
any012 回答时间:2016-6-4 16:57:05
安 发表于 2016-6-4 16:47
频率太高了。加个延时。

主循环太短吗?

按我的想法,主程序不停的检测PA2的状态,然后根据检测时PA2的状态,将PA1拉高或拉低。
any012 回答时间:2016-6-6 09:01:11
周一人多一些吧,希望有朋友能帮分析下。
dsjsjf 回答时间:2016-6-6 10:30:04
PA2() == 1 改成 PA2() != Bit_RESET再试试
any012 回答时间:2016-6-6 10:40:51
本帖最后由 any012 于 2016-6-6 10:41 编辑
dsjsjf 发表于 2016-6-6 10:30
PA2() == 1 改成 PA2() != Bit_RESET再试试
改成这样了,不行。
  1. int main(void)
  2. {
  3.         GPIO_Config();
  4.         while(1)
  5.         {
  6.                 if(GPIO_ReadInputDataBit(GPIOA, 1) != Bit_RESET)
  7.                         PA1(0);
  8.                 else
  9.                         PA1(1);
  10.         }
  11. }
复制代码
any012 回答时间:2016-6-6 10:43:56
这样也不行。

  1. int main(void)
  2. {
  3.         GPIO_Config();
  4.         while(1)
  5.         {
  6.        
  7.                 if(PA2() != Bit_RESET)
  8.                 {
  9.                         if(GPIO_ReadInputDataBit(GPIOA, 1) == Bit_RESET)
  10.                                 PA1(1);
  11.                 }
  12.                 else
  13.                 {
  14.                         if(GPIO_ReadInputDataBit(GPIOA, 1) != Bit_RESET)
  15.                                 PA1(0);
  16.                 }

  17.         }
  18. }
复制代码

any012 回答时间:2016-6-6 10:45:07
现在PA1引脚改成OD模式,输出外加个上拉电阻。
问题依旧。
12下一页

所属标签

相似问题

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
微信公众号二维码 微信公众号
手机版二维码 手机版