Guest User

Untitled

a guest
Oct 14th, 2019
74
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "N76E003.h"
  2. #include "Common.h"
  3. #include "SFR_Macro.h"
  4. #include "Function_define.h"
  5.  
  6. bit BIT_TMP;
  7. /*
  8. #define set_IAPEN BIT_TMP=EA;EA=0;TA=0Xaa;TA=0x55;CHPCON|=SET_BIT0 ;EA=BIT_TMP //redefinition
  9. #define set_IAPGO BIT_TMP=EA;EA=0;TA=0Xaa;TA=0x55;IAPTRG|=SET_BIT0 ;EA=BIT_TMP
  10. #define clr_IAPEN BIT_TMP=EA;EA=0;TA=0Xaa;TA=0x55;CHPCON&=~SET_BIT0;EA=BIT_TMP;
  11. */
  12. float Bandgap_Voltage, VDD_Voltage;
  13.  
  14. void READ_BANDGAP()
  15. {
  16. UINT8 BandgapHigh,BandgapLow;
  17. UINT16 Bandgap_Value;
  18.  
  19. set_IAPEN; // Enable IAPEN
  20. IAPAL = 0x0C;
  21. IAPAH = 0x00;
  22. IAPCN = 0x04;
  23. set_IAPGO; // Trig set IAPGO
  24. BandgapHigh = IAPFD;
  25. IAPAL = 0x0d;
  26. IAPAH = 0x00;
  27. IAPCN = 0x04;
  28. set_IAPGO; // Trig set IAPGO
  29. BandgapLow = IAPFD;
  30. BandgapLow = BandgapLow&0x0F;
  31. clr_IAPEN; // Disable IAPEN
  32.  
  33. Bandgap_Value = (BandgapHigh<<4)+BandgapLow;
  34. Bandgap_Voltage = Bandgap_Value * 3;
  35. Bandgap_Voltage = Bandgap_Voltage / 4; //is equal to datasheet formula: Bandgap_Voltage = (Bandgap_Value * 3072)/4096
  36.  
  37. //code from datasheet
  38. //Bandgap_Voltage = 3072/(0x1000/Bandgap_Value);
  39. }
  40.  
  41.  
  42. void ADC_Bypass (void) // The first three times convert should be bypass
  43. {
  44. unsigned char ozc;
  45. for (ozc=0;ozc<0x03;ozc++)
  46. {
  47. clr_ADCF;
  48. set_ADCS;
  49. while(ADCF == 0);
  50. }
  51. }
  52.  
  53.  
  54. void main (void)
  55. {
  56. double bgvalue;
  57. float real_bandgap;
  58.  
  59. READ_BANDGAP();
  60. Enable_ADC_BandGap;
  61. ADC_Bypass();
  62. clr_ADCF;
  63. set_ADCS;
  64. while(ADCF == 0);
  65. bgvalue = (ADCRH<<4) + ADCRL;
  66. //(actual voltage/4096)*bgvalue
  67. real_bandgap = (4130.0/4096.0)*bgvalue;
  68. VDD_Voltage = (4095.0/bgvalue)* Bandgap_Voltage;
  69. }
RAW Paste Data