?? pumpcontrol.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
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -