Hi all! Me and my group of friends have a project on hand about converting voltage input into readable data on LCD. We're still on the first phase of the programming. Any help with it would be greatly appreciated. We're using pic18F452 with the picdem 2 plus demo board. The input would be the pot connected via RA0.
I've managed to display 0V and 5V on the lcd when i turn the wiper of the pot CCW or CW completely. Now I'm trying to detect the other voltages when I adjust the wiper but there are 2 problems. Firstly, As you can see from my programming, my constant values are 0x01 and 0x05, which I assume should read analog inputs of lower than 1V and higher than 5V. (Am I right?) It works when I run the program but still would like someone's confirmation on it because I'm not so sure myself. My second problem is designing a subroutine to detect the other voltages. How should I go about it?
Code:
#include <p18xxxx.h>
#include <delays.h>
#include "lcd.h"
#include <string.h>
#define LCD_RS PORTAbits.RA3
#define LCD_EN PORTAbits.RA1
#define LCD_WR PORTAbits.RA2
#define set_dd_line1_pos1 0x80
#define set_dd_line2_pos1 0x80
char value1 [ ] = "0V";
char value2 [ ] = "5V";
void lcd_write_cmd(unsigned char cmd)
{
unsigned char temp2;
LCD_RS = 0;
Delay10TCYx(4);
temp2 = cmd;
temp2 = temp2 >> 4;
PORTD = temp2 & 0x0F;
Delay1KTCYx(1);
lcd_strobe();
Delay1KTCYx(1);
temp2 = cmd;
PORTD = temp2 & 0x0F;
Delay1KTCYx(1);
lcd_strobe();
Delay1KTCYx(1);
}
void lcd_clear(void)
{
lcd_write_cmd(0x01);
Delay1KTCYx(2);
}
void lcd_write_data(char data)
{
char temp1;
LCD_RS = 1;
Delay10TCYx(4);
temp1 = data;
temp1 = temp1 >> 4;
PORTD = temp1 & 0x0F;
Delay1KTCYx(1);
lcd_strobe();
Delay1KTCYx(1);
temp1 = data;
PORTD = temp1 & 0x0F;
Delay1KTCYx(1);
lcd_strobe();
Delay1KTCYx(1);
}
void lcd_goto(unsigned char LCD_POS)
{
lcd_write_cmd(LCD_POS);
Delay1KTCYx(2);
}
void lcd_strobe(void)
{
LCD_EN = 1;
Delay10TCYx(4);
LCD_EN = 0;
Delay10TCYx(4);
}
void lcd_init(void)
{
LCD_RS = 0;
LCD_WR = 0;
Delay1KTCYx(250);
Delay1KTCYx(250);
Delay1KTCYx(250);
Delay1KTCYx(250);
PORTD = 0x03;
lcd_strobe();
Delay1KTCYx(250);
PORTD = 0x03;
lcd_strobe();
Delay1KTCYx(250);
PORTD = 0x03;
lcd_strobe();
Delay1KTCYx(250);
PORTD = 0x02;
lcd_strobe();
Delay1KTCYx(250);
Delay1KTCYx(250);
lcd_write_cmd(0x28)
lcd_write_cmd(0x01);
Delay1KTCYx(20);
Delay1KTCYx(20);
lcd_write_cmd(0x0F);
Delay1KTCYx(20);
lcd_write_cmd(0x06);
Delay1KTCYx(20);
}
main(void)
{
char outchar;
unsigned char Sizevalue1, Sizevalue2, Sizevalue3;
unsigned char index;
#define constant [B]0x01[/B]
#define constant2 [B]0x05[/B]
TRISA = 0x00;
TRISD = 0x00;
PORTD = 0x00;
ADCON0 = 0b11000001;
ADCON1 = 0b00001110;
for(;;)
{
ADCON0bits.GO = 1;
(PIR1bits.ADIF == 0);
PORTD = ADRESH;
if(ADRESH < constant)
{
Sizevalue1 = strlen(value1); // get size of array
LCD_EN = 0;
LCD_RS = 0;
LCD_WR = 0;
lcd_init();
LCD_RS = 1;
lcd_goto(set_dd_line1_pos1);
for (index = 0; index < Sizevalue1; index++)
{
outchar = value1[index];
lcd_write_data(outchar);
}
Delay1KTCYx(250);
Delay1KTCYx(250);
Delay1KTCYx(250);
lcd_goto(set_dd_line1_pos1);
PIR1bits.ADIF = 0;
lcd_clear();
}
if(ADRESH > constant2)
{
Sizevalue2 = strlen(value2);
LCD_EN = 0;
LCD_RS = 0;
LCD_WR = 0;
lcd_init();
LCD_RS = 1;
lcd_goto(set_dd_line1_pos1);
for (index = 0; index < Sizevalue1; index++)
{
outchar = value2[index];
lcd_write_data(outchar);
}
Delay1KTCYx(250);
Delay1KTCYx(250);
Delay1KTCYx(250);
lcd_goto(set_dd_line1_pos1);
PIR1bits.ADIF = 0;
lcd_clear();
}
}
}
I've managed to display 0V and 5V on the lcd when i turn the wiper of the pot CCW or CW completely. Now I'm trying to detect the other voltages when I adjust the wiper but there are 2 problems. Firstly, As you can see from my programming, my constant values are 0x01 and 0x05, which I assume should read analog inputs of lower than 1V and higher than 5V. (Am I right?) It works when I run the program but still would like someone's confirmation on it because I'm not so sure myself. My second problem is designing a subroutine to detect the other voltages. How should I go about it?
Last edited: