Sunteți pe pagina 1din 3

#include <p32xxxx.

h>
#include <plib.h>
/*
*
*/
int b=0; int puls=0;
int a=0;
int desbord=0;
#pragma config POSCMOD = HS // modo de alta velocidad cristal
#pragma config FNOSC = PRIPLL // Uso oscilador principal con PLL (XT, SA, o EC)
#pragma config FPLLIDIV = DIV_1 // Dividir 12MHz a entre 4-5MHz antes de PLL (ahora
4 MHz)
#pragma config FPLLMUL = MUL_20 // Multiplicar con PLL (ahora 80 MHz)
#pragma config FPLLODIV= DIV_2 // Dividir Despu�s de PLL (ahora 40 MHz)
// see figure 8.1 in datasheet for more info
#pragma config FWDTEN = OFF // Watchdog Timer Disabled
#pragma config ICESEL = ICS_PGx1 // ICE/ICD Comm Channel Select
#pragma config JTAGEN = OFF // Disable JTAG
#pragma config FSOSCEN = OFF // Disable Secondary Oscillator
float valor=0;
int k=0, i;
// Defines
#define SYSCLK 40000000L // System Clock Frequency
// Macros
#define setPR2(seconds) (seconds * SYSCLK / 256)

void delay(float valor){


#define BLINK_DELAY 500
valor= (valor*10);}
void delay_us( unsigned t) // See Timers tutorial for more info on this
function
{
T1CON = 0x8030; // enable Timer1, source PBCLK, 1:1 prescaler

// delay 100us per loop until less than 100us remain


while( t >= 100){
t-=100;
TMR1 = 0;
while( TMR1 < SYSCLK/10000);
}

// delay 10us per loop until less than 10us remain


while( t >= 10){
t-=10;
TMR1 = 0;
while( TMR1 < SYSCLK/100000);
}

// delay 1us per loop until finished


while( t > 0)
{
t--;
TMR1 = 0;
while( TMR1 < SYSCLK/1000000);
}
while( t >= 0.1)
{ t-=0.1;
TMR1 = 0;
while( TMR1 < SYSCLK/10000000);
}
while( t >= 0.01)
{ t-=0.01;

TMR1 = 0;
while( TMR1 < SYSCLK/100000000);
}

// turn off Timer1 so function is self-contained


T1CONCLR = 0x8000;
} // END delay_us()

int leeADC(int ch){


AD1CHSbits.CH0SA=ch;
AD1CON1bits.SAMP=1;
while(!AD1CON1bits.DONE);
return ADC1BUF0;
}
void main(){
ANSELB=0;
SYSTEMConfigPerformance(SYSCLK);
AD1CON1=0x00E0;
AD1CSSL=0;
AD1CON2=0;
AD1CON3bits.SAMC=7;
AD1CON3bits.ADCS=3;
AD1CON1bits.ADON=1;
TRISBbits. TRISB5 = 0 ; // RB5 -> salida
TRISBbits. TRISB4 = 0 ; // Set RB5 as digital output
PORTSetPinsAnalogIn(IOPORT_B, BIT_1);
TRISBbits.TRISB2=1;
TRISBbits.TRISB3=0;
TRISBbits.TRISB4=1;
LATBbits.LATB3=1;

// Turn on 16-bit Timer2, set prescaler to 1:256 (frequency is Pbclk / 256)


T2CON = 0x8070;
// The slowest the interrupt will occur is every:
// prescaler * PR2max / Pbclk = 256 * 65535 / 40MHz = 0.42s
// thus, a setPR2() argument of anything above 0.42 will give undesired results
PR2 = setPR2(0.0005); // This will cause a full blink every 0.4s

// Only one interrupt source (Timer2) but still in multivectored mode


// : found in plib.h
INTEnableSystemMultiVectoredInt();

// Configure Interrupts
// In datasheet: TABLE 7-1: INTERRUPT IRQ, VECTOR AND BIT LOCATION
IEC0CLR = 0x0200; // disable Timer2 int, IEC0<9>
IFS0CLR = 0x0200; // clear Timer2 int flag, IFS0<9>
IPC2CLR = 0x001f; // clear Timer2 priority/subpriority fields IPC2<4:0>
IPC2SET = 0x0010; // set Timer2 int priority = 4, IPC2<4:2>
IPC2SET = 0x0000; // set Timer2 int subpriority = 0, IPC2<1:0>
IEC0SET = 0x0200; // enable Timer2 int, IEC0<9>
// Now we just wait in an infinite loop while interrupts do their thing!
while( 1){
if (PORTBbits.RB2==1){
a=0;
if(PORTBbits.RB4==1){
LATBbits.LATB5=0;
LATBbits.LATB3=0;
} if(PORTBbits.RB4==0){
LATBbits.LATB3=1;
LATBbits.LATB5=0;
}
}else{

if(desbord==1){
delay_us(1);
LATBINV = 0x0008;
desbord=0;}
}};
}

// Timer2 Interrupt Service Routine


void __ISR(8, ipl4) Timer2IntHandler(void){
if(PORTBbits.RB2==0){ a++;

if(a<puls){
if(desbord==0){
desbord=1;
}p
// note that iplx (interrupt priority level) must match the timers interrupt
priority level

LATBINV = 0x0020;
}else{desbord=0;
LATBbits.LATB5=0;
LATBbits.LATB3=0;
}} else{a=0; puls=leeADC(3)*16384/1023;//an3
if(PORTBbits.RB4==1){
LATBbits.LATB5=0;
LATBbits.LATB3=0;
} if(PORTBbits.RB4==0){
LATBbits.LATB3=1;
LATBbits.LATB5=0;
}
}

IFS0CLR = 0x0200; // clear timer 2 int flag, IFS0<9>


} // END Timer2 ISR

S-ar putea să vă placă și