[菜农助学交流] (第四批)tendence第三帖:KEY

[复制链接]
2268|3
 楼主| tendence 发表于 2011-11-18 19:44 | 显示全部楼层 |阅读模式
ce, TE, IO, gp, pi
本帖最后由 tendence 于 2011-11-18 19:48 编辑

再次感谢lixiaoxu2meng的帮助,其实这个程序真的是我写的,只是写好之后发现跟lixiaoxu2meng的key程序很像
该程序总共使用key1,key2两个键,key1使led亮灯数目增加,key2减少
直接上程序
includes.h

  1. #include <stdio.h>
  2. #include "NUC1xx.h"
  3. #include "variables.h"
  4. #include "hw_config.h"
  5. #include "Driver\DrvGPIO.h"
  6. #include "Driver\DrvSYS.h"
hw_config.c
  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. DrvGPIO_Open( E_GPB, 15, E_IO_INPUT );
  45. DrvGPIO_Open( E_GPB, 16, E_IO_INPUT );
  46. }

  47. /*************************************************************************************
  48. ** Function name: delay_ms
  49. ** Descriptions: 1ms(晶振为12MHZ)延时子程序
  50. ** input parameters: count
  51. ** output parameters: 无
  52. ** Returned value: 无
  53. *************************************************************************************/
  54. void delay_ms(uint32_t count)
  55. {
  56. uint32_t i,j;
  57. for(i=count;i>0;i--)
  58. for(j=2395;j>0;j--);
  59. }
main.c
  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. uint8_t lednum,flag_key1,flag_key2; //计数亮灯的数目
  17. Set_System();


  18. lednum=0;
  19. while(1)
  20. {
  21. while(!DrvGPIO_GetBit(E_GPB,15))
  22. {
  23. delay_ms(30);
  24. if(!DrvGPIO_GetBit(E_GPB,15))
  25. {
  26. flag_key1=1;
  27. while(!DrvGPIO_GetBit(E_GPB,15)) ;
  28. }
  29. }

  30. if(!DrvGPIO_GetBit(E_GPB,14))
  31. {
  32. delay_ms(30);
  33. if(!DrvGPIO_GetBit(E_GPB,14))
  34. {
  35. flag_key2=1;
  36. while(!DrvGPIO_GetBit(E_GPB,14)) ;
  37. }
  38. }

  39. if(flag_key1)
  40. {
  41. flag_key1=0;
  42. lednum++;
  43. if(lednum==5) lednum=0;
  44. }

  45. if(flag_key2)
  46. {
  47. flag_key2=0;
  48. if(lednum==0) lednum=5;
  49. lednum--;
  50. }

  51. switch(lednum)
  52. {
  53. case 0:
  54. DrvGPIO_SetBit(E_GPA,2);
  55. DrvGPIO_SetBit(E_GPA,3);
  56. DrvGPIO_SetBit(E_GPA,4);
  57. DrvGPIO_SetBit(E_GPA,5);
  58. break;
  59. case 1:
  60. DrvGPIO_ClrBit(E_GPA,2);
  61. break;
  62. case 2:
  63. DrvGPIO_ClrBit(E_GPA,3);
  64. break;
  65. case 3:
  66. DrvGPIO_ClrBit(E_GPA,4);
  67. break;
  68. case 4:
  69. DrvGPIO_ClrBit(E_GPA,5);
  70. break;
  71. }
  72. }
  73. }

 楼主| tendence 发表于 2011-11-18 19:47 | 显示全部楼层
本帖最后由 tendence 于 2011-11-18 19:48 编辑

该程序还有个问题就是我设计原本一盏灯都不亮,可偏偏亮一盏,我也很奇怪。

本帖子中包含更多资源

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

×
hotpower 发表于 2011-11-18 23:41 | 显示全部楼层
俺能看出是lixiaoxu2meng的风格~~~
 楼主| tendence 发表于 2011-11-19 15:05 | 显示全部楼层
3# hotpower
不好意思,因为风格是完全模仿的,主要还是因为他的风格很简洁,看着舒服
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:把技术记在心里

1

主题

164

帖子

0

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