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

📄 pumpcontrol.c

📁 Program for PIC18 microcontroller used to control a pump for an oil heater.
💻 C
字号:
// This code inputs a voltage on analog pin RA0 and converts it to a 20 kHz PWM
// on pin RB3.

#include <p18f1320.h>

void ISR(void);
void ConfigurePWM(void);
void ConfigureADC(void);

unsigned int ADResult;

#pragma code ISR_vector = 0x08
void high_interrupt (void)
{
	_asm
	goto	ISR
	_endasm
}

#pragma code
#pragma interrupt ISR
void ISR(void)
{
	if(PIR1bits.ADIF)
	{
		PIR1bits.ADIF = 0;
		ADResult = ADRESH;
		ADResult = (ADResult << 2) | (ADRESL >> 6);
		CCPR1L = (ADResult * 50 / 1023);
		ADCON0bits.GO = 1;
	}	
}


void main(void)
{	
	OSCCON = 0x60; // Set internal clock to 4MHz;
	
	ConfigurePWM();

	INTCON = 0xC0;
	RCONbits.IPEN = 0; //Disable interrupt priorities
	
	ConfigureADC();

	while(1);
}


void ConfigurePWM(void)
{
	TRISBbits.TRISB3 = 1; // Set PWM pin as input to allow setup
	PR2 = 0x31; // Load 49 into PR2 register (for 5KHz PWM @ 4MHz Fosc)	
	CCPR1L = 0x00; // Set duty cycle to 0%
	CCP1CON = 0x0C;
	PWM1CON = 0x80;
	PIR1bits.TMR2IF = 0;	
	T2CON = 0x04; // Turn clock on, 1:1 prescaler
	while(!PIR1bits.TMR2IF); // Wait for timer 2 to overflow
	TRISBbits.TRISB3 = 0;
	ECCPASbits.ECCPASE = 0; // Start the PWM
}

void ConfigureADC(void)
{
	TRISAbits.TRISA0 = 1;
	ADCON1bits.PCFG0 = 0; // RA0 set analog
	ADCON0 = 0x01;
	ADCON1 = 0x01;
	ADCON2 = 0x21; // A/D result left justified, 8TAD, Fosc/8
	PIR1bits.ADIF = 0;
	PIE1bits.ADIE = 1; // Enable A/D interrupt
	ADCON0bits.GO = 1; // Start A/D acquisition	
}

⌨️ 快捷键说明

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