EmbeddedRelated.com
Forums

writing ISR for UART

Started by sohagiut February 7, 2008
Arlet wrote:
> On Feb 8, 9:56 pm, "David T. Ashley" <d...@e3ft.com> wrote: >> "David Brown" <da...@westcontrol.removethisbit.com> wrote in message >> >> news:47ac2c51$0$14988$8404b019@news.wineasy.se... >> >> >> >>> If you *don't* have access to such an atomic decrement, you have two >>> options - disable interrupts around the critical code to make the >>> operation atomic, or use a better buffer structure! (Hint - do you really >>> need nchars?) >> Good eye! >> >> You are correct! Holding too much state is a common cause of software >> defects. >> >> Between getpoint, putpoint, and nchars : I think you can show that only two >> (any two) of the three are required. >> >> I think though that if you eliminate nchars you can't fill the queue. If >> (getpoint == putpoint), the buffer is either empty or full, and you can't >> tell which without slightly more information. >> >> However, if you have (putpoint and nchars) or (getpoint and nchars) I think >> it will work. > > I've always liked the following: > > #define SIZE 64 // should be power of 2, and < 256 > > volatile unsigned char putptr; > unsigned char getptr; > unsigned char buffer[SIZE]; > > isr( ) > { > unsigned char c = GET_CHAR_FROM_HARDWARE(); > > if( (int) (putptr - getptr) < SIZE ) > buffer[putptr++ % SIZE] = c; > } > > int uart_getchar() > { > if( putptr == getptr ) > return -1; > return buffer[getptr++ % SIZE]; > } > > SIZE should be a power of two, and less than 256 so that you can tell > the difference between an empty and completely full buffer. >
That's a neat way to do it, if you are already happy being restricted to power-of-two buffer sizes. Since most buffer sizes in practical programs are picked as rough guestimates, that's no big loss.
> As long as the incremented value for 'putptr++' can be atomically > written to memory, interrupts don't need to be disabled in the > uart_getchar() routine.
You are also relying on putptr and getptr being atomically readable. That's fine with 8-bit variables, but is important to consider when using 16-bit counters for larger buffers on 8-bit micros.
i need few example related to interrupt with serial port. please give me
related links. it's batter for me if i get sample code as i told you i am
not expert on C. please give me few times for it. 
sohagiut wrote:
> i need few example related to interrupt with serial port. please give me > related links. it's batter for me if i get sample code as i told you i am > not expert on C. please give me few times for it.
No, you don't need examples of interrupt code. You need to learn to program in C - interrupt routines and synchronising data transfers between interrupts and the main program can be a little tricky. You need to take courses or read a book, or employ a qualified programmer - begging people to do your job is not the answer. You might like to get a keyboard with capital letters - it would make your posts more legible.
>sohagiut wrote: >> i need few example related to interrupt with serial port. please give
me
>> related links. it's batter for me if i get sample code as i told you i
am
>> not expert on C. please give me few times for it. > >No, you don't need examples of interrupt code. You need to learn to >program in C - interrupt routines and synchronising data transfers >between interrupts and the main program can be a little tricky. You >need to take courses or read a book, or employ a qualified programmer - >begging people to do your job is not the answer. > >You might like to get a keyboard with capital letters - it would make >your posts more legible. >
ok, good suggestion. the blogs are the place for learning the tricks, i believe. do you? whetever... you might be very expert, then why do you hesitate to assist some one, as the objectives of the blogs serve this. or just to show off. i really like relevant suggestion. in all through the post i ask for specific questions (how to call case in ISR) and you shows your funny expertise!!!! all beginners might stupid. and the qualified programmer..............what about you?
sohagiut wrote:
>> sohagiut wrote: >>> i need few example related to interrupt with serial port. please give > me >>> related links. it's batter for me if i get sample code as i told you i > am >>> not expert on C. please give me few times for it. >> No, you don't need examples of interrupt code. You need to learn to >> program in C - interrupt routines and synchronising data transfers >> between interrupts and the main program can be a little tricky. You >> need to take courses or read a book, or employ a qualified programmer - >> begging people to do your job is not the answer. >> >> You might like to get a keyboard with capital letters - it would make >> your posts more legible. >> > > > ok, good suggestion. the blogs are the place for learning the tricks, i > believe. do you? whetever... you might be very expert, then why do you > hesitate to assist some one, as the objectives of the blogs serve this. or > just to show off. i really like relevant suggestion. in all through the > post i ask for specific questions (how to call case in ISR) and you shows > your funny expertise!!!! all beginners might stupid. and the qualified > programmer..............what about you?
It's possible that there a few blogs around with useful tricks, but you have a long way to go (measured in *years*) before you have to worry about learning tricks. It's not clear from your post - are you mixing up blogs and Usenet? Because blogs are, for the most part, public masturbation for people who think their own little lives are fascinating to the world at large (I'm very happy with my own life - but I can't imagine for a moment that others would be interested in it). Your original post was "i (sic.) am not very expert in C. ... can any one give me any tips or any link". The best tip anyone can give you is that you learn C properly - the best link would be a pointer to your local bookshop. And your keyboard is still broken.
In article <cLudnVARIPWQ4i3anZ2dnUVZ_h6hnZ2d@giganews.com>, 
sohagiut@yahoo.com says...
> >sohagiut wrote: > >> i need few example related to interrupt with serial port. please give > me > >> related links. it's batter for me if i get sample code as i told you i > am > >> not expert on C. please give me few times for it. > > > >No, you don't need examples of interrupt code. You need to learn to > >program in C - interrupt routines and synchronising data transfers > >between interrupts and the main program can be a little tricky. You > >need to take courses or read a book, or employ a qualified programmer - > >begging people to do your job is not the answer. > > > >You might like to get a keyboard with capital letters - it would make > >your posts more legible. > > > > > ok, good suggestion. the blogs are the place for learning the tricks, i > believe. do you? whetever... you might be very expert, then why do you > hesitate to assist some one, as the objectives of the blogs serve this. or > just to show off. i really like relevant suggestion. in all through the > post i ask for specific questions (how to call case in ISR) and you shows > your funny expertise!!!! all beginners might stupid. and the qualified > programmer..............what about you? >
I think most of the readers here are like C compilers----they are case-sensitive. If you want someone to help you with C code, you are going to have to learn to use the shift key. Mark Borgerson
On Feb 7, 3:55=A0am, "sohagiut" <sohag...@yahoo.com> wrote:
> hy, i am not very expert in C. Can any one give me a sample example on ISR=
> for UART.i am using MPLAB with c18. how to write IRQ in ISR. also how can > i add "case" in side ISR? can any =A0one give me any tips or any link. > > thanks in advance =A0
---------------------------------------------------------- Dont ISR . Main task polls the serial port , when busy it ignores you . You do want it to "work" for you ? dont you ? While developing the S/W , a key/button to break out of a faulty loop , saves time , but thats the only button . I have no interest in unstructured programming its a waste of my precoius time .