| View previous topic :: View next topic |
| Author |
Message |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
| Interrupts |
Posted: Fri Mar 09, 2012 4:20 am |
|
|
Hi princejude,
Like I said before there is NO RIGHT WAY. Here's a possible.
Assuming a roughly 1ms interrupt.
The easy one first.
Colon_LED control
Set up two variables, sec_old and a timer/counter "colon_count".
. sec_old is a copy of your variable sec as it was the last time you went round main() loop.
. colon_count keeps track of how long the LED is to be kept on.
In ISR; colon_count is decremented on each interrupt if colon_count > 0 (i.e. once per ms).
In main() loop;
(1) if sec_old == sec then do nothing, because sec value has not changed.
(2) if sec_old != sec then sec has changed and do the following.
(2a) make sec_old = sec. (So they will be the same next time round.)
(3b) make colon_count = 500 (say, or whatever it needs to be for 500ms).
(4) test value of colon_count.
(5) if colon_count == 0 turn; colon_led off, else turn colon_led on.
I see that you've done all your multiplxing in the ISR. That's not recommended. The general rule for ISRs is do as little as possible, i.e. get in, get out ASAP. In your case, you set a flag in the ISR; main() looks for the flag, if set; performs action, then resets flag; or else does nothing. But at this stage, you COULD do all the above in the ISR, then you will get a feel for how it works.
Key debounce
Start by considering one key only then expand to all four.
When a key is pressed or released the contacts bounce, the PIC can respond to each bounce, hence the problem.
Your code has to respond to a valid key press only.
Set up a timer/counter to test how long a key has been pressed.
If key is up (or released) timer/counter is reset to zero.
When key is down (or pressed):-
(1) timer/counter increments.
(2) at some point (say equivalent to 10ms) you decide key press is valid and perform that key's action.
(3) after the action you have choices.
(4) stop counting until key release.
(5) keep counting to say equivalent of 500ms, perform action again, and reduce count by ~100ms.
Option (4) requires separate key press for each action.
Option (5) does auto repeat at 10/s after key is held down for 500ms.
When you move to all keys you either:-
(a) do individual code for each key or
(b) create a single code value for any combination of keys, and work on that one code value. If all the keys are on one port, simply reading the port returns a value telling you which key, or combination of keys, is pressed.
Again you can do it all in the ISR, but the old adage, get in, get out ASAP, still stands!
You pays your money and takes your choice.
Mike |
|
 |
princejude
Joined: 07 Aug 2010 Posts: 72
|
|
Posted: Tue Apr 03, 2012 9:49 am |
|
|
Hello everyone,
Am sorry for not posting the latest update on this project for long. I was busy with some other things. Now I have suspended the interrupt section of the code. I am constructing the main hardware to test it with my non-interrupt driven code that have worked on my dev. kit. I am using big 7-segment displays that required at least 12V dc to be on. I used PNP (B772) transistors as my digit-select switch and also used ULN2003A to ground each segment. the issue am yet to solve is that the MCU is not controlling the PNPs. All the 4-digits are always ON, even if I only enabled one digit.
I will upload my circuit later.
here is the picture of my BIG DIGITAL CLOCK
http://i44.tinypic.com/2drwb9s.jpg
 |
|
 |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
| PNP drivers |
Posted: Tue Apr 03, 2012 11:01 am |
|
|
Show us the layout for just one PNP driver. You can do it with ASCII characters in a normal post. There are loads on samples on the forum.
Maybe we can see why all your drivers are permanently on.
Your topic is now difficult to follow with the photo. I'm having to use the scroll bars to read the right-hand side text.
Mike |
|
 |
princejude
Joined: 07 Aug 2010 Posts: 72
|
|
Posted: Wed Apr 04, 2012 6:34 am |
|
|
Ok Mike, sorry for the big picture. Am about to upload the circuit but instead let me try to explain the connections.
The transistor connection is as follow:
1K resistor from the MCU I/O to the base of B772.
Emitter of B772 was connected to 12v dc.
Collector to 7-seg digit select pin.
4k7 resistor from base to emitter.
ULN2003A Connections:
Pin 9 connected to 12V dc and pin 8 to gnd.
The MCU's 7 I/Os that drive the 7-segs were connected to 7 input pins of uln2003 and its corresponding outputs to the 7-segment display |
|
 |
jeremiah
Joined: 20 Jul 2010 Posts: 1426
|
|
Posted: Wed Apr 04, 2012 7:09 am |
|
|
I don't know if this is the reason for your problem, but your transistor setup is kind of interesting given your use of 12v at the emitter and PIC VCC at the base.
Take a look at this discussion:
http://www.w9xt.com/page_microdesign_pt8_pnp_switching.html
Again, I have no idea if that is causing your issue, but I noticed your transistor setup and that came to mind. |
|
 |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Apr 04, 2012 7:11 am |
|
|
"To Measure Is To Know"
What voltages do you measure on the base of the transistor in both the On and Off case?
I expect it to be 9.8V and 10.8V, resulting in a Vbe of respectively 2.2V and 1.2V, both large enough to switch the transistor on.
I leave it to you to figure out how I got these values.
Hint: would the PIC have had an Open Collector output your schematic would have worked. |
|
 |
princejude
Joined: 07 Aug 2010 Posts: 72
|
|
Posted: Thu Apr 05, 2012 5:22 am |
|
|
Here is my circuit connection: http://i42.tinypic.com/bj99a9.png
@ ckielstra, I will use meter to measure the voltage again (I measured it earlier and base voltage were 4.8v (my MCU Vcc) when I send logic 1 to the base and around 6.xxV or 9.xxV (not sure) if send logic 0).
@ jeremiah, I will go through the link to check if am doing something different.
Thanks for your time |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9658 Location: Greensville,Ontario
|
|
Posted: Thu Apr 05, 2012 6:03 am |
|
|
From your schematic, your 'digit select' pins are on I/O pins that are ADC inputs. Have you disabled the ADC? pretty sure the compiler defaults them as enabled.
Perhaps post your current program to clarify. |
|
 |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Thu Apr 05, 2012 8:39 am |
|
|
Of course your PNP driver is permanently on.
With your driver the base will be at ~11.3V for either the on or off states.
The base emitter junction of a bipolar device behaves like an epi diode.
Such diodes have 600 to 700mV across them when conducting in the forward direction.
You need some means of voltage shifting from the 5V level to 12V.
Options include, but are not limited to:-
(1) Either reduce the value of the 4k7 resistor, or increase the value of the 1k0 resistor. (Not recommnended)
(2) Add a zener in series with the 1k0 resistor. (Say 8V2 zener. Better than (1).)
(3) Add a common emitter NPN device to drive the PNP. (Better than (1) & (2).)
(4) Add a common base NPN device to drive the PNP. (Better than (3).)
(5) ............
What limits your LED current?
You are not showing any resistors in series with the cathodes.
Mike |
|
 |
princejude
Joined: 07 Aug 2010 Posts: 72
|
|
Posted: Wed Apr 11, 2012 7:04 am |
|
|
Yes, I thought of including the small NPN Common Emitter switch before the PNP earlier but I look at my soldered board (shown in the picture I posted earlier) and found out that I didn't create enough space for the eight transistors and there resistors.That is why I was trying to limit the number of transistors to four.
Maybe I will solder the eight transistors switch circuit on a separate small board and attach it to my main board. I will do that later because right now am busy with one RFID reader that uses USB HID.
@ Mike,
about current limiting resistors....
I was thinking that ULN2003A will take care of the resistors, but looking at its datasheet yesterday, I found out that there is no resistor along collector and emitter line. The internal resistors were at the base. If I use the PNP together with NPN on each digit and include the current limiting resistors, will I still use the ULN2003 ?
Thanks once more to you all, I love this forum. I have learnt a lot here. |
|
 |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed Apr 11, 2012 10:46 am |
|
|
You need the current capability of the ULN2003A or somthing similar, AND current limiting resitors in each lead from ULN2003 collector to LED cathode. I can't tell you the value to use because it depends on how many LEDs there are in series in your LED modules, and the average LED on current you want.
As hinted before, I'd prefer a common base NPN driver for the PNP devices, partly because it's faster and uses only one extra component per digit. Connect as follows:-
(1) Resistor from PIC output to emitter of NPN device.
(2) Base of NPN device to +5V rail.
(3) Collector of NPN device to base of PNP device.
(4) Resistor for base of PNP device to PNP emitter, as before.
(5) PNP emitter to +12V rail, as before.
(6) PNP collector to LED module common anode, as before.
Coding is the same as before. With common emitter, you will have to invert your high side drive signals.
Mike |
|
 |
princejude
Joined: 07 Aug 2010 Posts: 72
|
|
Posted: Fri Apr 13, 2012 8:02 am |
|
|
| Quote: | | (2) Base of NPN device to +5V rail. |
No base resistor for the NPN ? |
|
 |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Fri Apr 13, 2012 9:32 am |
|
|
Correct. No need for base resistor. Base-emitter current is part of current which sinks into PIC output.
Suppose we re-cycle your old components, i.e. 1k connected to PIC O/P and 4k7 across PNP base-emitter.
With PIC O/P high:-
Zero volts across NPN base-emitter.
Zero NPN base-emitter current.
Zero NPN collector current.
Zero PNP base-emitter current (PNP held off by 4k7).
Zero PNP current.
PNP device turned OFF.
With PIC O/P low:-
NPN base-emitter clamps emitter at ~700mV below rail i.e. ~4.3V.
PIC sinks ~4.7mA.
NPN emitter current ~4.7mA.
NPN collector current same as emitter current less base current, i.e. ~4.7mA
PNP base-emitter clamps at ~700mV below 12V rail.
Current through 4k7 is 700mV / 4k7 = ~0.15mA
PNP base current = 4.3mA = 0.15mA = 4.15mA.
PNP device turned ON. (assuming large enough LED current limiting resistor)
Mike |
|
 |
princejude
Joined: 07 Aug 2010 Posts: 72
|
|
Posted: Fri Apr 13, 2012 10:20 am |
|
|
@ Mike
Thanks for your quick reply, I will implement it this weekend. |
|
 |
princejude
Joined: 07 Aug 2010 Posts: 72
|
|
Posted: Mon Apr 16, 2012 2:29 am |
|
|
| I implemented the COMMON-BASE connections as explained by Mike Walne above and it works but I noticed something... The clock does not retain time. That is, if I ON the clock, set time, leave it ON for some time, OFF it, restart it: the time will start from initial value. I suspect that battery will come in to the RTC section. |
|
 |
|