#include <p24HJ128GP502.h>
#include <stdio.h>
#include <stdlib.h>
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 delay(void);
void beep(int);
#define E LATAbits.LATA4
#define RS LATAbits.LATA0
#define RW LATAbits.LATA1
#define busyflag PORTBbits.RB12
#define RW_TrisBit TRISAbits.TRISA1
#define D7_TrisBit TRISBbits.TRISB12
#define piezo LATBbits.LATB8
char sentence1[] = "pic24h ";
char sentence2[] = "bbbbb ";
char sentence3[] = "aaaa";
char sentence4[] = "dfsdfsfsdsd ";
int main(void)
{
ADPCFG = 0xffff; //all digital
TRISA = 0; //PORTs all outputs
TRISB = 0;
RW = 0; //set R/W low
E = 0; //set E low
lcd_busy(); //wait for LCD to settle
lcd_init();
while(1)
{
lcd_line1();
lcd_string(sentence1); //send string to LCD
lcd_line2();
lcd_string(sentence2);
delay();
lcd_line1();
lcd_string(sentence3); //send string to LCD
lcd_line2();
lcd_string(sentence4);
beep(80);
delay();
}
return 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 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
lcd_busy();
PORTB = PORTB << 5; //shift over to output high 4 bits on RB9,10,11,12
RS = 0; //RS low
e_togg(); //latch the data
PORTB = PORTB << 4; //shift over to output low 4 bits
RS = 0; //RS low
e_togg(); //latch it
}
void lcd_char(char letter)
{
LATB = letter; //put char in PORTB
lcd_busy();
PORTB = PORTB << 5; //shift over to output high 4 bits on RB9,10,11,12
RS = 1; //RS high
e_togg(); //latch the data
PORTB = PORTB << 4; //shift over to output low 4 bits
RS = 1; //RS high
e_togg(); //latch it
}
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 delay(void)
{
int x,y;
for(x=0;x<50;x++)
{
for(y=0;y<10000;y++){}
}
}