Hi Joep,
Thank's for reading. I've solved the problem, see below.

On Sun, May 16, 2010 at 1:33 AM, Joep Suijs <[email protected]> wrote:

> Hi Vasile,
>
> I don't know what the intention of your code is. It looks like you
> setup timer1 to run continues without actual interrupts.


TMR1 is free running starting with the read_button procedure untill a button
is pressed. Testing the PIR1_tmr1if AND logic with buttons it gaves the
button press. There is no interrupt routine necessary for that (as there is
not necessary for testing the tmr0if)



> However when
> you enter your procedure, you enable interrupts. If there are
> interrupts pending (depending on how and when you call your procedure
> - to bad you did not include a working copy of your code ;) there will
> be an interrupt at that point. The code that handles that interrupt is
> not supplied.
>

There is no ISR need for button read, the GIE and PEIE remains enabled from
some old application.

>
> I also noticed you don't handle PIR1_tmr1if in case there is no button
> pressed. Is this by design?


Yes, there is any problem?


> And also, timer1 is mostly off - it is set
> on at the start of you procedure and turned off within a condition or
> end of the procedure.


That's  to keep almost the same delay for button debounce, no matter the
lenght of the main forever loop program.


> A few lines of comment that describe (at a
> higher level) what the intention of the code is, would be helpfull.
>

I solved the problem, here is how the code works:

-------------------------------------------------------
-- TMR1 settings, used for buton_read procedure
-------------------------------------------------------
 var volatile bit T1CON_T1CKPS0 at T1CON_T1CKPS :0
 var volatile bit T1CON_T1CKPS1 at T1CON_T1CKPS :1

 var byte tmr1l_l = 0
 var byte tmr1h_l = 0

-- initialize timer1 for button delay generation
-- t = TMR1*prescaler*Tcy; prescaler = 1 (2 or 4 or 8)
tcy=1/14745600/4=0.27uS

  T1CON_TMR1CS = 0         -- tmr1 in timer mode,internal osc/4
  T1CON_T1CKPS0 = 1
  T1CON_T1CKPS1 = 1         -- prescaler 1:8
  T1CON_T1OSCEN = 0         -- stop external osc
  T1CON_TMR1GE = 0          -- TMR1 always counting
  T1CON_TMR1ON = 0          -- tmr1 is off
  INTCON_GIE = 0            -- disable all interrupts
  PIE1_TMR1IE = 0           -- disable tmr1 overflow interrupt

  tmr1l = tmr1l_l
  tmr1h = tmr1h_l           -- overflow period 256*TMR1H+TMR1L
  T1CON_TMR1ON = off        -- tmr1 is off
------------------------------------------------------
--------------------------------------
-- buttons
--------------------------------------

var volatile bit button1 is pin_B5
var volatile bit button1_direction is pin_B5_direction
var volatile bit button2 is pin_B4
var volatile bit button2_direction is pin_B4_direction
var volatile bit button3 is pin_B2
var volatile bit button3_direction is pin_B2_direction

procedure read_button is
-- ------------------------------------------

 button1_direction = input
 button2_direction = input
 button3_direction = input

 T1CON_tmr1on = on; turn the TMR1 on, else will not work

  if (! button1) & PIR1_tmr1if then
                            button1 = on             ; set the button flag
                            PIR1_tmr1if = low      ; reset the interrupt
flag for the next button read
                            T1CON_tmr1on = off  ; turn off TMR1 to clear it
or load it again
    tmr1l = tmr1l_l
                            tmr1h = tmr1h_l
  end if
  if (! button2) & PIR1_tmr1if then     button2 = on
                                       PIR1_tmr1if = low
              T1CON_tmr1on = off
      tmr1l = tmr1l_l
                                       tmr1h = tmr1h_l
  end if
  if (! button3) & PIR1_tmr1if then     button3 = on
                                       PIR1_tmr1if = low
      T1CON_tmr1on = off
 tmr1l = tmr1l_l
                                       tmr1h = tmr1h_l
  end if

 button1_direction = output
 button2_direction = output
 button3_direction = output


end procedure
--------------------------------------------


-- main program
----------------------------------------------


forever loop

-- user code

 button1 = low
 button2 = low
 button3 = low
 read_button ()

-- user code

if button1 then button1 = low
                 -- more user code
elsif button2 then button2 = low
                -- more user code
elsif button3 then button3 = low
               -- more user cod
end if

-- some user code

end loop





> (Personaly, I write too little comment at the first pass, which makes
> me figure out what happens at some point. At that time, I mosty add
> some comment - which is too late of course.)
>
> Joep
>
> ps For readability, please choose the most appropriate style of
> assigning a particular bit and use it throughout the program. This is
> a bit confusing and would be better if you used on/off only:
> INTCON_GIE = 0            -- disable all interrupts
> intcon_GIE = on ; enable interrupts
> intcon_GIE = low ; disable interrupts
>
>
>
> 2010/5/16 vasile surducan <[email protected]>:
> > It's quiet here these days...
> > I have a strange problem with TMR1 reading some buttons. Please take a
> look
> > on the code below, do you see something wrong? It seems the PIR1_tmr1 is
> > never set.
> > thx,
> > Vasile
> > -------------------------------------------------------
> > -- TMR1 settings, used for buton_read procedure
> > -------------------------------------------------------
> >  var volatile bit T1CON_T1CKPS0 at T1CON_T1CKPS :0
> >  var volatile bit T1CON_T1CKPS1 at T1CON_T1CKPS :1
> >  var byte tmr1l_l = 0
> >  var byte tmr1h_l = 127
> > -- initialize timer1 for button delay generation
> > -- t = TMR1*prescaler*Tcy; prescaler = 1 (2 or 4 or 8)
> > tcy=1/14745600/4=0.27uS
> > -- t = (256*127+255)*8*0.27uS = 70mS
> >   T1CON_TMR1CS = 0         -- tmr1 in timer mode,internal osc/4
> >   T1CON_T1CKPS0 = 1
> >   T1CON_T1CKPS1 = 1         -- prescaler 1:8
> >   T1CON_T1OSCEN = 0         -- stop external osc
> >   T1CON_TMR1GE = 0          -- TMR1 always counting
> >   T1CON_TMR1ON = 0          -- tmr1 is off
> >   INTCON_GIE = 0            -- disable all interrupts
> >   PIE1_TMR1IE = 0           -- disable tmr1 overflow interrupt
> >   tmr1l = tmr1l_l
> >   tmr1h = tmr1h_l                  -- overflow period
> >   T1CON_TMR1ON = off        -- tmr1 is off
> > ------------------------------------------------------
> > --------------------------------------
> > -- buttons
> > --------------------------------------
> > var volatile bit button1 is pin_B5
> > var volatile bit button1_direction is pin_B5_direction
> > var volatile bit button2 is pin_B4
> > var volatile bit button2_direction is pin_B4_direction
> > var volatile bit button3 is pin_B2
> > var volatile bit button3_direction is pin_B2_direction
> > procedure read_button is
> > intcon_GIE = on ; enable interrupts
> > PIE1_tmr1ie = on
> > T1CON_tmr1on = on
> >  button1_direction = input
> >  button2_direction = input
> >  button3_direction = input
> >   if (! button1) & PIR1_tmr1if then
> >                                       button1 = on
> >                                       PIR1_tmr1if = low
> >      T1CON_tmr1on = off
> >                       tmr1l = tmr1l_l
> >                                       tmr1h = tmr1h_l
> >   end if
> >   if (! button2) & PIR1_tmr1if then
> >                                        button2 = on
> >                                        PIR1_tmr1if = low
> >        T1CON_tmr1on = off
> >                tmr1l = tmr1l_l
> >                                        tmr1h = tmr1h_l
> >   end if
> >   if (! button3) & PIR1_tmr1if then
> >                                        button3 = on
> >                                        PIR1_tmr1if = low
> >                                        T1CON_tmr1on = off
> >        tmr1l = tmr1l_l
> >                                        tmr1h = tmr1h_l
> >   end if
> >   intcon_GIE = low ; disable interrupts
> >   PIE1_tmr1ie = low
> >   T1CON_tmr1on = low
> >  button1_direction = output
> >  button2_direction = output
> >  button3_direction = output
> >
> > end procedure
> > --------------------------------------------
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "jallib" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected]<jallib%[email protected]>
> .
> > For more options, visit this group at
> > http://groups.google.com/group/jallib?hl=en.
> >
>
> --
> You received this message because you are subscribed to the Google Groups
> "jallib" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<jallib%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/jallib?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"jallib" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/jallib?hl=en.

Reply via email to