Advertisement
Guest User

Untitled

a guest
Dec 7th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.77 KB | None | 0 0
  1. /*****************************************************************************
  2.  *   Peripherals such as temp sensor, light sensor, accelerometer,
  3.  *   and trim potentiometer are monitored and values are written to
  4.  *   the OLED display.
  5.  *
  6.  *   Copyright(C) 2009, Embedded Artists AB
  7.  *   All rights reserved.
  8.  *
  9.  ******************************************************************************/
  10.  
  11. /*
  12. #include <stdio.h>
  13. //#include "mcu_regs.h"
  14. #include "lpc_types.h"
  15. #include "lpc13xx_uart.h"
  16. #include "lpc13xx_timer.h"
  17. #include "lpc13xx_i2c.h"
  18. #include "lpc13xx_gpio.h"
  19. #include "lpc13xx_ssp.h"
  20. #include "lpc13xx_adc.h"
  21. */
  22. #include "mcu_regs.h"
  23. #include "type.h"
  24. #include "uart.h"
  25. #include "stdio.h"
  26. #include "timer32.h"
  27. #include "i2c.h"
  28. #include "gpio.h"
  29. #include "ssp.h"
  30. #include "adc.h"
  31.  
  32. #include "light.h"
  33. #include "oled.h"
  34. #include "temp.h"
  35. #include "acc.h"
  36. #include "myTimer.h"
  37.  
  38.  
  39.  
  40. static uint32_t msTicks = 0;
  41. static uint8_t buf[10];
  42.  
  43. static void intToString(int value, uint8_t* pBuf, uint32_t len, uint32_t base)
  44. {
  45.     static const char* pAscii = "0123456789abcdefghijklmnopqrstuvwxyz";
  46.     int pos = 0;
  47.     int tmpValue = value;
  48.  
  49.     // the buffer must not be null and at least have a length of 2 to handle one
  50.     // digit and null-terminator
  51.     if (pBuf == NULL || len < 2)
  52.     {
  53.         return;
  54.     }
  55.  
  56.     // a valid base cannot be less than 2 or larger than 36
  57.     // a base value of 2 means binary representation. A value of 1 would mean only zeros
  58.     // a base larger than 36 can only be used if a larger alphabet were used.
  59.     if (base < 2 || base > 36)
  60.     {
  61.         return;
  62.     }
  63.  
  64.     // negative value
  65.     if (value < 0)
  66.     {
  67.         tmpValue = -tmpValue;
  68.         value    = -value;
  69.         pBuf[pos++] = '-';
  70.     }
  71.  
  72.     // calculate the required length of the buffer
  73.     do {
  74.         pos++;
  75.         tmpValue /= base;
  76.     } while(tmpValue > 0);
  77.  
  78.  
  79.     if (pos > len)
  80.     {
  81.         // the len parameter is invalid.
  82.         return;
  83.     }
  84.  
  85.     pBuf[pos] = '\0';
  86.  
  87.     do {
  88.         pBuf[--pos] = pAscii[value % base];
  89.         value /= base;
  90.     } while(value > 0);
  91.  
  92.     return;
  93.  
  94. }
  95.  
  96. void SysTick_Handler(void) {
  97.     msTicks++;
  98. }
  99.  
  100. static uint32_t getTicks(void)
  101. {
  102.     return msTicks;
  103. }
  104.  
  105.  
  106. int main (void)
  107. {
  108.     //SystemInit();
  109.     SystemCoreClockUpdate();
  110.  
  111.     int32_t xoff = 0;
  112.     int32_t yoff = 0;
  113.     int32_t zoff = 0;
  114.  
  115.     int8_t x = 0;
  116.     int8_t y = 0;
  117.     int8_t z = 0;
  118.  
  119.     int32_t t = 0;
  120.     uint32_t lux = 0;
  121.     uint32_t trim = 0;
  122.  
  123.     GPIOInit();
  124.     init_myTimer(0);
  125.  
  126.     UARTInit(115200);
  127.     UARTSendString((uint8_t*)"OLED - Peripherals\r\n");
  128.  
  129.     I2CInit( (uint32_t)I2CMASTER, 0 );
  130.     SSPInit();
  131.     ADCInit( ADC_CLK );
  132.  
  133.     oled_init();
  134.     light_init();
  135.     acc_init();
  136.  
  137.     temp_init (&getTicks);
  138.  
  139.  
  140.     /* setup sys Tick. Elapsed time is e.g. needed by temperature sensor */
  141.     SysTick_Config(SystemCoreClock / 1000);
  142.     if ( !(SysTick->CTRL & (1<<SysTick_CTRL_CLKSOURCE_Msk)) )
  143.     {
  144.       /* When external reference clock is used(CLKSOURCE in
  145.       Systick Control and register bit 2 is set to 0), the
  146.       SYSTICKCLKDIV must be a non-zero value and 2.5 times
  147.       faster than the reference clock.
  148.       When core clock, or system AHB clock, is used(CLKSOURCE
  149.       in Systick Control and register bit 2 is set to 1), the
  150.       SYSTICKCLKDIV has no effect to the SYSTICK frequency. See
  151.       more on Systick clock and status register in Cortex-M3
  152.       technical Reference Manual. */
  153.       LPC_SYSCON->SYSTICKCLKDIV = 0x08;
  154.     }
  155.  
  156.     /*
  157.      * Assume base board in zero-g position when reading first value.
  158.      */
  159.     acc_read(&x, &y, &z);
  160.     xoff = 0-x;
  161.     yoff = 0-y;
  162.     zoff = 64-z;
  163.  
  164.     light_enable();
  165.     light_setRange(LIGHT_RANGE_4000);
  166.  
  167.     oled_clearScreen(OLED_COLOR_WHITE);
  168.  
  169.     oled_putString(1,1,  (uint8_t*)"Temp   : ", OLED_COLOR_BLACK, OLED_COLOR_WHITE);
  170.     oled_putString(1,9,  (uint8_t*)"Light  : ", OLED_COLOR_BLACK, OLED_COLOR_WHITE);
  171.     oled_putString(1,17, (uint8_t*)"Trimpot: ", OLED_COLOR_BLACK, OLED_COLOR_WHITE);
  172.     oled_putString(1,25, (uint8_t*)"Acc x  : ", OLED_COLOR_BLACK, OLED_COLOR_WHITE);
  173.     oled_putString(1,33, (uint8_t*)"Acc y  : ", OLED_COLOR_BLACK, OLED_COLOR_WHITE);
  174.     oled_putString(1,41, (uint8_t*)"Acc z  : ", OLED_COLOR_BLACK, OLED_COLOR_WHITE);
  175.  
  176.     while(1) {
  177.  
  178.         /* Accelerometer */
  179.         acc_read(&x, &y, &z);
  180.         x = x+xoff;
  181.         y = y+yoff;
  182.         z = z+zoff;
  183.  
  184.         /* Temperature */
  185.         t = temp_read();
  186.  
  187.         /* light */
  188.         lux = light_read();
  189.  
  190.         /* trimpot */
  191.         trim = ADCRead(0);
  192.  
  193.         /* output values to OLED display */
  194.  
  195.         intToString(t, buf, 10, 10);
  196.         oled_fillRect((1+9*6),1, 80, 8, OLED_COLOR_WHITE);
  197.         oled_putString((1+9*6),1, buf, OLED_COLOR_BLACK, OLED_COLOR_WHITE);
  198.  
  199.         intToString(lux, buf, 10, 10);
  200.         oled_fillRect((1+9*6),9, 80, 16, OLED_COLOR_WHITE);
  201.         oled_putString((1+9*6),9, buf, OLED_COLOR_BLACK, OLED_COLOR_WHITE);
  202.  
  203.         intToString(trim, buf, 10, 10);
  204.         oled_fillRect((1+9*6),17, 80, 24, OLED_COLOR_WHITE);
  205.         oled_putString((1+9*6),17, buf, OLED_COLOR_BLACK, OLED_COLOR_WHITE);
  206.  
  207.         intToString(x, buf, 10, 10);
  208.         oled_fillRect((1+9*6),25, 80, 32, OLED_COLOR_WHITE);
  209.         oled_putString((1+9*6),25, buf, OLED_COLOR_BLACK, OLED_COLOR_WHITE);
  210.  
  211.         intToString(y, buf, 10, 10);
  212.         oled_fillRect((1+9*6),33, 80, 40, OLED_COLOR_WHITE);
  213.         oled_putString((1+9*6),33, buf, OLED_COLOR_BLACK, OLED_COLOR_WHITE);
  214.  
  215.         intToString(z, buf, 10, 10);
  216.         oled_fillRect((1+9*6),41, 80, 48, OLED_COLOR_WHITE);
  217.         oled_putString((1+9*6),41, buf, OLED_COLOR_BLACK, OLED_COLOR_WHITE);
  218.  
  219.         /* delay */
  220.         delayMs(0, 1000);
  221.     }
  222.  
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement