Bez tytułu

z Crippled Parakeet, 1 miesiąc temu, napisane w Plain Text, wyświetlone 16 razy. [paste_expire] 9 miesiące.
URL https://pastebin.k4be.pl/view/68de1feb 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.  
  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.         GPIO_Write(GPIOD, (GPIOD->ODR & (~(1<<1))) | (!!((1<<7) & symbols[digit]) << 1)); // A - PD1
  98.         GPIO_Write(GPIOD, (GPIOD->ODR & (~(1<<4))) | (!!((1<<6) & symbols[digit]) << 4)); // B - PD4
  99.         GPIO_Write(GPIOB, (GPIOB->ODR & (~(1<<4))) | (!!((1<<5) & symbols[digit]) << 4)); // C - PB4
  100.         GPIO_Write(GPIOC, (GPIOC->ODR & (~(1<<5))) | (!!((1<<4) & symbols[digit]) << 5)); // D - PC5
  101.         GPIO_Write(GPIOC, (GPIOC->ODR & (~(1<<6))) | (!!((1<<3) & symbols[digit]) << 6)); // E - PC6
  102.         GPIO_Write(GPIOD, (GPIOD->ODR & (~(1<<5))) | (!!((1<<2) & symbols[digit]) << 5)); // F - PD5
  103.         GPIO_Write(GPIOB, (GPIOB->ODR & (~(1<<5))) | (!!((1<<1) & symbols[digit]) << 5)); // G - PB5
  104.         GPIO_Write(GPIOC, (GPIOC->ODR & (~(1<<4))) | (!!((1<<0) & symbols[digit]) << 4)); // . - PC4
  105.  
  106.         switch(led_disp){
  107.         case 1:
  108.                 GPIO_WriteHigh(GPIOC, GPIO_PIN_7);
  109.                 break;
  110.         case 2:
  111.                 GPIO_WriteHigh(GPIOA, GPIO_PIN_3);
  112.                 break;
  113.         case 3:
  114.                 GPIO_WriteHigh(GPIOD, GPIO_PIN_6);
  115.                 break;
  116.         case 4:
  117.                 GPIO_WriteHigh(GPIOC, GPIO_PIN_3);
  118.                 break;
  119.         }
  120.  
  121. }
  122. void main(void)
  123. {
  124.  
  125.  
  126.         GPIO_Init(GPIOA,
  127.                         GPIO_PIN_3,
  128.                         GPIO_MODE_OUT_PP_LOW_FAST);
  129.  
  130.         GPIO_Init(GPIOB,
  131.                         GPIO_PIN_4|
  132.                         GPIO_PIN_5,
  133.                         GPIO_MODE_OUT_PP_LOW_FAST);
  134.  
  135.         GPIO_Init(GPIOC,
  136.                         GPIO_PIN_3|
  137.                         GPIO_PIN_4|
  138.                         GPIO_PIN_5|
  139.                         GPIO_PIN_6|
  140.                         GPIO_PIN_7,
  141.                         GPIO_MODE_OUT_PP_LOW_FAST);
  142.  
  143.         GPIO_Init(GPIOD,
  144.                         GPIO_PIN_1|
  145.                         GPIO_PIN_4|
  146.                         GPIO_PIN_5|
  147.                         GPIO_PIN_6,
  148.                         GPIO_MODE_OUT_PP_LOW_FAST);
  149.  
  150.         //sensor
  151.         GPIO_Init(GPIOD,
  152.                         GPIO_PIN_2,
  153.                         GPIO_MODE_IN_PU_IT);
  154.         EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOD,EXTI_SENSITIVITY_FALL_ONLY);
  155.  
  156.         CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSE, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE);
  157.  
  158.         CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER4,ENABLE);
  159.         TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
  160.         TIM4_TimeBaseInit(TIM4_PRESCALER_128, 255);
  161.         enableInterrupts();
  162.         TIM4_Cmd(ENABLE);
  163.  
  164.         CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, ENABLE);
  165.         TIM2_TimeBaseInit(TIM2_PRESCALER_256, 0xFFFF);
  166.         TIM2_ICInit(TIM2_CHANNEL_3, TIM2_ICPOLARITY_FALLING,TIM2_ICSELECTION_DIRECTTI, TIM2_ICPSC_DIV1,0x00);
  167.         TIM2_ITConfig(TIM2_IT_UPDATE, ENABLE);
  168.         TIM2_Cmd(ENABLE);
  169.  
  170.         while (1) {
  171.  
  172.                 if (overflow) {
  173.                         rpm = 0;
  174.  
  175.                 } else {
  176.  
  177.  
  178.                         rpm =  (uint32_t)(62500 * 60) / average;
  179.                 }
  180.  
  181.                 for (uint16_t i = 1; i < 65200; i++) {
  182.                         for (uint16_t j = 1; j < 3; j++);
  183.                 }
  184.  
  185.         }
  186.  
  187. }
  188.  
  189.  
  190. INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)
  191. {
  192.         print(rpm);
  193.         TIM4_ClearITPendingBit(TIM4_IT_UPDATE);
  194. }
  195.  
  196.  
  197. INTERRUPT_HANDLER(EXTI_PORTD_IRQHandler, 6)
  198. {
  199.         if(TIM2_GetFlagStatus(TIM2_FLAG_CC3)){
  200.                 current_period = TIM2_GetCapture3();
  201.  
  202.  
  203.                 sum = sum - periods[pos];
  204.                 periods[pos] = current_period;
  205.                 sum += current_period;
  206.  
  207.                 if(++pos == sizeof(periods)/sizeof(*periods))
  208.                 {
  209.                         pos = 0;
  210.                         filled = true;
  211.                 }
  212.                 size_t count = filled ? sizeof(periods)/sizeof(*periods) : pos;
  213.                 average = (sum < 0 ? sum - count / 2 : sum + count / 2 ) / count;
  214.  
  215.  
  216.                 TIM2_ClearFlag(TIM2_FLAG_CC3);
  217.                 overflow = false;
  218.                 TIM2_SetCounter(0);
  219.         }
  220.  
  221. }
  222.  
  223. INTERRUPT_HANDLER(TIM2_UPD_OVF_BRK_IRQHandler, 13)
  224. {
  225.         overflow = true;
  226.         TIM2_ClearITPendingBit(TIM2_IT_UPDATE);
  227. }
  228.  
  229.  

odpowiedź "Bez tytułu"

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

captcha