thannara123
Advanced Member level 5
- Joined
- Jan 7, 2010
- Messages
- 1,602
- Helped
- 122
- Reputation
- 244
- Reaction score
- 116
- Trophy points
- 1,353
- Activity points
- 10,626
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
but how t send data as 4bit to port PD3,PD4,PD5,and PD6 . How to send the specified port (PD3,PD4,PD5,andPD6). I am new to in AVR .can i get an example ?.
//#define F_CPU 12000000UL
#include<avr/io.h>
#include<util/delay.h>
#include<inttypes.h>
#define rs PA0
#define rw PA1
#define en PA2
void lcd_init();
void dis_cmd(char);
void dis_data(char);
void lcdcmd(char);
void lcddata(char);
int main(void)
{
unsigned char data0[11]="ENGINEERS";
unsigned char data1[10]="GARAGE";
int i=0;
DDRA=0xFF;
lcd_init();
while(data0[i]!='\0')
{
dis_data(data0[i]);
_delay_ms(200);
i++;
}
dis_cmd(0xC5);
i=0;
while(data1[i]!='\0')
{
dis_data(data1[i]);
_delay_ms(200);
i++;
}
while(1);
}
void lcd_init() // fuction for intialize
{
dis_cmd(0x02); // to initialize LCD in 4-bit mode.
dis_cmd(0x28); //to initialize LCD in 2 lines, 5X7 dots and 4bit mode.
dis_cmd(0x0C);
dis_cmd(0x06);
dis_cmd(0x83);
}
void dis_cmd(char cmd_value)
{
char cmd_value1;
cmd_value1 = cmd_value & 0xF0; //mask lower nibble because PA4-PA7 pins are used.
lcdcmd(cmd_value1); // send to LCD
cmd_value1 = ((cmd_value<<4) & 0xF0); //shift 4-bit and mask
lcdcmd(cmd_value1); // send to LCD
}
void dis_data(char data_value)
{
char data_value1;
data_value1=data_value&0xF0;
lcddata(data_value1);
data_value1=((data_value<<4)&0xF0);
lcddata(data_value1);
}
void lcdcmd(char cmdout)
{
PORTA=cmdout;
PORTA&=~(1<<rs);
PORTA&=~(1<<rw);
PORTA|=(1<<en);
_delay_ms(1);
PORTA&=~(1<<en);
}
void lcddata(char dataout)
{
PORTA=dataout;
PORTA|=(1<<rs);
PORTA&=~(1<<rw);
PORTA|=(1<<en);
_delay_ms(1);
PORTA&=~(1<<en);
}
// Control 16 character LCD (2x8 chars) with 4 bit interface
// Copyright (C) 2012 Joonas Pihlajamaa. Released to public domain.
// No warranties, use at your own responsibility.
#include <avr/io.h>
#define F_CPU 12000000UL // 12 MHz
#include <util/delay.h>
#define DATA_PORT_DIR DDRB
#define DATA_PORT PORTB
#define DATA_PORT_IN PINB
#define RW_PIN (1<<PD4)
#define RS_PIN (1<<PD5)
#define EN_PIN (1<<PD6)
#define SET_CTRL_BIT(pin) (PORTD |= pin)
#define CLEAR_CTRL_BIT(pin) (PORTD &= ~pin)
// assumes EN_PIN is LOW in the beginning
void lcd_write_nibble(char rs, unsigned char data) {
if(DATA_PORT_DIR != 0xF)
DATA_PORT_DIR = 0xF;
CLEAR_CTRL_BIT(RW_PIN);
if(rs)
SET_CTRL_BIT(RS_PIN);
else
CLEAR_CTRL_BIT(RS_PIN);
DATA_PORT = data;
_delay_us(2);
SET_CTRL_BIT(EN_PIN);
_delay_us(2);
CLEAR_CTRL_BIT(EN_PIN);
}
void lcd_write_byte(char rs, unsigned char data) {
lcd_write_nibble(rs, data >> 4);
lcd_write_nibble(rs, data & 0xF);
}
unsigned char lcd_read_nibble(char rs) {
unsigned char data;
if(DATA_PORT_DIR != 0)
DATA_PORT_DIR = 0;
SET_CTRL_BIT(RW_PIN);
if(rs)
SET_CTRL_BIT(RS_PIN);
else
CLEAR_CTRL_BIT(RS_PIN);
_delay_us(2);
SET_CTRL_BIT(EN_PIN);
_delay_us(2);
data = DATA_PORT_IN & 0xF;
CLEAR_CTRL_BIT(EN_PIN);
return data;
}
unsigned char lcd_read_byte(char rs) {
unsigned char data;
data = lcd_read_nibble(rs) << 4;
data += lcd_read_nibble(rs) & 0xF;
return data;
}
void lcd_wait() {
while(lcd_read_byte(0) & 0x80); // wait until display is ready
}
void lcd_init() {
_delay_ms(50); // wait for VDD to rise
lcd_write_nibble(0, 0x3);
_delay_ms(5);
lcd_write_nibble(0, 0x3);
_delay_ms(1);
lcd_write_nibble(0, 0x2); // 4-bit mode
_delay_ms(1);
lcd_write_byte(0, 0x28); // 2 lines, normal font
_delay_ms(1);
lcd_write_byte(0, 0xC); // display on
_delay_ms(1);
lcd_write_byte(0, 1); // display clear
_delay_ms(1);
lcd_write_byte(0, 0x6); // increment, don't shift
_delay_ms(1);
}
void lcd_puts(char * string) {
char i;
lcd_write_byte(0, 0x80); // move to 1st line
lcd_wait();
for(i=0; i<8; i++) {
if(string[i] == '\0')
return;
lcd_write_byte(1, string[i]);
lcd_wait();
}
lcd_write_byte(0, 0x80+0x40); // move to 2nd line
lcd_wait();
for(i=8; i<16; i++) {
if(string[i] == '\0')
return;
lcd_write_byte(1, string[i]);
lcd_wait();
}
}
int main(void) {
unsigned char i = 0;
char message[] = "nn Mississippi..";
DDRD = RS_PIN + EN_PIN + RW_PIN; // Control outputs
DDRB = 0xF; // PB0..PB3 as DB4..DB7
lcd_init();
lcd_puts("Hello, 4-bit LCD!");
_delay_ms(2000);
while(1) {
if(++i >= 100)
i = 1;
if(i >= 10)
message[0] = i/10+'0';
else
message[0] = ' ';
message[1] = i%10+'0';
lcd_puts(message);
_delay_ms(1000);
}
return 1;
}
//*****************************************************************************
//
// File Name : 'lcd_lib.h'
// Title : 4 bit LCd interface header file
// Author : Scienceprog.com - Copyright (C) 2007
// Created : 2007-06-18
// Revised : 2007-06-18
// Version : 1.0
// Target MCU : Atmel AVR series
//
// This code is distributed under the GNU Public License
// which can be found at [url]https://www.gnu.org/licenses/gpl.txt[/url]
//
//*****************************************************************************
#ifndef LCD_LIB
#define LCD_LIB
#include <inttypes.h>
#define LCD_RS 0 //define MCU pin connected to LCD RS
#define LCD_RW 1 //define MCU pin connected to LCD R/W
#define LCD_E 2 //define MCU pin connected to LCD E
#define LCD_D4 4 //define MCU pin connected to LCD D3
#define LCD_D5 5 //define MCU pin connected to LCD D4
#define LCD_D6 6 //define MCU pin connected to LCD D5
#define LCD_D7 7 //define MCU pin connected to LCD D6
#define LDP PORTD //define MCU port connected to LCD data pins
#define LCP PORTD //define MCU port connected to LCD control pins
#define LDDR DDRD //define MCU direction register for port connected to LCD data pins
#define LCDR DDRD //define MCU direction register for port connected to LCD control pins
#define LCD_CLR 0 //DB0: clear display
#define LCD_HOME 1 //DB1: return to home position
#define LCD_ENTRY_MODE 2 //DB2: set entry mode
#define LCD_ENTRY_INC 1 //DB1: increment
#define LCD_ENTRY_SHIFT 0 //DB2: shift
#define LCD_ON_CTRL 3 //DB3: turn lcd/cursor on
#define LCD_ON_DISPLAY 2 //DB2: turn display on
#define LCD_ON_CURSOR 1 //DB1: turn cursor on
#define LCD_ON_BLINK 0 //DB0: blinking cursor
#define LCD_MOVE 4 //DB4: move cursor/display
#define LCD_MOVE_DISP 3 //DB3: move display (0-> move cursor)
#define LCD_MOVE_RIGHT 2 //DB2: move right (0-> left)
#define LCD_FUNCTION 5 //DB5: function set
#define LCD_FUNCTION_8BIT 4 //DB4: set 8BIT mode (0->4BIT mode)
#define LCD_FUNCTION_2LINES 3 //DB3: two lines (0->one line)
#define LCD_FUNCTION_10DOTS 2 //DB2: 5x10 font (0->5x7 font)
#define LCD_CGRAM 6 //DB6: set CG RAM address
#define LCD_DDRAM 7 //DB7: set DD RAM address
// reading:
#define LCD_BUSY 7 //DB7: LCD is busy
#define LCD_LINES 2 //visible lines
#define LCD_LINE_LENGTH 16 //line length (in characters)
// cursor position to DDRAM mapping
#define LCD_LINE0_DDRAMADDR 0x00
#define LCD_LINE1_DDRAMADDR 0x40
#define LCD_LINE2_DDRAMADDR 0x14
#define LCD_LINE3_DDRAMADDR 0x54
void LCDsendChar(uint8_t); //forms data ready to send to 74HC164
void LCDsendCommand(uint8_t); //forms data ready to send to 74HC164
void LCDinit(void); //Initializes LCD
void LCDclr(void); //Clears LCD
void LCDhome(void); //LCD cursor home
void LCDstring(uint8_t*, uint8_t); //Outputs string to LCD
void LCDGotoXY(uint8_t, uint8_t); //Cursor to X Y position
void CopyStringtoLCD(const uint8_t*, uint8_t, uint8_t);//copies flash string to LCD at x,y
void LCDdefinechar(const uint8_t *,uint8_t);//write char to LCD CGRAM
void LCDshiftRight(uint8_t); //shift by n characters Right
void LCDshiftLeft(uint8_t); //shift by n characters Left
void LCDcursorOn(void); //Underline cursor ON
void LCDcursorOnBlink(void); //Underline blinking cursor ON
void LCDcursorOFF(void); //Cursor OFF
void LCDblank(void); //LCD blank but not cleared
void LCDvisible(void); //LCD visible
void LCDcursorLeft(uint8_t); //Shift cursor left by n
void LCDcursorRight(uint8_t); //shif cursor right by n
#endif