[Cortex-M0技术交流] 菜鸟学习M0第一帖——GPIO

[复制链接]
 楼主| lixiaoxu2meng 发表于 2011-8-11 11:56 | 显示全部楼层 |阅读模式
本帖最后由 lixiaoxu2meng 于 2011-8-11 12:21 编辑

1.首先要建好自己的工程模板
2.配置好MDK选项
以上两点在论坛上都有详细的说明这里就不说了
3.按照自己的编程习惯向工程里添加代码
main函数代码
  1. *---------------------------------------------------------------------------------------------------------*/
  2. /* */
  3. /* Copyright(c) 2011 Nuvoton Technology Corp. All rights reserved. */
  4. /* */
  5. /*---------------------------------------------------------------------------------------------------------*/
  6. #include "includes.h" //包含所需的头文件
  7. /*************************************************************************************
  8. ** Function name: main
  9. ** Descriptions: 4个LED轮流点亮
  10. ** input parameters: 无
  11. ** output parameters: 无
  12. ** Returned value: 无
  13. *************************************************************************************/
  14. int main (void)
  15. {
  16. Set_System();

  17. while(1)
  18. {
  19. DrvGPIO_SetBit(E_GPA,5);
  20. DrvGPIO_ClrBit(E_GPA,2);
  21. delay_ms(1000);
  22. DrvGPIO_SetBit(E_GPA,2);
  23. DrvGPIO_ClrBit(E_GPA,3);
  24. delay_ms(1000);
  25. DrvGPIO_SetBit(E_GPA,3);
  26. DrvGPIO_ClrBit(E_GPA,4);
  27. delay_ms(1000);
  28. DrvGPIO_SetBit(E_GPA,4);
  29. DrvGPIO_ClrBit(E_GPA,5);
  30. delay_ms(1000);
  31. }
  32. }

hw_config函数代码 用来封装一些主函数以外的函数
  1. #include "includes.h" //包含所需的头文件
  2. /*************************************************************************************
  3. ** Function name: Set_System
  4. ** Descriptions: 系统配置
  5. ** input parameters: 无
  6. ** output parameters: 无
  7. ** Returned value: 无
  8. *************************************************************************************/
  9. void Set_System(void)
  10. {
  11. RCC_Configuration(); //配置系统时钟

  12. GPIO_Configuration(); //配置GPIO
  13. }



  14. /*************************************************************************************
  15. ** Function name: RCC_Configuration
  16. ** Descriptions: 系统时钟源设置
  17. ** input parameters: none
  18. ** output parameters: none
  19. ** Returned value: none
  20. *************************************************************************************/
  21. void RCC_Configuration(void)
  22. {
  23. UNLOCKREG(); // 对写保护位操作时 需要一次向0x50000 0100写入 0x59,0x16,0x88,
  24. DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1);//与其 SYSCLK->WRCON.XTL12M_EN = 1; 等同
  25. // PWRCON寄存器(这些寄存器在上电复位到用户解锁定之前是锁定的)除了 BIT[6]位其他位都受写保护
  26. // 解除这些需要向 0x50000 0100写入 0x59,0x16,0x88,
  27. // 令PWRCON寄存器的BITP[0]为1(即设定12M外部晶振)
  28. delay_ms(100); //while (DrvSYS_GetChipClockSourceStatus(E_SYS_XTL12M) != 1);//等待外部12MHZ晶振就绪
  29. LOCKREG(); // 向“0x5000_0100”写入任何值,就可以重锁保护寄存器
  30. }
  31. /*************************************************************************************
  32. ** Function name: GPIO_Configuration
  33. ** Descriptions: 端口配置
  34. ** input parameters: none
  35. ** output parameters: none
  36. ** Returned value: none
  37. *************************************************************************************/
  38. void GPIO_Configuration()
  39. {
  40. DrvGPIO_Open( E_GPA, 2, E_IO_OUTPUT );
  41. DrvGPIO_Open( E_GPA, 3, E_IO_OUTPUT );
  42. DrvGPIO_Open( E_GPA, 4, E_IO_OUTPUT );
  43. DrvGPIO_Open( E_GPA, 5, E_IO_OUTPUT );
  44. }

  45. /*************************************************************************************
  46. ** Function name: delay_ms
  47. ** Descriptions: 1ms(晶振为12MHZ)延时子程序
  48. ** input parameters: count
  49. ** output parameters: 无
  50. ** Returned value: 无
  51. *************************************************************************************/
  52. void delay_ms(uint32_t count)
  53. {
  54. uint32_t i,j;
  55. for(i=count;i>0;i--)
  56. for(j=2395;j>0;j--);
  57. }
hw_config.h函数代码 用来声明hw_config.c里函数
  1. void Set_System(void);
  2. void RCC_Configuration(void);
  3. void GPIO_Configuration(void);
  4. includes.h函数包含所用到的头文件
  5. [code]#include <stdio.h>
  6. #include "NUC1xx.h"
  7. #include "variables.h"
  8. #include "hw_config.h"
  9. #include "Driver\DrvGPIO.h"
  10. #include "Driver\DrvSYS.h"

void delay_ms(uint32_t count);
[/code]
下面上传工程

本工程实现 4个led的轮流点亮
可以将延时 时间设短一点编程流水灯
第一个工程 还请大家 指正

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
lixin445122911 发表于 2011-8-11 15:24 | 显示全部楼层
不错!沙发占了啊
hotpower 发表于 2011-8-15 00:48 | 显示全部楼层
按照惯例,发裤子一条鼓励。
 楼主| lixiaoxu2meng 发表于 2011-8-15 07:17 | 显示全部楼层
多谢菜农老师:$
Swallow_0322 发表于 2011-8-15 10:44 | 显示全部楼层
:P继续努力,加油!
 楼主| lixiaoxu2meng 发表于 2011-8-15 11:01 | 显示全部楼层
:handshake 谢谢
lxj19901115 发表于 2011-8-16 22:31 | 显示全部楼层
其实这个直接GPIO里面的寄存器,看的更直接,只有了解底层,写的东西才牢靠
kfzy6 发表于 2011-8-24 18:14 | 显示全部楼层
:handshake多谢菜农老师
wangjia2000 发表于 2011-9-29 12:31 | 显示全部楼层
学习一下
wangjia2000 发表于 2011-9-30 09:02 | 显示全部楼层
谢谢
adampan 发表于 2011-10-8 15:25 | 显示全部楼层
思路不错
 楼主| lixiaoxu2meng 发表于 2011-10-8 19:33 | 显示全部楼层
谢谢
aijinquan 发表于 2012-1-13 20:48 | 显示全部楼层
来学习~
244258769 发表于 2012-1-21 15:08 | 显示全部楼层
这是在什么编译器下编辑的?
 楼主| lixiaoxu2meng 发表于 2012-1-21 18:06 | 显示全部楼层
nomorehest 发表于 2012-1-28 18:09 | 显示全部楼层
不错, 但是没有讲到输入啊/
你所有的引脚都是作为输出的.
 楼主| lixiaoxu2meng 发表于 2012-1-29 07:46 | 显示全部楼层
我有个按键的程序有端口设置成输入了 你可以看一下
cgd 发表于 2012-11-30 15:39 | 显示全部楼层
cav268 发表于 2012-12-6 21:23 | 显示全部楼层
非常感谢!
gdpjsx 发表于 2012-12-14 20:57 | 显示全部楼层
能不能发个链接啊,我想知道怎样在KEIL4 MDK 下建立工程啊,研究了好久,没有找到详细的资料啊,我手上是研讨会发的NUC123的板子,新手求照顾啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

1679

帖子

2

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

0

主题

1679

帖子

2

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