luanktd
Newbie level 5
- Joined
- Apr 13, 2012
- Messages
- 9
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,328
This is my code. I try many times but the Program not run.
Can you help me fix it?
Thanks.
Can you help me fix it?
Thanks.
Code:
#include <htc.h>
#include "delay.h"
__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & MCLRE_ON & CP_OFF & BOREN_OFF
& IESO_OFF & FCMEN_OFF & LVP_OFF & DEBUG_OFF); //1st config. Word
__CONFIG(BOR4V_BOR21V); //2st config.
#define _XTAL_FREQ 8000000
#define LCD_EN RB5 //EN line
#define LCD_RS RB4 //R/S line
#define LCD_D7 RB3
#define LCD_D6 RB2
#define LCD_D5 RB1
#define LCD_D4 RB0
//data pins for 4bit mode
#define LCD_STROBE() ((LCD_EN = 1),(LCD_EN=0))
void lcd_write(unsigned char c)
{
__delay_us(40);
//MS nibble
LCD_D7 = (c & 0x80)?1:0;
c<<=1;
LCD_D6 = (c & 0x80)?1:0;
c<<=1;
LCD_D5 = (c & 0x80)?1:0;
c<<=1;
LCD_D4 = (c & 0x80)?1:0;
c<<=1;
LCD_STROBE();
//LS nibble
LCD_D7 = (c & 0x80)?1:0;
c<<=1;
LCD_D6 = (c & 0x80)?1:0;
c<<=1;
LCD_D5 = (c & 0x80)?1:0;
c<<=1;
LCD_D4 = (c & 0x80)?1:0;
LCD_STROBE();
}
/*
* Clear and home the LCD
*/
void lcd_clear(void)
{
LCD_RS = 0;
lcd_write(0x1);
__delay_ms(2);
}
/*
* Go to the specified position
*/
void lcd_goto(unsigned char pos)
{
LCD_RS = 0;
lcd_write(0x80+pos);
}
/* initialise the LCD - put into 4 bit mode */
void lcd_init()
{
LCD_RS = 0;
LCD_EN = 0;
__delay_ms(15); // wait 15mSec after power applied,
LCD_D4 = 1;//0x3 & 0x01; //bit0 000X
LCD_D5 = 1;//(0x3>>1) & 0x01; //bit1 00XY -> 000X
LCD_D6 = 0;//(0x3>>2) & 0x01; //bit2 0XYZ -> 000X
LCD_D7 = 0;//(0x3>>3) & 0x01; //bit3 XYZW -> 000X
LCD_STROBE();
__delay_ms(5);
LCD_STROBE();
__delay_us(200);
LCD_STROBE();
__delay_us(200);
// Four bit mode
LCD_D4 = 0;//2 & 0x01; //bit0 000X
LCD_D5 = 1;//(2>>1) & 0x01; //bit1 00XY -> 000X
LCD_D6 = 0;//(2>>2) & 0x01; //bit2 0XYZ -> 000X
LCD_D7 = 0;//(2>>3) & 0x01; //bit3 XYZW -> 000X
LCD_STROBE();
lcd_write(0x28); // Set interface length: nibblemode, 2line, 5x7dot
lcd_write(0b00001100); // Display On, Cursor Off, Cursor Blink off
lcd_clear(); // Clear screen
lcd_write(0x6); // Set entry Mode : increment, displayShiftOff
}
/* write one character to the LCD */
void lcd_putch(char c)
{
LCD_RS = 1; // write characters
lcd_write( c );
}
/* write a string of chars to the LCD */
void lcd_puts(const char * s)
{
LCD_RS = 1; // write characters
while(*s)
lcd_write(*s++);
}
void lcd_unum(unsigned int num){//sign-0 +ve sign-1 -ve
lcd_puts(" ");
lcd_putch(num%10+'0');
num/=10;
for(unsigned char i=0;i<4;i++){
//lcd_goto(i);
LCD_RS = 0;
lcd_write(0x10);
lcd_write(0x10);//shift cursor one step back without writing nothing
if(num>0)
lcd_putch(num%10+'0');
else
break;
num/=10;
}
}
void main()
{
TRISB=0;//output port for lcd.
lcd_init();
lcd_clear();
lcd_puts("4 bit operation!");
__delay_ms(20); //WAIT FOR TWO SECONDS TO SYNCRONIZE THE PICS
while(1);
}