26
8 Bit Hex to Decimal Conversion hextobcd: mov r0,#00h mov r1,#00h cjne a,#00h,c1_hextobcd ; //If number is not 0 then continue ret c1_hextobcd clr c: mov b,#100 ; //First divide by 100 div ab mov r0,a ;//MSB in R0 clr c mov a,b mov b,#10 ; //Divide by 10 div ab swap a mov r1,a ;; //save the tens place in the Higher nibble of R1 mov a,b orl a,r1 mov r1,a ////save the tens place in the Lower nibble of R1 ret ----------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;

8 Bit Hex to Decimal Conversion

Embed Size (px)

DESCRIPTION

conversion

Citation preview

Page 1: 8 Bit Hex to Decimal Conversion

8 Bit Hex to Decimal Conversionhextobcd:

mov r0,#00hmov r1,#00hcjne a,#00h,c1_hextobcd ; //If number is not 0 then continue

ret

c1_hextobcd clr c:mov b,#100 ; //First divide by 100div abmov r0,a ;//MSB in R0clr cmov a,bmov b,#10 ; //Divide by 10div abswap amov r1,a ;; //save the tens place in the Higher nibble of R1mov a,borl a,r1mov r1,a ////save the tens place in the Lower nibble of R1ret

----------lcd Display code (kiel)#include<reg51.h>sbit rs=P3^5;   //Register select (RS)sbit rw=P3^7;   //Read write (RW) pinsbit 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;

Page 2: 8 Bit Hex to Decimal Conversion

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

Page 3: 8 Bit Hex to Decimal Conversion

lcdcmd(0x80);       //Initialize Cursor to first character matrix of lcdwhile(sec[s]!='\0'){if(s==15)lcdcmd(0xC0);      //Initializing Cursor to second line first Character matrixdisplay(sec[s]);s++;}delay(100000);lcdcmd(0x01);lcdcmd(0xC0);      //Initializing Cursor to second line first Character matrixwhile(u[l]!='\0'){display(u[l]);     // printing same string on second linel++;}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);

Page 4: 8 Bit Hex to Decimal Conversion

void delay(void);

void main(void) {

functionSet();entryModeSet(1, 0); // increment and no shiftdisplayOnOffControl(1, 1, 1); // display on, cursor on and blinking onsendString("EdSim51 LCD Module Simulation");setDdRamAddress(0x40); // set address to start of second linesendString("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;

Page 5: 8 Bit Hex to Decimal Conversion

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;movDB6 = 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;

Page 6: 8 Bit Hex to Decimal Conversion

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;

Page 7: 8 Bit Hex to Decimal Conversion

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

}

Page 8: 8 Bit Hex to Decimal Conversion

// 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;}}

Page 9: 8 Bit Hex to Decimal Conversion

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

Page 10: 8 Bit Hex to Decimal Conversion

------------------------------------------------------------------LCD ASCII#include<reg51.h>sbit rs=P3^5;   //Register select (RS)sbit rw=P3^7;   //Read write (RW) pinsbit en=P3^6;   //Enable (EN) pin

char decimal[]={"Decimal ="};       //String displayed on first line of 16x2 lcdchar ascii[]={"ASCII = "};          //String displayed on second line of 16x2 lcdvoid 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 registerP3=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);

Page 11: 8 Bit Hex to Decimal Conversion

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

Page 12: 8 Bit Hex to Decimal Conversion

------------------------Analog To Digital and LCDAssembly 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.2EN EQU P3.4ORG 0000HLJMP MAINThe following code segment is used to initialize LCD to make it workable for basic operations.INITIALIZE_LCD:MOV A,#38HLCALL WRITE_CMDMOV A,#0CHLCALL WRITE_CMDMOV A,#06HLCALL WRITE_CMDRETFollowing function is used when we want to send a command to LCD.WRITE_CMD:;RS=0 FOR COMMANDCLR RSMOV P2,ASETB ENCLR ENLCALL DELAY_50msRETFollowing function is used when we want to send a Data to LCD.WRITE_CHAR:SETB RSMOV P2,ASETB ENCLR ENLCALL DELAY_50msRETAdjust 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,P1MOV B,#100DIV ABMOV R1,A

Page 13: 8 Bit Hex to Decimal Conversion

MOV A,BMOV B,#10DIV ABMOV R2,AMOV R3,BRETThis is the delay function used to produce some clock cycles between the events.DELAY_100uS:MOV TMOD,#01HMOV TH0,#HIGH(-200)MOV TL0,#LOW(-200)SETB TR0JNB TF0,$CLR TF0CLR TR0RETThis is the delay function used to produce some clock cycles between the events.DELAY_50ms:MOV TMOD,#01HMOV TH0,#HIGH(-50000)MOV TL0,#LOW(-50000)SETB TR0JNB TF0,$CLR TF0CLR TR0RETThis function is used to display the desired digit on desired location on LCD display.DISPLAY:MOV A,R1ADD A,#30H;LCALL LOOK_UP;MOV P0,A;SETB P2.2;CLR P2.2LCALL WRITE_CHARMOV A,R2ADD A,#30H;LCALL LOOK_UP;MOV P0,A;SETB P2.1;CLR P2.1LCALL WRITE_CHARMOV A,R3ADD A,#30H;LCALL LOOK_UP;MOV P0,A

Page 14: 8 Bit Hex to Decimal Conversion

;SETB P2.0;CLR P2.0LCALL WRITE_CHARRETThis is the function for displaying the letter “TEMP” on the LCD screen.TEXT:MOV A,#85HLCALL WRITE_CMDMOV A,#'T'ACALL WRITE_CHARACALL DELAY_50msMOV A,#'E'ACALL WRITE_CHARACALL DELAY_50msMOV A,#'M'ACALL WRITE_CHARACALL DELAY_50msMOV A,#'P'ACALL WRITE_CHARACALL DELAY_50msMOV A,#':'ACALL WRITE_CHARACALL DELAY_50msRETThis 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.1nopnopSETB P3.1LCALL DELAY_100usMOV A,P1RET;***********************************FIVESECOND:MOV R2,#10RETThis is the MAIN function from where we start all of our code!MAIN:DISP:LCALL INITIALIZE_LCDMOV A,P1MOV R1,#5SUBB A,R1JZ FIVESECOND

Page 15: 8 Bit Hex to Decimal Conversion

LCALL TEXTMOV A,R4LCALL ADJUSTLCALL DISPLAYMOV A,#80HLCALL WRITE_CMD; MOV A,#01H; LCALL WRITE_CMD; LJMP LOOPWAIT:LJMP WAITEND

Page 16: 8 Bit Hex to Decimal Conversion

------------------------------------------------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 outputsP2OUT = 0x03; // Start LCD (send 0x03)

lcdTriggerEN(); // Send 0x03 3 times at 5ms then 100 usdelay_ms(5);lcdTriggerEN();delay_ms(5);lcdTriggerEN();delay_ms(5);

P2OUT = 0x02; // Switch to 4-bit modelcdTriggerEN();delay_ms(5);

lcdWriteCmd(0x28); // 4-bit, 2 line, 5x8lcdWriteCmd(0x08); // Instruction FlowlcdWriteCmd(0x01); // Clear LCDlcdWriteCmd(0x06); // Auto-IncrementlcdWriteCmd(0x0C); // Display On, No blink

}

void lcdTriggerEN() {P2OUT |= EN;P2OUT &= ~EN;

}

void lcdWriteData(unsigned char data) {P2OUT |= RS; // Set RS to DataLOWNIB(data >> 4); // Upper nibblelcdTriggerEN();LOWNIB(data); // Lower nibblelcdTriggerEN();delay_us(50); // Delay > 47 us

}

void lcdWriteCmd(unsigned char cmd) {P2OUT &= ~RS; // Set RS to DataLOWNIB(cmd >> 4); // Upper nibblelcdTriggerEN();LOWNIB(cmd); // Lower nibblelcdTriggerEN();delay_ms(5); // Delay > 1.5ms

}

void lcdSetText(char* text, int x, int y) {int i;if (x < 16) {

Page 17: 8 Bit Hex to Decimal Conversion

x |= 0x80; // Set LCD for first line writeswitch (y){case 1:

x |= 0x40; // Set LCD for second line writebreak;

case 2:x |= 0x60; // Set LCD for first line write

reversebreak;

case 3:x |= 0x20; // Set LCD for second line write

reversebreak;

}lcdWriteCmd(x);

}i = 0;

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 stringlcdSetText(number_string, x, y);

}

void lcdClear() {lcdWriteCmd(CLEAR);

}

Page 18: 8 Bit Hex to Decimal Conversion

------------------LCD.h/*********************************************************************

LCD Driver Functions

LCD Pinouts

Pin 1 GroundPin 2 VCC (+3.3 to +5V)Pin 3 Contrast adjustmentPin 4 Register Select (RS). 0: Command, 1: DataPin 5 Read/Write (R/W). 0: Write, 1: Read -> Set to

GND for constant writePin 6 Clock (Enable). Falling edge triggeredPin 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 4Pin 12 Bit 5Pin 13 Bit 6Pin 14 Bit 7Pin 15 Backlight Anode (+)Pin 16 Backlight Cathode (-)

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); // Send a command

to the LCD- write_nibble(char l); // Send a

nibble to the LCD- void trigger_EN();

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

*********************************************************************/

#ifndef LCDLIB_H_#define LCDLIB_H_

Page 19: 8 Bit Hex to Decimal Conversion

#include <msp430g2553.h>#include <string.h>#include <stdio.h>

// Delay Functions#define delay_ms(x) __delay_cycles((long) x* 1000)#define delay_us(x) __delay_cycles((long) x)

// Pins#define EN BIT4#define RS BIT5#define DATA 0x0F

// Commands#define CLEAR 0x01

// Functionsvoid lcdInit(); // Initialize LCDvoid lcdTriggerEN(); // Trigger Enablevoid lcdWriteData(unsigned char data); // Send Data (Characters)void lcdWriteCmd(unsigned char cmd); // Send Commandsvoid lcdClear(); // Clear LCDvoid lcdSetText(char * text, int x, int y); // Write stringvoid lcdSetInt(int val, int x, int y); // Write integer

#endif /* LCDLIB_H_ */

Page 20: 8 Bit Hex to Decimal Conversion

-------------------0xff is converted to ACII ie 255 ascii – 32 35 35Logic -------- 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 LOOP2INC R6;LOOP2: DJNZ B, LOOP3;LOOP1: ADD A, R7;DA A;JNC LOOP4INC 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;