RCU Forums - View Single Post - New CDI - opensource project JMJ and Bigboat
Old 02-19-2010 | 04:24 AM
  #848  
Rallyfinnen
Senior Member
 
Joined: Dec 2009
Posts: 106
Likes: 0
Received 0 Likes on 0 Posts
From: , SWEDEN
Default RE: New CDI - opensource project JMJ and Bigboat

Hello guys!

I have now ventured into the programming world.. and I have no ideaf if I'm lost or not

Below is the code where I tried to make some changes (bold text) to use the 16bit timer1, what do you think?

void main() {
//
const unsigned int tspark[]={ 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 25, 25, 25, 26, 26, 27, 26, 27, 27, 26, 27, 27, 26, 26, 26, 27, 26, 26, 27, 27, 27, 25, 25, 26, 26, 26, 24, 24, 24, 25, 25, 25, 22, 22, 23, 23, 23, 23, 24, 22, 22, 23, 23, 23, 24, 24, 24, 22, 22, 23, 23, 23, 23, 24, 24, 24, 24, 25, 23, 23, 23, 23, 24, 24, 24, 24, 24, 25, 25, 25, 25, 23, 23, 23, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 26, 26, 26, 23, 23, 24, 24,
24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 27, 27, 27, 27, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 30, 30, 30, 30, 30, 30, 30, 30, 31, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28};
// Data type changed to int for 16bit timer values

short int spark=0;
unsigned int tspk, t0,t1,low;
unsigned int ofc; //Holds number of overflows
const unsigned int LowDelay=25; //Delay <1832 RPM, in degrees
const unsigned int ld=360/LowDelay; //Dont change this
TRISB = 0x0E; // set RB1,2,3 for input, all others for output RB0=SCR RB4=Strobe
PORTB = 0x11; // set gate to SCR high i.e pin 6 of PIC +strobe
OPTION_REG = 0x06; // tmr0 SOURCE INT CLOCK, 1:128 PRESCALER
INTCON = 0x00; //Disable interrupts +Resets interrupt flags
TMR1 = 0; // Reset timer1
TMR0 = 0; // Reset timer0
PR2 = 255; // This is how far timer2 counts before overflow
T1CON = 0x05; // start TMR1 counter 1:1 using internal clock ??TMR1ON 0x04 or 0x05??
while(1){ //Loop forever
ofc=0; // Reset overflow teller
INTCON = 0x00; // Disable interrupts + Resets interrupt flags
while(PORTB & 0x02){ //Wait for low pulse on Hall switch
if (INTCON&4) { // Overflow
ofc++; //Increase overflow counter
INTCON = 0x00; // Resets interrupt flags
} // End IF
spark = 1;
} //North pole of magnet has passed
if(spark){
t0 = TMR0;
TMR0 = 0; //Start new rev count
if(ofc > 0) { //Overflow timer0, we have < 1832 RPM
if ( ofc >= 36) ofc=36; //Min RPM 50 to avoid big calculations
low=ofc*ld; //Simplified calculation, gives about 25 degrees
delay_cyc (low); //extra delay based on overflow counter
delay_cyc (low);
delay_cyc (low);
delay_cyc (low);
delay_cyc (low); //Repeat 16 times - prescaler
delay_cyc (low);
delay_cyc (low);
delay_cyc (low);
delay_cyc (low);
delay_cyc (low);
delay_cyc (low);
delay_cyc (low);
delay_cyc (low);
delay_cyc (low);
delay_cyc (low);
delay_cyc (low);
} // max 255 -> 2550 cycles
tspk = tspark[t0]; //use table lookup for spark delay
T1CON = 0x05 // start TMR1 counter 1:1 using internal clock ??TMR1ON 0x04 or 0x05??
TMR1
= 0; // clear spark timer
while(TMR1 < tspk); // loop until timeout to send spark
PORTB &= 0xee; //sends a ground to the SCR gate
Delay_us(1650); // Calculated delay
PORTB |= 0x11; //ground off the SCR
spark = 0;
} // end if(spark)
} // end while(1)
} // end main