|
Welcome to the Australian Ford Forums forum. You are currently viewing our boards as a guest which gives you limited access to view most discussions and inserts advertising. By joining our free community you will have access to post topics, communicate privately with other members, respond to polls, upload content and access many other special features without post based advertising banners. Registration is simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact us. Please Note: All new registrations go through a manual approval queue to keep spammers out. This is checked twice each day so there will be a delay before your registration is activated. |
|
The Bar For non Automotive Related Chat |
|
Thread Tools | Display Modes |
30-03-2005, 11:46 AM | #1 | ||
FF.Com.Au Hardcore
Join Date: Dec 2004
Location: Brisbane
Posts: 691
|
Well its 3rd year team project time at Uni, and I could do with some help. We have to make a programmable key chain appointment reminder. It is programmed by light pulses emmitted by a computer monitor, picked up by a photodiode in the key chain device.
Now the part I need help with is C Code for a microchip microprocessor (16f628). As it obviously requires time to be kept (using an onboard 16 bit counter), and has to last on a set of batteries for a long period, the chip has to go into standby mode to conserve power. Upon overflow of the 16bit counter, it flags an interrupt, waking it from sleep mode, or whatever it was previously doing. Now here's the problem. I know a bit of C, but nothing about interrupts. Now my questions number in two: 1: If an interrupt flags in the middle of a method, does it simply return to its position in the method once the interrupt has been handled? 2: How could I code this (providing it can actually return to the method). Also any examples you might have of C coding interrupts would be great. Cheers Donno
__________________
93 ED Futura, I6, KKK500r Turbo, Dev 5 head, custom Surecam, TKO500, Lokka. 250rwkw@4000rpm, 9psi, and lots of boost taper. Comming soon: T04Z, plenum, TrueTrac. |
||
30-03-2005, 12:10 PM | #2 | ||
Sublime
Join Date: Jan 2005
Location: Wagga
Posts: 2,029
|
is this like C++
i have no idea about this stuff, sounds like fun..................NOT good luck
__________________
|
||
30-03-2005, 12:15 PM | #3 | ||
FF.Com.Au Hardcore
Join Date: Dec 2004
Location: South Australia
Posts: 3,173
|
[SARCASM]mmmm Programming[/SARCASM]
No idea sorry... good luck with it though !!
__________________
'09 SYII TTG | Mystic '06 BF XR6 | Mercury Silver
|
||
30-03-2005, 12:17 PM | #4 | ||
The 'Stihl' Man
Join Date: Jan 2005
Location: TAS
Posts: 27,587
|
hah yeah I got a 50 for Basic Programming at uni, it was seriously the worst subject you could ever do. I dont know how people do that for a living. Good on gatesy I say.
Sorry I cant help..I scrapped my butt through..lol
__________________
|
||
30-03-2005, 12:18 PM | #5 | ||
Sublime
Join Date: Jan 2005
Location: Wagga
Posts: 2,029
|
i noticed this site had C++ stuff on it, could be what your lookin for
???? http://www.pixel2life.com/tutorials/bcpp.php?tut=26
__________________
|
||
30-03-2005, 12:18 PM | #6 | |||
FF.Com.Au Hardcore
Join Date: Dec 2004
Location: The Patch
Posts: 1,011
|
Did you already try searching yourself? There are buttloads of coding sites that should have exactly what you are after or at least point you in the right direction.
Third year and you don't know C? No wonder all the good programmers I know either never went or dropped out of Uni.
__________________
Quote:
|
|||
30-03-2005, 12:19 PM | #7 | ||
bring it on
Join Date: Dec 2004
Location: Praying....for you
Posts: 987
|
I am not to sure about the C with chips and stuff...i did a little C with UNIX system programming. we did some signal catching and stuff (program interrupts) but i don't think that releates to you. if it does try using the SIGCONT signal..not sure how to use it though. sorry dude. This page is good for code but really it is for UNIX. http://www.cs.cf.ac.uk/Dave/C/
however just do a lot of googling...godluck anyways
__________________
Here is the devil-and-all to pay. |
||
30-03-2005, 12:36 PM | #8 | ||
Dual O2 sensors
Join Date: Dec 2004
Location: Brisneyworld
Posts: 1,437
|
Yeah... Google is your friend.
__________________
Black 1990 300zx Twin Turbo 5 Speed Manual : Blue 2004 Mini Cooper S 6 Speed Manual - Yes, thank you Amanda. I realise now that you updated my signature to include your car. : |
||
30-03-2005, 12:53 PM | #9 | ||
FF.Com.Au Hardcore
Join Date: Dec 2004
Location: Brisbane
Posts: 691
|
Did some googling, with moderate amount of success last night, but its way easier to work out having someone explaining what needs to be done, then going through 1000s of lines of code and try making sense of it.
You'd think they'd teach us C at uni, but nooo. So far I've learnt pascal, alot of java, sql, vhdl, assembly and a tiny bit of VB, but no C (at least its similar to java). For the last team project I had to teach myself (wasn't even my job, the software guys we're useless), but didn't have to go as far into it as interrupts. And for the record I'm not even a Software Engineer, I'm Systems. Alot more hardware based. Team project teams usually have 1 Electrical, 2 Software and 1 Systems. For some reason we got screwed over with 3 Systems and an Electrical :S.
__________________
93 ED Futura, I6, KKK500r Turbo, Dev 5 head, custom Surecam, TKO500, Lokka. 250rwkw@4000rpm, 9psi, and lots of boost taper. Comming soon: T04Z, plenum, TrueTrac. |
||
30-03-2005, 05:51 PM | #10 | ||
Hmmm Gold!!!
Join Date: Dec 2004
Location: Brisbane, Northside
Posts: 331
|
Bloody C...... Give me verilog and vhdl anyday!
__________________
1990 EA Series 2 Fairmont, Low Springs and shocks, 17 x 7 Advanti Flash Mags running on Falken rubber :, EL XR8 Sway Bars, NGK Iridium Plugs, New Trip Comp, Pacemaker 4480, 2.5 system 4L ED engine with surecam Now Sporting - OED666's 202+ RWHP engine!! :dj: fully
|
||
30-03-2005, 08:01 PM | #11 | |||
An Old Boss™©
Join Date: Dec 2004
Posts: 1,145
|
Quote:
__________________
Where did I go? What was I doing there?™© Last edited by MrSparkle; 30-03-2005 at 08:46 PM. |
|||
31-03-2005, 04:23 PM | #12 | |||
Guest
Posts: n/a
|
Quote:
The short answer to question 1 is yes. The slightly longer answer is that when an interrupt occurs the PIC automatically PUSHes the current operating state onto the stack and vectors to the interrupt handler for the particular interrupt that occured. At the end of the interrupt routine control is returned to the original point in the program using the RETFIE instruction. So, as far as the original code is concerned, the interrupted instruction just took a little longer to execute. Sorry, can't help you with question 2 but I can give a rough checklist of things that need to be done. Assuming we are only dealing with a timer overflow interrupt, which is a high priority interrupt, then you need to: 1. At the beginning of the main program, set up the interrupt vector and enable interrupts. The vector contains the address of the interrupt routine and is set up by writing the address of the interrupt subroutine to the vector register - check the compiler manual for how to do this. You'll also have to Read The Fine Manual (tm) to find how to enable the interrupts you need and to find out what else needs to be done, but the timer0 overflow interrupt is enabled by asserting the T0IE bit in the INTCON register and interrupts are enabled by asserting the GIE bit in the INTCON register. 2. In the interrupt subroutine you will need to test the interrupt flag bits to determine what the interrupt was and deal with it appropriately. Of course, if you are sure that there will only ever be one interrupt type you will still need to check that it was the one you were expecting! 3. Do the stuff you need to do. 4. Clear the interrupt flag. If this was a timer overflow interrupt, for instance, you would do this by unasserting T0IF in the INTCON register. 5. Return by issuing the RETFIE instruction. You should be able to convert this into C reasonably easily with assitance from the C compiler manual. The header files for the compiler should have all of these flags and vectors already defined to make life a little easier. Sorry I can't help you with the code but I'm sure a little Googling should provide the goods. I hope this has helped. Last edited by WeirdEL; 31-03-2005 at 05:58 PM. Reason: Corrected the flag names |
|||
31-03-2005, 04:47 PM | #13 | ||
FF.Com.Au Hardcore
Join Date: Dec 2004
Location: Brisbane
Posts: 691
|
That's bloody tops! Helped alot. Guess I'll be a pseudo coding tonight ;).
__________________
93 ED Futura, I6, KKK500r Turbo, Dev 5 head, custom Surecam, TKO500, Lokka. 250rwkw@4000rpm, 9psi, and lots of boost taper. Comming soon: T04Z, plenum, TrueTrac. |
||
31-03-2005, 05:10 PM | #14 | ||||
FF.Com.Au Hardcore
Join Date: Dec 2004
Posts: 1,381
|
Quote:
well I know a lot about C, but nothing about pics so I probably wont be much help to you
__________________
Quote:
|
||||
31-03-2005, 06:01 PM | #15 | ||
Guest
Posts: n/a
|
Please note I've made some small changes to my previous post to reflect the flag names as used in the 16F628 documentation. _
|
||
31-03-2005, 06:32 PM | #16 | ||
FF.Com.Au Hardcore
Join Date: Dec 2004
Location: Brisbane
Posts: 691
|
^^^^ You sir are a god....
__________________
93 ED Futura, I6, KKK500r Turbo, Dev 5 head, custom Surecam, TKO500, Lokka. 250rwkw@4000rpm, 9psi, and lots of boost taper. Comming soon: T04Z, plenum, TrueTrac. |
||
01-04-2005, 02:34 PM | #17 | |||
Guest
Posts: n/a
|
Quote:
I had another look at the 16F628 data sheet and, depending on what your interrupt routine is doing, you might need to save any registers that your interupt routine changes. This is because the interrupt logic only saves the PC register before running the interrupt routine. You probably won't need to worry about this if your C compiler is smart but it's worth keeping in mind as a gotcha. The C compiler manual should give you some tips for dealing with interrupts. Have fun! |
|||
01-04-2005, 05:58 PM | #18 | |||
Hmmm Gold!!!
Join Date: Dec 2004
Location: Brisbane, Northside
Posts: 331
|
Quote:
__________________
1990 EA Series 2 Fairmont, Low Springs and shocks, 17 x 7 Advanti Flash Mags running on Falken rubber :, EL XR8 Sway Bars, NGK Iridium Plugs, New Trip Comp, Pacemaker 4480, 2.5 system 4L ED engine with surecam Now Sporting - OED666's 202+ RWHP engine!! :dj: fully
|
|||
01-04-2005, 06:17 PM | #19 | ||
FF.Com.Au Hardcore
Join Date: Dec 2004
Location: Brisbane
Posts: 691
|
You did electrical didn't you bender? Where you working atm, if you don't mind me asking?
__________________
93 ED Futura, I6, KKK500r Turbo, Dev 5 head, custom Surecam, TKO500, Lokka. 250rwkw@4000rpm, 9psi, and lots of boost taper. Comming soon: T04Z, plenum, TrueTrac. |
||
01-04-2005, 06:30 PM | #20 | ||
FF.Com.Au Hardcore
Join Date: Dec 2004
Location: Brisbane
Posts: 691
|
WeirdEL, gues what, more questions (now look at what you got yourself into :p). With timer1, what does the prescaler do? Does it just increment the timer on the prescaler value? Ie: 1:8 ratio it increments timer1 every 8 clock cycles?
So if I have assumed all this correctly, if I was to make the built in Timer1 oscillator 32kHz by chucking in a 33pf capacitor in the right place (section 7-4 in the datasheet), with a prescaler at 1:8, the overflow on timer one would flag every 128 cycles, hence at 32kHz, it would flag 250 times a second. Would that be correct? Once again, thanks for all the help, this project is going places.
__________________
93 ED Futura, I6, KKK500r Turbo, Dev 5 head, custom Surecam, TKO500, Lokka. 250rwkw@4000rpm, 9psi, and lots of boost taper. Comming soon: T04Z, plenum, TrueTrac. |
||
01-04-2005, 11:32 PM | #21 | |||
Hmmm Gold!!!
Join Date: Dec 2004
Location: Brisbane, Northside
Posts: 331
|
Quote:
I currently for for DHL, the courier company in the call center.. big step up in the world i hear u say.... well its a job, and it pays for the fuel.. my marks werent shithouse, but they werent top, and thats not quite good enough for the IC world... sucks to be honest
__________________
1990 EA Series 2 Fairmont, Low Springs and shocks, 17 x 7 Advanti Flash Mags running on Falken rubber :, EL XR8 Sway Bars, NGK Iridium Plugs, New Trip Comp, Pacemaker 4480, 2.5 system 4L ED engine with surecam Now Sporting - OED666's 202+ RWHP engine!! :dj: fully
|
|||
01-04-2005, 11:41 PM | #22 | ||
FF.Com.Au Hardcore
Join Date: Dec 2004
Location: Brisbane
Posts: 691
|
Jease, you've got me worried now. My marks aren't all that flash, but I'm passing comfortably. Could always go to Iran for a couple of years :S.
But I'm sure with a masters something good will come your way soon.
__________________
93 ED Futura, I6, KKK500r Turbo, Dev 5 head, custom Surecam, TKO500, Lokka. 250rwkw@4000rpm, 9psi, and lots of boost taper. Comming soon: T04Z, plenum, TrueTrac. |
||
02-04-2005, 06:28 PM | #23 | |||
Guest
Posts: n/a
|
Quote:
If you look at the datasheet again you will see that the timer1 oscillator is designed for a 32.768 kHz watch crystal, so you will need the crystal and 2 15pf capacitors to make it work. Remember that timer1 is a 16 bit unsigned counter which means that an overflow will occur every 2^16 counts. Now the oscillator will produce 32768 cycles per second and the timer1 overflows every 65536 counts so, with a 1:1 prescaler ratio, timer1 will overflow every two seconds exactly. If the prescaler is set to a 1:8 ratio timer1 will then overflow every 2 x 8 = 16 seconds. So, if you enable the timer1 interrupt and set the prescaler to a 1:8 ratio you will be interrupted every 16 seconds. Because the timer1 oscillator runs even when the 16f628 is in sleep mode, you can arrange for the 16f628 to be woken from sleep every 16 seconds by the timer1 interrupt. However, the maths ends up being a bit messy (e.g. 60seconds / 16 = 3.75 times per minute) so you will have to think about it a bit (hint: 60 divides by 4 quite nicely). Have fun! |
|||
02-04-2005, 06:44 PM | #24 | ||
Guest
Posts: n/a
|
Oops, the 16f628 is a little different to the 16f628a in that it specifies 33pf capacitors in the crystal oscillator circuit instead of 15pf. The 16f628a datasheet also makes the designers' intention a little clearer in terms of the intended use of the 32.768 kHz watch crystal for the timer1 oscillator.
Something to watch out for is that T1SYNC is unasserted (i.e. set to 1) so that timer1 runs in asynchronous mode. If it's asserted (i.e. set to 0) timer1 will stop counting when you put the 16f628 to sleep. |
||
02-04-2005, 07:03 PM | #25 | ||
FF.Com.Au Hardcore
Join Date: Dec 2004
Location: Brisbane
Posts: 691
|
If I was gay I could kiss you lol.
I must have been asleep when I read about the 16 bit counter, for some reason I thought it was counting in base 10 rather then binary :S. 1:2 prescaler = interrupt every 4s. Tis all falling into place.
__________________
93 ED Futura, I6, KKK500r Turbo, Dev 5 head, custom Surecam, TKO500, Lokka. 250rwkw@4000rpm, 9psi, and lots of boost taper. Comming soon: T04Z, plenum, TrueTrac. |
||
02-04-2005, 07:40 PM | #26 | |||
Guest
Posts: n/a
|
Quote:
If I were doing this I'd rig up a quick prototype to make sure my interrupt logic was working OK by, say, toggling an LED on and off each time I get an interrupt. I don't know what prototyping facilities you've got but I'd set up a simple circuit with the PIC with its CPU crystal, 32.768 kHz oscillator and an LED and then write test programs to make sure I'd got it all right. My suggestion is a simple interrupt routine that just sets a flag every time it gets called. The main program would then loop waiting for the flag to be set and, when it is, toggle the state of the LED. That way you get a feel for the basics of C programming, interrupts and how the hardware works. If you use an IC socket for the PIC you can use it in the final circuit when you've finished learning how it works. Just don't kiss me! |
|||
02-04-2005, 08:09 PM | #27 | ||
FF.Com.Au Hardcore
Join Date: Dec 2004
Location: Brisbane
Posts: 691
|
Hopefully I'll be in the lab next week, and that is exactly what I plan to be doing. Good ole Hello World.
__________________
93 ED Futura, I6, KKK500r Turbo, Dev 5 head, custom Surecam, TKO500, Lokka. 250rwkw@4000rpm, 9psi, and lots of boost taper. Comming soon: T04Z, plenum, TrueTrac. |
||