Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. #include <plib.h>
  2. #include <math.h>
  3.  
  4.  
  5. void init_ADC(void) {
  6. AD1PCFG = 0x7FFF; // all PORTB = Digital but RB15 (AN15) = analog
  7. AD1CON1 = 0x00E0; // SSRC bits = 111 implies internal counter ends sampling and starts converting
  8. AD1CON2 = 0;
  9. AD1CON3 = 0x1F02; // Sample time = 31 TPB, Convertion time = 12*TAD = 12*[2*(ADCS+1)] TPB = 12*6 TPB
  10. AD1CSSL = 0;
  11. AD1CHS = 0x000F0000; // Connect RB15/AN15 as CH0 input
  12. AD1CON1SET = 0x8000; // turn ADC ON
  13. }
  14.  
  15. #define SAMP 1
  16. #define DONE 0
  17.  
  18. void acquire_ADC(void)
  19. {
  20. int v;
  21.  
  22. AD1CON1SET = (1<<SAMP); // start Sampling (SAMP = 1) and then start converting
  23. while (!(AD1CON1 & (1<<DONE))); // conversion done?
  24. v = ADC1BUF0; // yes then get ADC value
  25.  
  26. data[0] = (v >> 8) & 0x000000FF;
  27. data[1] = v & 0x000000FF;
  28. }
  29.  
  30. float distancia1( float T_infra1)
  31. {
  32. float dist1;
  33.  
  34. dist1 = 1/((T_infra1+ 0,3 -0,28)/17,77);
  35.  
  36. return dist1;
  37. }
  38.  
  39. float distancia2( float T_infra2)
  40. {
  41. float dist2;
  42.  
  43. dist2 = 1/((T_infra2+ 0,3 -0,28)/17,77);
  44.  
  45. return dist2;
  46. }
  47.  
  48. float decl(float dist1, float dist2, float dist_infra)
  49. {
  50. float dist_nao_comum, declive;
  51.  
  52. dist_nao_comum= dist1 - dist2;
  53.  
  54. if(dist_nao_comum < 0)
  55. {
  56. dist_nao_comum = dist_nao_comum * (-1)
  57. declive = atan(dist_nao_comum/dist_infra);
  58. }
  59. if(dist_nao_comum > 0)
  60. declive = atan(dist_nao_comum/dist_infra);
  61.  
  62. if(dist_nao_comum = 0)
  63. {
  64.  
  65. declive = 0;
  66. }
  67. return declive;
  68. }
  69.  
  70.  
  71. int main(void) {
  72.  
  73. float T_infra1, T_infra2, dist1, dist2, declive, dist_infra;
  74.  
  75. TRISRCLR = 0x0001; //definir como entrada led nº1
  76. TRISRCLR = 0x0002;//definir como entrada led nº2
  77.  
  78. dist_infra = 10 // valor a confirmar, distancia entre os sensores
  79.  
  80.  
  81. while (1)
  82. {
  83.  
  84. dist1 = distancia1(T_infra1);
  85. dist2 = distancia2(T_infra2);
  86. declive = decl(dist1, dist2, dist_infra);
  87.  
  88.  
  89.  
  90.  
  91.  
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement