;******************************************************************** ; ; Filename: C328_8051Demo.asm ; Version: 1.0 ; Description: c328 Demo Firmware with 8051 To communicate with C328: Send Sync Commend to C328 Receive ACK from C328 Send Ack to C328 ; Status: Stable. ; Created at: 2004-3 ; Copyright (c) COMedia Ltd. ; Modified record: ; ;******************************************************************** ;******************************Parameters****************************************** Cmd_Sync EQU 0dh Cmd_Ack EQU 0eh C328Header REG 30h C328CommID REG 31h C328Parameter1 REG 32h C328Parameter2 REG 33h C328Parameter3 REG 34h C328Parameter4 REG 35h McuHeader REG 36h McuCommID REG 37h McuParameter1 REG 38h McuParameter2 REG 39h McuParameter3 REG 3ah McuParameter4 REG 3bh delaycount REG 49h ;The counter of overtime to receive the order ;The different Baud rate has different overtime receivedata REG 20h.1 ;*********************************************************************************** org 0000h jmp Start org 0023H ajmp RS232 ;UART interrupt Start: mov sp,#60h ;assign the pointeroutside call initrs232 ;initialize rs232 setb EA ;open all interrupt setb ES ;enable serial interrupt setb TR1 ;open timer1 mov delaycount,#30h ;48*688us=33ms for 14400 BR call McuSyncWithC328 ;communicate with C328 sjmp $ ;************************************************************************************** ;**initrs232 ;Function: initialize rs232,mode 1,14400 baud rate, ;Entry: ;Return: ;Modified: ;Invoked: ;*************************************************************************************** initrs232: mov SCON, #50h ; uart mode 1, enable recevied mov TMOD, #21h ; timer1 mode 2 mov PCON, #80h ; double baud rate mov TH1,#0f8h ; timer1 initial value, 14400 BR for 22.1184MHz cyrstal mov TL1,#0f8h ret ;************************************************************************************** ;**McuSyncWithC328 ;Function: communication with c328 ;Entry: ;Return: if success c=0; else failure c=1 ;Modified: ;Invoked: SendSyncComm,GetCommFromC328,SendAckComm ;*************************************************************************************** McuSyncWithC328: mov r1,#00h ResendSync: call SendSyncComm call GetCommFromC328 ;get ack command from 528 jc SendSyncTimeLimit ;c=1 get nothing mov a, C328CommID cjne a, #cmd_Ack,SendSyncTimeLimit mov a, C328Parameter1 cjne a, #cmd_Sync,SendSyncTimeLimit call GetCommFromC328 ;get sync command from 528 jc SendSyncFail mov a, C328CommID cjne a, #cmd_Sync,SendSyncFail call SendAckComm clr c ;if Mcu Sync With C328 successfully, clr C as a flag ret SendSyncTimeLimit: inc r1 cjne r1,#3ch,ResendSync ;60 times limit SendSyncFail: jmp SendSyncFail ;if get the command fail, setb C as a flag ;~~~~~~~~~~~~~~~~~~~~~~~~~~send sync command to C328~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SendSyncComm: mov McuCommID,#Cmd_Sync ;set command ID clr A mov McuParameter1,A ;set parameter 1 mov McuParameter2,A ;send parameter 2 mov McuParameter3,A ;send parameter 3 mov McuParameter4,A ;send parameter 4 call SendCommToC328 ;send the sync command to C328 ret :~~~~~~~~~~~~~~~~~~~~~~~~~~send ACK to C328~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SendAckComm: mov McuCommID,#Cmd_Ack clr A mov McuParameter1,A mov McuParameter2,A mov McuParameter3,A mov McuParameter4,A call SendCommToC328 ret ;~~~~~~~~~~~~~~~~~~~~~~~~~send command to C328 function~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SendCommToC328: mov McuHeader,#0aah mov R0,#McuHeader ;Mcu command register start address mov R2,#06h ;Command format is 6 byte SendOneByteToC328: mov A,@R0 mov SBUF,A ;send the byte to serial buffer jnb TI, $ ;waiting the byte sned out clr TI inc R0 djnz R2,SendOneByteToC328 ret ;~~~~~~~~~~~~~~~~~~~~~~~~~get command from C328 function~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GetCommFromC328: mov r7,#00h clr receivedata mov R0,#C328Header ;set the command start address mov r3,delaycount ;The max wait time for get one byte mov R4, #00h ;time out 255*5*0.54us=688us for 22.1184MHZ GetTimeLimit: jb receivedata,GetOneByteFromC328 nop nop djnz r4,GetTimeLimit djnz r3,GetTimeLimit GetTimeLimit1: setb c ;if get the command fail, setb C as a flag ret GetOneByteFromC328: clr c ret ;~~~~~~~~~~~~~~~~~~~~~~~UART interrupt service~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ RS232: jbc RI,RSIN ;Receive data interrupt reti RSIN: push acc mov a,SBUF jb receivedata,RSRET mov @R0, A inc r0 inc r7 mov A,r7 cjne A,#6,RSRET ;if Receive 6 bytes then receivedata=1 setb receivedata RSRET: pop acc reti