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

📄 leddlg.cpp

📁 这是一个测试led的程序,在VC下可以很好的运行 我试过,效果不错
💻 CPP
字号:
// LedDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Led.h"
#include "LedDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

char* pLightReg = NULL;

USHORT	*v_pLEDBaseAddr1=NULL;
USHORT	*v_pLEDBaseAddr2=NULL;
USHORT	*v_pLEDBaseAddr3=NULL;

#define BIT7	(0x1<<7)
#define BIT15	(0x1<<15)

extern "C" __declspec(dllimport) BOOL VirtualCopy(LPVOID lpvDest, LPVOID lpvSrc, DWORD cbSize, DWORD fdwProtect );

#define LED_BASEADDR1	0x10200000
#define LED_BASEADDR2	0x10300000
#define LED_BASEADDR3	0x10400000

#define pLightIoBaseAddress 0x10500000

BYTE NumData[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};

UINT16 READ_PORT_USHORT(UINT16 *pAddr)
{
    UINT32 nAlignedAddr = ((UINT32)pAddr & ~0x3);

    if ((UINT32)pAddr & 0x2)    // Is the address an "odd" word within a longword?
    {
        // Yes - read a longword and data is in the upper word.
        return((UINT16) ((*(volatile unsigned long *)nAlignedAddr) >> 16));
    }
    else
    {
        // No - read single word.
        return(*(volatile unsigned short *)nAlignedAddr);
    }
}


void WRITE_PORT_USHORT(UINT16 *pAddr, UINT16 Data)
{
    UINT32 nAlignedAddr = ((UINT32)pAddr & ~0x3);

    if ((UINT32)pAddr & 0x2)    // Is the address an "odd" word within a longword?
    {
        // Yes - write a longword with data in the upper word.
        *(volatile UINT32 *)nAlignedAddr = (READ_PORT_USHORT(pAddr) | (Data << 16));
    }
    else
    {
        // No - write single word.
        *(volatile UINT16 *)pAddr = Data;
    }
}
// CLedDlg dialog

CLedDlg::CLedDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLedDlg::IDD, pParent)
	, m_LedValue(1234)
{
	count=0;
	outdata=0x80;
	setLightData=0;
	LedCount=0;
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

void CLedDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LED, m_ControlLed);
	DDX_Control(pDX, IDC_LeftShift, m_LeftShift);
	DDX_Control(pDX, IDC_RightShift, m_RightShift);
	DDX_Text(pDX, IDC_LED, m_LedValue);
}

BEGIN_MESSAGE_MAP(CLedDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
	ON_WM_SIZE()
#endif
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_LedSet, &CLedDlg::OnBnClickedLedset)
	ON_BN_CLICKED(IDC_CountTime, &CLedDlg::OnBnClickedCounttime)
	ON_BN_CLICKED(IDC_LedStop, &CLedDlg::OnBnClickedLedstop)
	ON_BN_CLICKED(IDC_LightStart, &CLedDlg::OnBnClickedLightstart)
	ON_BN_CLICKED(IDC_LightStop, &CLedDlg::OnBnClickedLightstop)
	ON_BN_CLICKED(IDC_LightSet, &CLedDlg::OnBnClickedLightset)
	ON_WM_TIMER()
	ON_WM_DESTROY()
END_MESSAGE_MAP()


// CLedDlg message handlers

BOOL CLedDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	m_LeftShift.SetCheck(1);
	if(!SetMemoryMap())
		return FALSE;
	*pLightReg=0xff;
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CLedDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
	DRA::RelayoutDialog(
		AfxGetInstanceHandle(), 
		this->m_hWnd, 
		DRA::GetDisplayMode() != DRA::Portrait ? 
			MAKEINTRESOURCE(IDD_LED_DIALOG_WIDE) : 
			MAKEINTRESOURCE(IDD_LED_DIALOG));
}
#endif


void CLedDlg::SetLedValue(unsigned int indata)
{
	USHORT Data;
	UINT buf;
	buf=indata;	
	buf=buf%1000000;	
	Data=NumData[buf/100000];
	buf=buf%100000;
	Data|=NumData[buf/10000]<<8;
	WRITE_PORT_USHORT(v_pLEDBaseAddr1,~(Data|BIT7|BIT15));
	
	//*v_pLEDBaseAddr1=~(Data|BIT7|BIT15);

	buf=buf%10000;
	Data=NumData[buf/1000];
	buf=buf%1000;
	Data|=NumData[buf/100]<<8;
	WRITE_PORT_USHORT(v_pLEDBaseAddr2,~(Data|BIT7|BIT15));

	//*v_pLEDBaseAddr2=~(Data|BIT7|BIT15);

	buf=buf%100;
	Data=NumData[buf/10];
	buf=buf%10;
	Data|=NumData[buf]<<8;
	WRITE_PORT_USHORT(v_pLEDBaseAddr3,~(Data|BIT7|BIT15));
	//*v_pLEDBaseAddr3=~(Data|BIT7|BIT15);
}

BOOL CLedDlg::SetMemoryMap(void)
{
	if(!(v_pLEDBaseAddr1=(USHORT*)VirtualAlloc(0,0x400,MEM_RESERVE,PAGE_READWRITE)))
	{
		MessageBox(TEXT("VirtualAlloc() failed!\r\n"),NULL,MB_OK); 
		return FALSE;
	}
	if(!VirtualCopy((PVOID)v_pLEDBaseAddr1,(PVOID)(LED_BASEADDR1>>8),0x400,PAGE_READWRITE|PAGE_NOCACHE|PAGE_PHYSICAL))
	{
		VirtualFree((PVOID)v_pLEDBaseAddr1,0,MEM_RELEASE);
		pLightReg = NULL;
		MessageBox(TEXT("VirtualCopy() failed!\r\n"),NULL,MB_OK); 
		return FALSE;
	}

	///////////////////////////////


	if(!(v_pLEDBaseAddr2=(USHORT*)VirtualAlloc(0,0x400,MEM_RESERVE,PAGE_READWRITE)))
	{
		MessageBox(TEXT("VirtualAlloc() failed!\r\n"),NULL,MB_OK); 
		return FALSE;
	}
	if(!VirtualCopy((PVOID)v_pLEDBaseAddr2,(PVOID)(LED_BASEADDR2>>8),0x400,PAGE_READWRITE|PAGE_NOCACHE|PAGE_PHYSICAL))
	{
		VirtualFree((PVOID)v_pLEDBaseAddr2,0,MEM_RELEASE);
		pLightReg = NULL;
		MessageBox(TEXT("VirtualCopy() failed!\r\n"),NULL,MB_OK); 
		return FALSE;
	}

	//////////////


	if(!(v_pLEDBaseAddr3=(USHORT*)VirtualAlloc(0,0x400,MEM_RESERVE,PAGE_READWRITE)))
	{
		MessageBox(TEXT("VirtualAlloc() failed!\r\n"),NULL,MB_OK); 
		return FALSE;
	}
	if(!VirtualCopy((PVOID)v_pLEDBaseAddr3,(PVOID)(LED_BASEADDR3>>8),0x400,PAGE_READWRITE|PAGE_NOCACHE|PAGE_PHYSICAL))
	{
		VirtualFree((PVOID)v_pLEDBaseAddr3,0,MEM_RELEASE);
		pLightReg = NULL;
		MessageBox(TEXT("VirtualCopy() failed!\r\n"),NULL,MB_OK); 
		return FALSE;
	}

	//////////////////

	if(!(pLightReg=(char*)VirtualAlloc(0,0x400,MEM_RESERVE,PAGE_READWRITE)))
	{
		MessageBox(TEXT("VirtualAlloc() failed!\r\n"),NULL,MB_OK); 
		return FALSE;
	}



	if(!VirtualCopy((PVOID)pLightReg,(PVOID)(pLightIoBaseAddress>>8),0x400,PAGE_READWRITE|PAGE_NOCACHE|PAGE_PHYSICAL))
	{
		VirtualFree((PVOID)pLightReg,0,MEM_RELEASE);
		pLightReg = NULL;
		MessageBox(TEXT("VirtualCopy() failed!\r\n"),NULL,MB_OK); 
		return FALSE;
	}
	return TRUE;
}

void CLedDlg::OnBnClickedLedset()
{
	UpdateData(TRUE);
	SetLedValue(m_LedValue);
	// TODO: Add your control notification handler code here
}

void CLedDlg::OnBnClickedCounttime()
{
	UpdateData(TRUE);
	SetTimer(2,500,NULL);	// TODO: Add your control notification handler code here
}

void CLedDlg::OnBnClickedLedstop()
{
	KillTimer(2);	
}

void CLedDlg::OnBnClickedLightstart()
{
	SetTimer(1,500,NULL);// TODO: Add your control notification handler code here
}

void CLedDlg::OnBnClickedLightstop()
{
	KillTimer(1);
	// TODO: Add your control notification handler code here
}

void CLedDlg::OnBnClickedLightset()
{
		KillTimer(1);
	setLightData=0;
	for(int Index=0;Index<8;Index++){
		if(IsDlgButtonChecked(IDC_L1+Index))

			setLightData|=0x1<<Index;
	}
	*pLightReg=~setLightData;
	// TODO: Add your control notification handler code here
}

void CLedDlg::OnTimer(UINT_PTR nIDEvent)
{
	switch(nIDEvent)
	{
	case 1:
		count++;
		if(count<8)
		{
			if(m_LeftShift.GetCheck())
				outdata=outdata>>1;
			else
				outdata=outdata<<1;
		}
		else
		{
			count=0;
			if(m_LeftShift.GetCheck())
				outdata=0x80;
			else
				outdata=0x01;
		}
		*pLightReg=~outdata;
		break;
	case 2:
		//LedCount++;
		m_LedValue++;
		SetLedValue(m_LedValue);
		UpdateData(FALSE);
		break;
	}	// TODO: Add your message handler code here and/or call default

	CDialog::OnTimer(nIDEvent);
}

void CLedDlg::OnDestroy()
{
	
	if(pLightReg)
		VirtualFree((PVOID)pLightReg,0,MEM_RELEASE);
	if(v_pLEDBaseAddr1)
		VirtualFree((PVOID)v_pLEDBaseAddr1,0,MEM_RELEASE);
	if(v_pLEDBaseAddr2)
		VirtualFree((PVOID)v_pLEDBaseAddr2,0,MEM_RELEASE);
	if(v_pLEDBaseAddr3)
		VirtualFree((PVOID)v_pLEDBaseAddr3,0,MEM_RELEASE);
	// TODO: Add your message handler code here
	CDialog::OnDestroy();
}

⌨️ 快捷键说明

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