Bez tytułu

z Crimson Wigeon, 1 miesiąc temu, napisane w C, wyświetlone 20 razy. [paste_expire] 9 miesiące.
URL https://pastebin.k4be.pl/view/a0bfea76 Udostępnij
Pobierz wklejkę lub Pokaż surowy tekst
  1. #include "stdint.h"
  2. #include "stdlib.h"
  3. #include "stdbool.h"
  4. #include "stm8s.h"
  5. #include "stm8s_gpio.h"
  6. #include "stm8s_tim2.h"
  7. #include "stm8s_tim4.h"
  8. #include "stm8s_clk.h"
  9. #include "stm8s_it.h"
  10. #include "stm8s_exti.h"
  11.  
  12.  
  13.  /*
  14.  
  15.    A
  16.   ___           A - PD1
  17.  |   |          B - PD4        DIG1 - PC7
  18. F| G |B         C - PB4        DIG2 - PA3
  19.  |___|          D - PC5        DIG3 - PD6
  20.  |   |          E - PC6        DIG4 - PC3
  21. E| D |C         F - PD5
  22.  |___|  .       G - PB5
  23.                 . - PC4
  24.  
  25.     0bABCDEFG.
  26. 0 - 0b00000011
  27. 1 - 0b10011111
  28. 2 - 0b00100101
  29. 3 - 0b00001101
  30. 4 - 0b10011001
  31. 5 - 0b01001001
  32. 6 - 0b01000001
  33. 7 - 0b00011111
  34. 8 - 0b00000001
  35. 9 - 0b00001001
  36.  
  37. */
  38.  
  39. #define SAMPLES 8
  40.  
  41. static  uint16_t rpm=0;
  42. bool timeout = false;
  43. static uint32_t sum=0;
  44. static uint8_t refresh_cnt;
  45.  
  46.  
  47. #define DISP_PB(pin) ((0x00000100ull)<<(pin))
  48. #define DISP_PC(pin) ((0x00010000ull)<<(pin))
  49. #define DISP_PD(pin) ((0x01000000ull)<<(pin))
  50.  
  51.  
  52. #define _DISP_V_PB(x) (((x)>>8) & 0xff)
  53. #define _DISP_V_PC(x) (((x)>>16) & 0xff)
  54. #define _DISP_V_PD(x) (((x)>>24) & 0xff)
  55.  
  56.  
  57. #define DISP_IDX_PB 0
  58. #define DISP_IDX_PC 1
  59. #define DISP_IDX_PD 2
  60.  
  61. #define DISP_V(x) {_DISP_V_PB(x), _DISP_V_PC(x), _DISP_V_PD(x)}
  62.  
  63. #define SEG_A     DISP_PD(1)
  64. #define SEG_B     DISP_PD(4)
  65. #define SEG_C     DISP_PB(4)
  66. #define SEG_D     DISP_PC(5)
  67. #define SEG_E     DISP_PC(6)
  68. #define SEG_F     DISP_PD(5)
  69. #define SEG_G     DISP_PB(5)
  70. #define SEG_DOT   DISP_PC(4)
  71. #define SEG_ALL   (SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G | SEG_DOT)
  72.  
  73.  
  74. void print(){
  75.         static uint8_t led_disp;
  76.         static const uint16_t digit_vals[4] = {1000, 100, 10, 1};
  77.  
  78.         uint8_t digit;
  79.  
  80.         static const uint8_t pins_symbol[10][3] = {
  81.                 [0] = DISP_V(SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F        ),
  82.                 [1] = DISP_V(        SEG_B | SEG_C                                ),
  83.                 [2] = DISP_V(SEG_A | SEG_B         | SEG_D | SEG_E         | SEG_G),
  84.                 [3] = DISP_V(SEG_A | SEG_B | SEG_C | SEG_D                 | SEG_G),
  85.                 [4] = DISP_V(        SEG_B | SEG_C                 | SEG_F | SEG_G),
  86.                 [5] = DISP_V(SEG_A         | SEG_C | SEG_D         | SEG_F | SEG_G),
  87.                 [6] = DISP_V(SEG_A         | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G),
  88.                 [7] = DISP_V(SEG_A | SEG_B | SEG_C                                ),
  89.                 [8] = DISP_V(SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G),
  90.                 [9] = DISP_V(SEG_A | SEG_B | SEG_C | SEG_D         | SEG_F | SEG_G),
  91.         };
  92.  
  93.         static const uint8_t pins_all[3] = DISP_V(SEG_ALL);
  94.  
  95.  
  96.         if(rpm>9999) rpm = 9999;
  97.         digit = (rpm / digit_vals[led_disp]) % 10;
  98.  
  99.         GPIOC->ODR &= ~GPIO_PIN_7;
  100.         GPIOA->ODR &= ~GPIO_PIN_3;
  101.         GPIOD->ODR &= ~GPIO_PIN_6;
  102.         GPIOC->ODR &= ~GPIO_PIN_3;
  103.  
  104.         /* all cathodes high (off) */
  105.         GPIOB->ODR |= pins_all[DISP_IDX_PB];
  106.         GPIOC->ODR |= pins_all[DISP_IDX_PC];
  107.         GPIOD->ODR |= pins_all[DISP_IDX_PD];
  108.  
  109.  
  110.         GPIOB->ODR &= ~pins_symbol[digit][DISP_IDX_PB];
  111.         GPIOC->ODR &= ~pins_symbol[digit][DISP_IDX_PC];
  112.         GPIOD->ODR &= ~pins_symbol[digit][DISP_IDX_PD];
  113.  
  114.  
  115.         switch(led_disp){
  116.         case 0:
  117.                 GPIOC->ODR |= GPIO_PIN_7;
  118.                 break;
  119.         case 1:
  120.                 GPIOA->ODR |= GPIO_PIN_3;
  121.                 break;
  122.         case 2:
  123.                 GPIOD->ODR |= GPIO_PIN_6;
  124.                 break;
  125.         case 3:
  126.                 GPIOC->ODR |= GPIO_PIN_3;
  127.                 break;
  128.         }
  129.  
  130.         led_disp++;
  131.         if(led_disp > 3)
  132.                 led_disp = 0;
  133. }
  134.  
  135. void main(void)
  136. {
  137.  
  138.         GPIOA->ODR &= (~GPIO_PIN_3);
  139.         GPIOA->DDR |= GPIO_PIN_3;
  140.         /* Pull-Up or Push-Pull */
  141.         GPIOA->CR1 |= GPIO_PIN_3;
  142.          /* No external interrupt or No slope control */
  143.         GPIOA->CR2 &= (~GPIO_PIN_3);
  144.  
  145.  
  146.         GPIOB->ODR &= (~(GPIO_PIN_4|GPIO_PIN_5));
  147.         GPIOB->DDR |= (GPIO_PIN_4|GPIO_PIN_5);
  148.         GPIOB->CR1 |= (GPIO_PIN_4|GPIO_PIN_5);
  149.         GPIOB->CR2 &= (~(GPIO_PIN_4|GPIO_PIN_5));
  150.  
  151.  
  152.         GPIOC->ODR &= (~(
  153.                         GPIO_PIN_3|
  154.                         GPIO_PIN_4|
  155.                         GPIO_PIN_5|
  156.                         GPIO_PIN_6|
  157.                         GPIO_PIN_7));
  158.         GPIOC->DDR |= (
  159.                         GPIO_PIN_3|
  160.                         GPIO_PIN_4|
  161.                         GPIO_PIN_5|
  162.                         GPIO_PIN_6|
  163.                         GPIO_PIN_7);
  164.         GPIOC->CR1 |= (
  165.                         GPIO_PIN_3|
  166.                         GPIO_PIN_4|
  167.                         GPIO_PIN_5|
  168.                         GPIO_PIN_6|
  169.                         GPIO_PIN_7);
  170.         GPIOC->CR2 &= (~(
  171.                         GPIO_PIN_3|
  172.                         GPIO_PIN_4|
  173.                         GPIO_PIN_5|
  174.                         GPIO_PIN_6|
  175.                         GPIO_PIN_7));
  176.  
  177.  
  178.         GPIOD->ODR &= (~(
  179.                         GPIO_PIN_1|
  180.                         GPIO_PIN_4|
  181.                         GPIO_PIN_5|
  182.                         GPIO_PIN_6));
  183.         GPIOD->DDR |= (
  184.                         GPIO_PIN_1|
  185.                         GPIO_PIN_4|
  186.                         GPIO_PIN_5|
  187.                         GPIO_PIN_6);
  188.         GPIOD->CR1 |= (
  189.                         GPIO_PIN_1|
  190.                         GPIO_PIN_4|
  191.                         GPIO_PIN_5|
  192.                         GPIO_PIN_6);
  193.         GPIOD->CR2 &= (~(
  194.                         GPIO_PIN_1|
  195.                         GPIO_PIN_4|
  196.                         GPIO_PIN_5|
  197.                         GPIO_PIN_6));
  198.  
  199.  
  200.         //sensor
  201.         /* Set Input mode */
  202.         GPIOD->DDR &= (~GPIO_PIN_2);
  203.         /* Pull-Up or Push-Pull */
  204.         GPIOD->CR1 |= GPIO_PIN_2;
  205.         /* Interrupt or Slow slope */
  206.         GPIOD->CR2 |= GPIO_PIN_2;
  207.  
  208.  
  209.         /* Enables Clock switch */
  210.         CLK->SWCR |= CLK_SWCR_SWEN;
  211.         /*  Disables Switch interrupt */
  212.         CLK->SWCR &= (uint8_t)(~CLK_SWCR_SWIEN);
  213.         /* Selection of the target clock source */
  214.         CLK->SWR = CLK_SOURCE_HSE;
  215.         /* Wait until the target clock source is ready */
  216.         while((CLK->SWCR & CLK_SWCR_SWBSY) != 0 );
  217.  
  218.  
  219.         EXTI->CR1 &= (~EXTI_CR1_PDIS);
  220.         EXTI->CR1 |= (EXTI_SENSITIVITY_FALL_ONLY << 6);
  221.  
  222.  
  223.         CLK->PCKENR1 |= (1 << CLK_PERIPHERAL_TIMER4);
  224.         TIM4->IER |= TIM4_IT_UPDATE;
  225.         TIM4->PSCR = TIM4_PRESCALER_128;
  226. //      TIM4->ARR = 255;
  227.         enableInterrupts();
  228.         TIM4->CR1 |= TIM4_CR1_CEN;
  229.  
  230.  
  231.         CLK->PCKENR1 |= (1 << CLK_PERIPHERAL_TIMER2 );
  232.         TIM2->PSCR = TIM2_PRESCALER_256;
  233.         TIM2->ARRH = 0xFF;
  234.         TIM2->ARRL = 0xFF;
  235.         TIM2->CCMR3 |=  TIM2_ICSELECTION_DIRECTTI;
  236.         /* Select the Polarity */
  237.         TIM2->CCER2 |= TIM2_CCER2_CC3P;
  238.         TIM2->CCER2 |= TIM2_CCER2_CC3E;
  239.         TIM2->IER |= TIM2_IT_UPDATE;
  240.         TIM2->CR1 |= TIM2_CR1_CEN;
  241.  
  242.         while (1) {
  243.                 if(refresh_cnt == 60){
  244.                         refresh_cnt = 0;
  245.  
  246.                         if (timeout) {
  247.                                 rpm = 0;
  248.                         } else{
  249.  
  250.                                 rpm =  (uint32_t)(62500 * 60 * SAMPLES) / sum;
  251.                         }
  252.                 }
  253.         }
  254.  
  255. }
  256.  
  257.  
  258. INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)
  259. {
  260.         refresh_cnt++;
  261.         print();
  262.         TIM4->SR1 = ~TIM4_IT_UPDATE;
  263. }
  264.  
  265.  
  266. INTERRUPT_HANDLER(EXTI_PORTD_IRQHandler, 6)
  267. {
  268.  
  269.         static uint8_t pos;
  270.         static uint16_t periods[SAMPLES];
  271.         uint16_t current_period;
  272.  
  273.         if(TIM2->SR1 & TIM2_FLAG_CC3){
  274.                 current_period =  ((uint16_t)TIM2->CCR3H << 8) | TIM2->CCR3L;
  275.  
  276.                 sum -=  periods[pos];
  277.                 periods[pos] = current_period;
  278.                 sum += current_period;
  279.  
  280.                 if(++pos == SAMPLES)
  281.                 {
  282.                         pos = 0;
  283.  
  284.                 }
  285.  
  286.                 TIM2->SR1 = (~TIM2_FLAG_CC3);
  287.                 timeout = false;
  288.                 TIM2->CNTRH = 0;
  289.                 TIM2->CNTRL = 0;
  290.         }
  291.  
  292. }
  293.  
  294. INTERRUPT_HANDLER(TIM2_UPD_OVF_BRK_IRQHandler, 13)
  295. {
  296.         timeout = true;
  297.         TIM2->SR1 = ~TIM2_IT_UPDATE;
  298. }
  299.  
  300.  

odpowiedź "Bez tytułu"

Tutaj możesz odpowiedzieć na wklejkę z góry

captcha