Microcontroller › 8051 › serial interfacing assembly code
- This topic has 0 replies, 1 voice, and was last updated 13 years, 7 months ago by Dharmit.
-
AuthorPosts
-
March 1, 2011 at 1:42 pm #766DharmitParticipant
Hey there,
I have got an assembly language code for the serial interfacing, but I dont quite get it, does it have just the interfacing part in it or the code is also for a stop watch timer???
Here is the code:
SDA1 EQU P1.1 ;SDA=PIN5
SCL1 EQU P1.0 ;SCL=PIN6
WTCMD EQU 10100000B ;WRITE DATA COMMANDRDCMD EQU 10100001B ;READ DATA COMMAND
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%; STORE A BYTE (Data 8F in address location 2AH)
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
MOV A,#WTCMD ;LOAD WRITE COMMAND
CALL OUTS ;SEND IT
MOV A,#2AH ;GET BYTE ADDRESS
CALL OUT ;SEND IT
MOV A,#8FH ;GET DATA
CALL OUT ;SEND IT
CALL STOP ;SEND STOP CONDITION
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
MOV A,#WTCMD ;LOAD WRITE COMMAND TO SEND ADDRESS
CALL OUTS ;SEND IT
MOV A,#4DH ;GET LOW BYTE ADDRESS
CALL OUT ;SEND IT
CALL CREAD ;GET DATA BYTE
MOV A,R1
CALL MDELAY
RET
;***********************************************************************
;
;***********************************************************************
;***********************************************************************; THIS ROUTINE SENDS OUT CONTENTS OF THE ACCUMULATOR
; and includes START condition. Refer to the data sheets
; for discussion of START and STOP conditions.
;***********************************************************************
OUTS: MOV R2,#8 ;LOOP COUNT — EQUAL TO BIT COUNTSETB SDA1 ;INSURE DATA IS HI
SETB SCL1 ;INSURE CLOCK IS HI
NOP ;NOTE 1
NOP
NOP
CLR SDA1 ;START CONDITION — DATA = 0
NOP ;NOTE 1
NOP
NOP
CLR SCL1 ;CLOCK = 0
OTSLP: RLC A ;SHIFT BIT
JNC BITLS
SETB SDA1 ;DATA = 1
JMP OTSL1 ;CONTINUE
BITLS: CLR SDA1 ;DATA = 0
OTSL1: SETB SCL1 ;CLOCK HI
NOP ;NOTE 1
NOP
NOP
CLR SCL1 ;CLOCK LOW
DJNZ R2,OTSLP ;DECREMENT COUNTER
SETB SDA1 ;TURN PIN INTO INPUT
NOP ;NOTE 1
SETB SCL1 ;CLOCK ACK
NOP ;NOTE 1
NOP
NOP
CLR SCL1
RET
;**********************************************************************; THIS ROUTINE SENDS OUT CONTENTS OF ACCUMLATOR
; without sending a START condition.
;**********************************************************************
OUT: MOV R2,#8 ;LOOP COUNT — EQUAL TO BIT COUNTOTLP: RLC A ;SHIFT BIT
JNC BITL
SETB SDA1 ;DATA = 1
JMP OTL1 ;CONTINUE
BITL: CLR SDA1 ;DATA = 0
OTL1: SETB SCL1 ;CLOCK HI
NOP ;NOTE 1
NOP
NOP
CLR SCL1 ;CLOCK LOW
DJNZ R2,OTLP ;DECREMENT COUNTER
SETB SDA1 ;TURN PIN INTO INPUT
NOP ;NOTE 1
SETB SCL1 ;CLOCK ACK
NOP ;NOTE 1
NOP
NOP
CLR SCL1
RET
STOP: CLR SDA1 ;STOP CONDITION SET DATA LOWNOP ;NOTE 1
NOP
NOP
SETB SCL1 ;SET CLOCK HI
NOP ;NOTE 1
NOP
NOP
SETB SDA1 ;SET DATA HIGH
RET
;*******************************************************************
; THIS ROUTINE READS A BYTE OF DATA
; From current address pointer.
; Returns the data byte in R1
;*******************************************************************
CREAD: MOV A,#RDCMD ;LOAD READ COMMAND
CALL OUTS ;SEND IT
CALL IN ;READ DATA
MOV R1,A ;STORE DATA
CALL STOP ;SEND STOP CONDITION
RET
;**********************************************************************; THIS ROUTINE READS IN A BYTE FROM
; and stores it in the accumulator
;**********************************************************************
IN: MOV R2,#8 ;LOOP COUNTSETB SDA1 ;SET DATA BIT HIGH FOR INPUT
INLP: CLR SCL1 ;CLOCK LOW
NOP ;NOTE 1
NOP
NOP
NOP
SETB SCL1 ;CLOCK HIGH
CLR C ;CLEAR CARRY
JNB SDA1,INL1 ;JUMP IF DATA = 0
CPL C ;SET CARRY IF DATA = 1
INL1: RLC A ;ROTATE DATA INTO ACCUMULATOR
DJNZ R2,INLP ;DECREMENT COUNTER
CLR SCL1 ;CLOCK LOW
RET
;*********************************************************************; This routine test for WRITE DONE condition
; by testing for an ACK.
; This routine can be run as soon as a STOP condition
; has been generated after the last data byte has been sent
; The routine loops until an ACK is received
; No ACK will be received until the EEPROM is done with
; the write operation.
;*********************************************************************
ACKTST: MOV A,#WTCMD ;LOAD WRITE COMMAND TO SEND ADDRESS
MOV R2,#8 ;LOOP COUNT — EQUAL TO BIT COUNT
CLR SDA1 ;START CONDITION — DATA = 0
NOP ;NOTE 1
NOP
NOP
CLR SCL1 ;CLOCK = 0
AKTLP: RLC A ;SHIFT BIT
JNC AKTLS
SETB SDA1 ;DATA = 1
JMP AKTL1 ;CONTINUE
AKTLS: CLR SDA1 ;DATA = 0
AKTL1: SETB SCL1 ;CLOCK HI
NOP ;NOTE 1
NOP
NOP
CLR SCL1 ;CLOCK LOW
DJNZ R2,AKTLP ;DECREMENT COUNTER
SETB SDA1 ;TURN PIN INTO INPUT
NOP ;NOTE 1
SETB SCL1 ;CLOCK ACK
NOP ;NOTE 1
NOP
NOP
JNB SDA1,EXIT ;EXIT IF ACK (WRITE DONE)
JMP ACKTST ;START OVER
EXIT: CLR SCL1 ;CLOCK LOW
CLR SDA1 ;DATA LOW
NOP ;NOTE 1
NOP
NOP
SETB SCL1 ;CLOCK HIGH
NOP
NOP
SETB SDA1 ;STOP CONDITION
RET
-
AuthorPosts
- You must be logged in to reply to this topic.