📄 newcomdlg.cpp
字号:
// NewComDlg.cpp : implementation file
//
#include "stdafx.h"
#include "NewCom.h"
#include "NewComDlg.h"
#include "ser.h"//关于硬件地址的映射的定义
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
volatile UART1reg *v_pUART1reg;
volatile IOPreg *v_pIOPRegs;
volatile PWMreg *v_pPWMreg;
CString strRec;
/////////////////////////////////////////////////////////////////////////////
// CNewComDlg dialog
CNewComDlg::CNewComDlg(CWnd* pParent /*=NULL*/)
: CDialog(CNewComDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CNewComDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CNewComDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CNewComDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CNewComDlg, CDialog)
//{{AFX_MSG_MAP(CNewComDlg)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
ON_BN_CLICKED(IDC_BUTTON10, OnButton10)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNewComDlg message handlers
BOOL CNewComDlg::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
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// TODO: Add extra initialization here
//映射寄存器地址
v_pUART1reg = (volatile UART1reg*)VirtualAlloc(0, sizeof(UART1reg), MEM_RESERVE, PAGE_NOACCESS);
if (v_pUART1reg == NULL)
{
DEBUGMSG (1,(TEXT("v_pUART1reg is not allocated\n\r")));
return TRUE;
}
if (!VirtualCopy((PVOID)v_pUART1reg, (PVOID)UART0_BASE, sizeof(UART1reg), PAGE_READWRITE|PAGE_NOCACHE)) {
DEBUGMSG (1,(TEXT("v_pUART1reg is not mapped\n\r")));
return TRUE;
}
DEBUGMSG (1,(TEXT("v_pUART1reg is mapped to %x\n\r"), v_pUART1reg));
v_pIOPRegs = (volatile IOPreg*)VirtualAlloc(0, sizeof(IOPreg), MEM_RESERVE, PAGE_NOACCESS);
if (v_pIOPRegs == NULL)
{
DEBUGMSG (1,(TEXT("v_pIOPRegs is not allocated\n\r")));
return TRUE;
}
if (!VirtualCopy((PVOID)v_pIOPRegs, (PVOID)IOP_BASE, sizeof(IOPreg), PAGE_READWRITE|PAGE_NOCACHE)) {
DEBUGMSG (1,(TEXT("v_pIOPRegs is not mapped\n\r")));
return TRUE;
}
DEBUGMSG (1,(TEXT("v_pIOPRegs is mapped to %x\n\r"), v_pIOPRegs));
v_pPWMreg = (volatile PWMreg*)VirtualAlloc(0, sizeof(PWMreg), MEM_RESERVE, PAGE_NOACCESS);
if (v_pPWMreg == NULL)
{
DEBUGMSG (1,(TEXT("v_pPWMreg is not allocated\n\r")));
return TRUE;
}
if (!VirtualCopy((PVOID)v_pPWMreg, (PVOID)PWM_BASE, sizeof(PWMreg), PAGE_READWRITE|PAGE_NOCACHE)) {
DEBUGMSG (1,(TEXT("v_pPWMreg is not mapped\n\r")));
return TRUE;
}
DEBUGMSG (1,(TEXT("v_pPWMreg is mapped to %x\n\r"), v_pPWMreg));
//初始化串口
myinit_uart();
//myinit_beep();
return TRUE; // return TRUE unless you set the focus to a control
}
void CNewComDlg::myinit_uart()
{
v_pUART1reg->rUFCON=0x00;/* not use FIFO*/
v_pUART1reg->rUMCON=0x0;/* disable flow control*/
v_pUART1reg->rULCON=0x03;/* normal mode, no parity, one stop bit, 8-bit*/
v_pUART1reg->rUCON=0x245;//控制寄存器,用pclk为波特率频率
v_pUART1reg->rUBRDIV=129;//9600
MYDelay(100);
}
void CNewComDlg::myinit_beep()
{
v_pIOPRegs->rGPBCON &= ~3; //set GPB0 as tout0, pwm output
v_pIOPRegs->rGPBCON |= 2;
v_pPWMreg->rTCFG0 &= ~0xff;
v_pPWMreg->rTCFG0 |= 15; //prescaler = 15+1
v_pPWMreg->rTCFG1 &= ~0xf;
v_pPWMreg->rTCFG1 |= 2; //mux = 1/8
//v_pIOPRegs->rTCNTB0 = (PCLK>>7)/freq;
v_pPWMreg->rTCNTB0 = 78;
v_pPWMreg->rTCMPB0 = v_pPWMreg->rTCNTB0>>1; // 50%
v_pPWMreg->rTCON &= ~0x1f;
v_pPWMreg->rTCON |= 0xb; //disable deadzone, auto-reload, inv-off, update TCNTB0&TCMPB0, start timer 0
v_pPWMreg->rTCON &= ~2; //clear manual update bit
}
unsigned char CNewComDlg::mygetc()
{
// while (! (UTRSTAT0 & RXD0_READY));
// return URXH0;
// while (! (v_pUART1reg->rUTRSTAT & RXD0_READY));
MYDelay(10);
return v_pUART1reg->rURXH;
}
void CNewComDlg::myputc(unsigned char ch)
{
// while (!(v_pUART1reg->rUTRSTAT & TXD0_READY));
MYDelay(10);
v_pUART1reg->rURXH = ch;
}
void CNewComDlg::OnButton1()
{
// TODO: Add your control notification handler code here
unsigned char ch;
// v_pIOPRegs->rGPFDAT &= 0x37F;
// v_pIOPRegs->rGPFDAT |= 1<<7;
ch = mygetc();
//MessageBox(_T("没受到东西!"));
// 如果接收到的是回车符就发送回车和换行符
myputc(0x35);
MYDelay(1000);
// v_pIOPRegs->rGPFDAT &= 0x37F;
}
void CNewComDlg::MYDelay(int t)
{
int i,j;
for(i=0;i<t;i++)
for(j=0;j<65530;j++);
}
void CNewComDlg::OnButton2()
{
int i,j,k;
// TODO: Add your control notification handler code here
for(k=0;k<5;k++)
{
myinit_beep();
for(j=0;j<70;j++)
{
for(i=0;i<50000;i++)
MYDelay(50000);
}
// TODO: Add your control notification handler code here
v_pIOPRegs->rGPBCON &= ~3; //set GPB0 as output
v_pIOPRegs->rGPBCON |= 1;
v_pIOPRegs->rGPBDAT &= ~1;
for(j=0;j<50;j++)
{
for(i=0;i<50000;i++)
MYDelay(50000);
}
myinit_beep();
for(j=0;j<50;j++)
{
for(i=0;i<50000;i++)
MYDelay(50000);
}
v_pIOPRegs->rGPBCON &= ~3; //set GPB0 as output
v_pIOPRegs->rGPBCON |= 1;
v_pIOPRegs->rGPBDAT &= ~1;
for(j=0;j<500;j++)
{
for(i=0;i<50000;i++)
MYDelay(50000);
}
}
}
void CNewComDlg::OnButton3()
{
// TODO: Add your control notification handler code here
v_pIOPRegs->rGPBCON &= ~3; //set GPB0 as output
v_pIOPRegs->rGPBCON |= 1;
v_pIOPRegs->rGPBDAT &= ~1;
}
void CNewComDlg::OnButton4()
{
// TODO: Add your control notification handler code here
v_pIOPRegs->rGPFDAT &= 0x37; //set GPB0 as tout0, pwm output
v_pIOPRegs->rGPFDAT |= 1<<7;
}
void CNewComDlg::OnButton5()
{
// TODO: Add your control notification handler code here
v_pIOPRegs->rGPFDAT &= 0x37; //set GPB0 as tout0, pwm output
}
void CNewComDlg::OnChangeEdit1()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
}
void CNewComDlg::OnButton7()
{
// TODO: Add your control notification handler code here
CDC *pDC=new CClientDC(this);
CBrush brush;
brush.CreateSolidBrush(RGB(255,0,0));
CBrush *pOldBrush=pDC->SelectObject(&brush);
pDC->Rectangle(100,100,200,200);
pDC->SelectObject(pOldBrush);
delete pDC;
}
void CNewComDlg::OnButton8()
{
// TODO: Add your control notification handler code here
CDC *pDC=new CClientDC(this);
CBrush brush;
brush.CreateSolidBrush(RGB(0,0,255));
CBrush *pOldBrush=pDC->SelectObject(&brush);
pDC->Rectangle(100,100,200,200);
pDC->SelectObject(pOldBrush);
delete pDC;
}
void CNewComDlg::OnButton9()
{
// TODO: Add your control notification handler code here
int i,j,randnum;
CDC *pDC=new CClientDC(this);
CBrush brush;
brush.CreateSolidBrush(RGB(0,255,0));
CBrush *pOldBrush=pDC->SelectObject(&brush);
pDC->Rectangle(100,199,300,200);
pDC->Rectangle(100,101,101,200);
for(i=0;i<10;i++)
pDC->Rectangle((100+i*10+5),(300-100-rand()%100),(100+i*10+10),200);
pDC->SelectObject(pOldBrush);
delete pDC;
}
void CNewComDlg::OnButton10()
{
// TODO: Add your control notification handler code here
// int randnum;
//得到绘图环境
CPaintDC dc(this);
CPen *pOldPen;
CPen *pNewPen=new CPen();
//CRect rect;
//创建画笔
pNewPen->CreatePen(PS_DASH,1,RGB(255,0,0));
pOldPen=dc.SelectObject(pNewPen);
//得到窗口区域
//GetClientRect(&rect);
//设置绘线当前点为窗体左上角
dc.MoveTo(100,100);
dc.LineTo(100,200);
dc.LineTo(200,200);
dc.LineTo(200,200);
dc.MoveTo(200,100);
dc.LineTo(200,300);
dc.SelectObject(pOldPen);
pNewPen->DeleteObject();
delete pNewPen;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -