|
// Timer2.cpp : implementation file
//
#include "stdafx.h"
#include "piccpc.h"
#include "Timer2.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTimer2 property page
IMPLEMENT_DYNCREATE(CTimer2, CPropertyPage)
CTimer2::CTimer2() : CPropertyPage(CTimer2::IDD)
{
//{{AFX_DATA_INIT(CTimer2)
m_dDesiredOverflow = 0.0;
//}}AFX_DATA_INIT
}
CTimer2::~CTimer2()
{
}
void CTimer2::DoDataExchange(CDataExchange* pDX)
{
CPropertyPage::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTimer2)
DDX_Control(pDX, IDC_T2_POSTSCALER, m_cT2Postscaler);
DDX_Control(pDX, IDC_T2_RESOLUTION, m_cT2Resolution);
DDX_Text(pDX, IDC_T2_COUNTER, m_dDesiredOverflow);
DDX_Control(pDX, IDC_T2_OVERFLOW, m_sInfo);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTimer2, CPropertyPage)
//{{AFX_MSG_MAP(CTimer2)
ON_CBN_SELCHANGE(IDC_T2_RESOLUTION, OnSelchangeT2Resolution)
ON_CBN_SELCHANGE(IDC_T2_POSTSCALER, OnSelchangeT2Postscaler)
ON_EN_CHANGE(IDC_T2_COUNTER, OnChangeT2Counter)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTimer2 message handlers
BOOL CTimer2::OnSetActive()
{
//update timer2
CheckDlgButton( IDC_T2_ENABLE, PICInfo.sfr.T2CON&0x04 );
UpdateT2ResolutionList();
UpdateT2Postscaler();
UpdateT2Interrupt();
return CPropertyPage::OnSetActive();
}
BOOL CTimer2::OnKillActive()
{
if ( !CPropertyPage::OnKillActive() )
return FALSE;
return TRUE;
}
void CTimer2::UpdateT2ResolutionList() {
m_cT2Resolution.ResetContent();
CString s;
for ( int i=1;i<=16;i*=4 ) {
s.Format( "%G", i*PICInfo.dInstructionTime*1000.0 );
m_cT2Resolution.AddString( s );
}
m_cT2Resolution.SetCurSel( PICInfo.sfr.T2CON&0x03 );
}
void CTimer2::UpdateT2Postscaler() {
m_cT2Postscaler.SetCurSel( (PICInfo.sfr.T2CON&0x78)>>3 );
}
void CTimer2::OnSelchangeT2Resolution()
{
PICInfo.sfr.T2CON &= 0xFC;
PICInfo.sfr.T2CON |= m_cT2Resolution.GetCurSel();
UpdateT2Interrupt();
}
void CTimer2::UpdateT2Interrupt() {
if ( !UpdateData() )
return;
if ( m_dDesiredOverflow < PICInfo.dInstructionTime*50000.0 && m_dDesiredOverflow != 0.0 ) {
m_dDesiredOverflow = PICInfo.dInstructionTime*50000.0;
UpdateData( FALSE );
}
DWORD pr2_off = MAXBYTE - PICInfo.sfr.PR2;
PICInfo.dwT2Offset = 0;
PICInfo.dwT2Counter = 0;
CString s;
GetDlgItemText( IDC_T2_RESOLUTION, s );
double fres = atof( s );
DWORD tmp = (DWORD)(m_dDesiredOverflow/fres);
m_dDesiredOverflow = tmp*fres;
int p = ((PICInfo.sfr.T2CON&0x78)>>3) + 1; //m_cT2Postscaler.GetCurSel() + 1;
for ( unsigned int i=MAXBYTE+1;i>0;i-- ) {
double y = fres*i;
double f = fmod( m_dDesiredOverflow, y );
double x = fabs(f-y);
if ( f == 0.0 || x<1e-13 ) {
PICInfo.dwT2Offset = MAXBYTE - i + 1;
PICInfo.dwT2Counter = (DWORD)(m_dDesiredOverflow/(fres*i*p));
if ( 0 == PICInfo.dwT2Counter )
PICInfo.dwT2Counter = 1;
s.Format( "Interrupt every/total:\t%fms, %fms\n"
"Timer offset:\t\t%u\n"
"Timer counter:\t\t%u",
i*fres*p,
((256-pr2_off-PICInfo.dwT2Offset)*fres)*p*PICInfo.dwT2Counter,
PICInfo.dwT2Offset,
PICInfo.dwT2Counter );
m_sInfo.SetWindowText( s );
break;
}
}
//fv = fv*p*(256 - (MAXBYTE-PICInfo.sfr.PR2));
//s.Format( "Timer interrupt every: %Gms\n", fv );
}
void CTimer2::OnSelchangeT2Postscaler()
{
PICInfo.sfr.T2CON &= 0x07;
PICInfo.sfr.T2CON |= (m_cT2Postscaler.GetCurSel()<<3);
UpdateT2Interrupt();
}
void CTimer2::OnChangeT2Counter()
{
CheckDlgButton( IDC_T2_INTEN, 1 );
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.