Hi, I am fairly new to programming PIC's, I have been dabbling for a few years but never really needed to create much more than timed switches which I have created by hacking up example code from the Development kit I purchased a few years back; a MikroElectronica EasyPIC 5.
Right now I am off work ill and while sat here decided to get more involved with Microcontrollers to keep my mind active so I found a small project to keep me busy and I am stuck at the first stage.
The first part of the project to to emulate on the bench 2 timing wheels so I can then play with the control unit without it being attached to the mechanics.
There are 2 wheels, one is effectively a speed sensor that has 4 x 5 degree lugs on a steel wheel 90 degrees apart this wheel turns twice every cycle, so I am basing all other events on the 720 degree cycle of this wheel, then there is a second wheel that spins at 50% the speed of wheel 1 that has 2 lugs on it 45 degrees apart that pulses at 45 and 135 degrees in the cycle. Below is a graphics overview of the cycle:
DE 0 90 180 270 360/450/540/630/
W1 =================^=================^=================^=================^===..................
W2 ^ ^
WHEEL 1 PULSE @ 0/90/180/270/350/450/540/630
WHEEL 2 PULSE @ 45/135
I thought the best way of doing this would be to use the timer of a pic, but although I can create perfectly the pulse of wheel 1, as soon as I add code to add the second wheels pulse it stops working; it looks like it works but then about every 5 pulses it seems to reset.
Below is the code I have tried to use which is simply a hacked up Microelectronics example.
unsigned cnt;
void interrupt() {
cnt++; // Increment value of cnt on every interrupt
TMR0 = 96;
INTCON = 0x20; // Set T0IE, clear T0IF
}
void main() {
OPTION_REG = 0x87; // Assign prescaler to TMR0 prescale 7=256 4=32
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
TRISB = 0; // PORTB is output
TRISD = 0; // PORTD is output
PORTB = 0x00; // Initialize PORTB
PORTD = 0x00; // Initialize PORTD
TMR0 = 96; // Timer0 initial value
INTCON = 0xA0; // Enable TMRO interrupt
cnt = 0; // Initialize cnt
do {
if (cnt < 18){ // 1 - 17 pulse on port B
PORTB = ~PORTB; // Toggle PORTB LEDs
}
if (cnt == 18){ // 18 2nd pulse port D
PORTB = ~PORTB; // Toggle PORTB LEDs
PORTD = ~PORTD; // Toggle PORTD LEDs
}
if (cnt == 19){ // 2nd Pluse Off 5 degrees
PORTB = ~PORTB; // Toggle PORTB LEDs
PORTD = ~PORTD; // Toggle PORTD LEDs
cnt = 1;
}
} while(1);
}
Can someone point me in the right direction to recreate these pluses with a Pic, I am sure its either me using the timer wrong (not understanding it fully) or simply I am trying to use the wrong feature to do this, maybe a pwm output may be better ?