|
From: <M.v...@ca...> - 2001-09-18 00:43:49
|
Matthew van de Werken
Electronics Engineer
CSIRO Exploration & Mining - Gravity Group
1 Technology Court - Pullenvale - Qld - 4069
ph: (07) 3327 4685 fax: (07) 3327 4455
email: m.v...@ca...
> -----Original Message-----
> From: Van De Werken, Matt (EM, Pinjarra Hills)
> Sent: Tuesday, 18 September 2001 10:42 AM
> To: 'mat...@st...'
> Subject: Function Pointers in SDCC
>
>
> Hi all:
>
> I was asked the question of how to use function pointers
> under sdcc; here's the reply:
>
> I use function pointers to provide a callback function for my
> timer interrupt routine. I haven't tried changing it
> on-the-fly yet - it's statically assigned at the moment (ie,
> it's not changed at run-time). I'm not sure whether changing
> the function at run-time will work, so any feedback on this
> would be welcome...
>
> Anyway, here's a skeleton of my code, using function pointers:
>
> /* intr.h - setting up the interrupt service routine for
> x-millisecond interrupts */
>
> #ifndef __INTR_H
> #define __INTR_H
>
> typedef void (*intr_func_ptr)() ;
> void init_intr(unsigned char, intr_func_ptr);
> void timer_intr() interrupt 5;
>
> #endif
>
> /* END INTR.H */
>
> /* intr.c - where the actual functions are defined */
>
> #include "intr.h"
> #include "ADuC812.h"
>
> intr_func_ptr intr_func; // This is the function actually called
> // by the timer_interrupt
>
> void init_intr(unsigned char msec, intr_func_ptr fptr){
>
> /* stuff deleted */
>
> intr_func = fptr; // Set the function to be called on interrupt
>
> /* setting up of timer deleted here */
>
> }
>
> void timer_intr() interrupt 5{
>
> // static unsigned char counter=0;
>
> TF2 = 0;
> EXF2 = 0; // This shouldn't have been set, but we check
> anyway...
> intr_func();
>
> }
>
> /* END INTR.C */
>
>
> /* main.c - the main loop */
>
> #include "intr.h"
>
> void f() ;
>
> void main(){
>
> intr_func_ptr fptr;
>
> fptr = f;
> init_intr(50, fptr); // Sets the interrupt for 50
> milliseconds, calling f() every time.
>
> while(1){}
>
> }
>
> void f(){
>
> /* Do stuff here (Flash a LED if you don't believe me it works) */
>
> }
>
> /* END MAIN */
>
>
> I hope this has helped someone.
>
> I'm sure changing the function at run-time would work - you'd
> have to either change the declaration of intr_func to intr.h
> or make a new function in intr.[ch]
> intr_set_callback(intr_func_ptr), which is easy enough to do.
>
> Cheers,
> Matthew van de Werken
> Electronics Engineer
> CSIRO Exploration & Mining - Gravity Group
> 1 Technology Court - Pullenvale - Qld - 4069
> ph: (07) 3327 4685 fax: (07) 3327 4455
> email: m.v...@ca...
>
>
> > -----Original Message-----
> > From: mat...@st...
> > [mailto:mat...@st...]
> > Sent: Monday, 17 September 2001 5:50 PM
> > To: M.v...@ca...
> > Subject: AW: RE: [Sdcc-user] A tale of two interrupts...
> >
> >
> > I have a question?
> > Can you tell me, how do you programm an pointer to a
> > function, and what compiler options do you use?
> > I have tried it, but it doesn't work.
> > Best regards
> > Matthias Hellge
> >
> > -- Original Nachricht--
> > Von: M.v...@ca...
> > An: sdc...@li...
> > Senden: 08:26 AM
> > Betreff: RE: [Sdcc-user] A tale of two interrupts...
> >
> > First of all, thankyou to those who have replied to me already. I've
> > narrowed down (some) of my problems, for those still willing
> > to give some
> > advice:
> >
> > I can 'putchar(c)', but I can't 'printf' - what gives? Where
> > should I define
> > my putchar - in main(), or in my serial.h routine which is included?
> >
> > Note that I'm more or less using the serial routines as
> > written by Victoria
> > Welch, on the okr web site, not the library routines.
> >
> > So, summary:
> >
> > putchar(c) works, definitely.
> > printf("Hello, world!
> >
> > ") does NOT work, definitely (I have the library
> > path set correctly, I'm using the winbin version downloaded
> > from okr very
> > recently. If I could get sdcc to compile on my Linux box then
> > I could check
> > the program there....)
> >
> > What gives?
> >
> > Cheers,
> > Matthew van de Werken
> > Electronics Engineer
> > CSIRO Exploration & Mining - Gravity Group
> > 1 Technology Court - Pullenvale - Qld - 4069
> > ph: (07) 3327 4685 fax: (07) 3327 4455
> > email: m.v...@ca...
> >
> >
> > _______________________________________________
> > Sdcc-user mailing list
> > Sdc...@li...
> > https://lists.sourceforge.net/lists/listinfo/sdcc-user
> >
> >
> >
> >
> >
>
|