Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Displaying ADC result on GLCD with C18

Status
Not open for further replies.

superbrew

Member
Hello, I would like to display the result of an ADC conversion on my GLCD, but all I am getting is about 2.5 lines of garbage. Here is the code that I am using:
(GLCD functions created by Pommie and modified for my use)
Code:
#include <p18f2520.h>
#include <GLCD.h>
#include <adc.h>
#include <delays.h>
#include <stdlib.h>

#pragma config WDT = OFF, LVP = OFF, OSC = INTIO67

void main (void){
	int read;
	char adc_ret[10];
	
	ADCON1 = 0x0d;
	CMCON = 0x07;		// no comparators
	OSCCON = 0x70;
	TRISA = 0XFF;
	TRISC = 0x00;	
	
	//Setup
	Init_GLCD();
	OpenADC(ADC_FOSC_RC & ADC_LEFT_JUST & ADC_12_TAD,
		ADC_CH1 & ADC_INT_OFF & ADC_VREFPLUS_VDD & 
			ADC_VREFMINUS_VSS, 13);
	SetChanADC( ADC_CH1 );

	PutMessage((rom char*)"test");
	Delay10KTCYx(200);

	while (1){
		ConvertADC(); // Start conversion
		while( BusyADC() ); // Wait for completion
		
		read = 523;ReadADC();
		itoa(read,adc_ret);
		PutMessage((rom char*)adc_ret);
		Delay10KTCYx(250);
		SetPos(0,0);	
	}
        //This section of code works, I used it check ADC function
	/*ConvertADC();
	while(BusyADC());
	i = ReadADC();
	if(i<125)PORTC = 0b00000001;
	else if(i>126 && i<250)PORTC = 0b00000011;
	else if(i>251 && i<375)PORTC = 0b00000011;
	else if(i>376 && i<400)PORTC = 0b00000111;
	else if(i>401 && i<525)PORTC = 0b00001111;
	else if(i>526 && i<650)PORTC = 0b00011111;
	else if(i>651 && i<750)PORTC = 0b00111111;	
	else if(i>751 && i<875)PORTC = 0b01111111;
	else if(i>876 && i<1023)PORTC = 0b11111111;
	*/
}

Stepping through using MPLAB simulator, it seems to get hung up at the itoa function. Thanks
 
Your problem is due to pointer differences. PutMessage expects a pointer to a rom string and you are passing it a ram variable.

You can get it working by doing,
Code:
    while (1){
        ConvertADC(); // Start conversion
        while( BusyADC() ); // Wait for completion
        read = 523;ReadADC();
        itoa(read,adc_ret);
        pos=0;
        while(adc_ret[pos])
            PutChar(adc_ret[pos++]);
        Delay10KTCYx(250);
        SetPos(0,0);    
    }

Not very elegant, but should work. Someday soon I'll get around to tidying those GLCD routines.

Mike.
 
Last edited:
It works! Thanks again. Now I just have to figure out how to get the numbers on the display to be right justified.
 
How about,
Code:
    while (1){
        ConvertADC(); // Start conversion
        while( BusyADC() ); // Wait for completion
        read = 523;ReadADC();
        itoa(read,adc_ret);
        [COLOR="Blue"]SetPos(0,16-strlen(adc_ret));[/COLOR]
        pos=0;
        while(adc_ret[pos])
            PutChar(adc_ret[pos++]);
        Delay10KTCYx(250);
        SetPos(0,0);    
    }

Mike.
Edit, forgot this was for a GLCD. The above is for a 16 character LCD. The principle is the same but it will be 128-strlen(adc_ret)*charwidth.
 
Last edited:
Glcd

hi, I neede the GLCD functions, please please tell me where I can find it or help me


thank you
 
Glcd

I can´t open the demo...I try to open but I can´t.... it s a not found page.....
Please help me with that I need the glcd libaries...How can I acces them..


thank you
 
In the microcontroller forum look for the thread Unicorn-GLCD-Demo. Al the code is in that thread.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top