futz
Active Member
Well engkhlaif, it's not 877A code but it works now. Took me a long time to find a foolish programming error that was messing up the LCD display of the result.
"Final" working 30F4013 code for continuously reading a Sharp GP2D12 and displaying the result on an LCD:
"Final" working 30F4013 code for continuously reading a Sharp GP2D12 and displaying the result on an LCD:
Code:
#include "p30f4013.h"
#include <stdlib.h>
_FOSC(FRC) //osc
_FWDT(WDT_OFF)
_FBORPOR(MCLR_EN & PWRT_OFF) //MCLR enable & power-up timers off
int readADC(void);
void lcd_line1(void); //function prototypes
void lcd_line2(void);
void lcd_cmd(unsigned char);
void lcd_char(char);
void e_togg(void);
void lcd_init(void);
void lcd_string(char *);
void lcd_busy(void);
void lcd_16number(int);
void beep(int);
void delay(void);
#define E LATDbits.LATD9
#define RS LATAbits.LATA11
#define RW LATCbits.LATC14
#define busyflag PORTBbits.RB12
#define RW_TrisBit TRISCbits.TRISC14
#define D7_TrisBit TRISBbits.TRISB12
#define piezo LATCbits.LATC13
char number[] = " ";
int main(void)
{
int a;
TRISA = TRISB = TRISC = TRISD = 0x0000;
ADPCFG = 0xffff; //all digital
ADCON1 = 0x00E0; // auto convert after end of sampling
//0000 0000 1110 0000
ADCSSL = 0; // no scanning required
ADCON3 = 0x1F3f; // max sample time = 31Tad, Tad = 2 x Tcy = 125ns >75ns
//0001 1111 0011 1111
ADCON2 = 0; // use MUXA, AVss and AVdd are used as Vref+/-
ADCHS = 0; // select analog input channel
ADCON1bits.ADON = 1; // turn on the ADC
RW = 0; //set R/W low
E = 0; //set E low
lcd_busy(); //wait for LCD to settle
lcd_init();
while(1)
{
TRISB = 0x0001;
ADPCFG = 0xfffe; //RB0 = analog
a = readADC();
ADPCFG = 0xffff; //all digital
TRISB = 0x0000;
lcd_line1();
lcd_16number(a);
lcd_string(number);
delay();
}
return 0;
}
void lcd_16number(int num)
{
number[0]=(char)(abs(num/10000)+0x30);
num=num-abs(num/10000)*10000;
number[1]=(char)(abs(num/1000)+0x30);
num=num-abs(num/1000)*1000;
number[2]=(char)(abs(num/100)+0x30);
num=num-abs(num/100)*100;
number[3]=(char)(abs(num/10)+0x30);
num=num-abs(num/10)*10;
number[4]=(char)(num+0x30);
}
int readADC(void)
{
ADCON1bits.SAMP = 1; // start sampling, automatic conversion will follow
delay();
while (!ADCON1bits.DONE); // wait to complete the conversion
return ADCBUF0; // read the conversion result
}
void lcd_string(char *senpoint)
{
while(*senpoint != '\0')
{
lcd_char(*senpoint);
senpoint++;
}
}
void lcd_busy(void)
{
RW_TrisBit = 1; //make R/W input (read)
D7_TrisBit = 1; //make D7 input
RS = 0; //set RS low
RW = 1; //set R/W high
E = 1; //set E high
while(busyflag); //wait for busy flag to go low
E = 0; //set E low
RW = 0; //set R/W low
TRISB = 0; //make D7 output
RW_TrisBit = 0; //make R/W output (write)
}
void lcd_line1(void)
{
lcd_cmd(0x80);
}
void lcd_line2(void)
{
lcd_cmd(0xc0);
}
void lcd_cmd(unsigned char letter)
{
LATB = letter; //put char in PORTB
asm("nop");
PORTB = PORTB << 5; //shift over to output high 4 bits on RB9,10,11,12
RS = 0; //RS low
e_togg(); //latch the data
lcd_busy();
LATB = letter; //put char in PORTB
asm("nop");
PORTB = PORTB << 9; //shift over to output low 4 bits
RS = 0; //RS low
e_togg(); //latch it
lcd_busy();
}
void lcd_char(char letter)
{
LATB = letter; //put char in PORTB
asm("nop");
PORTB = PORTB << 5; //shift over to output high 4 bits on RB9,10,11,12
RS = 1; //RS high
e_togg(); //latch the data
lcd_busy();
LATB = letter; //put char in PORTB
asm("nop");
PORTB = PORTB << 9; //shift over to output low 4 bits
RS = 1; //RS high
e_togg(); //latch it
lcd_busy();
}
void lcd_init(void)
{
LATB = 0x0600; //send 3
e_togg();
lcd_busy();
LATB = 0x0600;
e_togg();
lcd_busy();
LATB = 0x0600;
e_togg();
lcd_busy();
LATB = 0x0400; //send 2 - set 4-bit mode
e_togg();
lcd_busy();
lcd_cmd(0x28); //set 4-bit mode and 2 lines
lcd_busy();
lcd_cmd(0x10); //cursor move & shift left
lcd_busy();
lcd_cmd(0x06); //entry mode = increment
lcd_busy();
lcd_cmd(0x0d); //display on - cursor blink on
lcd_busy();
lcd_cmd(0x01); //clear display
lcd_busy();
}
void e_togg(void)
{
E=1;
E=0;
}
void beep(int tone)
{
int x,y;
for(x=0;x<80;x++)
{
piezo = 1;
for(y=0;y<tone;y++){}
piezo = 0;
for(y=0;y<tone;y++){}
}
}
void delay(void)
{
int x,y;
for(x=0;x<10;x++)
{
for(y=0;y<10000;y++){}
}
}
Last edited: