1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| /** I N C L U D E S **************************************************/
#include "p18f4620.h"
#include "delays.h"
#include <stdio.h>
#include <stdlib.h>
#include <usart.h>
/**Bits de configuration**********************************************/
#pragma config OSC=HSPLL
#pragma config PWRT=ON
#pragma config BOREN=OFF
#pragma config WDT=OFF
#pragma config MCLRE=ON
#pragma config PBADEN=OFF
#pragma config LVP=OFF
#pragma config XINST=OFF
#define CLOCK_FREQ (40000000) // Hz
/*#pragma config OSC = INTIO7//Internal oscillator block, port function on RA6 and RA7
#pragma config FCMEN = OFF//Fail-Safe Clock Monitor disabled
#pragma config PWRT = OFF//Power-up Timer disabled
#pragma config WDT = OFF//Watchdog Timer disabled
#pragma config MCLRE = ON//MCLR pin enabled; RE3 input pin disabled*/
/**Prototypes**********************************************************/
void InitInterrupt();
void InitUART(void);
void Fonction (int fct);
void UpdateDutyCycle();
void low_isr();
/**Variables***********************************************************/
unsigned int cnt8000 = 0;
char cntDty = 0;
char boolUpdateDty = 0;
const unsigned char Dty [24] = {128, 160, 191, 218, 238, 251, 255, 251, 238, 218, 191, 160, 128, 95, 64, 37, 17, 4, 0, 4, 17, 37, 64, 95}; // Testing
unsigned long int tempDty1 = 0, tempDty2 = 0;
/*SETUP ISR VECTOR******************************************************/
#pragma code low_vector=0x18 //setup the ISR (interupt service routenes) vector
void low_interrupt (){
_asm GOTO low_isr _endasm //jump to interrupt handler
}
#pragma code
/*ISR********************************************************************/
#pragma interruptlow low_isr //the ISR
void low_isr()
{
if (PIR1bits.TMR2IF == 1)
{
PIR1bits.TMR2IF=0;
boolUpdateDty = 1;
if (cnt8000 < 8000)
cnt8000++;
else
cnt8000 = 0;
}
}
#pragma code
/** D E C L A R A T I O N S *******************************************/
void main()
{
InitInterrupt();
T2CONbits.TMR2ON = 1;
while (1)
{
if (boolUpdateDty==1)
{
boolUpdateDty = 0;
UpdateDutyCycle();
}
}
}
void InitInterrupt()
{
PIE1bits.TMR2IE = 1; //enable Timer2 interrupt
INTCONbits.PEIE = 1; //enable peripheral interrupts
INTCONbits.GIE = 1; //enable glogal interrupts
IPR1bits.TMR2IP = 0; // low priority
}
void UpdateDutyCycle()
{
if ((cnt8000%334)==0)
{
tempDty1 = 50000/255;
tempDty2 = Dty[cntDty]*tempDty1/100;
SetDCPWM1(tempDty2);
if (cntDty < 22)
cntDty++;
else
cntDty = 0;
}
} |
Partager