Hi all,
Go easy on me, I'm very fresh to this, it's taken me all day to write this and the damn thing isn't working.
Let's start with the code:
#define _XTAL_FREQ 1000000
#define PULSE LATAbits.LATA0 //define pin RA0 as PULSE pin
#define DIR LATAbits.LATA1 //define pin RA1 as DIRECTION pin
#define SPEED PORTBbits.RB0 //Define SPEED H/L Pin
#define PROGDIR PORTBbits.RB2 //Define Direction Programming Pin
//Global Variables
int T0COUNT = 0; //Variable for Timer0 Counter
int LPFLG = 0; //Flag for MEDIUM PUMP speed timer
int MEDTIME = 5; //Loops for TIMER0 to run
//Set Timer Interrupt Procedure
void interrupt high_isr (void){
if(INTCONbits.TMR0IF == 1) { //Check for status of TMR0 Overflow
INTCONbits.TMR0IF == 0; //if OVERFLOW occurs reset TMR0 overflow flag.
TMR0L == 0; //Set TMR0 counter to 0, adjust this for time offsets
T0COUNT++; //Increment T0COUNT by 1 for every time TMR0 overflows.
if(T0COUNT==MEDTIME) {
LPFLG = 1; //When counter total matches time required, set flag to 1
}
}
if(INTCON3bits.INT3IF == 1){ //if programming pin is HIGH
INTCON3bits.INT3IF == 0; //Clear interupt flag
TRISAbits.RA0 == 1; //switch off motor
TRISBbits.RB2 == 1; //enable RB2 PROGDIR pin
if(PROGDIR == 1){ //check for HI PROGDIR pin
__delay_ms (50); //wait 50ms for bounce
if(PROGDIR == 1){
DIR= ~DIR;
}
}
}
}
int main (){
OSCCONbits.IRCF2 = 1; //Set clock speed to 1MHz
OSCCONbits.IRCF1 = 0; //Set clock speed to 1MHz
OSCCONbits.IRCF0 = 0; //Set clock speed to 1MHz
RCONbits.IPEN = 1; //enable interrupt priority
INTCONbits.GIE = 1; //enable all interupts
T0CONbits.TMR0ON = 0; //switch timer OFF in initial state
T0CONbits.T0PS0 = 1; //set TIMER0 prescaler to 1:256 on TOP0-2
T0CONbits.T0PS1 = 1;
T0CONbits.T0PS2 = 1;
T0CONbits.PSA = 1; //Enable pre-scaler
T0CONbits.T0CS = 0; //TIMER0 uses Internal Osccilator
T0CONbits.T016BIT = 0; //TIMER0 uses 16 bit counter
ADCON1 = 0xFF; //switch OFF Port-A ADC
CMCON = 0xFF; //switch OFF comparitors
PWMEN1 = 0xFF; //switch OFF PWM module
INTCON3bits.INT3E = 1; //enable INT3 External Interrupt
INTCON2bits.INTEDG3 = 1; //enable INT3 RIDING EDGE interrupt
TRISAbits.RA0 = 0; //define A0 as OUTPUT
TRISAbits.RA1 = 0; //define A1 as OUTPUT
TRISBbits.RB0 = 1; //define B0 as INPUT
DIR = 1; //Change value to '0' for reversed motor
while (1) //Do this forever
if(SPEED == 1 ) { //if SPPED pin HI
T0CONbits.TMR0ON = 1; //switch timer ON
if(LPFLG == 0){ //If Loop Counter FLAG LO
PULSE= ~PULSE; //Set PULSE toggle
__delay_us(25); //Toggle pulse MEDIUM
}
if(LPFLG == 1 ) { //if Loop Counter FLAG HI
T0CONbits.TMR0ON = 0; //Switch TIMER0 OFF!
PULSE= ~PULSE; //Set PULSE toggle
__delay_us(50); //Toggle pulse SLOW
}
}
else {
T0CONbits.TMR0ON = 0; //Switch TIMER0 OFF!
LPFLG == 0; //Reset loop counter
PULSE= ~PULSE;
__delay_us(5); //Toggle pulse FAST
}
return 0;
}
Now, this is as best I can explain what I'd hope it would have done!
- Initially the output on RB0 should be 'medium' for five minutes (roughly)
- Timer0 (16bit) should overflow 5 times, I want this to increment T0COUNT by 1 every time it goes around!
- When the number of times around the loop matches the MEDTIME it should set LPFLG to 1
- The output should then sit at 'SLOW' where it could theoretically sit for weeks in the application!
[li]If RB0 goes LOW the FAST cycle should be started, then the whole damn she-bang should reset and do the whole 5-minute nonsense again.
[/li][/list]
All other functions work fine, but in my emulator I'm not getting a loop increment, the thing just sits in 'MEDIUM' with RB0-HIGH and runs as expected with RB0 is low.
I only learned about interupts at 10AM this morning, so please go easy on me, I'm very new to all of this!
Any input most welcome!
Ed