// AT89S52.cpp : 定义 DLL 的初始化例程。 //
#include "stdafx.h" #include "AT89S52.h" #include "WinIO.h" #ifdef _DEBUG #define new DEBUG_NEW #endif int port_num,pin_mosi,pin_sck,pin_rst,pin_miso; WORD outValue; // // 注意! // // 如果此 DLL 动态链接到 MFC // DLL,从此 DLL 导出并 // 调入 MFC 的任何函数在函数的最前面 // 都必须添加 AFX_MANAGE_STATE 宏。 // // 例如: // // extern "C" BOOL PASCAL EXPORT ExportedFunction() // { // AFX_MANAGE_STATE(AfxGetStaticModuleState()); // // 此处为普通函数体 // } // // 此宏先于任何 MFC 调用 // 出现在每个函数中十分重要。这意味着 // 它必须作为函数中的第一个语句 // 出现,甚至先于所有对象变量声明, // 这是因为它们的构造函数可能生成 MFC // DLL 调用。 // // 有关其他详细信息, // 请参阅 MFC 技术说明 33 和 58。 //
// CAT89S52App
BEGIN_MESSAGE_MAP(CAT89S52App, CWinApp) END_MESSAGE_MAP()
// CAT89S52App 构造
CAT89S52App::CAT89S52App() { // TODO: 在此处添加构造代码, // 将所有重要的初始化放置在 InitInstance 中 }
// 唯一的一个 CAT89S52App 对象
CAT89S52App theApp;
// CAT89S52App 初始化 void delay(int t) { LARGE_INTEGER litmp ; LONGLONG QPart1,QPart2 ; double d=0; QueryPerformanceCounter(&litmp) ; // 获得初始值 QPart1 = litmp.QuadPart ; while (d<t)//你想要的时间 { QueryPerformanceCounter(&litmp) ; QPart2 = litmp.QuadPart ; d=(double)(QPart2 - QPart1); } } extern "C"_declspec(dllexport) bool _cdecl AT89S52_ENABLE() { unsigned char temp; AT89S52_CLR_SCK(); AT89S52_CLR_RST(); Sleep(50); AT89S52_SET_RST(); Sleep(50); //发送使能字节 AT89S52_PORT_OUT(0xAC); AT89S52_PORT_OUT(0x53); AT89S52_PORT_OUT(0xFF); temp=AT89S52_PORT_OUT(0xFF); if(temp!=0x69) { //MessageBox(NULL,"使能失败,请检查线路","操作",MB_OK); flag_enable=false; return false; } else { flag_enable=true; return true; } }
extern "C"_declspec(dllexport) inline void _cdecl AT89S52_ERASE() { int i=30000; AT89S52_PORT_OUT(0xAC); AT89S52_PORT_OUT(0x9F); AT89S52_PORT_OUT(0x0); AT89S52_PORT_OUT(0x0); } extern "C"_declspec(dllexport) inline void _cdecl AT89S52_WRITE_BYTE(int address,unsigned char data) { unsigned char temp; temp=address>>8; AT89S52_PORT_OUT(0x40); //输出编程命令字 AT89S52_PORT_OUT(temp); //输出地址高位 temp=address%256; AT89S52_PORT_OUT(temp); //输出地址低位 AT89S52_PORT_OUT(data); } extern "C"_declspec(dllexport) inline void _cdecl AT89S52_WRITE_PAGE(int address,unsigned char *pdata) {
} extern "C"_declspec(dllexport) inline void _cdecl AT89S52_READ_SIGNATURE(char*s) { unsigned char temp1,temp2,temp3; long ID; //AT89S51_ENABLE(); //////////////////////////// AT89S52_PORT_OUT(0x28); AT89S52_PORT_OUT(0); AT89S52_PORT_OUT(0); temp1=AT89S52_PORT_OUT(0); //////////////////////////// AT89S52_PORT_OUT(0x28); AT89S52_PORT_OUT(1); AT89S52_PORT_OUT(0); temp2=AT89S52_PORT_OUT(0); /////////////////////////// AT89S52_PORT_OUT(0x28); AT89S52_PORT_OUT(2); AT89S52_PORT_OUT(0); temp3=AT89S52_PORT_OUT(0); /////////////////////////// ID=temp2+temp3*256; sprintf(s,"Atmel ID:%2.2X
Device ID:%4.4X
",temp1,ID); } extern "C"_declspec(dllexport) void AT89S52_READ_LOCK_BIT(char*temp) { //char *temp=new char[100]; unsigned char temp1; //AT89S51_ENABLE(); AT89S52_PORT_OUT(0x24); AT89S52_PORT_OUT(0); AT89S52_PORT_OUT(0); temp1=AT89S52_PORT_OUT(0); temp1=temp1>>2; temp1&=0x7; if(temp1&0x4)sprintf(temp,"加密方式3"); else if(temp1&0x2)sprintf(temp,"加密方式2"); else if(temp1&0x1)sprintf(temp,"加密方式1"); else sprintf(temp,"器件没有加密"); } extern "C"_declspec(dllexport) inline unsigned char _cdecl AT89S52_READ_BYTE(int address) { unsigned char temp; temp=address>>8; //if(!flag_enable)AT89S52_ENABLE(); AT89S52_PORT_OUT(0x20); //输出读数据命令字 AT89S52_PORT_OUT(temp); //输出地址高位 temp=address%256; AT89S52_PORT_OUT(temp); //输出地址低位 temp=AT89S52_PORT_OUT(0); return temp; } extern "C"_declspec(dllexport) int _cdecl AT89S52_WRITE(char *pFile,CWnd *pWnd) { CStdioFile file1; CString strFile,str1,strTemp; unsigned char temp1; int data; int add1,lenth,i; strFile=pFile; str1=strFile.Right(3); if(str1=="HEX"||str1=="hex") { if(!file1.Open(pFile,CFile::modeRead|CFile::shareDenyRead)) MessageBox(NULL,"打开文件出错","提示",MB_OK); while(file1.ReadString(str1)) { //:CCAAAARR...ZZ
|