I use GCB-SynWrite to make a code for PIC12F683. Inside of Do...Loop I must reset the PIC. The Goto 0
not work in case3, works only in case1,case2 of Select Case .Is there some way to reset the PIC or initialize the code? There is no unused pin to use MCLR. I use only one switch (GPIO.3) to change modes 1-3 with momentary pressing ( using Interrupt GPIOCHANGE ) and with pressing 2sec or more to reset the code. Below is part of code:
;Chip Settings
chip 12F683,8
config OSC=INTRC_OSC_NOCLKOUT
;Variables
Dim Rbat As word
;Interrupt Handlers
On Interrupt GPIOCHANGE Call ChangeMode
Dir GPIO.3 In
Dir GPIO.4 In
Dir GPIO.2 Out
Dir GPIO.5 Out
Dir GPIO.0 Out
Dir GPIO.1 Out
Set IOC3 On
HPWM 1,20,0
Mode = 12
Main:
GoSub Reset2s
If CONTROL3=True Then
cc1:
HPWM 1,20,120
If GPIO.4 Off Then
HPWM 1,20,110
wait 10 ms
Else HPWM 1,20,120
wait 10 ms
If GPIO.3 Off Then
i=0
Do While GPIO.3 Off
Wait 50 ms
i=i+1
If i>40 Then
GoTo 0
wait 3 s
End If
Loop
GoTo main
End If
GoTo cc1
End If
Goto main
Sub ChangeMode
debounce=0
glitch=0
CheckSwitch:
If debounce=>3 then
goto Debounced
end if
If GPIO.3 Off Then
debounce=debounce+1
Else
glitch=glitch+1
end if
wait 10 ms
If glitch=>2 then
GoTo Glitched
end if
GoTo CheckSwitch
Debounced:
Mode = Mode + 1
if Mode= 13 then
Mode= 1
end if
Select Case Mode
Case 1
HPWM 1,20,30
Case 2
HPWM 1,20,60
Case 3
CONTROL3=True
end Select
Glitched:
End Sub
Sub Reset2s
If GPIO.3 Off Then
i=0
Do While GPIO.3 Off
Wait 50 ms
i=i+1
If i>40 Then
GoTo 0
wait 3 s
End If
Loop
End If
End Sub
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
As for the rest of the code, it is what some might call "spaghetti code". It is generally unintelligible due to bad code structure. I suggest you take the time to review the code structure and the excessive use of "goto".
When posting code, indent the code with a tab or 4 spaces. This will keep the code formatted and prevent directives from using large fonts.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
William, thanks for suggestions. About code structure, this is not the original structure as at GCB editor, but after paste of code. About PIC12F683 reset, asm reset not work, maybe not supported by GCB-SynWrite. Is there a way to reset the PIC anywhere in the code using On Interrupt TIMER1OVERFLOW ? It is possible to use it with On Interrupt GPIOCHANGE?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anobium, below is the code. Is experimental, so you will see many mistakes. The basic idea is as follows: A PIC 12F683 controls 54 white leds (2835) in 18 strings of 3.Another 3 pins controls 2 strings of 3 red led the same type (GPIO 0,1,5). 2 pins are used one for a tact switch (GPIO.3), and the other to control the white led current. The input power is from a car battery 12V , so because the input voltage may vary from 11V to 15v , is very important to stabilize mainly the white led current.There are 4 stages for white led and 8 for red led. On every stage, with pressing the switch for 2 sec. the code must reset (leds off). The white led current stabilization is better in my opinion to realize with software.I send you the schematic. Thanks.
I am trying to understand "reset" in your description. You say" pressing the switch for 2 sec. the code must reset (leds off)" So this is not a Hard reset but just shutting off the LEDs.
Going to some initialization routine with all the LEDs off would be much safer than doing a hard reset.
Hard reset not only resets the Program counter to '0' but initializes several other registers. Actually you are out of control for a time.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Please add comments to your code so that others can tell what you are trying to accomplish. Just because the code is "experimental" does not mean it should be
void of comments. Commenting is important, especially if you want others to help you.
I see absolutely no good reason to "RESET" the PIC. None.
Please explain why you think you need to "reset" the PIC in the code. Perhaps explain what you are trying to accomplish with the reset.
I can assure you that asm reset works just fine. It is your code/circuit that is not working. Or you do not correctly understand the use of reset.
I cannot imagine that this code is actually controlling any LEDs effectively.
When designing a circuit and developing code to operate/control a circuit it is generally a good practice to do it one stage at a time. Get that stage working,, then add the next. So on and so forth.
If the hardware design is flawed then the code will never work.
Last edit: William Roth 2015-05-15
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Comments for sure and I would recommend giving the GPIO pins names as well. Like: #define LEDS GPIO.2 or #define BUTTON GPIO.3 or #define LEDSTATUS GPIO.4. It will help reading the code.
Last edit: lhatch 2015-05-15
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You're right mmote. Basically I would like to start the code after reset (or some kind of code initialization) from Case 12, pressing the switch for more that specific time at any Select Case. I tried GOTO 0, it works at Case 1 and Case 2 , but not at Case 3. I would not like simply to shut off the leds with HPWM (that's easy and works), because that not initialize the code, so with the next pressing of switch the code continues to the next Case.Thanks.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Why not put the input voltage through a resistor divider to knock it down to under 5 volts and then read that voltage with an Analog to Digital input. Then you can read that value in your main loop and adjust the current dynamically to match the adjusting voltage dynamically instead of resets or delays.
This is how production automotive lighting systems do it.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I reviewed the schematic and this circuit/code scheme will not work. The transistor will be switching the GPIO.4 Pin at 20 KHz due to no hysteresis and poor signal integration.
It the integrator (Low Pass Filter) values were corrected and the transistor was replaced with comparator (with hysteresis > ripple) the signal at GPIO.4 will not oscillate.
Even if this were fixed, this is still a very poor way to try to control LED's.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
William , I agree with your notices ,so I added some comments in the code.I think that this code is simple even for me that I'm very new in programming and hardware design. So I'm learning correcting the mistakes.I make some changes in code deleting the current control , trying to find a hardware solution to limit the led current.About reset,I used wrong word. I mean that I would like to initialize the program counter to begin the mode loop from mode1.Below are the corrected code and schematic.
Many thanks again.
The transistor Q2 cannot turn on fast enough to effectively limit peak current. There will be a high current spike before the transistor turns on.
If Q2 turns on when GPIO.2 is High ( It will be), then the High output of GPIO.2 will be shorted to ground through Q2. This will likely damage the PIC. There needs to be some resistance between GPIO.2 and the MOSFET Gate
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
William, I think you're right , I'll see the pulse in the scope.In your opinion is there a way to limit the current effectively? What is the better method for that,with software or hardware? I made a similar circuit with led drivers that works fine,but I would like to try to make the same with a small MCU because of smaller PCB size.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I use GCB-SynWrite to make a code for PIC12F683. Inside of Do...Loop I must reset the PIC. The Goto 0
not work in case3, works only in case1,case2 of Select Case .Is there some way to reset the PIC or initialize the code? There is no unused pin to use MCLR. I use only one switch (GPIO.3) to change modes 1-3 with momentary pressing ( using Interrupt GPIOCHANGE ) and with pressing 2sec or more to reset the code. Below is part of code:
;Chip Settings
chip 12F683,8
config OSC=INTRC_OSC_NOCLKOUT
;Variables
Dim Rbat As word
;Interrupt Handlers
On Interrupt GPIOCHANGE Call ChangeMode
Dir GPIO.3 In
Dir GPIO.4 In
Dir GPIO.2 Out
Dir GPIO.5 Out
Dir GPIO.0 Out
Dir GPIO.1 Out
Set IOC3 On
HPWM 1,20,0
Mode = 12
Main:
GoSub Reset2s
If CONTROL3=True Then
cc1:
HPWM 1,20,120
If GPIO.4 Off Then
HPWM 1,20,110
wait 10 ms
Else HPWM 1,20,120
wait 10 ms
If GPIO.3 Off Then
i=0
Do While GPIO.3 Off
Wait 50 ms
i=i+1
If i>40 Then
GoTo 0
wait 3 s
End If
Loop
GoTo main
End If
GoTo cc1
End If
Goto main
Sub ChangeMode
debounce=0
glitch=0
CheckSwitch:
If debounce=>3 then
goto Debounced
end if
If GPIO.3 Off Then
debounce=debounce+1
Else
glitch=glitch+1
end if
wait 10 ms
If glitch=>2 then
GoTo Glitched
end if
GoTo CheckSwitch
Debounced:
Mode = Mode + 1
if Mode= 13 then
Mode= 1
end if
Select Case Mode
Case 1
HPWM 1,20,30
Case 2
HPWM 1,20,60
Case 3
CONTROL3=True
end Select
Glitched:
End Sub
Sub Reset2s
If GPIO.3 Off Then
i=0
Do While GPIO.3 Off
Wait 50 ms
i=i+1
If i>40 Then
GoTo 0
wait 3 s
End If
Loop
End If
End Sub
Goto 0 is not the preferred way to reset the PIC.
Use "asm reset" instead
Example:
As for the rest of the code, it is what some might call "spaghetti code". It is generally unintelligible due to bad code structure. I suggest you take the time to review the code structure and the excessive use of "goto".
When posting code, indent the code with a tab or 4 spaces. This will keep the code formatted and prevent directives from using large fonts.
William, thanks for suggestions. About code structure, this is not the original structure as at GCB editor, but after paste of code. About PIC12F683 reset, asm reset not work, maybe not supported by GCB-SynWrite. Is there a way to reset the PIC anywhere in the code using On Interrupt TIMER1OVERFLOW ? It is possible to use it with On Interrupt GPIOCHANGE?
Would you post your code as an attachment please. This may help us.
Cheers.
Anobium, below is the code. Is experimental, so you will see many mistakes. The basic idea is as follows: A PIC 12F683 controls 54 white leds (2835) in 18 strings of 3.Another 3 pins controls 2 strings of 3 red led the same type (GPIO 0,1,5). 2 pins are used one for a tact switch (GPIO.3), and the other to control the white led current. The input power is from a car battery 12V , so because the input voltage may vary from 11V to 15v , is very important to stabilize mainly the white led current.There are 4 stages for white led and 8 for red led. On every stage, with pressing the switch for 2 sec. the code must reset (leds off). The white led current stabilization is better in my opinion to realize with software.I send you the schematic. Thanks.
Anobium, below is the schematic.
I am trying to understand "reset" in your description. You say" pressing the switch for 2 sec. the code must reset (leds off)" So this is not a Hard reset but just shutting off the LEDs.
Going to some initialization routine with all the LEDs off would be much safer than doing a hard reset.
Hard reset not only resets the Program counter to '0' but initializes several other registers. Actually you are out of control for a time.
@Basil,
Please add comments to your code so that others can tell what you are trying to accomplish. Just because the code is "experimental" does not mean it should be
void of comments. Commenting is important, especially if you want others to help you.
I see absolutely no good reason to "RESET" the PIC. None.
Please explain why you think you need to "reset" the PIC in the code. Perhaps explain what you are trying to accomplish with the reset.
I can assure you that asm reset works just fine. It is your code/circuit that is not working. Or you do not correctly understand the use of reset.
I cannot imagine that this code is actually controlling any LEDs effectively.
When designing a circuit and developing code to operate/control a circuit it is generally a good practice to do it one stage at a time. Get that stage working,, then add the next. So on and so forth.
If the hardware design is flawed then the code will never work.
Last edit: William Roth 2015-05-15
Comments for sure and I would recommend giving the GPIO pins names as well. Like: #define LEDS GPIO.2 or #define BUTTON GPIO.3 or #define LEDSTATUS GPIO.4. It will help reading the code.
Last edit: lhatch 2015-05-15
You're right mmote. Basically I would like to start the code after reset (or some kind of code initialization) from Case 12, pressing the switch for more that specific time at any Select Case. I tried GOTO 0, it works at Case 1 and Case 2 , but not at Case 3. I would not like simply to shut off the leds with HPWM (that's easy and works), because that not initialize the code, so with the next pressing of switch the code continues to the next Case.Thanks.
Why not put the input voltage through a resistor divider to knock it down to under 5 volts and then read that voltage with an Analog to Digital input. Then you can read that value in your main loop and adjust the current dynamically to match the adjusting voltage dynamically instead of resets or delays.
This is how production automotive lighting systems do it.
Thanks Chuck for good idea . I'll try to test a resistor divider for input voltage with ReadAD.
I reviewed the schematic and this circuit/code scheme will not work. The transistor will be switching the GPIO.4 Pin at 20 KHz due to no hysteresis and poor signal integration.
It the integrator (Low Pass Filter) values were corrected and the transistor was replaced with comparator (with hysteresis > ripple) the signal at GPIO.4 will not oscillate.
Even if this were fixed, this is still a very poor way to try to control LED's.
William , I agree with your notices ,so I added some comments in the code.I think that this code is simple even for me that I'm very new in programming and hardware design. So I'm learning correcting the mistakes.I make some changes in code deleting the current control , trying to find a hardware solution to limit the led current.About reset,I used wrong word. I mean that I would like to initialize the program counter to begin the mode loop from mode1.Below are the corrected code and schematic.
Many thanks again.
Below is the schematic.
RE: New Schematic
This still will not work.
The transistor Q2 cannot turn on fast enough to effectively limit peak current. There will be a high current spike before the transistor turns on.
If Q2 turns on when GPIO.2 is High ( It will be), then the High output of GPIO.2 will be shorted to ground through Q2. This will likely damage the PIC. There needs to be some resistance between GPIO.2 and the MOSFET Gate
William, I think you're right , I'll see the pulse in the scope.In your opinion is there a way to limit the current effectively? What is the better method for that,with software or hardware? I made a similar circuit with led drivers that works fine,but I would like to try to make the same with a small MCU because of smaller PCB size.