Advertisement
Electgpl

8051 - Blink Silabs

Sep 28th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.25 KB | None | 0 0
  1. //-----------------------------------------------------------------------------
  2. // F85x_Blinky.c
  3. //-----------------------------------------------------------------------------
  4. // Copyright (C) 2013 Silicon Laboratories, Inc.
  5. // http://www.silabs.com
  6. //
  7. // Program Description:
  8. //
  9. // This program flashes the red LED on the C8051F850 UDP MCU board about
  10. // five times a second using the interrupt handler for Timer2.
  11. // How To Test:
  12. //
  13. // 1) Ensure shorting blocks are place on the following:
  14. //    - J27: DS5 P1.0 - P1.0
  15. //    - JP2
  16. // 2) Place the SW1 switch in the +3.3V_VREG position and power the board
  17. //    using the J6 power connector.
  18. // 3) Compile and download code to the 'F85x UDP MCU board.
  19. // 4) Run the code. If the P1.0 (DS5) LED flashes, the program is working.
  20. //
  21. // Target:         C8051F850
  22. // Tool chain:     Generic
  23. // Command Line:   None
  24. //-----------------------------------------------------------------------------
  25. // Includes
  26. //-----------------------------------------------------------------------------
  27. #include <compiler_defs.h>             // compiler declarations
  28. #include <C8051F850_defs.h>            // SFR declarations
  29. //-----------------------------------------------------------------------------
  30. // Global CONSTANTS
  31. //-----------------------------------------------------------------------------
  32. #define SYSCLK       24500000/8        // SYSCLK frequency in Hz
  33. SBIT(LED, SFR_P1, 0);                  // DS5 P1.0 LED
  34. //-----------------------------------------------------------------------------
  35. // Function PROTOTYPES
  36. //-----------------------------------------------------------------------------
  37. void Port_Init (void);
  38. void Timer2_Init (int counts);
  39. //-----------------------------------------------------------------------------
  40. // MAIN Routine
  41. //-----------------------------------------------------------------------------
  42. void main (void){
  43.    // Disable the watchdog timer
  44.    WDTCN = 0xDE;
  45.    WDTCN = 0xAD;
  46.    Port_Init ();
  47.    Timer2_Init (SYSCLK / 12 / 10);  // Init Timer2 to generate interrupts
  48.                                     // at a 10 Hz rate.
  49.    EA = 1;                          // Enable global interrupts
  50.    while (1) {}                     // Spin forever
  51. }
  52. //-----------------------------------------------------------------------------
  53. // Port_Init
  54. //-----------------------------------------------------------------------------
  55. //
  56. // Configure the Crossbar and GPIO ports.
  57. //
  58. // P1.0 - LED (push-pull output)
  59. //
  60. void Port_Init (void){
  61.    P1MDOUT |= 0x01;                    // Enable LED as a push-pull output
  62.    XBR2 = 0x40;                        // Enable crossbar and weak pull-ups
  63. }
  64. //-----------------------------------------------------------------------------
  65. // Timer2_Init
  66. //-----------------------------------------------------------------------------
  67. //
  68. // Configure Timer2 to 16-bit auto-reload and generate an interrupt at
  69. // interval specified by <counts> using SYSCLK/48 as its time base.
  70. //
  71. void Timer2_Init (int counts){
  72.    TMR2CN = 0x00;                      // Stop Timer2; Clear TF2;
  73.                                        // use SYSCLK/12 as timebase
  74.    CKCON &= ~0x60;                     // Timer2 clocked based on T2XCLK;
  75.    TMR2RL = -counts;                   // Init reload values
  76.    TMR2 = 0xffff;                      // Set to reload immediately
  77.    ET2 = 1;                            // Enable Timer2 interrupts
  78.    TR2 = 1;                            // Start Timer2
  79. }
  80. //-----------------------------------------------------------------------------
  81. // Interrupt Service Routines
  82. //-----------------------------------------------------------------------------
  83. //-----------------------------------------------------------------------------
  84. // Timer2_ISR
  85. //-----------------------------------------------------------------------------
  86. // This routine changes the state of the LED whenever Timer2 overflows.
  87. //
  88. INTERRUPT(Timer2_ISR, INTERRUPT_TIMER2){
  89.    TF2H = 0;                              // Clear Timer2 interrupt flag
  90.    LED = !LED;                            // Change state of LED
  91. }
  92. //-----------------------------------------------------------------------------
  93. // End Of File
  94. //-----------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement