Go Back   Australian Ford Forums > General Topics > Non Ford Related Community Forums > The Bar

The Bar For non Automotive Related Chat

Reply
 
Thread Tools Display Modes
Old 30-03-2005, 12:46 PM   #1
donno
FF.Com.Au Hardcore
 
donno's Avatar
 
Join Date: Dec 2004
Location: Brisbane
Posts: 691
Default Bizare request... Does anyone know C code very well?

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.
donno is offline   Reply With Quote Multi-Quote with this Post
Old 30-03-2005, 01:10 PM   #2
big_pete
Sublime
 
big_pete's Avatar
 
Join Date: Jan 2005
Location: Wagga
Posts: 2,029
Default

is this like C++

i have no idea about this stuff, sounds like fun..................NOT

good luck
__________________
big_pete is offline   Reply With Quote Multi-Quote with this Post
Old 30-03-2005, 01:15 PM   #3
Perana
FF.Com.Au Hardcore
 
Perana's Avatar
 
Join Date: Dec 2004
Location: South Australia
Posts: 3,173
Default

[SARCASM]mmmm Programming[/SARCASM]

No idea sorry... good luck with it though !!
__________________
'09 SYII TTG | Mystic
'06 BF XR6 | Mercury Silver
Perana is offline   Reply With Quote Multi-Quote with this Post
Old 30-03-2005, 01:17 PM   #4
Polyal
Virtuous Bogan (TM)
Donating Member2
 
Polyal's Avatar
 
Join Date: Jan 2005
Location: TAS
Posts: 27,467
Default

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
__________________
  • 2023 Mitsubishi Triton
  • 2017 Mitsubishi Pajero Sport
  • 2003 CL7 Honda Accord Euro R (JDM) - K20A 6MT
  • 1999 Lexus IS200 - 1G-FE Turbo 6MT
  • 1973 ZF Ford Fairlane
Polyal is offline   Reply With Quote Multi-Quote with this Post
Old 30-03-2005, 01:18 PM   #5
big_pete
Sublime
 
big_pete's Avatar
 
Join Date: Jan 2005
Location: Wagga
Posts: 2,029
Default

i noticed this site had C++ stuff on it, could be what your lookin for
????

http://www.pixel2life.com/tutorials/bcpp.php?tut=26
__________________
big_pete is offline   Reply With Quote Multi-Quote with this Post
Old 30-03-2005, 01:18 PM   #6
DivHunter
FF.Com.Au Hardcore
 
DivHunter's Avatar
 
Join Date: Dec 2004
Location: The Patch
Posts: 1,011
Default

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:
Speed cameras have changed the things we pay attention to and the things we regard as important. Instead of focusing on the dangers ahead, motorists feel that they have been relieved of responsibility for managing their own driving, and have ceded it instead to the mechanical intervention of the camera and other traffic signals.
DivHunter is offline   Reply With Quote Multi-Quote with this Post
Old 30-03-2005, 01:19 PM   #7
paul7v7
bring it on
 
paul7v7's Avatar
 
Join Date: Dec 2004
Location: Praying....for you
Posts: 987
Default

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.
paul7v7 is offline   Reply With Quote Multi-Quote with this Post
Old 30-03-2005, 01:36 PM   #8
BlueRaven
Dual O2 sensors
 
BlueRaven's Avatar
 
Join Date: Dec 2004
Location: Brisneyworld
Posts: 1,437
Default

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. :
BlueRaven is offline   Reply With Quote Multi-Quote with this Post
Old 30-03-2005, 01:53 PM   #9
donno
FF.Com.Au Hardcore
 
donno's Avatar
 
Join Date: Dec 2004
Location: Brisbane
Posts: 691
Default

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.
donno is offline   Reply With Quote Multi-Quote with this Post
Old 30-03-2005, 06:51 PM   #10
EA_Bender
Hmmm Gold!!!
 
EA_Bender's Avatar
 
Join Date: Dec 2004
Location: Brisbane, Northside
Posts: 331
Default

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
EA_Bender is offline   Reply With Quote Multi-Quote with this Post
Old 30-03-2005, 09:01 PM   #11
MrSparkle
An Old Boss™©
Contributing Member
 
MrSparkle's Avatar
 
Join Date: Dec 2004
Posts: 1,145
Default

Quote:
Originally Posted by donno
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
What code have you got so far? Is the interrupt handler in assembler?
__________________
Where did I go? What was I doing there?™©

Last edited by MrSparkle; 30-03-2005 at 09:46 PM.
MrSparkle is offline   Reply With Quote Multi-Quote with this Post
Old 31-03-2005, 05:23 PM   #12
WeirdEL
Guest
 
Posts: n/a
Default Long and Boring Reply so Avoid if not Interested

Quote:
Originally Posted by donno
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).
Donno
First of all let me say that I'm no PIC expert and I haven't done any C programming for PICs. However, I do understand how interrupts work, have messed around a little bit with the 16F84 and have just had a quick look at the section on interrupts from the 16F628A Data Sheet.

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 06:58 PM. Reason: Corrected the flag names
  Reply With Quote Multi-Quote with this Post
Old 31-03-2005, 05:47 PM   #13
donno
FF.Com.Au Hardcore
 
donno's Avatar
 
Join Date: Dec 2004
Location: Brisbane
Posts: 691
Default

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.
donno is offline   Reply With Quote Multi-Quote with this Post
Old 31-03-2005, 06:10 PM   #14
stockstandard
FF.Com.Au Hardcore
 
Join Date: Dec 2004
Posts: 1,381
Default

Quote:
Originally Posted by EA_Bender
Bloody C...... Give me verilog and vhdl anyday!
lol. other way around for me. I hate vhdl - I just use handel c when I need to work on the hardware side of things

well I know a lot about C, but nothing about pics so I probably wont be much help to you
__________________
Quote:
Originally Posted by casper
Yeah, the car is ugly, I can live with that.
stockstandard is offline   Reply With Quote Multi-Quote with this Post
Old 31-03-2005, 07:01 PM   #15
WeirdEL
Guest
 
Posts: n/a
Default

Please note I've made some small changes to my previous post to reflect the flag names as used in the 16F628 documentation. _
  Reply With Quote Multi-Quote with this Post
Old 31-03-2005, 07:32 PM   #16
donno
FF.Com.Au Hardcore
 
donno's Avatar
 
Join Date: Dec 2004
Location: Brisbane
Posts: 691
Default

^^^^ 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.
donno is offline   Reply With Quote Multi-Quote with this Post
Old 01-04-2005, 03:34 PM   #17
WeirdEL
Guest
 
Posts: n/a
Default

Quote:
Originally Posted by donno
^^^^ You sir are a god....
No, just been messing around with computers and electronics for a while.

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!
  Reply With Quote Multi-Quote with this Post
Old 01-04-2005, 06:58 PM   #18
EA_Bender
Hmmm Gold!!!
 
EA_Bender's Avatar
 
Join Date: Dec 2004
Location: Brisbane, Northside
Posts: 331
Default

Quote:
Originally Posted by stockstandard
lol. other way around for me. I hate vhdl - I just use handel c when I need to work on the hardware side of things

well I know a lot about C, but nothing about pics so I probably wont be much help to you
Hey mate, do u work in IC design at all, ive got my masters in engineeing and am DYING to get into it. Just having trouble finding places...
__________________
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
EA_Bender is offline   Reply With Quote Multi-Quote with this Post
Old 01-04-2005, 07:17 PM   #19
donno
FF.Com.Au Hardcore
 
donno's Avatar
 
Join Date: Dec 2004
Location: Brisbane
Posts: 691
Default

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.
donno is offline   Reply With Quote Multi-Quote with this Post
Old 01-04-2005, 07:30 PM   #20
donno
FF.Com.Au Hardcore
 
donno's Avatar
 
Join Date: Dec 2004
Location: Brisbane
Posts: 691
Default

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.
donno is offline   Reply With Quote Multi-Quote with this Post
Old 02-04-2005, 12:32 AM   #21
EA_Bender
Hmmm Gold!!!
 
EA_Bender's Avatar
 
Join Date: Dec 2004
Location: Brisbane, Northside
Posts: 331
Default

Quote:
Originally Posted by donno
You did electrical didn't you bender? Where you working atm, if you don't mind me asking?
Ive got my degree in Microelectronic Engineering and a Masters in VLSI System design.

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
EA_Bender is offline   Reply With Quote Multi-Quote with this Post
Old 02-04-2005, 12:41 AM   #22
donno
FF.Com.Au Hardcore
 
donno's Avatar
 
Join Date: Dec 2004
Location: Brisbane
Posts: 691
Default

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.
donno is offline   Reply With Quote Multi-Quote with this Post
Old 02-04-2005, 07:28 PM   #23
WeirdEL
Guest
 
Posts: n/a
Default

Quote:
Originally Posted by donno
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.
The prescaler is a counter itself which increments with each input cycle. So a 1:8 ratio means that 8 input cycles are required for one cycle of the prescaler. So, yes, this means that timer1 increments every 8 input cycles.

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!
  Reply With Quote Multi-Quote with this Post
Old 02-04-2005, 07:44 PM   #24
WeirdEL
Guest
 
Posts: n/a
Default

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.
  Reply With Quote Multi-Quote with this Post
Old 02-04-2005, 08:03 PM   #25
donno
FF.Com.Au Hardcore
 
donno's Avatar
 
Join Date: Dec 2004
Location: Brisbane
Posts: 691
Default

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.
donno is offline   Reply With Quote Multi-Quote with this Post
Old 02-04-2005, 08:40 PM   #26
WeirdEL
Guest
 
Posts: n/a
Default

Quote:
Originally Posted by donno
If I was gay I could kiss you lol.
Don't even think about it. :jab: LOL

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!
  Reply With Quote Multi-Quote with this Post
Old 02-04-2005, 09:09 PM   #27
donno
FF.Com.Au Hardcore
 
donno's Avatar
 
Join Date: Dec 2004
Location: Brisbane
Posts: 691
Default

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.
donno is offline   Reply With Quote Multi-Quote with this Post
Reply


Forum Jump


All times are GMT +11. The time now is 04:11 PM.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Other than what is legally copyrighted by the respective owners, this site is copyright www.fordforums.com.au
Positive SSL