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.

Problem on getting ADC values through UART

Status
Not open for further replies.

cnandha19

Member level 3
Joined
Aug 28, 2015
Messages
59
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Activity points
604
I am getting adc values in LCD but output not came through UART

Code:
#include "Includes.h"

// Configuration word for PIC16F877
__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON 
		& LVP_OFF & CPD_OFF & WRT_ON & DEBUG_OFF);



#define BAUDRATE 9600  //bps


void InitUART(void)
{
	TRISC6 = 1;   					// TX Pin
	TRISC7 = 1;   					// RX Pin
	
	SPBRG = ((_XTAL_FREQ/16)/BAUDRATE) - 1;
	BRGH  = 1;                   	// Fast baudrate
	SYNC  = 0;						// Asynchronous
	SPEN  = 1;						// Enable serial port pins
	CREN  = 1;						// Enable reception
	SREN  = 0;						// No effect
	TXIE  = 0;						// Disable tx interrupts
	RCIE  = 1;						// Enable rx interrupts
	TX9   = 0;						// 8-bit transmission
	RX9   = 0;						// 8-bit reception
	TXEN  = 0;						// Reset transmitter
	TXEN  = 1;						// Enable the transmitter
}






// Main Function
void main(void)
{  

   unsigned char ch="ADC DATA ACQUISTION";
unsigned int x,y;
	unsigned int ADC_value = 0;
	unsigned int digit1, digit2, digit3, digit4;

	InitADC();			// Initialize ADC
	InitLCD();			// Initialize LCD
    InitUART();
	while(1)
	{
		ClearLCDScreen();					// Clear LCD screen



         void SendByteSerially(unsigned char);
unsigned char ReceiveByteSerially(void);
void SendStringSerially(const unsigned char*);

		ADC_value = GetADCValue(AN7);		// Read ADC value from RE2(AN7) pin
__delay_ms(1000);         
while(!TXIF);  // wait for previous transmission to finish
    	TXREG = ADC_value;
     __delay_ms(1000);
    

// SendStringSerially("ADC_value");	// Send string on UART
   	
	GIE  = 1;  							// Enable global interrupts
    PEIE = 1;  							

		// ADC_value can have a value from 0 (0v) to 1023(5v) only.
		// SO display 4 digits of ADC_value
		digit1 = (unsigned int)(ADC_value/1000);									 // Calculate digit1 of ADC_value
		digit2 = (unsigned int)((ADC_value - digit1*1000)/100);						 // Calculate digit2 of ADC_value
		digit3 = (unsigned int)((ADC_value - (digit1*1000+digit2*100))/10);			 // Calculate digit3 of ADC_value
		digit4 = (unsigned int)(ADC_value - (digit1*1000+digit2*100+digit3*10));	 // Calculate digit4 of ADC_value

		WriteDataToLCD(digit1+0x30);		// Display digit1 of ADC_value on LCD
		WriteDataToLCD(digit2+0x30);		// Display digit2 of ADC_value on LCD
		WriteDataToLCD(digit3+0x30);		// Display digit3 of ADC_value on LCD
		WriteDataToLCD(digit4+0x30);		// Display digit4 of ADC_value on LCD

		__delay_ms(500);					// Half second delay before next reading
         




	}
}
 

Hi,

I assume TxReg is an 8 bit register, and ADC_Value is an 16 bit value.

I don´t hink th complier is intelligent enough to send two bytes...

****
You try to send binary data. So you can´t show the values in a terminal program.
I recommend to send ASCII values through UART.. especially for debugging.. because the values are human readable.

Klaus
 
The code is confusing. Why wait 1 second then check if there is still data in the UART? Any data would ahve been sent long ago.
You also seem to enable the GIE and PEIE bits inside a loop and at the same time not use interrupts anyway!

As Klauss points out "TXREG = ADC_value;" can at best only send 8 of the 10 ADC bits. If you really want to send the whole 10 bits in binary, send ADRESH first then ADRESL as a second instruction.

If you indent the lines of your code to show function start and finish it would be a lot easier to read.

Brian.
 
Code:
/*  
#include "Includes.h"

// Configuration word for PIC16F877
__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON 
		& LVP_OFF & CPD_OFF & WRT_ON & DEBUG_OFF);


#define BAUDRATE 9600  //bps

#define AN0  0
#define AN1  1
#define AN2  2
#define AN3  3
#define AN4  4
#define AN5  5
#define AN6  6
#define AN7  7

void InitADC(void)
{
	ADCON1  = 0x80;	     // Make PORTA and PORTE analog pins
						 // Also, Vref+ = 5v and Vref- = GND
	TRISA   = 0x2f;      // Make RA5, RA3, RA2, RA1, RA0 input
	TRISE   = 0x07;		 // Make RE0, RE1 and RE2 input
	ADCON0  = 0x81;		 // Turn on the A/D Converter
}

unsigned int GetADCValue(unsigned char Channel)
{
	ADCON0 &= 0xc7;         // Clear Channel selection bits
	ADCON0 |= (Channel<<3); // Select channel pin as ADC input
    
    __delay_ms(10);         // Time for Acqusition capacitor 
							// to charge up and show correct value
	GO_nDONE  = 1;		    // Enable Go/Done

	while(GO_nDONE);        // Wait for conversion completion

	return ((ADRESH<<8)+ADRESL);   // Return 10 bit ADC value
}

void InitUART(void)
{
	TRISC6 = 1;   					// TX Pin
	TRISC7 = 1;   					// RX Pin
	
	SPBRG = ((_XTAL_FREQ/16)/BAUDRATE) - 1;
	BRGH  = 1;                   	// Fast baudrate
	SYNC  = 0;						// Asynchronous
	SPEN  = 1;						// Enable serial port pins
	CREN  = 1;						// Enable reception
	SREN  = 0;						// No effect
	TXIE  = 0;						// Disable tx interrupts
	RCIE  = 1;						// Enable rx interrupts
	TX9   = 0;						// 8-bit transmission
	RX9   = 0;						// 8-bit reception
	TXEN  = 0;						// Reset transmitter
	TXEN  = 1;						// Enable the transmitter
}


void SendByteSerially(unsigned char Byte)  // Writes a character to the serial port
{
	while(!TXIF);  // wait for previous transmission to finish
	TXREG = Byte;
}

unsigned char ReceiveByteSerially(void)   // Reads a character from the serial port
{
	if(OERR) // If over run error, then reset the receiver
	{
		CREN = 0;
		CREN = 1;
	}
	
	while(!RCIF);  // Wait for transmission to receive
	
	return RCREG;
}

void SendStringSerially(const unsigned char* st)
{
	while(*st)
		SendByteSerially(*st++);
}

// Main Function
void main(void)
{




    void InitUART(void);
    void SendByteSerially(unsigned char);
    unsigned char ReceiveByteSerially(void);
    void SendStringSerially(const unsigned char*);
    void InitADC(void);
     unsigned int GetADCValue(unsigned char);
	unsigned int ADC_value = 0;
	unsigned int digit1, digit2, digit3, digit4;

	InitADC();			// Initialize ADC
	InitLCD();			// Initialize LCD
    InitUART;
	while(1)
	{
		ClearLCDScreen();					// Clear LCD screen
		ADC_value = GetADCValue(AN7);		// Read ADC value from RE2(AN7) pin

		// ADC_value can have a value from 0 (0v) to 1023(5v) only.
		// SO display 4 digits of ADC_value
		digit1 = (unsigned int)(ADC_value/1000);									 // Calculate digit1 of ADC_value
		digit2 = (unsigned int)((ADC_value - digit1*1000)/100);						 // Calculate digit2 of ADC_value
		digit3 = (unsigned int)((ADC_value - (digit1*1000+digit2*100))/10);			 // Calculate digit3 of ADC_value
		digit4 = (unsigned int)(ADC_value - (digit1*1000+digit2*100+digit3*10));	 // Calculate digit4 of ADC_value

		WriteDataToLCD(digit1+0x30);		// Display digit1 of ADC_value on LCD
		WriteDataToLCD(digit2+0x30);		// Display digit2 of ADC_value on LCD
		WriteDataToLCD(digit3+0x30);		// Display digit3 of ADC_value on LCD
		WriteDataToLCD(digit4+0x30);		// Display digit4 of ADC_value on LCD

		__delay_ms(500);					// Half second delay before next reading
	}
}

I am getting the values o LCD how to get adc values on hyperterminal pls help

I am using hitech c compiler
 

I'm surprised Hitech allows function prototypes inside functions.

Your code doesn't send anything through the UART. As already stated, if you want to send the binary values through the UART you have to split the 10 bit ADC result into two. As you are already combining two halves into one, it make sense to use the original values before you combined them. The code is therefore:
Code:
SendByteSerially(ADRESH);
SendByteSerially(ADRESL);

If you want to send the value in readable (ASCII) format, try this:
Code:
sprintf(ADC_String,"%d",ADC_value);
SendStringSerially(ADC_String);

You will have to declare a 'char' array to hold ADC_String with enough spaces to hold the value and a terminating null.

Brian.
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top