adc.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #ifndef _ADC_H__
  2. #define _ADC_H__
  3. #include "bsp/bsp.h"
  4. #include "os/os_types.h"
  5. float adc_vref_compesion(void);
  6. float adc_5vref_compesion(void);
  7. /*
  8. inserted ADC 由timer0 ch3触发,
  9. 注意:adc所有外部触发都是下降沿触发
  10. */
  11. #define ISQ0_OFFSET 0
  12. #define ISQ1_OFFSET 5
  13. #define ISQ2_OFFSET 10
  14. #define ISQ3_OFFSET 15
  15. #define IL_OFFSET 20
  16. #define ADC_REGCHAN_SAMPLE_TIME ADC_SAMPLETIME_239_5
  17. #define ADC_INSERT_SAMPLE_TIME ADC_SAMPLETIME_28_5
  18. #define ADC_TRIGGER_PHASE ADC12_PREEMPT_TRIG_TMR1CH4
  19. #define PHASE_AB 0
  20. #define PHASE_AC 1
  21. #define PHASE_BC 2
  22. static void __inline adc_phase_current_read(u8 phases, s32 *v1, s32 *v2) {
  23. *v1 = (s32)((float)((ADC1->pdt2_bit.pdt2 + ADC1->pdt3_bit.pdt3)/2.0f) * adc_5vref_compesion());
  24. *v2 = (s32)((float)((ADC2->pdt2_bit.pdt2 + ADC2->pdt3_bit.pdt3)/2.0f) * adc_5vref_compesion());
  25. }
  26. static void __inline adc_current_sample_config(u8 phases) {
  27. //ADC1->psq_bit.psn4 = V_PHASE_I_CHAN;
  28. //ADC2->psq_bit.psn4 = W_PHASE_I_CHAN;
  29. }
  30. static void __inline adc_disable_ext_trigger(void) {
  31. //ADC1->ctrl2_bit.pcten = FALSE;
  32. //ADC2->ctrl2_bit.pcten = FALSE;
  33. }
  34. static void __inline adc_enable_ext_trigger(void) {
  35. //ADC1->ctrl2_bit.pcten = TRUE;
  36. //ADC2->ctrl2_bit.pcten = TRUE;
  37. }
  38. static __inline__ void adc_clear_irq_flags(void) {
  39. /* clear the ADC flag */
  40. adc_flag_clear(ADC1, ADC_PCCE_FLAG);
  41. adc_flag_clear(ADC2, ADC_PCCE_FLAG);
  42. }
  43. #define adc_update_ext_trigger(trigger) \
  44. adc_preempt_conversion_trigger_set(ADC1, trigger, TRUE)
  45. void adc_init(void);
  46. void adc_start_convert(void);
  47. void adc_stop_convert(void);
  48. u16 adc_get_vbus(void);
  49. u16 adc_get_acc(void);
  50. u16 adc_get_throttle(void);
  51. void adc_get_uvw_phaseV(u16 *uvw);
  52. u16 adc_get_mos_temp(void);
  53. u16 adc_get_motor_temp(void);
  54. u16 adc_get_ibus(void);
  55. u16 adc_get_vref(void);
  56. void adc_set_vref_calc(float v);
  57. void adc_vref_filter(void);
  58. u16 adc_get_5v_ref(void);
  59. void adc_set_5vref_calc(float v);
  60. u16 adc_get_throttle2(void);
  61. u16 adc_get_thro_5v(void);
  62. u16 adc_get_thro2_5v(void);
  63. #endif /* _ADC_H__ */