Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Kurenai_ryu said:which result?
could you post your actual code and circuit?
have you a working LCD hardware/firmware?
or just binary-to-decimal convert problem?
#include <p18f45k20.h>
#include <stdio.h>
#define LCD_DATA PORTD
#define LCD_RS PORTCbits.RC0
#define LCD_E PORTCbits.RC1
void main (void);
void PImain (void);
void Initialize(void);
void a2d_conversion(void);
void lcd_init(void);
void lcd_command(unsigned char);
void lcd_affiche(unsigned float);
void lcd_goto(unsigned char);
void lcd_delay(void);
#pragma code
/*************************
* Definition des Variables
*************************/
float conversion;
double U0;
double U1;
double prop;
double integ;
char error0;
char error1;
int Yc;
char Co;
char M;
unsigned char kp;
unsigned char ki;
unsigned char delay;
/*********************
* Fonction principal
********************/
void main()
{
kp=8;
ki=2;
Co=98;
TRISD=0;
ADCON1=0x0E;
ADCON0=0x01;
ADCON2=0x00;
lcd_init();
lcd_affiche('Y');
lcd_affiche('c');
lcd_affiche('=');
while(1)
{
delay = 0x01;
ADCON0bits.ADON = 1;
while (delay != 0)
{
delay -= 1;
}
ADCON0bits.GO = 1;
while (ADCON0bits.GO)
{
//pas de opcode;
}
Yc = ADRESH ;
conversion=(5*Yc)/1024;
lcd_goto(3);
[b] // display result of ADC in LCD[/b]
}
}
/******************************************
* Initialisation de l’afficheur LCD 1x16
******************************************/
void lcd_init(void)
{
TRISC=0;
TRISD=0;
lcd_command(0x30); //accès 8 bits, 1 ligne de 16 C,
lcd_command(0x01); //reset l'afficheur
lcd_command(0x0C); //affichage ON - curseur ON
lcd_command(0x06); //deplacement du curseur vers la droite
}
/******************************************
* configuration de l’afficheur LCD 1x16
******************************************/
void lcd_command(unsigned char comand)
{
LCD_RS = 0;
LCD_DATA = comand;
LCD_E = 1;
lcd_delay();
LCD_E = 0;
}
/*********************
* afficher sur LCD
*********************/
void lcd_affiche(unsigned float affich)
{
LCD_RS = 1;
LCD_DATA=affich;
LCD_E = 1;
lcd_delay();
LCD_E = 0;
}
/*******************************
* choisir une Position sur LCD
*******************************/
void lcd_goto(unsigned char pos)
{
TRISC = 0;
TRISD = 0;
LCD_RS = 0;
lcd_command(0x80+pos);
}
/*************************
* Temporisation pour LCD
*************************/
void lcd_delay(void)
{
int i=0;
for (i=0;i<250;i++);
}
void print_float(float n){
int i,j;
i=(int)n; //first integer digit //assuming its from 0.0 to 5.0
lcd_affiche(0x30|i);
lcd_affiche('.');
for(j=0;j<3;j++){
n*=10.0;
i=(int)(n);
lcd_affiche(0x30|i);
}
}
conversion=(5.0*Yc)/1024.0;
lcd_goto(3);
// display result of ADC in LCD
print_float(conversion);
Kurenai_ryu said:ok, so you have a working lcd, and a adc value (properly scaled at 'float conversion') you need a 'float' to 'ascii' convertion...
mmm... it's kinda difficult you know? maybe using the 'printf' or 'sprintf' from C18... mmm... should check that... checking... too bad! it doesn't implement "%f"... maybe converting from float to int...
if you need ( or are planning) to implement this convertion from scratch... say, how many digits do you need? (0.0 to 4.9 or 0.000 to 4.999) that way you can implement something like this...
Code:void print_float(float n){ int i,j; i=(int)n; //first integer digit //assuming its from 0.0 to 5.0 lcd_affiche(0x30|i); lcd_affiche('.'); for(j=0;j<3;j++){ n*=10.0; i=(int)(n); lcd_affiche(0x30|i); } }
and place it where you marked!
Code:conversion=(5.0*Yc)/1024.0; lcd_goto(3); // display result of ADC in LCD print_float(conversion);
Added after 6 minutes:
sorry! I think you should use ADRES (and not ADRESH) if you want the 10 bit value...
void print_float(float n){
int i,j;
i=(int)n; //first integer digit //assuming its from 0.0 to 5.0
lcd_affiche(0x30|i);
lcd_affiche('.');
for(j=0;j<3;j++){
n-=i; //hope it's enought...
n*=10.0;
i=(int)(n);
lcd_affiche(0x30|i);
}
}
void print_float(float n){
int i,j;
i=(int)n; //first integer digit //assuming its from 0.0 to 5.0
lcd_affiche(0x30|i);
lcd_affiche('.');
for(j=0;j<3;j++){
n*=10.0;
i=((int)(n))%10;//just the digit!
lcd_affiche(0x30|i);
}
}
Kurenai_ryu said:mmmm its true! i'm sorry I made the code right here, so i couldn't check it closely....
my correction would be...
Code:void print_float(float n){ int i,j; i=(int)n; //first integer digit //assuming its from 0.0 to 5.0 lcd_affiche(0x30|i); lcd_affiche('.'); for(j=0;j<3;j++){ n-=i; //hope it's enought... n*=10.0; i=(int)(n); lcd_affiche(0x30|i); } }
or the classic...
Code:void print_float(float n){ int i,j; i=(int)n; //first integer digit //assuming its from 0.0 to 5.0 lcd_affiche(0x30|i); lcd_affiche('.'); for(j=0;j<3;j++){ n*=10.0; i=((int)(n))%10;//just the digit! lcd_affiche(0x30|i); } }
hope some of this quick changes will help...
Kurenai_ryu said:ok ok, time to get serious, i'll download your code, and test it troughtfully....
wait a little time...
Added after 28 minutes:
ok, it's done (at least for a 0 - 5v value!)
I changed the Yc variable definition (from signed char to unsigned int)
modified ADC configuration
mostly to get an 0-1023 16-bit ansnwer on Yc
so it's scaled to 0.0 to 5.0 on conversion.
added a temporal variable to check conversion value on isis, forgot to get rid of it... do it!
have fun!