Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC INTERFACE with POT analog out

Status
Not open for further replies.

ajit_nayak87

Member level 5
Joined
Oct 30, 2017
Messages
86
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Activity points
981
I am trying to read output from output channel using Pot . I used 2X10K pot and out put used anaalog channel ANO & AN1 .
Here i could not able to change in analog value through serially. i found port reading the value i.e if i check voltage is showing data properly,

i have pasted config setting done to execute project & outpur file.

Please help me to read exact output data from analog

Code:
void Serial_1_Send_byte(uint8_t trbuf1) {
    TX1REG = trbuf1;

    while (0 == PIR3bits.TXIF);

//while (!TX1IF);

}


void Send_string_uart1(const unsigned char *string) {
    unsigned char i = 0;

    do {
        TX1REG = string[i++];
        while (0 == TX1STAbits.TRMT);
    } while (string[i] != '\0');
}




void Blink_Count() {
    if (PIR0bits.TMR0IF == 1) {
        PIR0bits.TMR0IF = 0;
        count = count + 1;

        if (count >= 10) {
            LED = !LED;
            count = 0;

            Send_string_uart1("ADC _Value");
            ser_data[0]=convertedValue;
            Serial_1_Send_byte(ser_data[0]);
        }
    }

}


void main(void) {
    // Initialize the device
    SYSTEM_Initialize();


    unsigned char c=0;

    while (1)

    {

        ADCC_StartConversion(channel_ANA1);
        while(!ADCC_IsConversionDone());
        convertedValue = ADCC_GetConversionResult();
        voltage = (convertedValue * 5.0)/1023;
        __delay_ms(100);

    }


}





 
 
 
 
 


void PIN_MANAGER_Initialize(void) {
 /**
 LATx registers
 */
 LATA = 0x00;
 LATB = 0x00;
 LATC = 0x00;

 /**
 TRISx registers
 */
 TRISA = 0x03;
 TRISB = 0x01;
 TRISC = 0x7F;

 /**
 ANSELx registers
 */
 ANSELC = 0x80;
 ANSELB = 0x00;
 ANSELA = 0x03;

 /**
 WPUx registers
 */
 WPUE = 0x00;
 WPUB = 0x00;
 WPUA = 0x00;
 WPUC = 0x00;

 /**
 ODx registers
 */
 ODCONA = 0x00;
 ODCONB = 0x00;
 ODCONC = 0x00;

 bool state = (unsigned char)GIE;
 GIE = 0;
 PPSLOCK = 0x55;
 PPSLOCK = 0xAA;
 PPSLOCKbits.PPSLOCKED = 0x00; // unlock PPS

 RXPPSbits.RXPPS = 0x16; //RC6->EUSART:RX;
 T0CKIPPSbits.T0CKIPPS = 0x08; //RB0->TMR0:T0CKI;
 RC7PPS = 0x09; //RC7->EUSART:TX;
 ADACTPPSbits.ADACTPPS = 0x08; //RB0->ADCC:ADACT;

 PPSLOCK = 0x55;
 PPSLOCK = 0xAA;
 PPSLOCKbits.PPSLOCKED = 0x01; // lock PPS

 GIE = state;
}

void ADCC_Initialize(void)
{
    // set the ADCC to the options selected in the User Interface
    // ADDSEN disabled; ADGPOL digital_low; ADIPEN disabled; ADPPOL VSS; 
    ADCON1 = 0x00;
    // ADCRS 0; ADMD Average_mode; ADACLR disabled; ADPSIS ADFLTR; 
    ADCON2 = 0x02;
    // ADCALC First derivative of Single measurement; ADTMD disabled; ADSOI ADGO is cleared; 
    ADCON3 = 0x08;
    // ADACT disabled; 
    ADACT = 0x00;
    // ADAOV ACC or ADERR not Overflowed; 
    ADSTAT = 0x00;
    // ADCS FOSC/8; 
    ADCLK = 0x03;
    // ADNREF VSS; ADPREF VDD; 
    ADREF = 0x00;
    // ADCAP Additional uC disabled; 
    ADCAP = 0x00;
    // ADPRE 0; 
    ADPRE = 0x00;
    // ADACQ 100; 
    ADACQ = 0x64;
    // ADPCH ANA0; 
    ADPCH = 0x00;
    // ADRPT 0; 
    ADRPT = 0x00;
    // ADLTHL 0; 
    ADLTHL = 0x00;
    // ADLTHH 0; 
    ADLTHH = 0x00;
    // ADUTHL 0; 
    ADUTHL = 0x00;
    // ADUTHH 0; 
    ADUTHH = 0x00;
    // ADSTPTL 0; 
    ADSTPTL = 0x00;
    // ADSTPTH 0; 
    ADSTPTH = 0x00;
    
    // ADGO stop; ADFM right; ADON enabled; ADCONT enabled; ADCS FOSC/ADCLK; 
    ADCON0 = 0xC4;
    
    // Clear the ADC interrupt flag
    PIR1bits.ADIF = 0;
    // Enabling ADCC interrupt.
    PIE1bits.ADIE = 1;
    
    ADCC_SetADIInterruptHandler(ADCC_DefaultInterruptHandler);

    // Clear the ADC Threshold interrupt flag
    PIR1bits.ADTIF = 0;
    // Enabling ADCC threshold interrupt.
    PIE1bits.ADTIE = 1;
    
    ADCC_SetADTIInterruptHandler(ADCC_DefaultInterruptHandler);
}

void ADCC_StartConversion(adcc_channel_t channel)
{
    // select the A/D channel
    ADPCH = channel; 
  
    // Turn on the ADC module
    ADCON0bits.ADON = 1;

    // Start the conversion
    ADCON0bits.ADGO = 1;
}

bool ADCC_IsConversionDone()
{
    // Start the conversion
    return ((unsigned char)(!ADCON0bits.ADGO));
}

adc_result_t ADCC_GetConversionResult(void)
{
    // Return the result
    return ((adc_result_t)((ADRESH << 8) + ADRESL));
}

adc_result_t ADCC_GetSingleConversion(adcc_channel_t channel)
{
    // select the A/D channel
    ADPCH = channel; 

    // Turn on the ADC module
    ADCON0bits.ADON = 1;
 
    //Disable the continuous mode.
    ADCON0bits.ADCONT = 0; 

    // Start the conversion
    ADCON0bits.ADGO = 1;


    // Wait for the conversion to finish
    while (ADCON0bits.ADGO)
    {
    }
    
    // Conversion finished, return the result
    return ((adc_result_t)((ADRESH << 8) + ADRESL));
}

void ADCC_StopConversion(void)
{
    //Reset the ADGO bit.
    ADCON0bits.ADGO = 0;
}

void ADCC_SetStopOnInterrupt(void)
{
    //Set the ADSOI bit.
    ADCON3bits.ADSOI = 1;
}

void ADCC_DischargeSampleCapacitor(void)
{
    //Set the ADC channel to AVss.
    ADPCH = 0x3C; 
}

void ADCC_LoadAcquisitionRegister(uint8_t acquisitionValue)
{
    //Load the ADACQ register.
    ADACQ = acquisitionValue; 
}

void ADCC_SetPrechargeTime(uint8_t prechargeTime)
{
    //Load the ADPRE register.
    ADPRE = prechargeTime; 
}

void ADCC_SetRepeatCount(uint8_t repeatCount)
{
    //Load the ADRPT register.
    ADRPT = repeatCount; 
}

uint8_t ADCC_GetCurrentCountofConversions(void)
{
    //Return the contents of ADCNT register
    return ADCNT;
}

void ADCC_ClearAccumulator(void)
{
    //Reset the ADCON2bits.ADACLR bit.
    ADCON2bits.ADACLR = 1;
}

uint16_t ADCC_GetAccumulatorValue(void)
{
    //Return the contents of ADACCH and ADACCL registers
    return ((uint16_t)((ADACCH << 8) + ADACCL));
}

bool ADCC_HasAccumulatorOverflowed(void)
{
    //Return the status of ADSTATbits.ADAOV
    return ADSTATbits.ADAOV;
}

uint16_t ADCC_GetFilterValue(void)
{
    //Return the contents of ADFLTRH and ADFLTRL registers
    return ((uint16_t)((ADFLTRH << 8) + ADFLTRL));
}

uint16_t ADCC_GetPreviousResult(void)
{
    //Return the contents of ADPREVH and ADPREVL registers
    return ((uint16_t)((ADPREVH << 8) + ADPREVL));
}

void ADCC_DefineSetPoint(uint16_t setPoint)
{
    //Sets the ADSTPTH and ADSTPTL registers
    ADSTPTH = setPoint >> 8;
    ADSTPTL = setPoint;
}

void ADCC_SetUpperThreshold(uint16_t upperThreshold)
{
    //Sets the ADUTHH and ADUTHL registers
    ADUTHH = upperThreshold >> 8;
    ADUTHL = upperThreshold;
}

void ADCC_SetLowerThreshold(uint16_t lowerThreshold)
{
    //Sets the ADLTHH and ADLTHL registers
    ADLTHH = lowerThreshold >> 8;
    ADLTHL = lowerThreshold;
}

uint16_t ADCC_GetErrorCalculation(void)
{
 //Return the contents of ADERRH and ADERRL registers
 return ((uint16_t)((ADERRH << 8) + ADERRL));
}

void ADCC_EnableDoubleSampling(void)
{
    //Sets the ADCON1bits.ADDSEN
    ADCON1bits.ADDSEN = 1;
}

void ADCC_EnableContinuousConversion(void)
{
    //Sets the ADCON0bits.ADCONT
    ADCON0bits.ADCONT = 1;
}

void ADCC_DisableContinuousConversion(void)
{
    //Resets the ADCON0bits.ADCONT
    ADCON0bits.ADCONT = 0;
}

bool ADCC_HasErrorCrossedUpperThreshold(void)
{
    //Returns the value of ADSTATbits.ADUTHR bit.
    return ADSTATbits.ADUTHR;
}

bool ADCC_HasErrorCrossedLowerThreshold(void)
{
    //Returns the value of ADSTATbits.ADLTHR bit.
    return ADSTATbits.ADLTHR;
}

uint8_t ADCC_GetConversionStageStatus(void)
{
    //Returns the contents of ADSTATbits.ADSTAT field.
    return ADSTATbits.ADSTAT;
}

void ADCC_ISR(void)
{
    // Clear the ADCC interrupt flag
    PIR1bits.ADIF = 0;

    if (ADCC_ADI_InterruptHandler)
            ADCC_ADI_InterruptHandler();
}

void ADCC_SetADIInterruptHandler(void (* InterruptHandler)(void)){
    ADCC_ADI_InterruptHandler = InterruptHandler;
}

void ADCC_ThresholdISR(void)
{
    // Clear the ADCC Threshold interrupt flag
    PIR1bits.ADTIF = 0;

    if (ADCC_ADTI_InterruptHandler)
        ADCC_ADTI_InterruptHandler();
}

void ADCC_SetADTIInterruptHandler(void (* InterruptHandler)(void)){
    ADCC_ADTI_InterruptHandler = InterruptHandler;
}

void ADCC_DefaultInterruptHandler(void){
    // add your ADCC interrupt custom code
    // or set custom function using ADCC_SetADIInterruptHandler() or ADCC_SetADTIInterruptHandler()
}


[ATTACH=CONFIG]148416._xfImport[/ATTACH][ATTACH=CONFIG]148416._xfImport[/ATTACH]
 

Hi,

initialize your variables:
* convertedValue (I assume this is a float, means 4 bytes of data)
* ser_data[0] (should be the same type as above)

... but you want to send out text. Thus you need to transform the float into an ASCII string.

Klaus
 

i have tried already but it not showing any data
Code:
unsigned char rxbuf[50], ser_data[100], crc_data[100], Max_scroll = 0;
unsigned int val;
uint16_t convertedValue = 0;
float  voltage ;
void Blink_Count() {
	if (PIR0bits.TMR0IF == 1) {
		PIR0bits.TMR0IF = 0;
		count = count + 1;
		//TMR0H = 0x4F;
		// TMR0L = 0x00;
        

        
		if (count >= 40) {
			LED = !LED;
			count = 0;
         
            
                    Send_string_uart1("ADC _Value");Send_string_uart1("\n");
        ser_data[0]=(char)convertedValue;
 Serial_1_Send_byte(ser_data[0]);
		}
	}

}
 

In that code 'convertedValue' is always zero. Nothing changes it after initialization and it appears you are casting a 16-bit variable as an 8-bit unsigned char.

You haven't said what PIC and which compiler you are using but I assume the 'ADCC_GetConversionResult();' routine returns a numeric value of some kind. You probably need to convert it from a binary value to an alphanumeric string before you can send it serially as text.

Brian.
 

I am using

Product Revision : PIC10 / PIC12 / PIC16 / PIC18 MCUs - 1.45
Device : PIC18F24K40
Driver Version : 2.00
The generated drivers are tested against the following:
Compiler : XC8 1.35
MPLAB : MPLAB X 3.40

You can check adcc.c which is copied in first thread which Mcc generated file. i club in main code itself
I have tried changing variable declaration but it didnt worked . display showing same



Code:
void ADCC_Initialize(void)
{
    // set the ADCC to the options selected in the User Interface
    // ADDSEN disabled; ADGPOL digital_low; ADIPEN disabled; ADPPOL VSS; 
    ADCON1 = 0x00;
    // ADCRS 0; ADMD Average_mode; ADACLR disabled; ADPSIS ADFLTR; 
    ADCON2 = 0x02;
    // ADCALC First derivative of Single measurement; ADTMD disabled; ADSOI ADGO is cleared; 
    ADCON3 = 0x08;
    // ADACT disabled; 
    ADACT = 0x00;
    // ADAOV ACC or ADERR not Overflowed; 
    ADSTAT = 0x00;
    // ADCS FOSC/8; 
    ADCLK = 0x03;
    // ADNREF VSS; ADPREF VDD; 
    ADREF = 0x00;
    // ADCAP Additional uC disabled; 
    ADCAP = 0x00;
    // ADPRE 0; 
    ADPRE = 0x00;
    // ADACQ 100; 
    ADACQ = 0x64;
    // ADPCH ANA0; 
    ADPCH = 0x00;
    // ADRPT 0; 
    ADRPT = 0x00;
    // ADLTHL 0; 
    ADLTHL = 0x00;
    // ADLTHH 0; 
    ADLTHH = 0x00;
    // ADUTHL 0; 
    ADUTHL = 0x00;
    // ADUTHH 0; 
    ADUTHH = 0x00;
    // ADSTPTL 0; 
    ADSTPTL = 0x00;
    // ADSTPTH 0; 
    ADSTPTH = 0x00;
    
    // ADGO stop; ADFM right; ADON enabled; ADCONT enabled; ADCS FOSC/ADCLK; 
    ADCON0 = 0xC4;
    
    // Clear the ADC interrupt flag
    PIR1bits.ADIF = 0;
    // Enabling ADCC interrupt.
    PIE1bits.ADIE = 1;
    
    ADCC_SetADIInterruptHandler(ADCC_DefaultInterruptHandler);

    // Clear the ADC Threshold interrupt flag
    PIR1bits.ADTIF = 0;
    // Enabling ADCC threshold interrupt.
    PIE1bits.ADTIE = 1;
    
    ADCC_SetADTIInterruptHandler(ADCC_DefaultInterruptHandler);
}

void ADCC_StartConversion(adcc_channel_t channel)
{
    // select the A/D channel
    ADPCH = channel; 
  
    // Turn on the ADC module
    ADCON0bits.ADON = 1;

    // Start the conversion
    ADCON0bits.ADGO = 1;
}

bool ADCC_IsConversionDone()
{
    // Start the conversion
    return ((unsigned char)(!ADCON0bits.ADGO));
}

adc_result_t ADCC_GetConversionResult(void)
{
    // Return the result
    return ((adc_result_t)((ADRESH << 8) + ADRESL));
}

adc_result_t ADCC_GetSingleConversion(adcc_channel_t channel)
{
    // select the A/D channel
    ADPCH = channel; 

    // Turn on the ADC module
    ADCON0bits.ADON = 1;
 
    //Disable the continuous mode.
    ADCON0bits.ADCONT = 0; 

    // Start the conversion
    ADCON0bits.ADGO = 1;


    // Wait for the conversion to finish
    while (ADCON0bits.ADGO)
    {
    }
    
    // Conversion finished, return the result
    return ((adc_result_t)((ADRESH << 8) + ADRESL));
}

void ADCC_StopConversion(void)
{
    //Reset the ADGO bit.
    ADCON0bits.ADGO = 0;
}

void ADCC_SetStopOnInterrupt(void)
{
    //Set the ADSOI bit.
    ADCON3bits.ADSOI = 1;
}

void ADCC_DischargeSampleCapacitor(void)
{
    //Set the ADC channel to AVss.
    ADPCH = 0x3C; 
}

void ADCC_LoadAcquisitionRegister(uint8_t acquisitionValue)
{
    //Load the ADACQ register.
    ADACQ = acquisitionValue; 
}

void ADCC_SetPrechargeTime(uint8_t prechargeTime)
{
    //Load the ADPRE register.
    ADPRE = prechargeTime; 
}

void ADCC_SetRepeatCount(uint8_t repeatCount)
{
    //Load the ADRPT register.
    ADRPT = repeatCount; 
}

uint8_t ADCC_GetCurrentCountofConversions(void)
{
    //Return the contents of ADCNT register
    return ADCNT;
}

void ADCC_ClearAccumulator(void)
{
    //Reset the ADCON2bits.ADACLR bit.
    ADCON2bits.ADACLR = 1;
}

uint16_t ADCC_GetAccumulatorValue(void)
{
    //Return the contents of ADACCH and ADACCL registers
    return ((uint16_t)((ADACCH << 8) + ADACCL));
}

bool ADCC_HasAccumulatorOverflowed(void)
{
    //Return the status of ADSTATbits.ADAOV
    return ADSTATbits.ADAOV;
}

uint16_t ADCC_GetFilterValue(void)
{
    //Return the contents of ADFLTRH and ADFLTRL registers
    return ((uint16_t)((ADFLTRH << 8) + ADFLTRL));
}

uint16_t ADCC_GetPreviousResult(void)
{
    //Return the contents of ADPREVH and ADPREVL registers
    return ((uint16_t)((ADPREVH << 8) + ADPREVL));
}

void ADCC_DefineSetPoint(uint16_t setPoint)
{
    //Sets the ADSTPTH and ADSTPTL registers
    ADSTPTH = setPoint >> 8;
    ADSTPTL = setPoint;
}

void ADCC_SetUpperThreshold(uint16_t upperThreshold)
{
    //Sets the ADUTHH and ADUTHL registers
    ADUTHH = upperThreshold >> 8;
    ADUTHL = upperThreshold;
}

void ADCC_SetLowerThreshold(uint16_t lowerThreshold)
{
    //Sets the ADLTHH and ADLTHL registers
    ADLTHH = lowerThreshold >> 8;
    ADLTHL = lowerThreshold;
}

uint16_t ADCC_GetErrorCalculation(void)
{
 //Return the contents of ADERRH and ADERRL registers
 return ((uint16_t)((ADERRH << 8) + ADERRL));
}

void ADCC_EnableDoubleSampling(void)
{
    //Sets the ADCON1bits.ADDSEN
    ADCON1bits.ADDSEN = 1;
}

void ADCC_EnableContinuousConversion(void)
{
    //Sets the ADCON0bits.ADCONT
    ADCON0bits.ADCONT = 1;
}

void ADCC_DisableContinuousConversion(void)
{
    //Resets the ADCON0bits.ADCONT
    ADCON0bits.ADCONT = 0;
}

bool ADCC_HasErrorCrossedUpperThreshold(void)
{
    //Returns the value of ADSTATbits.ADUTHR bit.
    return ADSTATbits.ADUTHR;
}

bool ADCC_HasErrorCrossedLowerThreshold(void)
{
    //Returns the value of ADSTATbits.ADLTHR bit.
    return ADSTATbits.ADLTHR;
}

uint8_t ADCC_GetConversionStageStatus(void)
{
    //Returns the contents of ADSTATbits.ADSTAT field.
    return ADSTATbits.ADSTAT;
}

void ADCC_ISR(void)
{
    // Clear the ADCC interrupt flag
    PIR1bits.ADIF = 0;

    if (ADCC_ADI_InterruptHandler)
            ADCC_ADI_InterruptHandler();
}

void ADCC_SetADIInterruptHandler(void (* InterruptHandler)(void)){
    ADCC_ADI_InterruptHandler = InterruptHandler;
}

void ADCC_ThresholdISR(void)
{
    // Clear the ADCC Threshold interrupt flag
    PIR1bits.ADTIF = 0;

    if (ADCC_ADTI_InterruptHandler)
        ADCC_ADTI_InterruptHandler();
}

void ADCC_SetADTIInterruptHandler(void (* InterruptHandler)(void)){
    ADCC_ADTI_InterruptHandler = InterruptHandler;
}

void ADCC_DefaultInterruptHandler(void){
    // add your ADCC interrupt custom code
    // or set custom function using ADCC_SetADIInterruptHandler() or ADCC_SetADTIInterruptHandler()
}
ADCC_setting.jpgpinout.jpgMcc_config1.jpg
 
Last edited:

I think you are making a mistake in assuming that MCC is all you need to create a working program. You must also look at the data sheet and understand how the device works.
Looking at the 'Blink_Count()' function, it look s like it is set up to be called from a timer 1 interrupt. (I can't see any code that initialises the timer or the ISR - I know that MCC generates a lot of useless code but it really pays to show us all of the code (including the CONFIG settings) when you ask for help. It also pays to provide us with a small but complete app that shows the problem.) Blink_Count calls the 'Send_string_uart1()' function that blocks until the string is sent through the UART. This is a BAD IDEA - you should never have delays and blocking code in an ISR.
Rather, have the 'Blink_Count' function set a flag when the conditions are met and use that flag in the main loop to write to the UART.
To address your problem I suggest that you start again with just the ADC being read in the main loop and use the debugger to read the received value. Once you know that you can get a raw value (which will be a 10-bit value either left or right justified within a 16-bit unsigned value between 0 and 1023) then you can start adding the rest of the code.
Susan
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top