Bez tytułu

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

odpowiedź "Bez tytułu"

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

captcha