Click here to Skip to main content
15,893,588 members
Articles / Desktop Programming / MFC

A PIC C Code Wizard

Rate me:
Please Sign up or sign in to vote.
4.94/5 (27 votes)
5 May 2003CPOL5 min read 156.2K   7.9K   46  
Creates C code templates for PIC microcontrollers. The default templates are for use with the Hi-Tech (tm) PICC compiler.
// CCP1.cpp : implementation file
//

#include "stdafx.h"
#include "piccpc.h"
#include "CCP1.h"
#include <math.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CCCP1 property page

IMPLEMENT_DYNCREATE(CCCP1, CPropertyPage)

CCCP1::CCCP1() : CPropertyPage(CCCP1::IDD)
{
	//{{AFX_DATA_INIT(CCCP1)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

CCCP1::~CCCP1()
{
}

void CCCP1::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCCP1)
	DDX_Control(pDX, IDC_CCP1_DC, m_cCCP1DC);
	DDX_Control(pDX, IDC_CCP1_MODE, m_cCCP1Mode);
	DDX_Control(pDX, IDC_CCP1_FREQ, m_cCCP1Freq);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CCCP1, CPropertyPage)
	//{{AFX_MSG_MAP(CCCP1)
	ON_CBN_SELCHANGE(IDC_CCP1_MODE, OnSelchangeCcp1Mode)
	ON_CBN_SELCHANGE(IDC_CCP1_FREQ, OnSelchangeCcp1Freq)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCCP1 message handlers

BOOL CCCP1::OnKillActive() 
{
	if ( !CPropertyPage::OnKillActive() )
		return FALSE;

	int i = m_cCCP1Mode.GetCurSel();
	if ( i )
		i += 3;
	PICInfo.sfr.CCP1CON &= 0xF0;
	PICInfo.sfr.CCP1CON |= i;
	PICInfo.sfr.PIE1 &= 0xFB;
	PICInfo.sfr.PIE1 |= (IsDlgButtonChecked(IDC_CCP1_INTERRUPT)<<2);
	// assign pwm dc
	int dc = m_cCCP1DC.GetCurSel();
	PICInfo.sfr.CCPR1L = (dc>>2);
	PICInfo.sfr.CCP1CON &= 0xCF;
	PICInfo.sfr.CCP1CON |= ((dc&0x0003)<<4);
	return TRUE;
}

BOOL CCCP1::OnSetActive() 
{
	int i = PICInfo.sfr.CCP1CON&0x0F;
	if ( i ) {
		i -= 3;
	}
	m_cCCP1Mode.SetCurSel( i );
	OnSelchangeCcp1Mode();
	CheckDlgButton( IDC_CCP1_INTERRUPT, PICInfo.sfr.PIE1&0x04 );
	UpdatePWMFreq();
	UpdatePWMDC();
	return CPropertyPage::OnSetActive();
}

void CCCP1::UpdatePWMFreq( void ) {
	CString s;
	m_cCCP1Freq.ResetContent();
	for ( int i=0;i<256;i++ ) {
		double f = GetPWMFrequency( i );
		if ( f > 1000.0 ) {
			s.Format( "%G KHz", f/1000.0 );
		}
		else {
			s.Format( "%G Hz", f );
		}
		m_cCCP1Freq.AddString( s );
	}
	m_cCCP1Freq.SetCurSel( PICInfo.sfr.PR2 );
}

void CCCP1::UpdatePWMDC( void ) {
	// set PWM freq and duty cyle;
	int dc = m_cCCP1DC.GetCurSel();
	if ( dc > -1 ) {
		PICInfo.sfr.CCPR1L = dc>>2;
		PICInfo.sfr.CCP1CON |= ((dc&0x0003)<<4);
		PICInfo.sfr.PR2 = m_cCCP1Freq.GetCurSel();
	}
	CString s;
	m_cCCP1DC.ResetContent();
	int n = (int)(pow( 2, GetPWMDCResolution() ));
	for ( int i=0;i<n;i++ ) {
		double f = GetT2Prescaler()*i*(PICInfo.dInstructionTime/4.0);
		f = (f/(1.0/GetPWMFrequency(PICInfo.sfr.PR2)))*100.0;
		s.Format( "%G%%", f );
		m_cCCP1DC.AddString( s );
	}
	if ( n < 1024 )
		m_cCCP1DC.AddString( "100%" );
	dc = 0;
	dc |= (PICInfo.sfr.CCPR1L<<2);
	dc |= (PICInfo.sfr.CCP1CON&0x30)>>4;
	--n;
	m_cCCP1DC.SetCurSel( dc&n );
	UpdatePWMInfo();
}

void CCCP1::UpdatePWMInfo( void ) {
	CString s;
	double res = GetPWMDCResolution();
	s.Format( "NOTE: to modify frequency range, "
		"change Timer 2 Prescaler.\n"
		"Actual tmr2 prescaler: %G\nDuty cycle resolution: %G-bit", GetT2Prescaler(), res );
	SetDlgItemText( IDC_CCP1_INFO, s );
}

double CCCP1::GetPWMDCResolution( void ) {
	double r = log(PICInfo.dwOSCFrequency/GetPWMFrequency(PICInfo.sfr.PR2))/log(2);
	if ( r > 10.0 )
		r = 10.0;
	return r;
}

double CCCP1::GetPWMFrequency( int dc ) {
	double f = (dc+1)*PICInfo.dInstructionTime*GetT2Prescaler();
	return 1.0/f;
}

double CCCP1::GetT2Prescaler( void ) {
	double t2p = 16.0;
	switch ( PICInfo.sfr.T2CON&0x03 ) {
	case 0:
		t2p = 1.0;
		break;
	case 1:
		t2p = 4.0;
	}
	return t2p;
}

void CCCP1::OnSelchangeCcp1Mode() 
{
	if ( (m_cCCP1Mode.GetCurSel()+1) == m_cCCP1Mode.GetCount() ) {
		GetDlgItem(IDC_CCP1_FREQ)->EnableWindow();
		GetDlgItem(IDC_CCP1_DC)->EnableWindow();
		CheckDlgButton( IDC_CCP1_INTERRUPT, 0 );
		PICInfo.sfr.T2CON |= 0x04;
	}
	else {
		GetDlgItem(IDC_CCP1_FREQ)->EnableWindow( FALSE );
		GetDlgItem(IDC_CCP1_DC)->EnableWindow( FALSE );
		CheckDlgButton( IDC_CCP1_INTERRUPT, 1 );
		PICInfo.sfr.T2CON &= 0xFB;
		if ( m_cCCP1Mode.GetCurSel() == 0 )
			CheckDlgButton( IDC_CCP1_INTERRUPT, 0 );
	}
}

void CCCP1::OnSelchangeCcp1Freq() 
{
	UpdatePWMDC();
}

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Delphi
United States United States
Carlos Buelna works in the automotive industry.

Comments and Discussions