Bez tytułu

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

odpowiedź "Bez tytułu"

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

captcha