123下一页
返回列表 发新帖我要提问本帖赏金: 6.00元(功能说明)

[技术问答] 新手上路,求教关于寄存器的操作

[复制链接]
5748|45
 楼主| pankration 发表于 2015-11-20 18:52 | 显示全部楼层 |阅读模式
最近项目用到了NUC220,不过之前一直用其他品牌的8位机,对于这款单片机比较陌生。

拿到开发板后,先搭建了开发环境,用的KEIL MDK511。

之后做了一个小实验,使一个发光二极管闪烁,效果还可以。
1339984940.jpg
之后想做一个流水灯实验,结果就遇到了问题,我无法对寄存器进行操作,KEIL提示没有定义标识符,
test_io_led_changbit.JPG test_io_led_changbit_datasheet.JPG
所以,想请教一下,这个问题如何解决,谢谢。

最后附上程序,希望大家多多指教。
LED.rar (295.01 KB, 下载次数: 5)

打赏榜单

21ic小喇叭 打赏了 5.00 元 2015-11-27

gejigeji521 发表于 2015-11-20 22:39 | 显示全部楼层
楼主看看这个芯片头文件啊,看看头文件定义的端口是什么名字?可能你这个手册和头文件不一致。

评分

参与人数 1威望 +1 收起 理由
pankration + 1 很给力!

查看全部评分

gejigeji521 发表于 2015-11-20 22:42 | 显示全部楼层
  1. #define GPIO_PIN_DATA(port, pin)    (*((volatile uint32_t *)((GPIO_PIN_DATA_BASE+(0x40*(port))) + ((pin)<<2))))
  2. #define PA0             GPIO_PIN_DATA(0, 0 ) /*!< Specify PA.0 Pin Data Input/Output */
  3. #define PA1             GPIO_PIN_DATA(0, 1 ) /*!< Specify PA.1 Pin Data Input/Output */
  4. #define PA2             GPIO_PIN_DATA(0, 2 ) /*!< Specify PA.2 Pin Data Input/Output */
  5. #define PA3             GPIO_PIN_DATA(0, 3 ) /*!< Specify PA.3 Pin Data Input/Output */
  6. #define PA4             GPIO_PIN_DATA(0, 4 ) /*!< Specify PA.4 Pin Data Input/Output */
  7. #define PA5             GPIO_PIN_DATA(0, 5 ) /*!< Specify PA.5 Pin Data Input/Output */
  8. #define PA6             GPIO_PIN_DATA(0, 6 ) /*!< Specify PA.6 Pin Data Input/Output */
  9. #define PA7             GPIO_PIN_DATA(0, 7 ) /*!< Specify PA.7 Pin Data Input/Output */
  10. #define PA8             GPIO_PIN_DATA(0, 8 ) /*!< Specify PA.8 Pin Data Input/Output */
  11. #define PA9             GPIO_PIN_DATA(0, 9 ) /*!< Specify PA.9 Pin Data Input/Output */
  12. #define PA10            GPIO_PIN_DATA(0, 10) /*!< Specify PA.10 Pin Data Input/Output */
  13. #define PA11            GPIO_PIN_DATA(0, 11) /*!< Specify PA.11 Pin Data Input/Output */
  14. #define PA12            GPIO_PIN_DATA(0, 12) /*!< Specify PA.12 Pin Data Input/Output */
  15. #define PA13            GPIO_PIN_DATA(0, 13) /*!< Specify PA.13 Pin Data Input/Output */
  16. #define PA14            GPIO_PIN_DATA(0, 14) /*!< Specify PA.14 Pin Data Input/Output */
  17. #define PA15            GPIO_PIN_DATA(0, 15) /*!< Specify PA.15 Pin Data Input/Output */
  18. #define PB0             GPIO_PIN_DATA(1, 0 ) /*!< Specify PB.0 Pin Data Input/Output */
  19. #define PB1             GPIO_PIN_DATA(1, 1 ) /*!< Specify PB.1 Pin Data Input/Output */
  20. #define PB2             GPIO_PIN_DATA(1, 2 ) /*!< Specify PB.2 Pin Data Input/Output */
  21. #define PB3             GPIO_PIN_DATA(1, 3 ) /*!< Specify PB.3 Pin Data Input/Output */
  22. #define PB4             GPIO_PIN_DATA(1, 4 ) /*!< Specify PB.4 Pin Data Input/Output */
  23. #define PB5             GPIO_PIN_DATA(1, 5 ) /*!< Specify PB.5 Pin Data Input/Output */
  24. #define PB6             GPIO_PIN_DATA(1, 6 ) /*!< Specify PB.6 Pin Data Input/Output */
  25. #define PB7             GPIO_PIN_DATA(1, 7 ) /*!< Specify PB.7 Pin Data Input/Output */
  26. #define PB8             GPIO_PIN_DATA(1, 8 ) /*!< Specify PB.8 Pin Data Input/Output */
  27. #define PB9             GPIO_PIN_DATA(1, 9 ) /*!< Specify PB.9 Pin Data Input/Output */
  28. #define PB10            GPIO_PIN_DATA(1, 10) /*!< Specify PB.10 Pin Data Input/Output */
  29. #define PB11            GPIO_PIN_DATA(1, 11) /*!< Specify PB.11 Pin Data Input/Output */
  30. #define PB12            GPIO_PIN_DATA(1, 12) /*!< Specify PB.12 Pin Data Input/Output */
  31. #define PB13            GPIO_PIN_DATA(1, 13) /*!< Specify PB.13 Pin Data Input/Output */
  32. #define PB14            GPIO_PIN_DATA(1, 14) /*!< Specify PB.14 Pin Data Input/Output */
  33. #define PB15            GPIO_PIN_DATA(1, 15) /*!< Specify PB.15 Pin Data Input/Output */
  34. #define PC0             GPIO_PIN_DATA(2, 0 ) /*!< Specify PC.0 Pin Data Input/Output */
  35. #define PC1             GPIO_PIN_DATA(2, 1 ) /*!< Specify PC.1 Pin Data Input/Output */
  36. #define PC2             GPIO_PIN_DATA(2, 2 ) /*!< Specify PC.2 Pin Data Input/Output */
  37. #define PC3             GPIO_PIN_DATA(2, 3 ) /*!< Specify PC.3 Pin Data Input/Output */
  38. #define PC4             GPIO_PIN_DATA(2, 4 ) /*!< Specify PC.4 Pin Data Input/Output */
  39. #define PC5             GPIO_PIN_DATA(2, 5 ) /*!< Specify PC.5 Pin Data Input/Output */
  40. #define PC6             GPIO_PIN_DATA(2, 6 ) /*!< Specify PC.6 Pin Data Input/Output */
  41. #define PC7             GPIO_PIN_DATA(2, 7 ) /*!< Specify PC.7 Pin Data Input/Output */
  42. #define PC8             GPIO_PIN_DATA(2, 8 ) /*!< Specify PC.8 Pin Data Input/Output */
  43. #define PC9             GPIO_PIN_DATA(2, 9 ) /*!< Specify PC.9 Pin Data Input/Output */
  44. #define PC10            GPIO_PIN_DATA(2, 10) /*!< Specify PC.10 Pin Data Input/Output */
  45. #define PC11            GPIO_PIN_DATA(2, 11) /*!< Specify PC.11 Pin Data Input/Output */
  46. #define PC12            GPIO_PIN_DATA(2, 12) /*!< Specify PC.12 Pin Data Input/Output */
  47. #define PC13            GPIO_PIN_DATA(2, 13) /*!< Specify PC.13 Pin Data Input/Output */
  48. #define PC14            GPIO_PIN_DATA(2, 14) /*!< Specify PC.14 Pin Data Input/Output */
  49. #define PC15            GPIO_PIN_DATA(2, 15) /*!< Specify PC.15 Pin Data Input/Output */
  50. #define PD0             GPIO_PIN_DATA(3, 0 ) /*!< Specify PD.0 Pin Data Input/Output */
  51. #define PD1             GPIO_PIN_DATA(3, 1 ) /*!< Specify PD.1 Pin Data Input/Output */
  52. #define PD2             GPIO_PIN_DATA(3, 2 ) /*!< Specify PD.2 Pin Data Input/Output */
  53. #define PD3             GPIO_PIN_DATA(3, 3 ) /*!< Specify PD.3 Pin Data Input/Output */
  54. #define PD4             GPIO_PIN_DATA(3, 4 ) /*!< Specify PD.4 Pin Data Input/Output */
  55. #define PD5             GPIO_PIN_DATA(3, 5 ) /*!< Specify PD.5 Pin Data Input/Output */
  56. #define PD6             GPIO_PIN_DATA(3, 6 ) /*!< Specify PD.6 Pin Data Input/Output */
  57. #define PD7             GPIO_PIN_DATA(3, 7 ) /*!< Specify PD.7 Pin Data Input/Output */
  58. #define PD8             GPIO_PIN_DATA(3, 8 ) /*!< Specify PD.8 Pin Data Input/Output */
  59. #define PD9             GPIO_PIN_DATA(3, 9 ) /*!< Specify PD.9 Pin Data Input/Output */
  60. #define PD10            GPIO_PIN_DATA(3, 10) /*!< Specify PD.10 Pin Data Input/Output */
  61. #define PD11            GPIO_PIN_DATA(3, 11) /*!< Specify PD.11 Pin Data Input/Output */
  62. #define PD12            GPIO_PIN_DATA(3, 12) /*!< Specify PD.12 Pin Data Input/Output */
  63. #define PD13            GPIO_PIN_DATA(3, 13) /*!< Specify PD.13 Pin Data Input/Output */
  64. #define PD14            GPIO_PIN_DATA(3, 14) /*!< Specify PD.14 Pin Data Input/Output */
  65. #define PD15            GPIO_PIN_DATA(3, 15) /*!< Specify PD.15 Pin Data Input/Output */
  66. #define PE0             GPIO_PIN_DATA(4, 0 ) /*!< Specify PE.0 Pin Data Input/Output */
  67. #define PE1             GPIO_PIN_DATA(4, 1 ) /*!< Specify PE.1 Pin Data Input/Output */
  68. #define PE2             GPIO_PIN_DATA(4, 2 ) /*!< Specify PE.2 Pin Data Input/Output */
  69. #define PE3             GPIO_PIN_DATA(4, 3 ) /*!< Specify PE.3 Pin Data Input/Output */
  70. #define PE4             GPIO_PIN_DATA(4, 4 ) /*!< Specify PE.4 Pin Data Input/Output */
  71. #define PE5             GPIO_PIN_DATA(4, 5 ) /*!< Specify PE.5 Pin Data Input/Output */
  72. #define PE6             GPIO_PIN_DATA(4, 6 ) /*!< Specify PE.6 Pin Data Input/Output */
  73. #define PE7             GPIO_PIN_DATA(4, 7 ) /*!< Specify PE.7 Pin Data Input/Output */
  74. #define PE8             GPIO_PIN_DATA(4, 8 ) /*!< Specify PE.8 Pin Data Input/Output */
  75. #define PE9             GPIO_PIN_DATA(4, 9 ) /*!< Specify PE.9 Pin Data Input/Output */
  76. #define PE10            GPIO_PIN_DATA(4, 10) /*!< Specify PE.10 Pin Data Input/Output */
  77. #define PE11            GPIO_PIN_DATA(4, 11) /*!< Specify PE.11 Pin Data Input/Output */
  78. #define PE12            GPIO_PIN_DATA(4, 12) /*!< Specify PE.12 Pin Data Input/Output */
  79. #define PE13            GPIO_PIN_DATA(4, 13) /*!< Specify PE.13 Pin Data Input/Output */
  80. #define PE14            GPIO_PIN_DATA(4, 14) /*!< Specify PE.14 Pin Data Input/Output */
  81. #define PE15            GPIO_PIN_DATA(4, 15) /*!< Specify PE.15 Pin Data Input/Output */
  82. #define PF0             GPIO_PIN_DATA(5, 0 ) /*!< Specify PF.0 Pin Data Input/Output */
  83. #define PF1             GPIO_PIN_DATA(5, 1 ) /*!< Specify PF.1 Pin Data Input/Output */
  84. #define PF2             GPIO_PIN_DATA(5, 2 ) /*!< Specify PF.2 Pin Data Input/Output */
  85. #define PF3             GPIO_PIN_DATA(5, 3 ) /*!< Specify PF.3 Pin Data Input/Output */


评分

参与人数 1威望 +2 收起 理由
pankration + 2 很给力!

查看全部评分

gejigeji521 发表于 2015-11-20 22:43 | 显示全部楼层
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     gpio.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V3.00
  4. * $Revision: 9 $
  5. * $Date: 15/05/04 3:59p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    GPIO driver source file
  7. *
  8. * @note
  9. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/

  11. #include "NUC200Series.h"

  12. /** @addtogroup Standard_Driver Standard Driver
  13.   @{
  14. */

  15. /** @addtogroup GPIO_Driver GPIO Driver
  16.   @{
  17. */

  18. /** @addtogroup GPIO_EXPORTED_FUNCTIONS GPIO Exported Functions
  19.   @{
  20. */

  21. /**
  22. * @brief       Set GPIO operation mode
  23. *
  24. * @param[in]   port        GPIO port. It could be PA, PB, PC, PD, PE or PF.
  25. * @param[in]   u32PinMask  The single or multiple pins of specified GPIO port.
  26. *                          It could be BIT0 ~ BIT15 for PA, PB, PC, PD and PE GPIO port.
  27. *                          It could be BIT0 ~ BIT3 for PF GPIO port.
  28. * @param[in]   u32Mode     Operation mode. . It could be \n
  29. *                          GPIO_PMD_INPUT, GPIO_PMD_OUTPUT, GPIO_PMD_OPEN_DRAIN, GPIO_PMD_QUASI.
  30. *
  31. * [url=home.php?mod=space&uid=266161]@return[/url]      None
  32. *
  33. * [url=home.php?mod=space&uid=1543424]@Details[/url]     This function is used to set specified GPIO operation mode.
  34. */
  35. void GPIO_SetMode(GPIO_T *port, uint32_t u32PinMask, uint32_t u32Mode)
  36. {
  37.     uint32_t i;

  38.     for(i = 0; i < GPIO_PIN_MAX; i++)
  39.     {
  40.         if(u32PinMask & (1 << i))
  41.         {
  42.             port->PMD = (port->PMD & ~(0x3 << (i << 1))) | (u32Mode << (i << 1));
  43.         }
  44.     }
  45. }

  46. /**
  47. * @brief       Enable GPIO interrupt
  48. *
  49. * @param[in]   port            GPIO port. It could be PA, PB, PC, PD, PE or PF.
  50. * @param[in]   u32Pin          The pin of specified GPIO port.
  51. *                              It could be 0 ~ 15 for PA, PB, PC, PD and PE GPIO port.
  52. *                              It could be 0 ~ 3 for PF GPIO port.
  53. * @param[in]   u32IntAttribs   The interrupt attribute of specified GPIO pin. It could be \n
  54. *                              GPIO_INT_RISING, GPIO_INT_FALLING, GPIO_INT_BOTH_EDGE, GPIO_INT_HIGH, GPIO_INT_LOW.
  55. *
  56. * @return      None
  57. *
  58. * @details     This function is used to enable specified GPIO pin interrupt.
  59. */
  60. void GPIO_EnableInt(GPIO_T *port, uint32_t u32Pin, uint32_t u32IntAttribs)
  61. {
  62.     port->IMD |= (((u32IntAttribs >> 24) & 0xFFUL) << u32Pin);
  63.     port->IEN |= ((u32IntAttribs & 0xFFFFFFUL) << u32Pin);
  64. }


  65. /**
  66. * @brief       Disable GPIO interrupt
  67. *
  68. * @param[in]   port        GPIO port. It could be PA, PB, PC, PD, PE or PF.
  69. * @param[in]   u32Pin      The pin of specified GPIO port.
  70. *                          It could be 0 ~ 15 for PA, PB, PC, PD and PE GPIO port.
  71. *                          It could be 0 ~ 3 for PF GPIO port.
  72. *
  73. * @return      None
  74. *
  75. * @details     This function is used to enable specified GPIO pin interrupt.
  76. */
  77. void GPIO_DisableInt(GPIO_T *port, uint32_t u32Pin)
  78. {
  79.     port->IMD &= ~(1UL << u32Pin);
  80.     port->IEN &= ~((0x00010001UL) << u32Pin);
  81. }


  82. /*@}*/ /* end of group GPIO_EXPORTED_FUNCTIONS */

  83. /*@}*/ /* end of group GPIO_Driver */

  84. /*@}*/ /* end of group Device_Driver */

  85. /*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/


评分

参与人数 1威望 +2 收起 理由
pankration + 2

查看全部评分

gejigeji521 发表于 2015-11-20 22:44 | 显示全部楼层
看到没,?新唐的库函数写的非常好,感觉比STM32的好多了,你可以都认真看一遍。定义端口什么的,都要根据头文件的为准。

评分

参与人数 1威望 +2 收起 理由
pankration + 2

查看全部评分

 楼主| pankration 发表于 2015-11-21 07:43 | 显示全部楼层
gejigeji521 发表于 2015-11-20 22:44
看到没,?新唐的库函数写的非常好,感觉比STM32的好多了,你可以都认真看一遍。定义端口什么的,都要根据 ...

好的,非常感谢,我去试试
 楼主| pankration 发表于 2015-11-21 08:21 | 显示全部楼层
非常感谢前辈,程序按照预想正常运行了。
323403579.jpg 1601595316.jpg
稳稳の幸福 发表于 2015-11-21 16:24 | 显示全部楼层
接触一个新的单片机确实是要以头文件为准的,那个编译时候是调用的头文件,不是手册,手册有时候也会错。
 楼主| pankration 发表于 2015-11-21 16:36 | 显示全部楼层
稳稳の幸福 发表于 2015-11-21 16:24
接触一个新的单片机确实是要以头文件为准的,那个编译时候是调用的头文件,不是手册,手册有时候也会错。 ...

嗯,我要仔细看看头文件和官方驱动了。不过,datasheet中的GPIO都能错的话。。。也真不知道该说什么好了
稳稳の幸福 发表于 2015-11-21 16:44 | 显示全部楼层
pankration 发表于 2015-11-21 16:36
嗯,我要仔细看看头文件和官方驱动了。不过,datasheet中的GPIO都能错的话。。。也真不知道该说什么好了 ...

:lol
我猜是这样的,那个BSP包是比较新的,采用了库函数的方法,然后手册是基于存寄存器的方法,估计老版本的头文件是那样的。
 楼主| pankration 发表于 2015-11-21 16:54 | 显示全部楼层
稳稳の幸福 发表于 2015-11-21 16:44
我猜是这样的,那个BSP包是比较新的,采用了库函数的方法,然后手册是基于存寄存器的方法,估计老版 ...

也许是吧,这样只是有些不习惯。不过目前看来库函数还是很好用的。继续学习吧。
gejigeji521 发表于 2015-11-23 22:34 | 显示全部楼层
开发板很酷啊,上面那个大的是51单片机吧,一般提供STC89C52/51单片机。还可以使用内部的EEPROM。
 楼主| pankration 发表于 2015-11-24 07:54 | 显示全部楼层
gejigeji521 发表于 2015-11-23 22:34
开发板很酷啊,上面那个大的是51单片机吧,一般提供STC89C52/51单片机。还可以使用内部的EEPROM。 ...

是公司买的开发板,100pin的NUC220。上面那个是51,以前做项目用到的。
734774645 发表于 2015-11-24 21:12 | 显示全部楼层
http://www.nuvoton.com/hq/suppor ... ftware/?__locale=zh
从这里下载最新的BSP开发包,里面有丰富的例程和最新版的头文件和底层驱动

评分

参与人数 1威望 +1 收起 理由
pankration + 1

查看全部评分

734774645 发表于 2015-11-24 21:13 | 显示全部楼层
V3.00.002
2015-5-12
NUC200/220系列 (NUC200 / NUC220) 软件开发包是基于CMSIS 3.00版本,同时支持IAR以及Keil开发环境,提供NuTiny-SDK- NUC200/220开发板和学习板的各类周边驱动以及范例程序。详细内容,请参考说明文件。

评分

参与人数 1威望 +1 收起 理由
pankration + 1

查看全部评分

玛尼玛尼哄 发表于 2015-11-24 22:30 | 显示全部楼层
#define GPIO_PIN_DATA(port, pin)    (*((volatile uint32_t *)((GPIO_PIN_DATA_BASE+(0x40*(port))) + ((pin)<<2))))
#define PA0             GPIO_PIN_DATA(0, 0 ) /*!< Specify PA.0 Pin Data Input/Output */
这个宏定义的很巧妙啊。直接使用了地址。

评分

参与人数 1威望 +1 收起 理由
pankration + 1

查看全部评分

 楼主| pankration 发表于 2015-11-25 07:46 | 显示全部楼层
734774645 发表于 2015-11-24 21:12
http://www.nuvoton.com/hq/support/tool-and-software/software/?__locale=zh
从这里下载最新的BSP开发包 ...

谢谢提供资源
 楼主| pankration 发表于 2015-11-25 07:50 | 显示全部楼层
玛尼玛尼哄 发表于 2015-11-24 22:30
#define GPIO_PIN_DATA(port, pin)    (*((volatile uint32_t *)((GPIO_PIN_DATA_BASE+(0x40*(port))) + (( ...

是的,其实我觉得使用提供的库函数和宏定义应该会简化操作和降低误操作性,不过我第一次接触这种芯片,不太适应,以前一直用8位机
玛尼玛尼哄 发表于 2015-11-28 18:04 | 显示全部楼层
从51过渡到ARM确实是感觉难,以前51就一个P0,P1,的寄存器,没有什么方向了,第二功能了,比较好学。
 楼主| pankration 发表于 2015-11-30 07:51 | 显示全部楼层
玛尼玛尼哄 发表于 2015-11-28 18:04
从51过渡到ARM确实是感觉难,以前51就一个P0,P1,的寄存器,没有什么方向了,第二功能了,比较好学。 ...

51用于入门还不错,不过有些开发就不能胜任了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

29

帖子

0

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