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.

ADC, float to ascii sned data to LCD

Status
Not open for further replies.

sikdeath

New Member
ADC, float to ascii send data to LCD

hey,
so first off im Using C18 compiler in MPlab, my programming knowledge is terrible, i've been searching around for weeks and managed to get this far

Code:
#include <p18f252.h>

#define RS   			    PORTCbits.RC0
#define RW   				PORTCbits.RC1
#define E 	 				PORTCbits.RC2
#define NewLine             0xC0  
#define ClrDisp     		0x01  
#define TwoLine10dots		0x38	 
#define ScrOnCurOffBlinkOn  0x0F  
#define IncCurDntMovDis		0x06
#define CurHome             0x02  
float V1,V2;
//#define	NOP() asm("nop")        // 1 uS delay for 4MHz clock

void delay_uS(unsigned int d) {
 	unsigned int	j;
	for (j = 0; j < d; j++)
              ;	
}

void delay_mS(unsigned int D) {
	unsigned int	i;
	for (i = 0; i < D; i++) 
               delay_uS(1000);
}

void sendCmd(unsigned char Cmd){// write text to LCD
	PORTB= Cmd;					// put char into RAM
	RS = 0;                    	// command mode
    RW = 0;                    	// write operation
	delay_mS(1);
	E  = 1;
	delay_mS(1);
	E  = 0;                    	// send command to LCD
	return;
}
 
void sendData(unsigned char Ch){// write text to LCD
	PORTB= Ch;					// put char into RAM
	RS = 1;                    	// data mode
    RW = 0;                    	// write operation
	delay_mS(1);
	E  = 1;
	delay_mS(1);
	E  = 0;                    	// send data to LCD
	return;
}

void initialiseLCD(void){
    delay_mS(100);
    sendCmd(TwoLine10dots);  
  	delay_mS(100);
    sendCmd(TwoLine10dots); 
	delay_mS(100);
    sendCmd(TwoLine10dots); 
	delay_mS(100);
    sendCmd(TwoLine10dots); 
    delay_mS(100);
    sendCmd(ScrOnCurOffBlinkOn);
    delay_mS(10);
    sendCmd(ClrDisp);    		
 	delay_mS(10);         
    sendCmd(CurHome);    		
 	delay_mS(10);         
    sendCmd(0x06);    			
}

float ReadADC1(int ch) {
	if (ch == 1) {

		ADCON0bits.CHS0=1; 
				}
	else {                               // this determins using pin AN0 or AN1

		ADCON0bits.CHS0=0;
		}

	ADCON0bits.GO=1;
	while (ADCON0bits.GO==1);
	return (((ADRESH*256)+ADRESL)*0.00489);
}




void main(void){
//                    RA0,1,2 as I/Os
	TRISA = 0x01;
	TRISC = 0x00;               // set to outputs
    TRISB = 0x00;
	RW = 0;						// Write mode
	RS = 0;						// Command mode
	E = 0;						// Start talking to LCD
	
	ADCON1 = 0x80;
	ADCON0 = 0x81;
	ADCON0bits.CHS1=0;
	ADCON0bits.CHS2=0;

	V1 = ReadADC1(1);
	

    initialiseLCD();
 	sendData('B');
 	sendData('a');
 	sendData('t');
 	sendData('t');
 	sendData('e');
 	sendData('r');
	sendData('y');
 	sendData(' ');
	sendData('T');
 	sendData('i');
 	sendData('m');
 	sendData('e');
	sendData(' ');
 	sendData('t');
	sendData('o');
 	sendData(' ');
 	sendData('D');
 	sendData('E');
	sendData('A');
 	sendData('D');
    sendCmd(NewLine);
 	sendData('"');
 	sendData('X');
 	sendData('"');
	sendData(' ');
 	sendData('m');
 	sendData('i');
 	sendData('n');
 	sendData('u');
 	sendData('t');
 	sendData('e');
 	sendData('s');
	while(1);				// wait for ever
}

This code is being using to display the voltage of a battery, i still have a lot more to do but that is where im currently stuck,

from my code i hope is clear that im trying to input a voltage into AN1 store the voltage that comes in to "V1"

what im stuck with is then making that number display on my LCD, i know i have to convert the data in V1 into ascii and then take that conversion and display it on my LCD, i'd like to put it where i currently have "X" just to see if i can actually do it.

if anyone could give me some tips i would be so thankfull, i have seen a lot about sprintf but im not sure if this would be right for me.
i thought this might be the place to come to people who have more experience writing code.

thanks
 
Last edited:
Here create a function to print C is a brilliant language for strings lets use it.
Code:
printLcd("Battery time to Dead");


void printLcd(char* str)
	{
	
	while(*str !='\0')
		sendData(*str++);
	
	}

Then when you use sprintf() you'll love it..

sprint f(lcdbuffer," %d minutes",mins);

Cheers Ian
 
Last edited:
hey, thanks so much for replying, i've used the code you gave me, and i guess its idea is to take each character in the () and send then through one at a time... the str++ refering to going along the string of text?

just on my LCD i get a jumbled line of text...

xX (jap characters) b) t1-|io ORC '"

i dont know if that means anything im guessing not...

any more help would be amazing
 
ok so after being useless with the code you gave me, i tried something new,
Code:
void floattoascii(void)
{ unsigned int Hun, Ten;
	Hun = V1/100;
	V1char[0] = (unsigned char) Hun;
	V1char[0] +=0x48;
		V1 = V1 % 100;

	Ten = V1/10;
	V1char[1] = (unsigned char) Ten;
	V1char[1] +=0x48;
		V1 = V1 % 10;

	V1char[2] = (unsigned char) V1;
	V1char[2] +=0x48;

}

trying to get voltage in my a to d displayed on my lcd using an array

Code:
sendData('D');
    sendCmd(NewLine);
	floattoascii();
 	sendData('V1char[0]');
 	sendData('V1char[1]');
 	sendData('V1char[2]');

this is just a snipit of where i've put the addtions to my code in the first post..

im getting error
[1135] integer operands required for modulus operator

i'm thinking it might be something to do with my float value being stored in V1 so then this can't be the interger i need it to be? im confused and i hope im not confusing anyone trying to explain...

thanks for any help you can give
 
I'm trying not to appear dense but why are you using floats? The compiler is right the modulus operator cannot be used on floats. If you're only displaying 100's, 10's and units.

The LCD displays garbage because as this was an example it was simplified you need a " rom char* " to print rom strings (this is because I use sprintf())

Ian
 
Last edited:
please the only one here who is dense is me, tbh someone suggested using floats, so i read about them and came up with what i have shown, i dont know why im using them, i was told something about they are the best way to work with non whole numbers.
 
In my job I have to program oodles of geometry algorithms cos, sin, tan and Arctan and much more.. I've never used a float... The ADC returns an integer you can manipulate this number in every way possible JUST using integers..

Ian
 
so just change this

Code:
float ReadADC1(int ch) {

to this
Code:
void ReadADC1(int ch) {

then it will be stored as an interger?

sorry im so useless
 
No you're not! we've all been there.." I'm still there "

Code:
 int ReadADC1(int ch)

you need to return the result.

Ian

Code:
 return (((ADRESH*256)+ADRESL) * 489)
change this aswell then instead of 5.0739 max you will have 50739

You can put the decimal place in during the print float routine %10000,"." ,%1000,% 100,%10... you get the idea
 
Last edited:
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top