Вы находитесь на странице: 1из 20

8 Bit Hex to Decimal Conversion

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

; //If number is not 0 then continue

; //First divide by 100


;//MSB in R0
; //Divide by 10
;; //save the tens place in the Higher nibble

////save the tens place in the Lower nibble of

----------lcd Display code (kiel)


#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
void delay(unsigned int time) //Time delay function
{
unsigned int i,j;
for(i=0;i<time;i++)
for(j=0;j<5;j++);
}
//Function for sending values to the command register of LCD
void lcdcmd(unsigned char value)
{
P1=value;
P3=0x40;

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++);
}

// program to convert hexadecimal data to decimal format


#include<reg51.h>
#include"lcd.h"
sbit cs=P3^4;
sbit rd=P3^5;
sbit wr=P3^6;
sbit intr=P3^7;
sfr mydata=0x90;
unsigned char k0,k1,k2,k3,k4;
void conv_and_disp(unsigned char);
void conv_and_disp(unsigned char x)
{
init_lcd();
k0=x/10;
k1=x%10;
k2=k0%10;
k3=k0/10;
disp_lcd(k3+0x30);
disp_lcd(k2+0x30);
disp_lcd(k1+0x30);
while(1);
}
void main()
{
unsigned char val;
//val=0xff;
mydata=0xff;
//cs=1;
rd=1;
wr=1;
intr=1;
while(1)
{
//cs=0;
wr=0;
wr=1;
while(intr==1);
rd=0;
val=mydata;
conv_and_disp(val);
rd=1;
}
}

***************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++;
}}

------------------------Analog To Digital and LCD


Assembly Code:
EQU directive equates the variable on the left side of EQU with the value or variable on
the right side of EQU.ORG 0000H does initialize your code at the zero memory location.
It can be any memory location other than 0000h within the whole memory.
LJMP MAIN jumps to the function named MAIN to start executing from there.
RS EQU P3.2
EN EQU P3.4
ORG 0000H
LJMP MAIN
The following code segment is used to initialize LCD to make it workable for basic
operations.
INITIALIZE_LCD:
MOV A,#38H
LCALL WRITE_CMD
MOV A,#0CH
LCALL WRITE_CMD
MOV A,#06H
LCALL WRITE_CMD
RET
Following function is used when we want to send a command to LCD.
WRITE_CMD:
;RS=0 FOR COMMAND
CLR RS
MOV P2,A
SETB EN
CLR EN
LCALL DELAY_50ms
RET
Following function is used when we want to send a Data to LCD.
WRITE_CHAR:
SETB RS
MOV P2,A
SETB EN
CLR EN
LCALL DELAY_50ms
RET
Adjust Function is a module to separate digits in an 8 bit number into different registers
so that we can display our desired digit on desired location on LCD display. e.g. if we
have 65 then we have to display 6 on one location and 5 on separate location. For that we
need to separate out the digits and ADJUST function does this for us.
ADJUST:
//MOV A,P1
MOV B,#100
DIV AB
MOV R1,A

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

------------------------------------------------12bit code for LCD


#include "lcdLib.h"
#define LOWNIB(x)

P2OUT = (P2OUT & 0xF0) + (x & 0x0F)

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);

//
//
//
//
//

4-bit, 2 line, 5x8


Instruction Flow
Clear LCD
Auto-Increment
Display On, No blink

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) {

x |= 0x80; // Set LCD for first line write


switch (y){
case 1:
x |= 0x40; // Set LCD for second line write
break;
case 2:
x |= 0x60; // Set LCD for first line write

reverse

case 3:

break;
x |= 0x20; // Set LCD for second line write

reverse

}
i = 0;

break;
}
lcdWriteCmd(x);

while (text[i] != '\0') {


lcdWriteData(text[i]);
i++;
}
}
void lcdSetInt(int val, int x, int y){
char number_string[16];
sprintf(number_string, "%d", val); // Convert the integer to
character string
lcdSetText(number_string, x, y);
}
void lcdClear() {
lcdWriteCmd(CLEAR);
}

------------------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

Top level functions available


- lcd_init();
//
Initialize LCD
- write_byte(char l);
// Send a single
character (1 byte)
- write_string(char s[]);
// Print a
character string
- write_int(int num);
// Write an
integer
- gotoXy(x, y);
// Move
cursor to (x, y) location (Lines and columns start at 0)
- clear_lcd();
// Clear
LCD and move cursor to (0, 0)
Not important to the user:
- send_cmd(char cmd);
to the LCD
- write_nibble(char l);
nibble to the LCD
- void trigger_EN();

// Send a command
// Send a

*** DELAY FUNCTION ASSUMES 1 MHz CLOCK ***

*********************************************************************/
#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

-------------------0xff is converted to ACII ie 255 ascii 32 35


35
Logic -------- FF)16 can be converted in (255)10 and then the output is 32h, 35h, 35h.
i,j,k are the variables, you can use any RAM location at the place of i,j,k. i = LSB and k =
MSB.
MOV R6, #00H;
MOV A, Count;
MOV B, A;
ANL A, #0FH;
DA A;
MOV R7, A;
MOV A, B;
ANL A, #0F0H;
SWAP A;
JZ LOOP1;
MOV B, A;
CLR A;
LOOP3: ADD A, #16H;
DA A;
JNC LOOP2
INC R6;
LOOP2: DJNZ B, LOOP3;
LOOP1: ADD A, R7;
DA A;
JNC LOOP4
INC R6;
LOOP4: MOV B, A;
ANL A, #0FH;
ADD A, #30H;
MOV i, A;
MOV A, B;
SWAP A;
ANL A, #0FH;
ADD A, #30H;
MOV j, A;
MOV A, R6;
ADD A, #30H;
MOV k, A;
MOV A, B;
MOV B, R6;

Вам также может понравиться