Tutorial Microcontroller 8051
 
 
 
 
 
   

BACK

4.5. Writing a char serially from PC to LCD Character 2x16

This experiment will be done for process to print character out to LCD Character, in this experiment the delivery is just for one character, you need to modify to write more character. You should learn more about Serial communication RS232.

Microcontroller Serial Communication

Figure 4.2. Transmit data serially to LCD Character

Step 1st
Build the circuit as shown in figure 4.2. As you seen on figure 4.2. Remember, that all we want to do with this lesson transmit.

Step 2nd
In this step, you must tipe the assembly program to make your serial working, we assume that you have already known the editor, we used MIDE-51 to edit the program. ( Download File asm : exp42.zip ,Download Complete Circuit File : SerialLCD.PDF )

       org 0h
       call initserial
call init_LCD ; start: call GetChar
mov r1,#0c0h
acall write_inst
add a,#30h
mov r1,a
acall write_data
sjmp start ; GetChar:
detect: jnb ri,detect ; clr ri
mov a,sbuf
ret ;
initserial: mov scon,#52h;initialize serial mode 1 mov tmod,#20h;timer1 mode 2 mov th1,#0F3h;Reload value for baud rate 2400 setb tr1 ret ; Init_lcd: mov r1,#00000001b ;Display clear acall write_inst ; mov r1,#00111000b ;Function set, ;Data 8 bit,2 line font 5x7 acall write_inst ; mov r1,#00001100b ;Display on,
;cursor off,cursor blink off acall write_inst mov r1,#00000110b ;Entry mode, Set increment acall write_inst ret
;
Write_inst:
clr P2.0 ; RS = P2.0 = 0, write mode instruction
mov P0,R1 ; D7 s/d D0 = P0 = R1
setb P2.1 ; EN = 1 = P2.1
call delay; call delay time
clr P2.1 ; EN = 0 = P2.1
ret ; Write_data:
setb P2.0 ; RS = P2.0 = 1, write mode data
mov P0,R1 ; D7 s/d D0 = P0 = R1
setb P2.1 ; EN = 1 = P2.1
call delay; call delay time
clr p2.1 ; EN = 0 = P2.1
ret ; delay: mov R0,#0
delay1:mov R7,#0fh
djnz R7,$
djnz R0,delay1
ret end ;

To get the data from microcontroller serially, computer must run the program to get data from port communication RS232, in this experiment I have been used Delphi programming: ( Download File Delphi : LCDserial.zip )



Figure 4.3. Delphi Programming Sends Data to LCD Character

unit sendlcd2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls;
type
         TForm1 = class(TForm)
         Label1: TLabel;
         Label2: TLabel;
         Timer1: TTimer;
         Label3: TLabel;
         procedure FormCreate(Sender: TObject);
         procedure Timer1Timer(Sender: TObject);
         private
         { Private declarations }
         public
         { Public declarations }
         end;
var
         Form1: TForm1;
         data,status:byte;
         const
         base = $3f8;{base address port serial}
         lcr = 3; {line control register}
         dll = 0; {divisor lacht low byte}
         dlh = 1; {divisor lacht high byte}
         lsr = 5; {line status register}
implementation
{$R *.DFM}
Procedure Initserial;
begin
 asm
  mov dx,base+lcr; {address line control register}
  mov al,$80 ; {10000000b = access bit divisor lacht}
  out dx,al
         ;
  mov dx,base+dll; {address divisor lacht low byte}
  mov al,$30 ; {DLLB = 30h}
  out dx,al
         ;
  mov dx,base+dlh; {address divisor lacht high byte}
  mov al,$00 ; {DLLH = 00h}
  out dx,al
         ; {Pada saat ini Port serial}
         ; {memp.baud rate = 2400 bps}
  mov dx,base+lcr; {address line control register}
  mov al,$03 ; {00000011b =}
  out dx,al ; {bit 7=0, access to Rx buffer & Tx
         ; {bit 6=0, set break disable
         ; {bit 5-4-3=000, no parity
         ; {bit 2=0, one stop bit
         ; {bit 1-0=11,data lenght 8 bit}
  end;
end;
Procedure Send_Data_Serial;
begin
 asm
  mov dx,base
  mov al,data
  out dx,al
 end
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
 Initserial;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
 repeat
  asm
   mov dx,base+lsr ; {address line status register }
   in al,dx
   and al,$20 ; {00100000b =not masking bit 5}
   mov status,al ; {bit5=Empty Transmitting holding reg}
  end;
  until status = $20;{If ETHR=1 then data ready tobe send }
  data:=strtoint(edit1.text);
  Send_Data_Serial;
end;

end.

Step 3rd
Safe your assembly program above, and name it with seri2.asm (for example) Compile the program that you have been save by using MIDE-51, see the software instruction.

Step 4th
Download your hex file ( exp42.hex ) into the microcontroller by using Microcontroller ATMEL ISP software, see the instruction.After download this hex file you'll see the action of serial transmition( of course if your cable connection and your program are corrected ).

Best Link

 

BACK

 

Lesson 1:
T o o l
1.1. Programmer
1.2.
Edsim 51
1.3. MIDE-51
1.4. ATMEL ISP

Lesson 2:
Input Output
2.1.LED
2.2.Swicht
2.3.7 Segmen
2.4.LCD Character
2.5.ADC
2.6.DAC
2.7.Motor Stepper
2.8.Keypad

Lesson 3:

Timer Counter

3.1.Basic
3.2.Mode 0
3.3.Mode 1
3.4.Mode 2
3.5.Mode 3

Lesson 4:

Serial Comm.

4.1.Basic
4.2.LED
4.3.Rotate LED
4.2 ADC
4.3.LCD

Lesson 5:
Interuption

5.1.Basic
5.2.Timer
5.2.External