adc.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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_SAMPLE_TIME ADC_SAMPLETIME_13_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->pdt1_bit.pdt1 * adc_5vref_compesion());
  24. *v2 = (s32)((float)ADC2->pdt1_bit.pdt1 * adc_5vref_compesion());
  25. }
  26. static void __inline adc_current_sample_config(u8 phases) {
  27. ADC1->psq_bit.psn3 = V_PHASE_I_CHAN;
  28. ADC2->psq_bit.psn3 = W_PHASE_I_CHAN;
  29. }
  30. static void __inline adc_disable_ext_trigger(void) {
  31. ADC1->ctrl2_bit.octen = FALSE;
  32. }
  33. static void __inline adc_enable_ext_trigger(void) {
  34. ADC1->ctrl2_bit.octen = TRUE;
  35. }
  36. static __inline__ void adc_clear_irq_flags(void) {
  37. /* clear the ADC flag */
  38. adc_flag_clear(ADC1, ADC_PCCE_FLAG);
  39. adc_flag_clear(ADC2, ADC_PCCE_FLAG);
  40. }
  41. #define adc_update_ext_trigger(trigger) \
  42. adc_preempt_conversion_trigger_set(ADC1, trigger, TRUE)
  43. void adc_init(void);
  44. void adc_start_convert(void);
  45. void adc_stop_convert(void);
  46. u16 adc_get_vbus(void);
  47. u16 adc_get_acc(void);
  48. u16 adc_get_throttle(void);
  49. void adc_get_uvw_phaseV(u16 *uvw);
  50. u16 adc_get_mos_temp(void);
  51. u16 adc_get_motor_temp(void);
  52. u16 adc_get_ibus(void);
  53. u16 adc_get_vref(void);
  54. void adc_set_vref_calc(float v);
  55. void adc_vref_filter(void);
  56. u16 adc_get_5v_ref(void);
  57. void adc_set_5vref_calc(float v);
  58. u16 adc_get_throttle2(void);
  59. u16 adc_get_thro_5v(void);
  60. u16 adc_get_thro2_5v(void);
  61. #endif /* _ADC_H__ */