Bez tytułu

z Subtle Prairie Dog, 1 miesiąc temu, napisane w C, wyświetlone 22 razy. [paste_expire] 9 miesiące.
URL https://pastebin.k4be.pl/view/391eb7ed Udostępnij
Pobierz wklejkę lub Pokaż surowy tekst
  1. #define __SDCC__
  2.  
  3. #include "stdint.h"
  4. #include "stdlib.h"
  5. #include "stdbool.h"
  6. #include "stm8s_gpio.h"
  7. #include "stm8s_tim2.h"
  8. #include "stm8s_tim4.h"
  9. #include "stm8s_clk.h"
  10. #include "stm8s_it.h"
  11. #include "stm8s_exti.h"
  12.  
  13. #define _BV(x) (1 << x)
  14.  /*
  15.  
  16.    A
  17.   ___           A - PD1
  18.  |   |          B - PD4        DIG1 - PC7
  19. F| G |B         C - PB4        DIG2 - PA3
  20.  |___|          D - PC5        DIG3 - PD6
  21.  |   |          E - PC6        DIG4 - PC3
  22. E| D |C         F - PD5
  23.  |___|  .       G - PB5
  24.                 . - PC4
  25.  
  26.     0bABCDEFG.
  27. 0 - 0b00000011
  28. 1 - 0b10011111
  29. 2 - 0b00100101
  30. 3 - 0b00001101
  31. 4 - 0b10011001
  32. 5 - 0b01001001
  33. 6 - 0b01000001
  34. 7 - 0b00011111
  35. 8 - 0b00000001
  36. 9 - 0b00001001
  37.  
  38. */
  39.  
  40. volatile  uint16_t rpm=0;
  41. volatile static uint16_t current_period=0;
  42. volatile static uint16_t periods[8]={0};
  43. volatile static int16_t sum = 0;
  44. volatile static uint8_t pos = 0;
  45. volatile static bool filled = false;
  46. volatile static bool overflow = false;
  47. volatile  uint32_t average;
  48.  
  49.  
  50. void print(uint16_t value){
  51.  
  52.         volatile static uint8_t led_disp;
  53.         led_disp++;
  54.         if(led_disp>4) led_disp = 1;
  55.  
  56.  
  57.         uint8_t digit;
  58.  
  59.         const uint8_t symbols[10] = {
  60.                         0b00000011,
  61.                         0b10011111,
  62.                         0b00100101,
  63.                         0b00001101,
  64.                         0b10011001,
  65.                         0b01001001,
  66.                         0b01000001,
  67.                         0b00011111,
  68.                         0b00000001,
  69.                         0b00001001,
  70.         };
  71.  
  72.  
  73.         if(value>9999) value = 9999;
  74.  
  75.         GPIO_WriteLow(GPIOC, GPIO_PIN_7);
  76.         GPIO_WriteLow(GPIOA, GPIO_PIN_3);
  77.         GPIO_WriteLow(GPIOD, GPIO_PIN_6);
  78.         GPIO_WriteLow(GPIOC, GPIO_PIN_3);
  79.  
  80.  
  81.         switch(led_disp){
  82.         case 1:
  83.                 digit = (value/1000)%10;
  84.                 break;
  85.         case 2:
  86.                 digit = (value/100)%10;
  87.                 break;
  88.         case 3:
  89.                 digit = (value/10)%10;
  90.                 break;
  91.         case 4:
  92.                 digit = value%10;
  93.                 break;
  94.         }
  95.  
  96.  
  97.         GPIOD->ODR &= ~(_BV(1)|_BV(4)|_BV(5));
  98.         GPIOB->ODR &= ~(_BV(4)|_BV(5));
  99.         GPIOC->ODR &= ~(_BV(5)|_BV(6)|_BV(4));
  100.  
  101.  
  102.         GPIOD->ODR |= ((_BV(7) & symbols[digit]) >> 6) // A - PD1
  103.                                 | ((_BV(6) & symbols[digit]) >> 2) // B - PD4
  104.                                 | ((_BV(2) & symbols[digit]) << 3); // F - PD5
  105.  
  106.         GPIOB->ODR |= ((_BV(5) & symbols[digit]) >> 1) // C - PB4
  107.                                 | ((_BV(1) & symbols[digit]) << 4); // G - PB5
  108.  
  109.         GPIOC->ODR |= ((_BV(4) & symbols[digit]) << 1) // D - PC5
  110.                                 | ((_BV(3) & symbols[digit]) << 3) // E - PC6
  111.                                 | ((_BV(0) & symbols[digit]) << 4); // . - PC4
  112.  
  113. //      GPIOD->ODR = (GPIOD->ODR & (~(1<<1))) | (!!((1<<7) & symbols[digit]) << 1); // A - PD1
  114. //      GPIOD->ODR = (GPIOD->ODR & (~(1<<4))) | (!!((1<<6) & symbols[digit]) << 4); // B - PD4
  115. //      GPIOD->ODR = (GPIOD->ODR & (~(1<<5))) | (!!((1<<2) & symbols[digit]) << 5); // F - PD5
  116. //
  117. //      GPIOB->ODR = (GPIOB->ODR & (~(1<<4))) | (!!((1<<5) & symbols[digit]) << 4); // C - PB4
  118. //      GPIOB->ODR = (GPIOB->ODR & (~(1<<5))) | (!!((1<<1) & symbols[digit]) << 5); // G - PB5
  119. //
  120. //      GPIOC->ODR = (GPIOC->ODR & (~(1<<5))) | (!!((1<<4) & symbols[digit]) << 5); // D - PC5
  121. //      GPIOC->ODR = (GPIOC->ODR & (~(1<<6))) | (!!((1<<3) & symbols[digit]) << 6); // E - PC6
  122. //      GPIOC->ODR = (GPIOC->ODR & (~(1<<4))) | (!!((1<<0) & symbols[digit]) << 4); // . - PC4
  123.  
  124.         switch(led_disp){
  125.         case 1:
  126.                 GPIO_WriteHigh(GPIOC, GPIO_PIN_7);
  127.                 break;
  128.         case 2:
  129.                 GPIO_WriteHigh(GPIOA, GPIO_PIN_3);
  130.                 break;
  131.         case 3:
  132.                 GPIO_WriteHigh(GPIOD, GPIO_PIN_6);
  133.                 break;
  134.         case 4:
  135.                 GPIO_WriteHigh(GPIOC, GPIO_PIN_3);
  136.                 break;
  137.         }
  138.  
  139. }
  140. void main(void)
  141. {
  142.  
  143.  
  144.         GPIO_Init(GPIOA,
  145.                         GPIO_PIN_3,
  146.                         GPIO_MODE_OUT_PP_LOW_FAST);
  147.  
  148.         GPIO_Init(GPIOB,
  149.                         GPIO_PIN_4|
  150.                         GPIO_PIN_5,
  151.                         GPIO_MODE_OUT_PP_LOW_FAST);
  152.  
  153.         GPIO_Init(GPIOC,
  154.                         GPIO_PIN_3|
  155.                         GPIO_PIN_4|
  156.                         GPIO_PIN_5|
  157.                         GPIO_PIN_6|
  158.                         GPIO_PIN_7,
  159.                         GPIO_MODE_OUT_PP_LOW_FAST);
  160.  
  161.         GPIO_Init(GPIOD,
  162.                         GPIO_PIN_1|
  163.                         GPIO_PIN_4|
  164.                         GPIO_PIN_5|
  165.                         GPIO_PIN_6,
  166.                         GPIO_MODE_OUT_PP_LOW_FAST);
  167.  
  168.         //sensor
  169.         GPIO_Init(GPIOD,
  170.                         GPIO_PIN_2,
  171.                         GPIO_MODE_IN_PU_IT);
  172.         EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOD,EXTI_SENSITIVITY_FALL_ONLY);
  173.  
  174.         CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSE, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE);
  175.  
  176.         CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER4,ENABLE);
  177.         TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
  178.         TIM4_TimeBaseInit(TIM4_PRESCALER_128, 255);
  179.         enableInterrupts();
  180.         TIM4_Cmd(ENABLE);
  181.  
  182.         CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, ENABLE);
  183.         TIM2_TimeBaseInit(TIM2_PRESCALER_256, 0xFFFF);
  184.         TIM2_ICInit(TIM2_CHANNEL_3, TIM2_ICPOLARITY_FALLING,TIM2_ICSELECTION_DIRECTTI, TIM2_ICPSC_DIV1,0x00);
  185.         TIM2_ITConfig(TIM2_IT_UPDATE, ENABLE);
  186.         TIM2_Cmd(ENABLE);
  187.  
  188.         while (1) {
  189.  
  190.                 if (overflow) {
  191.                         rpm = 0;
  192.  
  193.                 } else {
  194.  
  195.  
  196.                         rpm =  (uint32_t)(62500 * 60) / average;
  197.                 }
  198.  
  199.                 for (uint16_t i = 1; i < 65200; i++) {
  200.                         for (uint16_t j = 1; j < 3; j++);
  201.                 }
  202.  
  203.         }
  204.  
  205. }
  206.  
  207.  
  208. INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)
  209. {
  210.         print(rpm);
  211.         TIM4_ClearITPendingBit(TIM4_IT_UPDATE);
  212. }
  213.  
  214.  
  215. INTERRUPT_HANDLER(EXTI_PORTD_IRQHandler, 6)
  216. {
  217.         if(TIM2_GetFlagStatus(TIM2_FLAG_CC3)){
  218.                 current_period = TIM2_GetCapture3();
  219.  
  220.  
  221.                 sum = sum - periods[pos];
  222.                 periods[pos] = current_period;
  223.                 sum += current_period;
  224.  
  225.                 if(++pos == sizeof(periods)/sizeof(*periods))
  226.                 {
  227.                         pos = 0;
  228.                         filled = true;
  229.                 }
  230.                 size_t count = filled ? sizeof(periods)/sizeof(*periods) : pos;
  231.                 average = (sum < 0 ? sum - count / 2 : sum + count / 2 ) / count;
  232.  
  233.  
  234.                 TIM2_ClearFlag(TIM2_FLAG_CC3);
  235.                 overflow = false;
  236.                 TIM2_SetCounter(0);
  237.         }
  238.  
  239. }
  240.  
  241. INTERRUPT_HANDLER(TIM2_UPD_OVF_BRK_IRQHandler, 13)
  242. {
  243.         overflow = true;
  244.         TIM2_ClearITPendingBit(TIM2_IT_UPDATE);
  245. }
  246.  
  247.  

odpowiedź "Bez tytułu"

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

captcha