richie244
Newbie level 2
pic18f252
I am currently doing a project using HD44780 20x4 LCD display and PIC18F252 to produce a simplistic Battery monitor. At the moment all I wish to achieve is taking a voltage from a shunt amplified to around 3v and the voltage from the battery reduced to 3v and display these on the LCD display. obviously the voltage acros the shunt is proportional to the Current so I wish to display this as a current on the screen.
The hard ware andtheory side of this is simple for me where I fall over is the programming as I have no experience in this field I have written (comme across) the following I think the initial part is ok but I am really strugling with the LCD Display.
I am currently doing a project using HD44780 20x4 LCD display and PIC18F252 to produce a simplistic Battery monitor. At the moment all I wish to achieve is taking a voltage from a shunt amplified to around 3v and the voltage from the battery reduced to 3v and display these on the LCD display. obviously the voltage acros the shunt is proportional to the Current so I wish to display this as a current on the screen.
The hard ware andtheory side of this is simple for me where I fall over is the programming as I have no experience in this field I have written (comme across) the following I think the initial part is ok but I am really strugling with the LCD Display.
Code:
#include <p18f252.h>
#include <stdlib.h>
#include <string.h>
#include <delays.h>
void Initial()
{
TRISA = 0xFF; // port A, all bits, inputs
TRISB = 0x00;
ADCON1 = 0b10000000; //AD port configuration
//AAAAAAAA VDD VSS
ADCON0bits.ADCS1=1;
ADCON0bits.ADCS0=0; // ADC clock = Fsoc/32
ADCON0bits.CHS2 =0;
ADCON0bits.CHS1 =0;
}
unsigned char readADC(int ch){
unsigned char adcValue;
if(ch == 0) ADCON0bits.CHS0 =0; // select channel 0
else ADCON0bits.CHS0 =1;
ADCON0bits.ADON =1; // turn on ADC
ADCON0bits.GO_DONE = 1; // start conversion
while(ADCON0bits.GO_DONE) // wait for conversion end
;
adcValue = ADRESH;
return adcValue;
}
void LcdEnable();
/*****************************************
* Display
/*****************************************/
#define lcd_rs PORTCbits.RC2
#define lcd_rw PORTCbits.RC3
#define lcd_e PORTCbits.RC4
#define lcd_cmd_wri 0x00
#define lcd_data_wri 0x01
#define lcd_busy_rd 0x02
#define lcd_set_function 0x3C //00111100 8 bit interface, 4 line mode, 5x11 dot format
#define lcd_set_visible 0x0F //Display on, cursor underline on, cursor blink on
#define lcd_set_shift 0x16 //Cursor move, Right shift
#define lcd_set_mode 0x06 //Increment, display shift off
#define lcd_set_cgaddr 0x40
#define lcd_set_ddaddr 0x80
#define lcd_clr 0x01
#define lcd_init 0x30
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int i,j;
const int Twentyms = 640000;
const int Tenms = 320000;
const int Onetwofivems = 4000000;
const int Twohundredus = 6400;
const int Onehundredus = 3200;
const int Sixtyfourms = 2048000;
void WrCmd2Lcd(char cmd);
void WrDat2Lcd(char data);
void InitLcd();
void Speech(char words,int position);
void LcdWait();
void LcdPutPar(int par);
void LcdEnable()
{
lcd_e=1;
j=Tenms;
for (i = 0; i < j; i++); //delay for 10ms
lcd_e=0;
}
void WrCmd2Lcd(char cmd)
{
TRISB = 0x00;
lcd_rw=0;
lcd_rs=1;
PORTB=cmd;
LcdEnable();
}
void WrDat2Lcd(char data)
{
TRISB =0x00;
lcd_rw=0;
lcd_rs=0;
PORTB = data;
LcdEnable();
}
void InitLcd()
{
lcd_e=0;
j=Onetwofivems;
for (i = 0; i < j; i++); //delay for 125 ms
WrCmd2Lcd(lcd_init);
j=Twentyms;
for (i = 0; i < j; i++); //delay for 20ms
LcdEnable();
j=Twohundredus;
for (i = 0; i < j; i++); //delay for 200us
LcdEnable();
j=Onehundredus;
for (i = 0; i < j; i++); //delay for 100us
WrCmd2Lcd(lcd_set_function);
j=Tenms;
for (i = 0; i < j; i++); //delay for 10ms
WrCmd2Lcd(lcd_set_visible);
j=Tenms;
for (i = 0; i < j; i++); //delay for 10ms
WrCmd2Lcd(lcd_clr);
j=Onehundredus;
for (i = 0; i < j; i++); //delay for 100us
WrCmd2Lcd(lcd_set_mode);
j=Tenms;
for (i = 0; i < j; i++); //delay for 10ms
WrCmd2Lcd(lcd_set_ddaddr);
j=Onehundredus;
for (i = 0; i < j; i++); //delay for 100us
}
void LcdWait()
{
int status;
TRISB=0xFF;
TRISB = lcd_busy_rd;
do
{
lcd_e=1;
status = ADCON1;
lcd_e=0;
}
while(status & 0x80); // test busy flag.
}
void Speech(char words,int position)
{
char *textptr;
//textptr = words;
WrCmd2Lcd(position);
LcdWait();
j=Sixtyfourms;
for (i = 0; i < j; i++); //delay for 64ms
do
{
WrDat2Lcd(*textptr);
*textptr++;
}
while(*textptr != '\n');
}
void LcdPutPar(int par)
{
TRISBbits.TRISB0 = PORTBbits.RB0,par & 0X01;
TRISBbits.TRISB1 = PORTBbits.RB1,par & 0X02;
TRISBbits.TRISB2 = PORTBbits.RB2,par & 0X04;
TRISBbits.TRISB3 = PORTBbits.RB3,par & 0X08;
TRISBbits.TRISB0 = PORTBbits.RB4,par & 0X10;
TRISBbits.TRISB1 = PORTBbits.RB5,par & 0X20;
TRISBbits.TRISB2 = PORTBbits.RB6,par & 0X40;
TRISBbits.TRISB3 = PORTBbits.RB7,par & 0X80;
}
void main(void) {
unsigned char voltage1, voltage2;
while(1){
// voltage1 = readADC(0);
// voltage2 = readADC(1);
InitLcd();
// WrCmd2Lcd(0X06);
// LcdEnable();
WrDat2Lcd("A");
// LcdWait();
while(1);
}
}