//configure internal oscilator, watch dog off, low voltage protection off
#pragma config OSC = INTIO2, WDT = OFF, LVP = OFF
#include <p18f1320.h>
//#include <delays.h>
//#include<timers.h>
//setup pins for LED control
#define SoundPin LATBbits.LATB4
#define SoundTris TRISBbits.TRISB4
unsigned int soundPeriod = 40;
unsigned int freq = 1000;//in hz
unsigned int volume = 50;//in percent
unsigned int counter1 = 0;
unsigned int counter2 = 0;
//set up interrupt
void low_ISR(void);//prototype
#pragma code low_vector = 0x08 //0X08 IS LOW 0X18 IS HIGH
void low_interrupt (void){
_asm goto low_ISR _endasm
}
#pragma code
#pragma interrupt low_ISR
void main(void)
{
// speed up the clock to 8MHz, 18F1320
OSCCONbits.IRCF0=1;
OSCCONbits.IRCF1=1;
OSCCONbits.IRCF2=1;
//timer setup
INTCONbits.GIE = 1; //enable global interupts
INTCONbits.PEIE = 1; //enable periferial interrupts
IPR1bits.TMR2IP = 0; //low priority
PIE1bits.TMR2IE = 1; //enable timer 2
//Timer2 Registers Prescaler= 4 - TMR2 PostScaler = 2 - PR2 = 1 - Freq = 250000.00 Hz - Period = 0.000004 seconds
T2CON |= 8; // bits 6-3 Post scaler 1:1 thru 1:16
T2CONbits.TMR2ON = 1; // bit 2 turn timer2 on;
T2CONbits.T2CKPS1 = 0; // bits 1-0 Prescaler Rate Select bits
T2CONbits.T2CKPS0 = 1;
PR2 = 1; // PR2 (Timer2 Match value)
soundPeriod = 250;
SoundTris = 0;
SoundPin = 1;
while(1){}
}
void low_ISR(void)
{
//Timer 2 flag interrupts at 250000.00 Hz
if(PIR1bits.TMR2IF)
{
PIR1bits.TMR2IF = 0; //clears the timer 1 interupt flag
counter1++;
if(counter1 > soundPeriod)
{
counter1 = 1;
}
if(counter1 > soundPeriod/2)
{
SoundPin = 1;
}
else
{
SoundPin = 0;
}
}
}