Newbie Help required please with PIC12F683

Status
Not open for further replies.

----AJ----

Junior Member level 1
Joined
Mar 23, 2013
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,401
Hi Guys,

I'm an absolute beginner so I've set myself a little task as follows:

I'm trying to start and stop a motor using a start button, a position sensor, and also incorporating a selector switch which will select either 1 rotation or 3 depending on its position i would also like the start button to override the 1 & 3 rotations if it still in the on position at the end of the cycle.

So in other words the 3 functions I'm trying to achieve are:
Selector switch = 1
Start button = 0,1,0
Motor Starts
Position Sensor = 0,1,0
Motor stops

Selector switch = 3
Start button = 0,1,0
Motor Starts
Position Sensor = 0,1,0,1,0,1,0
Motor Stops

Selector switch = 3
Start button = 1
Motor starts
position sensor = 10101010101010101010101010101................ etc etc
start button = 0
position sensor = 0,1,0
Motor Stops

This is the code i've written and all is well except for the fact that when I'm in 3 shot mode. So when the start button is continusly on it is overriding the count of 3 which is fine, but when its released the position sensor is still counting another 3 revolutions before stoping the motor

Here is whats happening:
Start Button = 1
Motor Starts
position sensor = 01010101010101010101............ etc etc
start button = 0
position sensor = 0101010 (this is the problem, its still counting 3 before stopping the motor, i want it to just count 1)
motor stops

I know its probably a really simple thing but i'm nearly pulling my hair out :?: any help would be appreciated.

Code:
unsigned char shots = 0;
void fire (void){
                 GPIO.B0 = 1;  // Start the motor
                 TMR0 = 0;     // Reset the counter (Timer0)
                 }
void main(void){
     TRISIO = 0b0010110; // B1 = Trigger Input switch, B2 = Counter input (Timer0), B4 = Shots Selector Switch
     CMCON0 = 7;
     ANSEL = 0;
     ADCON0 = 0;
     OPTION_REG = 0b10111000;
     TMR0 = 0;
     GPIO.B0 = 0;
while (1){
          if (GPIO.B4 == 0) shots = 3; else shots = 1;  // select the number of counts before switch off motor
          if (TMR0 == shots) GPIO.B0=0;     // when counter = shots switch off motor
          if (GPIO.B1 == 1) fire();         // trigger

}
}
 
Last edited:

Hi,

I don't have a circuit proper circuit as I'm just using the demo board which came with the PICK KIT 2

It seems to be working without the debounce code and counting correctly, so at this point I've not bothered with it. I am aware that I can use 'button' command but like I say it seems to count properly without it.

Also it seems to be working without an ISR, do you think that by adding an ISR that will cure the problem?
 

Can you please show where the timer0 is counting in the code? Your code if (TMR0 == shots) GPIO.B0=0; ckecks for TMR0==shots. When timer0 is not enabled and timer interrupt code is not implemented then how do you expect value of TMR0 will be 3 or something else to compare with the value of shots? Yes, your problem will solve if timer0 interrupt is used but what is the value of 3 for timer0? Is it 3 us, 3 ms, 3 sec, 3hours... or just 3 interrupts for some random timer0 value?
 

Unless I'm missing something:

Timer0 is enabled in the OPTION_REG as a counter and set to zero in MAIN. The way TMR0 is being used is due to the special function on GPIO.B2 so everytime GPIO.B2 = 1 the TMR0 increments by 1 (I think). Like i say, it seems to count to 3 and then switch off the motor without a problem as long as GPIO.B1 is not still =1

Maybe it shouldn't be working in that way but on the demo board it is, so I'm not sure.
 

No, I already had what you suggested in the code I'm afraid, so no.

I wonder if i should be tackling it from a different angle?

Do you think that if i added an ISR using TIMER1 to override the the count.

I.e if the start button is pressed for more that 2 seconds it overrides the current set count of 3 down to 1. Would the ISR override the set value of the counter?
 

No. You have to use external interrupt i.e., interrupt on change. See if any pin supports interrupt on change for high to low or low to high transition. If yes, use that pin to trigger external interrupt and for three triggering you will get 3 counts.
 

Yes its already doing that. The pin which is supplying the counts has that feature.

We're running out of ideas?????????????
 

 

Adding the debounce (button) makes no difference at all.

The problem is not with debounce.

1. when I press and release the start button the motor starts and the counter counts to 3 and stops the motor,no problem.

2. if I press the start button and keep it pressed the motor starts and keeps on going while the start button is being pressed and this is what I want it to do.

but in when i finally release the start button i don't want the motor to do another 3 revolutions as in (1.) above I just want it to do 1 (i.e find the start position again) so I need to override the count of 3 down to 1.

If you see what i mean.
 

Explain how your GPIO's are connected. A schematic will be helpful. Which pin is switch, which pin drives the motor, etc... Explain when you want the motor to run and when to stop i.e., the conditions.
 

here you go

The 3 functions I'm trying to achieve are:
Selector switch = 1 (sets counter value to 1) (i.e stay-put switch)
Start button = 0,1,0 (i.e Momentary)
Motor Starts
Position Sensor = 0,1,0 (i.e Momentary)
Motor stops

Selector switch = 0 (sets counter value to 3)
Start button = 0,1,0
Motor Starts
Position Sensor = 0,1,0,1,0,1,0
Motor Stops

Selector switch = 0 (sets counter value to 3)
Start button = 1 (it stays pressed in)
Motor starts
position sensor = 10101010101010101010101010101................ etc etc
start button = 0
position sensor = 0,1,0
Motor Stops


i can see in my code why its doing what its doing but I can't work out a way of solving the issue. What is happening is that whilst the start button is being pressed its holding the counter at ZERO then when its released that when its counting.

I.e this is what its doing

Start Button = 1 (this is holding the counter at ZERO)
Motor Starts
position sensor = 01010101010101010101............ etc etc
start button = 0 (when released the counter is then able to count)
position sensor = 0101010 (this is the problem, its still counting 3 before stopping the motor, i want it to just count 1)
motor stops
 
Last edited:

sorry my mistake i've ammended it, it should have read the same as function 2. i.e 'Selector switch = 0 (sets counter value to 3)'

The position sensor is there to stop the motor at the same location every time. it could be connected to a gear or whatever its not important.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…