Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "N76E003.h"
- #include "Common.h"
- #include "SFR_Macro.h"
- #include "Function_define.h"
- bit BIT_TMP;
- /*
- #define set_IAPEN BIT_TMP=EA;EA=0;TA=0Xaa;TA=0x55;CHPCON|=SET_BIT0 ;EA=BIT_TMP //redefinition
- #define set_IAPGO BIT_TMP=EA;EA=0;TA=0Xaa;TA=0x55;IAPTRG|=SET_BIT0 ;EA=BIT_TMP
- #define clr_IAPEN BIT_TMP=EA;EA=0;TA=0Xaa;TA=0x55;CHPCON&=~SET_BIT0;EA=BIT_TMP;
- */
- float Bandgap_Voltage, VDD_Voltage;
- void READ_BANDGAP()
- {
- UINT8 BandgapHigh,BandgapLow;
- UINT16 Bandgap_Value;
- set_IAPEN; // Enable IAPEN
- IAPAL = 0x0C;
- IAPAH = 0x00;
- IAPCN = 0x04;
- set_IAPGO; // Trig set IAPGO
- BandgapHigh = IAPFD;
- IAPAL = 0x0d;
- IAPAH = 0x00;
- IAPCN = 0x04;
- set_IAPGO; // Trig set IAPGO
- BandgapLow = IAPFD;
- BandgapLow = BandgapLow&0x0F;
- clr_IAPEN; // Disable IAPEN
- Bandgap_Value = (BandgapHigh<<4)+BandgapLow;
- Bandgap_Voltage = Bandgap_Value * 3;
- Bandgap_Voltage = Bandgap_Voltage / 4; //is equal to datasheet formula: Bandgap_Voltage = (Bandgap_Value * 3072)/4096
- //code from datasheet
- //Bandgap_Voltage = 3072/(0x1000/Bandgap_Value);
- }
- void ADC_Bypass (void) // The first three times convert should be bypass
- {
- unsigned char ozc;
- for (ozc=0;ozc<0x03;ozc++)
- {
- clr_ADCF;
- set_ADCS;
- while(ADCF == 0);
- }
- }
- void main (void)
- {
- double bgvalue;
- float real_bandgap;
- READ_BANDGAP();
- Enable_ADC_BandGap;
- ADC_Bypass();
- clr_ADCF;
- set_ADCS;
- while(ADCF == 0);
- bgvalue = (ADCRH<<4) + ADCRL;
- //(actual voltage/4096)*bgvalue
- real_bandgap = (4130.0/4096.0)*bgvalue;
- VDD_Voltage = (4095.0/bgvalue)* Bandgap_Voltage;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement