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

📄 epwm.c

📁 picc_18v 8.35pl35 PIC18系列单片机开发C编译器
💻 C
字号:
#include <pic18.h>
#include <stdio.h>

void get_dutycycle(void);
void get_period(void);
void update_pwm(void);
extern void putch(unsigned char);

unsigned int DUTYCYCLE=0x0080;
unsigned char PERIOD=0xFF;

void get_dutycycle(void)
{
	ADFM=1;	/* need 10 bit result, right justify */
	ADCON0=0xC9;	/* select the channel of the duty cycle input pot */
	GODONE=1;
	while(!ADIF)continue;
	ADIF=0;
	DUTYCYCLE=ADRES;	/*right justified 10 bit result */
}

void get_period(void)
{
	ADFM=0;	/* period is an 8 bit value, left justify to get result from ADRESH */
	ADCON0=0xC1;	/* select the channel of the period input pot */
	GODONE=1;		/* start A2D */
	while(!ADIF)continue;
	ADIF=0;
	PERIOD=ADRESH;	/* Return 8 most signifigant bits of result */ 
}
		
void update_pwm(void)
{
unsigned int percentage;
static unsigned int old_percentage;

	get_period();
	get_dutycycle();
	
/* update the PWM period SFR */
	PR2=PERIOD;
/* update the PWM duty cycle SFRs */
	EDC1B0=(bit)DUTYCYCLE;
	EDC1B1=(bit)(DUTYCYCLE>>1);
	ECCPR1L=(DUTYCYCLE>>2);
/* update results */
	if (PERIOD==0) percentage=0xFFFF;
	else	percentage=(DUTYCYCLE*100/PERIOD);
	if (percentage!=old_percentage)
		printf("\rPeriod: %X, Duty: %X                    \r",PERIOD,DUTYCYCLE);
	old_percentage=percentage;
	
}

⌨️ 快捷键说明

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