⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 24cx.cpp

📁 通过PC并口读写24c64A芯片的C源程序
💻 CPP
字号:
// 24cx.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#define HIGH  1
#define LOW   0

#define  BASE_PORT_ADDR  0x378

typedef int BOOL;

void WriteSda(BOOL b)
{
	unsigned long lPortVal;

	GetPortVal(BASE_PORT_ADDR, &lPortVal, 1);

	lPortVal &= 0xfd;
	lPortVal |= b<<1;

	SetPortVal(BASE_PORT_ADDR, lPortVal, 1);
}

BOOL ReadSda()
{
	unsigned long lPortVal;

	GetPortVal(BASE_PORT_ADDR+1, &lPortVal, 1);

	lPortVal >>= 4;

	return lPortVal&1;
}

void WriteScl(BOOL b)
{
	unsigned long lPortVal;

	GetPortVal(BASE_PORT_ADDR, &lPortVal, 1);

	lPortVal &= 0xfe;
	lPortVal |= b;

	SetPortVal(BASE_PORT_ADDR, lPortVal, 1);
}

BOOL ReadScl()
{
	unsigned long lPortVal;

	GetPortVal(BASE_PORT_ADDR, &lPortVal, 1);

	return lPortVal&1;
}

BOOL SendByte(unsigned char c)
{
	int i, ret;
	BOOL b;

	WriteScl(LOW);

	for(i=7; i>=0; i--)
	{
		b = (c>>i) & 1;
		WriteSda(b);
		//printf("%d\n", b);
		WriteScl(HIGH);
		//Sleep(1);
		WriteScl(LOW);
	}

	// Receive Ack
	WriteScl(HIGH);
	WriteSda(HIGH);
	if(ReadSda())
	{
		printf("Can't Receive ACK!!\n");
		ret = 0;
	}
	else
	{
		printf("ACK=0\n");
		ret = 1;
	}

	WriteScl(LOW);

	return ret;
}

int main(int argc, char* argv[])
{
	unsigned long lPortVal=0;
	unsigned char cDevAddrW=0xa0;
	unsigned char cFWordAddr=0, cSWordAddr=2;
	unsigned char cByteData='b';
	unsigned char sPageData[] = "abcdefghijklmnopqrst";
	int iPageLen, i;

	printf("Hello World!\n");

	InitializeWinIo();
	
	// Start
	WriteScl(HIGH);
	WriteSda(HIGH);
	WriteSda(LOW);

	// Byte write
	/*SendByte(cDevAddrW);
	SendByte(cFWordAddr);
	SendByte(cSWordAddr);
	SendByte(cByteData);*/

	// Page wirte
	SendByte(cDevAddrW);
	SendByte(cFWordAddr);
	SendByte(cSWordAddr);
	iPageLen = strlen((const char *)sPageData);
	for(i=0; i<iPageLen; i++)
	{
		SendByte(*(sPageData+i));
	}

	//Stop
	WriteSda(LOW);
	WriteScl(HIGH);
	WriteSda(HIGH);

	ShutdownWinIo();
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -