• 班班
    了解作者
  • C/C++
    开发工具
  • 3KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 117
    下载次数
  • 2010-04-16 16:48
    上传日期
三线通信DSA协议源代码。 使用状态机方式处理,可节省CPU时间。 可直接使用。
dsa_CODE.rar
  • dsa通信CODE
  • Dsa.c
    15.4KB
  • Dsa.h
    753B
内容介绍
#include "Dsa.h" /******************************************************************* **Module:DSA **Name: DsaReset **Param:none **Return: none **func: **author: KT OEM Coagent **date: 2008/12/15 **ver: 1.0 *******************************************************************/ void DsaReset(void) { GPIO_config(GPIO0,dvd_di,GPIO_Mode_IN_TRI_TTL); GPIO_config(GPIO3,dvd_stb,GPIO_Mode_IN_TRI_TTL); GPIO_config(GPIO3,dvd_ack,GPIO_Mode_IN_TRI_TTL); g_u8DsaMode = DSA_IDLE; g_u8DsaSendFlg = FALSE; } /******************************************************************* **Module:DSA **Name: DsaInit **Param:none **Return: none **func: **author: KT OEM Coagent **date: 2008/12/15 **ver: 1.0 *******************************************************************/ void DsaInit(void) { u8 i; for(i=0;i<DSA_SEND_BUF_SIZE;i++) { u16DsaSendBuf[i] = 0; } for(i=0;i<DSA_RECV_BUF_SIZE;i++) { u16DsaReceiveBuf[i] = 0; } DsaReset(); g_u8DsaMode = DSA_IDLE; g_u8DsaSendFlg = FALSE; } /******************************************************************* **Module:DSA **Name: WriteDataToDsaSendBuf **Param:u16DsaCode **Return: none **func: **author: KT OEM Coagent **date: 2008/12/15 **ver: 1.0 *******************************************************************/ void WriteDataToDsaSendBuf(u16 u16DsaCode) { u16DsaSendBuf[u8DsaSendBufTail] = u16DsaCode; u8DsaSendBufTail++; if(u8DsaSendBufTail >= DSA_SEND_BUF_SIZE) u8DsaSendBufTail = 0; } /******************************************************************* **Module:DSA **Name: ReadDataFromDsaSendBuf **Param:none **Return: none **func: **author: KT OEM Coagent **date: 2008/12/15 **ver: 1.0 *******************************************************************/ void ReadDataFromDsaSendBuf(void) { if((g_u8DsaSendFlg == FALSE) && (u8DsaSendBufHead != u8DsaSendBufTail)) { u16DsaOutData = u16DsaSendBuf[u8DsaSendBufHead]; g_u8DsaSendFlg = TRUE; u8DsaSendBufHead++; if(u8DsaSendBufHead >= DSA_SEND_BUF_SIZE) u8DsaSendBufHead = 0; } } /******************************************************************* **Module:DSA **Name: WriteDataToDsaReceiveBuf **Param:u16DsaCode **Return: none **func: **author: KT OEM Coagent **date: 2008/12/15 **ver: 1.0 *******************************************************************/ void WriteDataToDsaReceiveBuf(u16 u16DsaCode) { u16DsaReceiveBuf[u8DsaReceiveBufTail] = u16DsaCode; u8DsaReceiveBufTail++; if(u8DsaReceiveBufTail >= DSA_RECV_BUF_SIZE) u8DsaReceiveBufTail = 0; } /******************************************************************* **Module:DSA **Name: TopDsaReceiveStackHandler **Param:none **Return: none **func: **author: KT OEM Coagent **date: 2008/12/15 **ver: 1.0 *******************************************************************/ void TopDsaReceiveStackHandler(void) { if(u8DsaReceiveBufHead != u8DsaReceiveBufTail) { u16DsaRecvData = u16DsaReceiveBuf[u8DsaReceiveBufHead]; u8DsaReceiveBufHead++; if(u8DsaReceiveBufHead >= DSA_RECV_BUF_SIZE) u8DsaReceiveBufHead = 0; DsaReceiveHandler(u16DsaRecvData); } } /******************************************************************* **Module:DSA **Name: Dsa_Process **Param:none **Return: none **func: DSA RTX state machine **author: KT OEM Coagent **date: 2008/12/15 **ver: 1.0 *******************************************************************/ void Dsa_Process(void) { static u8 u8BitsCounter; static u16 u16DsaRtxTimeOutCounter = 0; static u8 u8DsaModeTemp = 0; static u8 u8DsaReSend = DISABLE; static u16 u16DsaOutDataBackUp = 0; //u8 u8DsaDataOsdBuf[2]; if(g_u8DsaMode^u8DsaModeTemp) { u8DsaModeTemp =g_u8DsaMode; u16DsaRtxTimeOutCounter = 0; } switch(g_u8DsaMode) { case DSA_IDLE: if(GPIO_BitRead(GPIO0,dvd_di)==RESET) { g_u8DsaMode = DSA_R_SYNC; GPIO_config(GPIO3,dvd_ack,GPIO_Mode_OUT_PP); GPIO_BitWrite(GPIO3,dvd_ack,Bit_RESET); u8BitsCounter = 0; } else if(g_u8DsaSendFlg == TRUE) { g_u8DsaMode = DSA_T_SYNC; GPIO_config(GPIO0,dvd_di,GPIO_Mode_OUT_PP); GPIO_config(GPIO3,dvd_ack,GPIO_Mode_IN_TRI_TTL); GPIO_BitWrite(GPIO0,dvd_di,Bit_RESET); u8BitsCounter = 0; } else { ReadDataFromDsaSendBuf(); u8DsaReSend = DISABLE; u16DsaOutDataBackUp = u16DsaOutData; } break; case DSA_T_SYNC: u16DsaRtxTimeOutCounter++; if(u16DsaRtxTimeOutCounter >3000) { DsaReset(); } else { if(GPIO_BitRead(GPIO3,dvd_ack)==RESET) { GPIO_BitWrite(GPIO0,dvd_di,Bit_SET); g_u8DsaMode = DSA_T_SYNC_WAIT; } } break; case DSA_T_SYNC_WAIT: u16DsaRtxTimeOutCounter++; if(u16DsaRtxTimeOutCounter >3000) { DsaReset(); } else { if(GPIO_BitRead(GPIO0,dvd_ack)==SET) { g_u8DsaMode = DSA_T_SEND; GPIO_config(GPIO3,dvd_stb,GPIO_Mode_OUT_PP); } } break; case DSA_T_SEND: u16DsaRtxTimeOutCounter++; if(u16DsaRtxTimeOutCounter >300) { DsaReset(); u8DsaReSend = ENABLE; } else { if(u16DsaOutData&(1<<15-u8BitsCounter)) { GPIO_BitWrite(GPIO0,dvd_di,Bit_SET); } else { GPIO_BitWrite(GPIO0,dvd_di,Bit_RESET); } GPIO_BitWrite(GPIO3,dvd_stb,Bit_RESET); g_u8DsaMode = DSA_T_WAIT; } break; case DSA_T_WAIT: u16DsaRtxTimeOutCounter++; if(u16DsaRtxTimeOutCounter >300) { DsaReset(); u8DsaReSend = ENABLE; } else { if(GPIO_BitRead(GPIO3,dvd_ack)==RESET) { GPIO_BitWrite(GPIO3,dvd_stb,Bit_SET); g_u8DsaMode = DSA_T_ACK; } } break; case DSA_T_ACK: u16DsaRtxTimeOutCounter++; if(u16DsaRtxTimeOutCounter >300) { DsaReset(); u8DsaReSend = ENABLE; } else { if(GPIO_BitRead(GPIO3,dvd_ack)==SET) { if(u8BitsCounter == 15) { GPIO_config(GPIO0,dvd_di,GPIO_Mode_IN_TRI_TTL); GPIO_config(GPIO3,dvd_stb,GPIO_Mode_IN_TRI_TTL); GPIO_config(GPIO3,dvd_ack,GPIO_Mode_OUT_PP); GPIO_BitWrite(GPIO3,dvd_ack,Bit_RESET); g_u8DsaMode = DSA_T_END; } else { u8BitsCounter++; g_u8DsaMode = DSA_T_SEND; } } } break; case DSA_T_END: u16DsaRtxTimeOutCounter++; if(u16DsaRtxTimeOutCounter >300) { DsaReset(); u8DsaReSend = ENABLE; } else { if(GPIO_BitRead(GPIO3,dvd_stb)==RESET) { u8 u8DsaResponse = GPIO_BitRead(GPIO0,dvd_di); GPIO_BitWrite(GPIO3,dvd_ack,Bit_SET); g_u8DsaMode = DSA_T_END_ALL; if(u8DsaResponse) { g_u8DsaSendFlg = FALSE; } } } break; case DSA_T_END_ALL: u16DsaRtxTimeOutCounter++; if(u16DsaRtxTimeOutCounter >300) { DsaReset(); u8DsaReSend = ENABLE; } else { if(GPIO_BitRead(GPIO3,dvd_stb)==SET) { GPIO_config(GPIO0,dvd_di,GPIO_Mode_IN_TRI_TTL); GPIO_config(GPIO3,dvd_stb,GPIO_Mode_IN_TRI_TTL); GPIO_config(GPIO3,dvd_ack,GPIO_Mode_IN_TRI_TTL); g_u8DsaMode = DSA_IDLE; //menu_display(WARR_MENU); } } break; case DSA_R_SYNC: u16DsaRtxTimeOutCounter++; if(u16DsaRtxTimeOutCounter >3000) { DsaReset(); } else { if(GPIO_BitRead(GPIO0,dvd_di)==SET) { GPIO_config(GPIO3,dvd_stb,GPIO_Mode_IN_TRI_TTL); GPIO_BitWrite(GPIO3,dvd_ack,Bit_SET); u16DsaInData = 0; g_u8DsaMode = DSA_R_READ; } } break; case DSA_R_READ: u16DsaRtxTimeOutCounter++; if(u16DsaRtxTimeOutCounter >300) { DsaReset(); } else { if(GPIO_BitRead(GPIO3,dvd_stb)==RESET) { u16DsaInData |= GPIO_BitRead(GPIO0,dvd_di); GPIO_BitWrite(GPIO3,dvd_ack,Bit_RESET); g_u8DsaMode = DSA_R_WAIT; } } break;
评论
    相关推荐