Академический Документы
Профессиональный Документы
Культура Документы
hextobcd:
mov r0,#00h
mov r1,#00h
cjne a,#00h,c1_hextobcd
ret
c1_hextobcd
clr c:
mov b,#100
div ab
mov r0,a
clr c
mov a,b
mov b,#10
div ab
swap a
mov r1,a
of R1
mov a,b
orl a,r1
mov r1,a
R1
ret
delay(50);
en=0;
delay(50);
return;
}
//Function for sending values to the data register of LCD
void display(unsigned char value)
{
P1=value;
P3=0x60;
delay(500);
en=0;
delay(50);
return;
}
//function to initialize the register and pins of LCD
//always use with every lcd of hitachi
void lcdint(void)
{
P1=0x00;
P3=0x00;
delay(15000);display(0x30);delay(4500);display(0x30);delay(300);
display(0x30);delay(650);lcdcmd(0x38);delay(50);lcdcmd(0x0F);
delay(50);lcdcmd(0x01);delay(50);lcdcmd(0x06);delay(50);lcdcmd(0x80);
delay(50);
}
//Main FUNCTION
void main()
{
int i,j,k=0,l=0,s=0;
char u[]={"USMAN ALI BUTT"};
char sec[]={"JUMPING TO SECOND LINE"};
lcdint();
lcdcmd(0x01);
lcdcmd(0x80);
while(u[k]!='\0')
{
display(u[k]);
k++;
}
delay(100000);
lcdcmd(0x01);
//Clear all contents of lcd
lcdcmd(0x80);
//Initialize Cursor to first character matrix of lcd
while(sec[s]!='\0')
{
if(s==15)
lcdcmd(0xC0);
//Initializing Cursor to second line first Character matrix
display(sec[s]);
s++;
}
delay(100000);
lcdcmd(0x01);
lcdcmd(0xC0);
//Initializing Cursor to second line first Character matrix
while(u[l]!='\0')
{
display(u[l]); // printing same string on second line
l++;
}
delay(100000);
}
---------------------------------LCD Code
#include <reg51.h>
sbit DB7 = P1^7;
sbit DB6 = P1^6;
sbit DB5 = P1^5;
sbit DB4 = P1^4;
sbit RS = P1^3;
sbit E = P1^2;
sbit clear = P2^4;
sbit ret = P2^5;
sbit left = P2^6;
sbit right = P2^7;
void returnHome(void);
void entryModeSet(bit id, bit s);
void displayOnOffControl(bit display, bit cursor, bit blinking);
void cursorOrDisplayShift(bit sc, bit rl);
void functionSet(void);
void setDdRamAddress(char address);
void sendChar(char c);
void sendString(char* str);
bit getBit(char c, char bitNumber);
void delay(void);
void main(void) {
functionSet();
entryModeSet(1, 0); // increment and no shift
displayOnOffControl(1, 1, 1); // display on, cursor on and blinking on
sendString("EdSim51 LCD Module Simulation");
setDdRamAddress(0x40); // set address to start of second line
sendString("Based on Hitachi HD44780");
// The program can be controlled via some of the switches on port 2.
// If switch 5 is closed the cursor returns home (address 0).
// Otherwise, switches 6 and 7 are read - if both switches are open or both
switches
//
are closed, the display does not shift.
// If switch 7 is closed, continuously shift left.
// If switch 6 is closed, continuously shift right.
while (1) {
if (ret == 0) {
returnHome();
}
else {
if (left == 0 && right == 1) {
cursorOrDisplayShift(1, 0); // shift display left
}
else if (left == 1 && right == 0) {
cursorOrDisplayShift(1, 1); // shift display right
}
}
}
}
// LCD Module instructions ------------------------------------------// A full explanation of the LCD Module: HD44780.pdf
void returnHome(void) {
RS = 0;
DB7 = 0;
DB6 = 0;
DB5 = 0;
DB4 = 0;
E = 1;
E = 0;
DB5 = 1;
E = 1;
E = 0;
delay();
}
void entryModeSet(bit id, bit s) {
RS = 0;
DB7 = 0;
DB6 = 0;
DB5 = 0;
DB4 = 0;
E = 1;
E = 0;
DB6 = 1;
DB5 = id;
DB4 = s;
E = 1;
E = 0;
delay();
}
void displayOnOffControl(bit display, bit cursor, bit blinking) {
DB7 = 0;
DB6 = 0;
DB5 = 0;
DB4 = 0;
E = 1;
E = 0;
DB7 = 1;mov
DB6 = display;
DB5 = cursor;
DB4 = blinking;
E = 1;
E = 0;
delay();
}
void cursorOrDisplayShift(bit sc, bit rl) {
RS = 0;
DB7 = 0;
DB6 = 0;
DB5 = 0;
DB4 = 1;
E = 1;
E = 0;
DB7 = sc;
DB6 = rl;
E = 1;
E = 0;
delay();
}
void functionSet(void) {
// The high nibble for the function set is actually sent twice.
DB7 = 0;
DB6 = 0;
DB5 = 1;
DB4 = 0;
RS = 0;
E = 1;
E = 0;
delay();
E = 1;
E = 0;
DB7 = 1;
E = 1;
E = 0;
delay();
}
void setDdRamAddress(char address) {
RS = 0;
DB7 = 1;
DB6 = getBit(address, 6);
DB5 = getBit(address, 5);
DB4 = getBit(address, 4);
E = 1;
E = 0;
DB7 = getBit(address, 3);
DB6 = getBit(address, 2);
DB5 = getBit(address, 1);
DB4 = getBit(address, 0);
E = 1;
E = 0;
delay();
}
void sendChar(char c) {
DB7 = getBit(c, 7);
DB6 = getBit(c, 6);
DB5 = getBit(c, 5);
DB4 = getBit(c, 4);
RS = 1;
E = 1;
E = 0;
DB7 = getBit(c, 3);
DB6 = getBit(c, 2);
DB5 = getBit(c, 1);
DB4 = getBit(c, 0);
E = 1;
E = 0;
delay();
}
// -- End of LCD Module instructions
// -------------------------------------------------------------------void sendString(char* str) {
int index = 0;
while (str[index] != 0) {
sendChar(str[index]);
index++;
}
}
bit getBit(char c, char bitNumber) {
return (c >> bitNumber) & 1;
}
void delay(void) {
char c;
for (c = 0; c < 50; c++);
}
***************lcd.h*********************
#include"delay.h"
sfr dat=0x0a0;
sbit rs=P3^0;
sbit rw=P3^1;
sbit en=P3^2;
void write_lcd(char);
void init_lcd();
void cmd_lcd(char );
void disp_lcd(char );
void str_lcd(char *);
void write_lcd(char cmd)
{
dat=cmd;
rw=0;
en=1;
delay_ms(2);
en=0;
delay_ms(2);
}
void init_lcd()
{
cmd_lcd(0x01);
cmd_lcd(0x38);
cmd_lcd(0x0c);
cmd_lcd(0x80);
cmd_lcd(0x06);
}
void cmd_lcd(char ch)
{
rs=0;
write_lcd(ch);
}
void disp_lcd(char cmd)
{
rs=1;
write_lcd(cmd);
}
void str_lcd(char *str)
{
while(*str)
disp_lcd(*str++);
}
------------------------------------------------------------------LCD ASCII
#include<reg51.h>
sbit rs=P3^5; //Register select (RS)
sbit rw=P3^7; //Read write (RW) pin
sbit en=P3^6; //Enable (EN) pin
char decimal[]={"Decimal ="};
//String displayed on first line of 16x2 lcd
char ascii[]={"ASCII = "};
//String displayed on second line of 16x2 lcd
void delay(unsigned int time) //Time delay function
{
unsigned int i,j;
for(i=0;i<time;i++)
for(j=0;j<5;j++);
}
void lcdcmd(unsigned char value) //Function for sending values to the command register
of LCD
{
P2=value;
//sending commands on port 2 Means to the lcd command
register
P3=0x40;
delay(50);
en=0;
delay(50);
return;
}
void display(unsigned char value) //Function for sending values to the data register of
LCD
{
P2=value;
//sending data on port 2 Means to the lcd data register
P3=0x60;
delay(500);
en=0;
delay(50);
return;
}
void lcdint(void)
{
P2=0x00;
P3=0x00;
delay(15000);
display(0x30);
delay(4500);
display(0x30);
delay(300);
display(0x30);
delay(650);
lcdcmd(0x38);
delay(50);
lcdcmd(0x0F);
delay(50);
lcdcmd(0x01);
delay(50);
lcdcmd(0x06);
delay(50);
lcdcmd(0x80);
delay(50);
}
void main()
{
char c=0x00;
int count=0;digit=0,i;
for( i=0;i<=255;i++)
{
while(count!='\0')
{
display(decimal[count]);
count++;
}
display(digit);
count=0;
lcdcmd(0xC0);
while(count!='\0')
{
display(ascii[count]);
count++;
}
display(c);
delay(40000);
lcdcmd(0x01);
lcdcmd(0x80);
c++;
digit++;
}}
MOV A,B
MOV B,#10
DIV AB
MOV R2,A
MOV R3,B
RET
This is the delay function used to produce some clock cycles between the events.
DELAY_100uS:
MOV TMOD,#01H
MOV TH0,#HIGH(-200)
MOV TL0,#LOW(-200)
SETB TR0
JNB TF0,$
CLR TF0
CLR TR0
RET
This is the delay function used to produce some clock cycles between the events.
DELAY_50ms:
MOV TMOD,#01H
MOV TH0,#HIGH(-50000)
MOV TL0,#LOW(-50000)
SETB TR0
JNB TF0,$
CLR TF0
CLR TR0
RET
This function is used to display the desired digit on desired location on LCD display.
DISPLAY:
MOV A,R1
ADD A,#30H
;LCALL LOOK_UP
;MOV P0,A
;SETB P2.2
;CLR P2.2
LCALL WRITE_CHAR
MOV A,R2
ADD A,#30H
;LCALL LOOK_UP
;MOV P0,A
;SETB P2.1
;CLR P2.1
LCALL WRITE_CHAR
MOV A,R3
ADD A,#30H
;LCALL LOOK_UP
;MOV P0,A
;SETB P2.0
;CLR P2.0
LCALL WRITE_CHAR
RET
This is the function for displaying the letter TEMP on the LCD screen.
TEXT:
MOV A,#85H
LCALL WRITE_CMD
MOV A,#'T'
ACALL WRITE_CHAR
ACALL DELAY_50ms
MOV A,#'E'
ACALL WRITE_CHAR
ACALL DELAY_50ms
MOV A,#'M'
ACALL WRITE_CHAR
ACALL DELAY_50ms
MOV A,#'P'
ACALL WRITE_CHAR
ACALL DELAY_50ms
MOV A,#':'
ACALL WRITE_CHAR
ACALL DELAY_50ms
RET
This is the function in which ADC start conversion from analog to digital form and after
conversion we read the data from the port into the Accumulator of the Microcontroller.
READ_ADC:
CLR P3.1
nop
nop
SETB P3.1
LCALL DELAY_100us
MOV A,P1
RET
;***********************************
FIVESECOND:
MOV R2,#10
RET
This is the MAIN function from where we start all of our code!
MAIN:
DISP:
LCALL INITIALIZE_LCD
MOV A,P1
MOV R1,#5
SUBB A,R1
JZ FIVESECOND
LCALL TEXT
MOV A,R4
LCALL ADJUST
LCALL DISPLAY
MOV A,#80H
LCALL WRITE_CMD
; MOV A,#01H
; LCALL WRITE_CMD
; LJMP LOOP
WAIT:LJMP WAIT
END
void lcdInit() {
delay_ms(100);
// Wait for 100ms after power is applied.
P2DIR = EN + RS + DATA; // Make pins outputs
P2OUT = 0x03; // Start LCD (send 0x03)
lcdTriggerEN(); // Send 0x03 3 times at 5ms then 100 us
delay_ms(5);
lcdTriggerEN();
delay_ms(5);
lcdTriggerEN();
delay_ms(5);
P2OUT = 0x02; // Switch to 4-bit mode
lcdTriggerEN();
delay_ms(5);
lcdWriteCmd(0x28);
lcdWriteCmd(0x08);
lcdWriteCmd(0x01);
lcdWriteCmd(0x06);
lcdWriteCmd(0x0C);
//
//
//
//
//
void lcdTriggerEN() {
P2OUT |= EN;
P2OUT &= ~EN;
}
void lcdWriteData(unsigned char data) {
P2OUT |= RS; // Set RS to Data
LOWNIB(data >> 4); // Upper nibble
lcdTriggerEN();
LOWNIB(data); // Lower nibble
lcdTriggerEN();
delay_us(50); // Delay > 47 us
}
void lcdWriteCmd(unsigned char cmd) {
P2OUT &= ~RS; // Set RS to Data
LOWNIB(cmd >> 4); // Upper nibble
lcdTriggerEN();
LOWNIB(cmd); // Lower nibble
lcdTriggerEN();
delay_ms(5); // Delay > 1.5ms
}
void lcdSetText(char* text, int x, int y) {
int i;
if (x < 16) {
reverse
case 3:
break;
x |= 0x20; // Set LCD for second line write
reverse
}
i = 0;
break;
}
lcdWriteCmd(x);
------------------LCD.h
/*********************************************************************
LCD Driver Functions
LCD Pinouts
Pin 1
Ground
Pin 2
VCC (+3.3 to +5V)
Pin 3
Contrast adjustment
Pin 4
Register Select (RS). 0: Command, 1: Data
Pin 5
Read/Write (R/W). 0: Write, 1: Read
GND for constant write
Pin 6
Clock (Enable). Falling edge triggered
Pin 7
Bit 0 (Not used in 4-bit operation)
Pin 8
Bit 1 (Not used in 4-bit operation)
Pin 9
Bit 2 (Not used in 4-bit operation)
Pin 10 Bit 3 (Not used in 4-bit operation)
Pin 11 Bit 4
Pin 12 Bit 5
Pin 13 Bit 6
Pin 14 Bit 7
Pin 15 Backlight Anode (+)
Pin 16 Backlight Cathode (-)
-> Set to
// Send a command
// Send a
*********************************************************************/
#ifndef LCDLIB_H_
#define LCDLIB_H_
#include <msp430g2553.h>
#include <string.h>
#include <stdio.h>
// Delay Functions
#define delay_ms(x)
#define delay_us(x)
// Pins
#define EN
#define RS
#define DATA
0x0F
// Commands
#define CLEAR
0x01
__delay_cycles((long) x* 1000)
__delay_cycles((long) x)
BIT4
BIT5
// Functions
void lcdInit();
// Initialize LCD
void lcdTriggerEN();
Trigger Enable
void lcdWriteData(unsigned char data);
(Characters)
void lcdWriteCmd(unsigned char cmd);
void lcdClear();
// Clear LCD
void lcdSetText(char * text, int x, int y);
void lcdSetInt(int val, int x, int y);
#endif /* LCDLIB_H_ */
//
// Send Data
// Send Commands
// Write string
// Write integer