using pic18f4520 and mplab c18 compiler for the below program
1) the program stops working within a minute(stack overflow?)
2) the first character of last line in 20x4 lcd keeps blinking
3) code works in 16x2 lcd but on a 20x4 lcd displays output of its choice and cannot be controlled
initialization of lcd and adc is fine
pls help me out,point out the error
1) the program stops working within a minute(stack overflow?)
2) the first character of last line in 20x4 lcd keeps blinking
3) code works in 16x2 lcd but on a 20x4 lcd displays output of its choice and cannot be controlled
initialization of lcd and adc is fine
pls help me out,point out the error
Rich (BB code):
#include <p18f4520.h>
#include <adc.h>
#include <stdlib.h>
#include <delays.h>
#include <stdio.h>
#define lcdport LATB
#define E LATDbits.LATD7
#define RS LATDbits.LATD6
#define p_bar PORTAbits.RA1
#define wh_l PORTCbits.RC0
#define wh_ul PORTCbits.RC1
#define sph_l PORTCbits.RC2
#define sph_ul PORTCbits.RC3
#define sp_on PORTDbits.RD0
#define sp_off PORTDbits.RD1
#define _XTAL_FREQ 20000000
#pragma config WDT=OFF,LVP=OFF,STVREN=OFF,MCLRE=OFF
unsigned int adc_out=0;
int reg1,reg2,reg3;
float adc_rpm;
unsigned char convolt[5];
unsigned char anim[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char anim0[8]={0x19,0x19,0x19,0x19,0x19,0x19,0x19,0x19};
unsigned char anim1[8]={0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E};
unsigned char anim2[8]={0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F};
unsigned char anim3[8]={0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F};
unsigned char anim4[8]={0x13,0x13,0x13,0x13,0x13,0x13,0x13,0x13};
void lcdbuild(unsigned char,unsigned char *);
void lcdmsg(const rom char *);
void lcdstr(unsigned char *);
void lcdcomm(unsigned char);
void lcddata(unsigned char);
void lcdout(unsigned char);
void dispchar(void);
void animate(void);
void animate1(void);
void animate2(void);
void animate3(void);
void animate4(void);
void lcdinit(void);
void clear(void);
void blank(void);
void blink(void);
void blink_1(void);
void blink_2(void);
void blink_3(void);
void loop(void);
void ucall(void);
void pcall(void);
void scall(void);
void adc(void);
void pressure(void);
void workhead(void);
void sphere(void);
void spindle(void);
void main (void)
{
TRISA = 0x0F;
TRISB = 0x00;
TRISC = 0xFF;
TRISD = 0x0F;
lcdinit();
dispchar();
animate();
lcdcomm(0x86);
lcdmsg("MAXPRECI");
animate1();
animate2();
animate3();
animate4();
animate3();
animate2();
animate1();
animate();
Delay10KTCYx(1000);
Delay10KTCYx(1000);
lcdcomm(0xCD);
lcdmsg("RPM:");
lcdcomm(0x94);
lcdmsg("WH:");
lcdcomm(0x9E);
lcdmsg("SPH:");
OpenADC(ADC_FOSC_32 &
ADC_RIGHT_JUST &
ADC_4_TAD,ADC_CH0 &
ADC_INT_OFF &
ADC_VREFPLUS_VDD &
ADC_VREFMINUS_VSS, 7);
while(1)
{
loop();
}
CloseADC();
}
void loop(void)
{
adc();
// pressure();
if(wh_l==1 | wh_ul==1)
workhead();
else if(sph_l==1 | sph_ul==1)
sphere();
else if(sp_on==1 | sp_off==1)
spindle();
}
void pressure(void)
{
if(p_bar==1)
{
reg3=0;
clear();
lcdcomm(0xC0);
lcdmsg("Pressure OK");
}
if(p_bar==0)
{
reg3=1;
blank();
lcdcomm(0xD4);
lcdmsg("Pressure Below 6BAR");
pcall();
}
}
void workhead(void)
{
if(wh_l==1)
{
reg1=0;
lcdcomm(0x97);
lcdmsg("Lock");
}
if(wh_ul==1)
{
reg1=1;
ucall();
}
}
void sphere(void)
{
if(sph_l==1)
{
reg2=0;
lcdcomm(0xA2);
lcdmsg("Lock");
}
if(sph_ul==1)
{
reg2=1;
ucall();
}
}
void spindle(void)
{
if(sp_on==1)
{
scall();
}
if(sp_off==1)
{
blank();
lcdcomm(0xD4);
lcdmsg("Spindle:OFF");
}
}
void ucall(void)
{
while(reg1==1 & reg2!=1)
{
lcdcomm(0x97);
lcdmsg("UnLock");
blink_1();
loop();
}
while(reg2==1 & reg1!=1)
{
lcdcomm(0xA2);
lcdmsg("UnLock");
blink_2();
loop();
}
while(reg1==1 & reg2==1)
{
lcdcomm(0x97);
lcdmsg("UnLock");
lcdcomm(0xA2);
lcdmsg("UnLock");
blink_3();
loop();
}
}
void pcall(void)
{
while(reg3==1)
{
lcdcomm(0xC0);
lcdmsg("Low Pressure");
blink();
pressure();
}
}
void scall(void)
{
if(reg1==0 & reg2==0)
{
blank();
lcdcomm(0xD4);
lcdmsg("Spindle:ON");
}
if(reg1==1 | reg2==1)
{
blank();
lcdcomm(0xD4);
lcdmsg("Error WH/SPH UnLock");
}
}
void adc(void)
{
SetChanADC (ADC_CH0);
Delay10KTCYx(10);
lcdcomm(0xD1);
lcdmsg(" ");
ConvertADC();
while(BusyADC());
adc_out=ReadADC();
adc_rpm=(adc_out*500.0)/1024;
itoa(adc_rpm,convolt);
lcdcomm(0xD1);
lcdstr(convolt);
Delay10KTCYx(1000);
}
void dispchar(void)
{
lcdbuild(0,anim);
lcdbuild(1,anim1);
lcdbuild(2,anim2);
lcdbuild(3,anim3);
lcdbuild(4,anim4);
lcdbuild(5,anim0);
}
void lcdbuild(unsigned char location, unsigned char *ptr)
{
unsigned char i;
if(location<8)
{
lcdcomm(0x40+(location*8));
for(i=0;i<8;i++)
{
lcddata(ptr);
}
}
}
void lcdmsg(const rom char *Message)
{
const rom char *Pos = Message;
while(*Pos!=0)
lcddata(*Pos++);
}
void lcdstr(unsigned char *str)
{
int i=0;
while(str!='\0')
{
lcddata(str);
i++;
}
}
void lcdcomm(unsigned char i)
{
RS =0;
lcdout(i);
Delay1KTCYx(2);
}
void lcddata(unsigned char i)
{
RS =1;
lcdout(i);
Delay1KTCYx(2);
}
void lcdout(unsigned char dat)
{
lcdport = dat;
E = 1;
Delay1TCY();
E = 0;
}
void lcdinit(void)
{
lcdport=0x00;
Delay1KTCYx(40);
lcdout(0x30);
Delay1KTCYx(15);
lcdout(0x20);
Delay1KTCYx(15);
lcdcomm(0x38);
lcdcomm(0x0C);
lcdcomm(0x01);
lcdcomm(0x06);
lcdcomm(0x80);
}
void blank(void)
{
lcdcomm(0xD4);
lcdmsg(" ");
}
void clear(void)
{
lcdcomm(0xC0);
lcdmsg(" ");
}
void blink(void)
{
Delay10KTCYx(200);
lcdcomm(0xC0);
lcdmsg(" ");
Delay10KTCYx(100);
}
void blink_1(void)
{
Delay10KTCYx(200);
lcdcomm(0x97);
lcdmsg(" ");
Delay10KTCYx(100);
}
void blink_2(void)
{
Delay10KTCYx(200);
lcdcomm(0xA8);
lcdmsg(" ");
Delay10KTCYx(100);
}
void blink_3(void)
{
Delay10KTCYx(200);
lcdcomm(0x97);
lcdmsg(" ");
lcdcomm(0xA8);
lcdmsg(" ");
Delay10KTCYx(100);
}
void animate(void)
{
lcdcomm(0x80);
lcdmsg(" ");
lcdcomm(0x82);
lcddata(3);
lcdcomm(0x83);
lcddata(2);
lcdcomm(0x84);
lcddata(1);
lcdcomm(0x85);
lcddata(5);
lcdcomm(0x8E);
lcddata(4);
lcdcomm(0x8F);
lcddata(1);
lcdcomm(0x90);
lcddata(2);
lcdcomm(0x91);
lcddata(3);
lcdcomm(0x92);
lcdmsg(" ");
Delay10KTCYx(100);
}
void animate1(void)
{
lcdcomm(0x80);
lcdmsg(" ");
lcdcomm(0x83);
lcddata(2);
lcdcomm(0x84);
lcddata(1);
lcdcomm(0x85);
lcddata(5);
lcdcomm(0x8E);
lcddata(4);
lcdcomm(0x8F);
lcddata(1);
lcdcomm(0x90);
lcddata(2);
lcdcomm(0x91);
lcdmsg(" ");
Delay10KTCYx(100);
}
void animate2(void)
{
lcdcomm(0x80);
lcdmsg(" ");
lcdcomm(0x84);
lcddata(1);
lcdcomm(0x85);
lcddata(5);
lcdcomm(0x8E);
lcddata(4);
lcdcomm(0x8F);
lcddata(1);
lcdcomm(0x90);
lcdmsg(" ");
Delay10KTCYx(100);
}
void animate3(void)
{
lcdcomm(0x80);
lcdmsg(" ");
lcdcomm(0x85);
lcddata(5);
lcdcomm(0x8E);
lcddata(4);
lcdcomm(0x8F);
lcdmsg(" ");
Delay10KTCYx(100);
}
void animate4(void)
{
lcdcomm(0x80);
lcdmsg(" ");
lcdcomm(0x8E);
lcdmsg(" ");
Delay10KTCYx(100);
}
Attachments
-
42.5 KB Views: 26
Last edited by a moderator: