1.2.2 Code Setup

The code is written so that there are five steps, four of which are within an endless while loop. The steps use the code mentioned in Disabled through Basic Indicator Code.

The steps involve:

  1. Enabling the Digital-to-Analog Converter (DAC).
  2. Enabling the charge pump internal oscillator.
  3. Setting the level of the DAC.
  4. Displaying the charge pump indicator bits to pins.

Each of the remaining steps set the correct CPON bits for the setting that is being examined, then a 5-second delay occurs, and then the indicator bits are displayed by turning on or off the pins associated with them. The operating code can be seen in Charge Pump Operating Code. The function “displayBits()” is the same code shown in Basic Indicator Code above.

Charge Pump Operating Code

int main(void)
{
    SYSTEM_Initialize(); 
    DAC1CONbits.EN = 1;     //enable the DAC
    CPCONbits.CPOS = 0b1;   //set Charge Pump Oscillator to the 
                            //internal oscillator
    DAC1DATL = 0xE6;        //set DAC to output 90% of Vdd
    displayBits();          //show CPT, CPRDY, and CPREQ bits on pins 
                            //RA4, RA5, and RA6

    while(1)
    {
        //Disabled
        CPCONbits.CPON = 0b00;  //Default state
        __delay_ms(5000);       //wait 5 seconds
        displayBits();
        
        //Manually Enabled
        CPCONbits.CPON = 0b11;
         __delay_ms(5000);      //wait 5 seconds
        displayBits();
        
        //Automatically Enabled - Hardware only
        CPCONbits.CPON = 0b10;
        __delay_ms(5000);       //wait 5 seconds
        displayBits();
        
        //Automatically Enabled - Hardware and DAC
        CPCONbits.CPON = 0b01;
        __delay_ms(5000);       //wait 5 seconds
        displayBits();
    }    
}