Advertisement
Guest User

Untitled

a guest
Feb 29th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. #include <msp430FG4618.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4.  
  5. #define a 0x08 // LCD индикатора
  6. #define b 0x04
  7. #define c 0x02
  8. #define d 0x01
  9. #define e 0x20
  10. #define f 0x10
  11. #define g 0x40
  12. #define h 0x80
  13. #define minus 0x40
  14. #define LCD_MEM_OFFSET 2 // Offset from LCDMEM[0]
  15. #define LCD_MEM_LOC 11 // Num of LCDMEM[] locations used
  16.  
  17. volatile unsigned int count = 0; // impulse counter
  18. volatile float vel1 = 0; // first velocity
  19. volatile float vel2 = 0; // second velocity
  20. volatile float accel = 0; // acceleration
  21. unsigned char digits[10] = { // 0 - 9 numbers
  22. a+b+c+d+f+g,
  23. b+c,
  24. a+d+c+e+g,
  25. a+b+c+d+e,
  26. b+c+e+f,
  27. d+f+e+b+a,
  28. d+f+g+a+b+e,
  29. f+d+c+b,
  30. a+b+c+d+f+e+g,
  31. a+b+c+d+f+e
  32. };
  33.  
  34. void lcd_init() {
  35. LCDACTL = LCDON + LCD4MUX + LCDFREQ_128; // turn LCD on
  36. LCDAPCTL0 = LCDS4 + LCDS8 + LCDS12 + LCDS16 + LCDS20 + LCDS24;
  37. P5SEL |= BIT2 + BIT3 + BIT4; // COM1 + COM2 + COM3
  38. LCDMEM[3] = 0;
  39. LCDMEM[4] = 0;
  40. LCDMEM[5] = 0;
  41. LCDMEM[6] = 0;
  42. LCDMEM[7] = 0;
  43. LCDMEM[8] = 0;
  44. LCDMEM[9] = 0;
  45. }
  46.  
  47. void lcd_clear() {
  48. for (int i = LCD_MEM_OFFSET; i < (LCD_MEM_OFFSET + LCD_MEM_LOC); i++) {
  49. LCDMEM[i] = 0;
  50. }
  51. }
  52.  
  53. void input_init() {
  54. P1DIR &= ~0x05; // P1.0 - CCI0A, P1.2 - CCI1A
  55. P1SEL |= 0x05;
  56. }
  57.  
  58. void timerA0_init() {
  59. TACCR0 = 31250 - 1; // SMCLK - 1 MHz, divided by 8 it is 125000, 31250 is 1/4 of SMCLK divided by 8
  60. TACCTL0 |= CCIE; // CCR0 interrupt enabled
  61. TACCTL1 |= CM_1 + CCIS_0 + CAP + CCIE; // capture front rising + CCI1A + capture mode + CCR1 interrupt enabled
  62. TACTL = TASSEL_2 + ID_3 + MC_1 + TACLR; // SMCLK + divider /8 + continuous mode + TAIE
  63. }
  64.  
  65. void calculations() {
  66. vel1 = vel2; // 32 max counts per sec, 8 counts per 1/4 sec
  67. vel2 = count * 4 * M_PI; // vel = (count * M_PI) / (1/4)
  68. accel = abs((vel2 - vel1)) * 4; // accel = abs((vel2 - vel1)) / (1/4)
  69. count = 0; // reset counter
  70. }
  71. void lcd_print() {
  72. int k = (int)acceleration % 10;
  73. LCDMEM[2] = digits[(k & 0x0F)];
  74. int n = (int)acceleration / 10;
  75.  
  76. k = n % 10;
  77. LCDMEM[3] = digits[(k & 0x0F)];
  78. n = n / 10;
  79.  
  80. k = n % 10;
  81. LCDMEM[4] = digits[(k & 0x0F)];
  82. n = n / 10;
  83.  
  84. k = n % 10;
  85. LCDMEM[5] = digits[(k & 0x0F)];
  86. n = n / 10;
  87. }
  88.  
  89. void main(void)
  90. {
  91. WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
  92. lcd_init();
  93. lcd_clear();
  94. input_init();
  95. timerA0_init();
  96. __enable_interrupt(); // Enable interrupts
  97. }
  98.  
  99. #pragma vector=TIMERA0_VECTOR
  100. __interrupt void timer_accel_calculation(void) {
  101. calculations();
  102. printf("acceleration %.2f \n", accel);
  103. lcd_print();
  104. }
  105.  
  106. #pragma vector=TIMERA1_VECTOR
  107. __interrupt void timer_input_capture(void) {
  108. switch(__even_in_range(TAIV,10))
  109. {
  110. case 2 : // TACCR1 CCIFG
  111. count++; // counting input pulses
  112. break;
  113. default:
  114. break;
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement