epwm.c

来自「picc_18v 8.35pl35 PIC18系列单片机开发C编译器」· C语言 代码 · 共 55 行

C
55
字号
#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 + =
减小字号Ctrl + -
显示快捷键?