+ Post New Thread
Results 1 to 5 of 5
  1. #1
    Member level 3
    Points: 633, Level: 5

    Join Date
    Aug 2015
    Posts
    59
    Helped
    1 / 1
    Points
    633
    Level
    5

    Problem on getting ADC values through UART

    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
             
    
    
    
    
    	}
    }

    •   AltAdvertisment

        
       

  2. #2
    Super Moderator
    Points: 69,659, Level: 64
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    14,229
    Helped
    3251 / 3251
    Points
    69,659
    Level
    64

    Re: Problem on getting ADC values through UART

    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


    1 members found this post helpful.

    •   AltAdvertisment

        
       

  3. #3
    Super Moderator
    Points: 76,769, Level: 67
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    12,552
    Helped
    4186 / 4186
    Points
    76,769
    Level
    67

    Re: Problem on getting ADC values through UART

    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.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.


    1 members found this post helpful.

    •   AltAdvertisment

        
       

  4. #4
    Member level 3
    Points: 633, Level: 5

    Join Date
    Aug 2015
    Posts
    59
    Helped
    1 / 1
    Points
    633
    Level
    5

    Re: Problem on getting ADC values 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
    
    #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



  5. #5
    Super Moderator
    Points: 76,769, Level: 67
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    12,552
    Helped
    4186 / 4186
    Points
    76,769
    Level
    67

    Re: Problem on getting ADC values through UART

    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.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.


    2 members found this post helpful.

--[[ ]]--