Advertisement
igendel

EFM8BB1CLK UART TX demo

Jul 10th, 2020
1,916
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.66 KB | None | 0 0
  1. // by Ido Gendel, 2020
  2. // Share and enjoy!
  3.  
  4. #include <SI_EFM8BB1_Register_Enums.h>
  5.  
  6. char msg[] = "Hello World!        \n"; // Includes room for numbers
  7. const uint8_t lastDigitPos = 19;
  8. volatile char *cp;
  9.  
  10. // Do not read directly - use millis()
  11. volatile uint32_t _millis = 0;
  12.  
  13. const uint8_t DEBOUNCE_MS = 10;
  14. volatile uint8_t debounceMSLeft;
  15. uint8_t oldButtonState = 1;
  16.  
  17.  
  18. // This function is called immediately after reset, in SILABS_STARTUP.A51
  19. void SiLabs_Startup (void) {
  20.  
  21.     // Disable watchdog
  22.     WDTCN = 0xDE;
  23.     WDTCN = 0xAD;
  24.  
  25. }
  26.  
  27.  
  28. uint32_t millis(void) {
  29.  
  30.     uint32_t result;
  31.  
  32.     IE_EA = 0; // Disable interrupts
  33.     result = _millis;
  34.     IE_EA = 1; // Re-enable interrupts
  35.     return result;
  36.  
  37. }
  38.  
  39.  
  40. void setup(void) {
  41.  
  42.     P1MDOUT |= P1MDOUT_B4__PUSH_PULL; // Set built-in LED pin (P1.4) to push-pull output
  43.     // Built-in button pin (P0.2) digital input by default
  44.     // Weak pull-ups enabled by default.
  45.     P0_B2 = 1;// Turn WPU on for button input
  46.  
  47.     // Default clock is internal 24.5MHz div 8
  48.     // As a default, timers 0/1 uses system clock div 12 prescaler
  49.     TMOD |= TMOD_T1M__MODE2; // Set Timer1 mode to 8-bit auto-reload
  50.     TH1 = 243; // Set reload value to produce OVF @19.2KHz
  51.     TCON_TR1 = 1; // Enable timer1
  52.  
  53.     TMOD |= TMOD_T0M__MODE1; // Set Timer0 mode to 16-bit
  54.     TH0 = 0xFF; // Set Timer0 counter to overflow after 255 "ticks"
  55.     TL0 = 0x01;
  56.     TCON_TR0 = 1; // Enable timer0
  57.  
  58.     // Enable UART in crossbar; TX pin (P0.4) will become output automatically
  59.     XBR0 |= XBR0_URT0E__ENABLED;
  60.     // Enable crossbar
  61.     XBR2 = 0x40;
  62.  
  63.     IE_ET0 = 1; // Enable timer0 interrupt
  64.     IE_ES0 = 1; // Enable UART0 interrupt
  65.     IE_EA = 1; // Enable interrupts globally
  66.  
  67. }
  68.  
  69.  
  70. void populateSecondsText(void) {
  71.  
  72.     uint8_t i = lastDigitPos;
  73.     uint32_t s = millis() / 1000;
  74.  
  75.     do {
  76.         msg[i--] = s % 10 + 48;
  77.         s = s / 10;
  78.     } while (s > 0);
  79.  
  80. }
  81.  
  82.  
  83. int main (void) {
  84.  
  85.     setup();
  86.  
  87.     for ( ; ; ) {
  88.  
  89.         if ((0 == debounceMSLeft) && (P0_B2 != oldButtonState)) {
  90.  
  91.             debounceMSLeft = DEBOUNCE_MS;
  92.             oldButtonState = P0_B2;
  93.             if (P0_B2) {
  94.  
  95.                 P1_B4 = 0; // LED ON
  96.                 populateSecondsText();
  97.                 cp = &msg[1];
  98.                 SBUF0 = msg[0]; // Start UART transmission
  99.  
  100.             }
  101.  
  102.         }
  103.  
  104.     }
  105.  
  106. }
  107.  
  108.  
  109. SI_INTERRUPT (TIMER0_OVF_ISR, TIMER0_IRQn) {
  110.  
  111.     // Interrupt flag is cleared automatically
  112.     TH0 = 0xFF; // Reload counter value
  113.     TL0 = 0x01;
  114.     ++_millis;
  115.     if (debounceMSLeft) {
  116.         --debounceMSLeft;
  117.     }
  118.  
  119. }
  120.  
  121. SI_INTERRUPT (UART0_ISR, UART0_IRQn) {
  122.  
  123.     // Transmit interrupt flag must be cleared in firmware:
  124.     SCON0_TI = 0;
  125.  
  126.     // Anything left to transmit? (assuming null-terminated string)
  127.     if (0 != *cp) {
  128.         SBUF0 = *cp++;
  129.     } else {
  130.         P1_B4 = 1; // LED OFF
  131.       }
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement