ajit_nayak87
Member level 5
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
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]