Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. #include <xc.h>
  2. #include "stdint.h"
  3. #include "stdio.h"
  4. #include "mcc_generated_files/mcc.h"
  5.  
  6. #define FILTERTAPS 15
  7. float test = 1;
  8. uint16_t CH1 = 0;
  9. uint16_t CH2 = 0;
  10. uint16_t CH2int = 0;
  11. float CH2fir = 0;
  12. float CH2float = 0;
  13. float values[FILTERTAPS] = {0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,0};
  14. float input = 1; // without a real input, looking at the step respons (input at unity, 1) would be nice to see
  15. float output = 0; // output as a 0, but that doesn't really matter
  16. uint8_t n = 0;
  17. float fir(float in){
  18. static uint8_t k;
  19. uint8_t i = 0;
  20. float out = 0;
  21. values[k] = in;
  22.  
  23. // declare variables for coefficients
  24. const float coef[FILTERTAPS] = { 0.00469777274253568, 0.0101883383139945, 0.0260326830879377, 0.0536287086268619, 0.0894202133696346,
  25. 0.125555180991128, 0.152477534229966, 0.162440811786503, 0.152477534229966, 0.125555180991128, 0.0894202133696346,
  26. 0.0536287086268619, 0.0260326830879377, 0.0101883383139945, 0.00469777274253568};
  27.  
  28.  
  29. //wzmocnienie
  30. float gain = 0.1; // set to 1 and input unity to see what this needs to be
  31.  
  32.  
  33. for (i=0; i<FILTERTAPS; i++) {
  34. out += coef[i] * values[(i + k) % FILTERTAPS];
  35.  
  36. }
  37. out /= gain;
  38.  
  39. k = (k+1) % FILTERTAPS;
  40.  
  41. return out;
  42.  
  43. }
  44. void main(void)
  45. {
  46. // initialize the device
  47. SYSTEM_Initialize();
  48.  
  49. // When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
  50. // Use the following macros to:
  51.  
  52. // Enable the Global Interrupts
  53. //INTERRUPT_GlobalInterruptEnable();
  54.  
  55. // Enable the Peripheral Interrupts
  56. //INTERRUPT_PeripheralInterruptEnable();
  57.  
  58. // Disable the Global Interrupts
  59. //INTERRUPT_GlobalInterruptDisable();
  60.  
  61. // Disable the Peripheral Interrupts
  62. //INTERRUPT_PeripheralInterruptDisable();
  63.  
  64. while (1)
  65. {
  66. ADC_Initialize();
  67. // Add your application code
  68. CH1 = ADC_GetConversion(IN2); //sygnal po diodzie
  69. CH2 = ADC_GetConversion(IN); //wyjscie ukladu
  70. CH2float = (float)CH2;
  71. CH2fir = fir(CH2float);
  72. CH2int = (uint16_t) CH2fir;
  73. printf("%d %d\r\n",CH1,CH2int);
  74. __delay_ms(5); //200Hz
  75.  
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement