标题: c8051f320单片机15个例程源码(都是一些常用功能) [打印本页]

作者: 无敌游    时间: 2018-7-24 19:27
标题: c8051f320单片机15个例程源码(都是一些常用功能)
c8051单片机例程源码


单片机源程序如下:
  1. //-----------------------------------------------------------------------------
  2. // F32x_External_Interrupts.c
  3. //-----------------------------------------------------------------------------
  4. // Copyright 2007 Silicon Laboratories, Inc.

  5. // Program Description:
  6. //
  7. // This software shows the necessary configuration to use External Interrupt 0
  8. // (/INT0) or External Interrupt 1 (/INT1) as an interrupt source.  The code
  9. // executes the initialization routines and then spins in an infinite while()
  10. // loop.  If the button on P1.6 (on the target board) is pressed, then the
  11. // edge-triggered /INT0 input on P0.0 will cause an interrupt and toggle the
  12. // LED.
  13. //
  14. // Pinout:
  15. //
  16. // P0.0 - /INT0
  17. // P0.1 - /INT1
  18. //
  19. // P2.0 - SW1 (Switch 1)
  20. // P2.1 - SW2 (Switch 2)
  21. // P2.2 - LED1
  22. // P2.3 - LED2
  23. //
  24. // How To Test:
  25. //
  26. // 1) Compile and download code to a 'F32x target board.
  27. // 2) On the target board, connect P2.0_SW and P2.1_SW signals on J3 to P0.0
  28. //    for /INT0 and P0.1 for /INT1.
  29. // 3) Press the switches.  Every time a switch is pressed, the P2.2 or P2.3
  30. //    LED should toggle.
  31. //
  32. // Target:         C8051F32x
  33. // Tool chain:     Keil C51 7.50 / Keil EVAL C51
  34. // Command Line:   None
  35. //
  36. //
  37. // Release 1.0
  38. //    -Initial Revision (TP)
  39. //    -14 JUN 2007
  40. //

  41. //-----------------------------------------------------------------------------
  42. // Include Files
  43. //-----------------------------------------------------------------------------

  44. #include <C8051F320.h>

  45. //-----------------------------------------------------------------------------
  46. // Global Constants
  47. //-----------------------------------------------------------------------------

  48. #define SYSCLK             12000000    // Clock speed in Hz

  49. sbit SW1 = P2^0;                       // Push-button switch on board
  50. sbit SW2 = P2^1;                       // Push-button switch on board
  51. sbit LED1 = P2^2;                      // Green LED
  52. sbit LED2 = P2^3;                      // Green LED

  53. //-----------------------------------------------------------------------------
  54. // Function Prototypes
  55. //-----------------------------------------------------------------------------

  56. void Oscillator_Init (void);           // Configure the system clock
  57. void Port_Init (void);                 // Configure the Crossbar and GPIO
  58. void Ext_Interrupt_Init (void);        // Configure External Interrupts (/INT0
  59.                                        // and /INT1)

  60. //-----------------------------------------------------------------------------
  61. // MAIN Routine
  62. //-----------------------------------------------------------------------------
  63. void main (void)
  64. {
  65.    PCA0MD &= ~0x40;                    // Disable Watchdog timer

  66.    Oscillator_Init();                  // Initialize the system clock
  67.    Port_Init ();                       // Initialize crossbar and GPIO
  68.    Ext_Interrupt_Init();               // Initialize External Interrupts

  69.    EA = 1;

  70.    while(1);                           // Infinite while loop waiting for
  71.                                        // an interrupt from /INT0 or /INT1
  72. }

  73. //-----------------------------------------------------------------------------
  74. // Initialization Subroutines
  75. //-----------------------------------------------------------------------------

  76. //-----------------------------------------------------------------------------
  77. // Oscillator_Init
  78. //-----------------------------------------------------------------------------
  79. //
  80. // Return Value : None
  81. // Parameters   : None
  82. //
  83. // This routine initializes the system clock to use the precision internal
  84. // oscillator as its clock source.
  85. //-----------------------------------------------------------------------------
  86. void Oscillator_Init (void)
  87. {
  88.    OSCICN = 0x83;                      // Set internal oscillator to run
  89.                                        // at its maximum frequency
  90. }

  91. //-----------------------------------------------------------------------------
  92. // Port_Init
  93. //-----------------------------------------------------------------------------
  94. //
  95. // Return Value : None
  96. // Parameters   : None
  97. //
  98. // This function configures the crossbar and GPIO ports.
  99. //
  100. // Pinout:
  101. //
  102. // P0.0 - digital        open-drain         /INT0
  103. // P0.1 - digital        open-drain         /INT1
  104. //
  105. // P2.0 - digital        open-drain         SW1 (Switch 1)
  106. // P2.1 - digital        open-drain         SW2 (Switch 2)
  107. // P2.2 - digital        push-pull         LED1
  108. // P2.3 - digital        push-pull        LED2
  109. //
  110. //-----------------------------------------------------------------------------
  111. void Port_Init (void)
  112. {
  113.    XBR1     = 0x40;                    // Enable crossbar and weak pullups

  114.    P2MDOUT  = 0x0C;                    // LED1 and LED2 are push-pull outputs
  115. }

  116. //-----------------------------------------------------------------------------
  117. // Ext_Interrupt_Init
  118. //-----------------------------------------------------------------------------
  119. //
  120. // Return Value : None
  121. // Parameters   : None
  122. //
  123. // This function configures and enables /INT0 and /INT1 (External Interrupts)
  124. // as negative edge-triggered.
  125. //
  126. //-----------------------------------------------------------------------------
  127. void Ext_Interrupt_Init (void)
  128. {
  129.    TCON = 0x05;                        // /INT 0 and /INT 1 are edge triggered

  130.    IT01CF = 0x10;                      // /INT0 active low; /INT0 on P0.0;
  131.                                        // /INT1 active low; /INT1 on P0.1

  132.    EX0 = 1;                            // Enable /INT0 interrupts
  133.    EX1 = 1;                            // Enable /INT1 interrupts
  134. }

  135. //-----------------------------------------------------------------------------
  136. // Interrupt Service Routines
  137. //-----------------------------------------------------------------------------

  138. //-----------------------------------------------------------------------------
  139. // /INT0 ISR
  140. //-----------------------------------------------------------------------------
  141. //
  142. // Whenever a negative edge appears on P0.0, LED1 is toggled.
  143. // The interrupt pending flag is automatically cleared by vectoring to the ISR
  144. //
  145. //-----------------------------------------------------------------------------
  146. ……………………

  147. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
320-15个例程.zip (743.48 KB, 下载次数: 135)



作者: lyl_420819    时间: 2019-9-16 15:27
关于C8051F系列单片机的例程代码可到 https://github.com下载---C8051F-master.zip




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1