此文章简单介绍 单片机 C语言和汇编语言混合编程的例子。主要用单片机汇编语言编写DS1302的底层驱动,在C语言里通过调用汇编语言,从而实现C和汇编的混合编程。
;汇编语言源文件
;======================================
; DS1302 DRIVER.
;EMBEDY ASSEMBLY LANGUAGE.
;WHEN CHAR-STYLE (R7,R5,R3) RETURN(R7)
;INT ..."_"--_FUNCTION TELL HAVE PARA TRANS.
;PROGRAMER : MicroAC
;DATE : 2012.03.10
;======================================
NAME DS1302 ;MODULE NAME
;DS1302时钟IC控制CS BIT P1.2
SDA BIT P1.1
SCL BIT P1.0
FLAG BIT 20H.1
;**************************************
;--Initial : DS1302 with Assembly L
;--Programer : MicroAC
;--Date : 2012.03.10
;**************************************
;?CODE ATTRIBUTE?FUNCTION NAME?MODULE NAME SEGM CODE?PR?INITIAL?DS1302 SEGMENT CODE
PUBLIC INITIAL ;SERVE TO EXTERN.
RSEG ?PR?INITIAL?DS1302
USING 0
INITIAL:
CLR CS ;DISABLE->CLEAR CS CLR SCL ;WHEN CS BE HIGH SCL MUST BE LOW CLR SDA;END INITIAL RET
;**************************************
;--S-DATA INPUT: DS1302 with Assembly L
;--Programer : MicroAC
;--Parameter : R7,R5
;--Date : 2012.03.10
;--NOTE: WRITE COMMAND FIRST AND THEN IN
;**************************************?PR?_Ds1302DatIn?DS1302 SEGMENT CODE
PUBLIC _Ds1302DatIn ;SERVE TO EXTERN.RSEG ?PR?_Ds1302DatIn?DS1302
USING 0
_Ds1302DatIn:
MOV A ,#01H
ANL A ,R5 ;GET FLAG. CLR C
RRC A
MOV FLAG,C
SETB CS ;START-> ENABLE - > HIGH
MOV A,R7;COMMAND CLR C
MOV R6,#8;EIGHT CIRCLE.
WDAT_LOOP:
RLC A ;MSB WILL BE TRANSFER FIRSTLY.
MOV SDA,C ;TRANSFER BIT DATA SETB SCL ;RISING EDGE. CLR C ;RESET C REGISTER.
CLR SCL ;FALLING EDGE. DJNZ R6,WDAT_LOOP
JBC FLAG,IN_EXIT
CLR CS ;STOP
IN_EXIT:
RET
;**************************************
;--DATA OUTPUT : DS1302 with Assembly L
;--Programer : MicroAC
;--Parameter : FLAG DETERMIN EN OR DISABLE
;--Date : 2012.03.10
;--NOTE: READ COMMAND FIRST AND THEN OUT
;**************************************?PR?_Ds1302DatRed?DS1302 SEGMENT CODE
PUBLIC _Ds1302DatRed ;SERVE TO EXTERN.RSEG ?PR?_Ds1302DatRed?DS1302
USING 0
_Ds1302DatRed:
MOV A ,#01H
ANL A ,R7 ;GET FLAG.
CLR C
RRC A
MOV FLAG,C
CLR C
CLR A
SETB SCL ;RISING EDGE. MOV R6,#8 ;EIGHT CIRCLE.RDAT_LOOP:
CLR SCL ;FALLING EDGE. MOV C,SDA ;TRANSFER BIT DATA SETB SCL ;RISING EDGE. RLC A ;MSB WILL BE TRANSFER FIRSTLY.
CLR C ;RESET C REGISTER.
DJNZ R6,RDAT_LOOP
JBC FLAG,OUT_EXIT
CLR CS ;STOP
MOV R7,A ;RETURN DATA YOU READ.
OUT_EXIT:
RET
END
//C语言源程序
#include"1602.h"//这个是用户自定义头文件
extern void INITIAL(void);
//command OR data/add will be transfered,AND SECOND IS EN OR DISABLE FALG .
//FLAG IS 1 WILL CONTINUTE.0 WILL STOP WRITE.
extern void Ds1302DatIn(unsigned char DAT ,unsigned char FLAG);
extern unsigned char Ds1302DatRed(unsigned char FLAG);
unsigned char dis[]="0123456789-";
unsigned char code time[]="10:20:50";
void main()
{
LCDInit();
INITIAL(); DisplayListChar(0,0,time);
while(1)
{
//Ds1302DatIn(0xff,1); // Ds1302DatRed(1); }