[技术问答] IO口配置了,却不能控制输出

[复制链接]
5708|26
 楼主| 前功尽弃 发表于 2015-1-9 10:26 | 显示全部楼层 |阅读模式
本帖最后由 前功尽弃 于 2015-1-9 13:23 编辑

#define BIT14   0x00004000        
SYS->GPAMFP.PWM2_AD15 = 0;//使能IO功能,设置为输出,初始化低电平
GPIOA->PMD.PMD14 = 1;
GPIOA->DOUT &= ~BIT14;
现象改端口一直输出为高电平,晕了
补充一下,我用的芯片是新唐的NUC100RE3AN
原因找到了。我在别的地方复用了该端口的pWM功能,原来是在在改引脚复用为PWM功能时,在PWM波定时器未使能,PWM波端口使能输出时,该端口会输出高电平的。其实答案就在时序图中啊。
感谢各位解答,坐等结贴发分给你们:lol
gaoyang9992006 发表于 2015-1-9 10:39 | 显示全部楼层
你这个错了吧,不是这样配置的,你有官方的例程吗,看看那个IO怎么配置的。
gaoyang9992006 发表于 2015-1-9 10:39 | 显示全部楼层
/* Configure P1.2 as Output mode and P4.1 as Input mode */
    GPIO_SetMode(P1, BIT2, GPIO_PMD_OUTPUT);
    GPIO_SetMode(P4, BIT1, GPIO_PMD_INPUT);
gaoyang9992006 发表于 2015-1-9 10:40 | 显示全部楼层
要这样写,使用库函数,你看你那,很难懂,使用库吧,我早不看寄存器了
gaoyang9992006 发表于 2015-1-9 10:41 | 显示全部楼层
/* Configure P1.2 and P4.1 to default Quasi-bidirectional mode */
    GPIO_SetMode(P1, BIT2, GPIO_PMD_QUASI);
    GPIO_SetMode(P4, BIT1, GPIO_PMD_QUASI);
这个是类似双向
gaoyang9992006 发表于 2015-1-9 10:42 | 显示全部楼层
这样做。
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V3.00
  4. * $Revision: 3 $
  5. * $Date: 14/01/28 11:44a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    M051 Series GPIO Driver Sample Code
  7. *
  8. * @note
  9. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  10. ******************************************************************************/
  11. #include <stdio.h>
  12. #include "M051Series.h"


  13. #define PLL_CLOCK           50000000


  14. void SYS_Init(void)
  15. {
  16.     /*---------------------------------------------------------------------------------------------------------*/
  17.     /* Init System Clock                                                                                       */
  18.     /*---------------------------------------------------------------------------------------------------------*/
  19.     /* Enable Internal RC 22.1184MHz clock */
  20.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  21.     /* Waiting for Internal RC clock ready */
  22.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  23.     /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
  24.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  25.     /* Enable external XTAL 12MHz clock */
  26.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  27.     /* Waiting for external XTAL clock ready */
  28.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  29.     /* Set core clock as PLL_CLOCK from PLL */
  30.     CLK_SetCoreClock(PLL_CLOCK);

  31.     /* Enable UART module clock */
  32.     CLK_EnableModuleClock(UART0_MODULE);

  33.     /* Select UART module clock source */
  34.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_PLL, CLK_CLKDIV_UART(1));

  35.     /*---------------------------------------------------------------------------------------------------------*/
  36.     /* Init I/O Multi-function                                                                                 */
  37.     /*---------------------------------------------------------------------------------------------------------*/

  38.     /* Set P3 multi-function pins for UART0 RXD and TXD */
  39.     SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk);
  40.     SYS->P3_MFP |= (SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0);

  41. }

  42. void UART0_Init(void)
  43. {
  44.     /*---------------------------------------------------------------------------------------------------------*/
  45.     /* Init UART                                                                                               */
  46.     /*---------------------------------------------------------------------------------------------------------*/
  47.     /* Reset UART */
  48.     SYS_ResetModule(UART0_RST);

  49.     /* Configure UART0 and set UART0 Baudrate */
  50.     UART_Open(UART0, 115200);
  51. }

  52. /*---------------------------------------------------------------------------------------------------------*/
  53. /* MAIN function                                                                                           */
  54. /*---------------------------------------------------------------------------------------------------------*/
  55. int main(void)
  56. {
  57.     int32_t i32Err;

  58.     /* Unlock protected registers */
  59.     SYS_UnlockReg();

  60.     /* Init System, peripheral clock and multi-function I/O */
  61.     SYS_Init();

  62.     /* Lock protected registers */
  63.     SYS_LockReg();

  64.     /* Init UART0 for printf */
  65.     UART0_Init();

  66.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
  67.     printf("+-------------------------------------------------+\n");
  68.     printf("|    P1.2(Output) and P4.1(Input) Sample Code     |\n");
  69.     printf("+-------------------------------------------------+\n\n");

  70.     /* Configure P1.2 as Output mode and P4.1 as Input mode */
  71.     GPIO_SetMode(P1, BIT2, GPIO_PMD_OUTPUT);
  72.     GPIO_SetMode(P4, BIT1, GPIO_PMD_INPUT);

  73.     i32Err = 0;
  74.     printf("GPIO P1.2(output mode) connect to P4.1(input mode) ......");

  75.     /* Use Pin Data Input/Output Control to pull specified I/O or get I/O pin status */
  76.     P12 = 0;
  77.     if(P41 != 0)
  78.     {
  79.         i32Err = 1;
  80.     }

  81.     P12 = 1;
  82.     if(P41 != 1)
  83.     {
  84.         i32Err = 1;
  85.     }

  86.     if(i32Err)
  87.     {
  88.         printf("  [FAIL].\n");
  89.     }
  90.     else
  91.     {
  92.         printf("  [OK].\n");
  93.     }

  94.     /* Configure P1.2 and P4.1 to default Quasi-bidirectional mode */
  95.     GPIO_SetMode(P1, BIT2, GPIO_PMD_QUASI);
  96.     GPIO_SetMode(P4, BIT1, GPIO_PMD_QUASI);

  97.     while(1);
  98. }

  99. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/

mintspring 发表于 2015-1-9 10:49 | 显示全部楼层
#include "M051Series.h"
楼主可以看这个头函数如果是051系列,其他的系列看其他相关的这种头函数,现在ARM系列的单片机都不要用寄存器学了,直接库函数,方便移植。
 楼主| 前功尽弃 发表于 2015-1-9 10:50 | 显示全部楼层
mintspring 发表于 2015-1-9 10:49
#include "M051Series.h"
楼主可以看这个头函数如果是051系列,其他的系列看其他相关的这种头函数,现在ARM ...

难道我是任性了吗?:'(
 楼主| 前功尽弃 发表于 2015-1-9 10:51 | 显示全部楼层
gaoyang9992006 发表于 2015-1-9 10:39
你这个错了吧,不是这样配置的,你有官方的例程吗,看看那个IO怎么配置的。 ...

而且我刚刚把他配置成准双端模式,居然一直是低电平了,苍天啊
mintspring 发表于 2015-1-9 11:00 | 显示全部楼层
前功尽弃 发表于 2015-1-9 10:50
难道我是任性了吗?

淡定啊,不要较真,等会用了,再去深究里面的原理。
gaoyang9992006 发表于 2015-1-9 11:01 | 显示全部楼层
前功尽弃 发表于 2015-1-9 10:51
而且我刚刚把他配置成准双端模式,居然一直是低电平了,苍天啊

这个,或许你通过寄存器设置漏掉了某个环节,建议别用寄存器操作ARM这种复杂度高的芯片。得不偿失
 楼主| 前功尽弃 发表于 2015-1-9 11:09 | 显示全部楼层
gaoyang9992006 发表于 2015-1-9 11:01
这个,或许你通过寄存器设置漏掉了某个环节,建议别用寄存器操作ARM这种复杂度高的芯片。得不偿失 ...

现在,好了,我设置了一下准双端模式,又设置回来输出模式,居然能控制了,我也是醉了。
 楼主| 前功尽弃 发表于 2015-1-9 11:10 | 显示全部楼层
mintspring 发表于 2015-1-9 11:00
淡定啊,不要较真,等会用了,再去深究里面的原理。

已经较真2天了:Q
gaoyang9992006 发表于 2015-1-9 11:21 | 显示全部楼层
前功尽弃 发表于 2015-1-9 11:10
已经较真2天了

那样方向错了,建议你先用库函数完成,然后你看库函数的圆形函数是怎么做的。这样学习寄存器比较好。
 楼主| 前功尽弃 发表于 2015-1-9 11:24 | 显示全部楼层
gaoyang9992006 发表于 2015-1-9 11:21
那样方向错了,建议你先用库函数完成,然后你看库函数的圆形函数是怎么做的。这样学习寄存器比较好。 ...

库函数试过以后才搞这个的,感觉还是这个亲切:lol
 楼主| 前功尽弃 发表于 2015-1-9 11:25 | 显示全部楼层
gaoyang9992006 发表于 2015-1-9 11:01
这个,或许你通过寄存器设置漏掉了某个环节,建议别用寄存器操作ARM这种复杂度高的芯片。得不偿失 ...

怎么结贴啊
gaoyang9992006 发表于 2015-1-9 11:30 | 显示全部楼层
前功尽弃 发表于 2015-1-9 11:25
怎么结贴啊

不知道今天可以结贴不,你点给分结贴,把分都给我就行了,哈哈
gaoyang9992006 发表于 2015-1-9 11:31 | 显示全部楼层
前功尽弃 发表于 2015-1-9 11:24
库函数试过以后才搞这个的,感觉还是这个亲切

那你就点击右键,然后定位到函数的原型位置,看他们原型里面怎么操作的寄存器。
北斗小星 发表于 2015-1-9 12:43 | 显示全部楼层
fangzimo 发表于 2015-1-9 13:24 | 显示全部楼层
前功尽弃 发表于 2015-1-9 11:25
怎么结贴啊

参考https://bbs.21ic.com/forum.php?mod=viewthread&tid=442857&highlight=如何结贴
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:最难的不是决定它,而是承受它

7

主题

772

帖子

4

粉丝
快速回复 在线客服 返回列表 返回顶部