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