Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 26, 2024, 05:21:57 17:21


Login with username, password and session length


Pages: [1]
Print
Author Topic: Learning: How to make LED toggle with button  (Read 8297 times)
0 Members and 1 Guest are viewing this topic.
iphone
Active Member
***
Offline Offline

Posts: 168

Thank You
-Given: 115
-Receive: 10


« on: September 06, 2007, 10:02:27 10:02 »

Hi friends.

I make a simple ISIS DSN file to toggle LED on PortD.4. I just connect 2 button on PortB. One button PortB.4 as reset button. PortB.7 as toggle swicth. I also put a delayMS = 500 as protection from button bouncing.

My question,

1. How to make LED toggle on rising edge instead of PIC have to scan the state condition of the button and respond according if.. then statement. That mean (for edge rising signal ... 0 to 5V) PIC been triggered by rising edge of signal from low to high or hight to low.

2. I'm using Xtal = 32Mhz ... and I dont connect any crytal (actually I want to use internal oscilator with PLL ) .... how to write the code for that ? any example ?

Thanks for any helps.
Logged
foxelec
Newbie
*
Offline Offline

Posts: 22

Thank You
-Given: 11
-Receive: 12


« Reply #1 on: September 06, 2007, 10:19:23 10:19 »

hi friends,

iphone , to make the led toggle without polling the state of the button, you must use the interrupt system.
Read the datasheet of the used pic to choose the correct pin and to select the right mode ( interrupt are not avaliable for all the pins )
for example for the 18f452_PORTB have an Interrupt-on-Change function, reading the datasheet :
An input change on PORTB<7:4> sets flag bit RBIF (INTCON<0>). The interrupt can be enabled/disabled by setting/clearing enable bit, RBIE (INTCON<3>).

for the oscillator , sorry but again read the datasheet of the pic, oscillator section  and after just set the oscilator registers to the correct value.

hope this help.

foxelec

Logged
nosepick
Guest
« Reply #2 on: September 06, 2007, 04:24:19 16:24 »


Look at Proton forum, there you will see good code. But do not ask Wink they do not think good of people not having dongles Shocked
Logged
Ret12_12
Junior Member
**
Offline Offline

Posts: 75

Thank You
-Given: 104
-Receive: 32


« Reply #3 on: September 09, 2007, 11:10:35 11:10 »

Hi,

For the first question no interrupt need it, just right the below code

Don't forgot to set the trisb.7 = 1 'this say portb.7 is input and if you say = 0 it is output
Main:

IF Button1 =  1 then
   While Butto1 = 1
   wend
   toggle led
endif

If ResetButton = 1 then Led = 0

goto main

and no delayms needed

for the crystall sorry i havent test it, but in wars case change the crystal. Roll Eyes

Hope this help you.  Roll Eyes
« Last Edit: September 09, 2007, 11:13:36 11:13 by Ret12_12 » Logged
iphone
Active Member
***
Offline Offline

Posts: 168

Thank You
-Given: 115
-Receive: 10


« Reply #4 on: September 09, 2007, 06:14:47 18:14 »

Ret12_12,

Is it proton or PICBasic code ? ... sorry I'm still  in studying the manual mode ....  Grin

BTW, it just one button ... how about 3 button or more to be monitor whether it's been press or not.

Thanks for helps.
Logged
Ret12_12
Junior Member
**
Offline Offline

Posts: 75

Thank You
-Given: 104
-Receive: 32


« Reply #5 on: September 09, 2007, 10:50:19 22:50 »

The code it was written in proton, I prefer Proton from the PBP.

You can say:

Symbol Button1 = Portb.0 ' You give and this name to listen as well
Symbol Button2 = Portb.1
Symbol Button3 = Portb.2
Symbol Led       = Portb.3

Trisb.0 = 1 ' You confirm Portb.0 as input
Trisb.1 = 1
Trisb.2 = 1
Trisb.3 = 0 ' You confirm Portb.3 as output

Main:
   If button1 = 1 then
      While button1 = 1 'wait here until leave the button
      wend
      toggle led
   Endif

   If button2 = 1
      while button2 = 1
      wend
      ' do something here
   Endif

   If button3 = 1
      while button3 = 1
      wend
      ' do something here
   Endif

' You can also say
  If button1 = 1 and button2 = 0 and button3 = 1 then
      'Do something here
  Endif

'You can also say
Dim res as byte
   res = Portb & %00000111

   Select case res
      case 1
          'do something
   
      case 2
          'do something
   EndSelect
'For the result you must convert the bin to dec to see what numbers you will compare with the case.


Goto Main
 

Is that you mean ?


Logged
foxelec
Newbie
*
Offline Offline

Posts: 22

Thank You
-Given: 11
-Receive: 12


« Reply #6 on: September 10, 2007, 12:09:15 12:09 »

Hi friends,

Ret12_12, i agree with you interrupt is not mandatory for the iphone application, but he asked for a code without poolling the input pin.

Below a simple example extract from the proton help samples.

' Demonstrate wake on interrupt.

' The program outputs the condition of the switches on the
' LEDs, then immediately goes into power-down mode.  When
' the condition on any switch changes, the RB port change
' interrupt occurs, waking the part.
' Since the global interrupt enable bit is not set, no
' jump to the interrupt vector occurs.  Program execution
' is resumed, the LEDs are updated, and the part goes back
' to sleep.
' To further reduce the power consumption, all unused
' hardware peripherals should be disabled.  The PORTB
' pullups should be replaced with external resistors, and
' the internal pullups disabled.

      Device = 16F877
        XTAL = 4
             
' Define the pins that are connected to pushbuttons.
' The switches must be connected to PORTB, pins 4,5,6,
' or 7 in order to use the RB port change interrupt.
      Symbol SW1   = PORTB.4
      Symbol SW2   = PORTB.5
      Symbol SW3   = PORTB.6

' Define the pins that are connected to LEDs
      Symbol LEDS = PORTD
      Symbol LED1   = PORTD.0
      Symbol LED2   = PORTD.1
      Symbol LED3   = PORTD.2

      Set INTCON.3            ' Enable the RB port change interrupt
      PORTB_PULLUPS = On         ' Enable the internal PORTB pull-up resistors
      Input PORTB               ' Set PORTB to all inputs for the switches
      Output PORTD            ' Set PORTD to all outputs for the LEDs
      
'------------------------------------------------------------
' MAIN PROGRAM BEGINS HERE
      While 1 = 1               ' Create an infinite loop   

         Clear LEDS            ' Turn off all LEDs

' Check any button pressed to toggle an LED
         If SW1 = 0 Then Set LED1
         If SW2 = 0 Then Set LED2
         If SW3 = 0 Then Set LED3
   
         Clear INTCON.0         ' Clear the RB port change flag bit
   
         Nap 7               ' Go to sleep.  When the watchdog is
                           ' disabled, NAP won't wake up until
                           ' an interrupt occurs.
      Wend                  ' Do it again upon waking

foxelec
Logged
Ret12_12
Junior Member
**
Offline Offline

Posts: 75

Thank You
-Given: 104
-Receive: 32


« Reply #7 on: September 10, 2007, 12:34:43 12:34 »

Sorry but i did it show it  Cry

foxelec, yes i agree with you with interrupt you don't have to check all the time the pins and you can do it more other things, but you must use for pins Portb.0 or Portb.4 - 7.  Roll Eyes

Enjoy... Roll Eyes
Logged
iphone
Active Member
***
Offline Offline

Posts: 168

Thank You
-Given: 115
-Receive: 10


« Reply #8 on: September 10, 2007, 04:06:55 16:06 »

foxelec & Ret12_12,

I'm more than happy with your helps ... thanks a lot.

Both example is usefull for me ... suit 2 condition.

Example from foxelec is good on interrupt on port B and Ret12_12 is good checking on any pin. So I'm very happy to receive your example.

Let me re-write my ISIS using your example and share it overhere .... maybe useful for newbie.



Logged
iphone
Active Member
***
Offline Offline

Posts: 168

Thank You
-Given: 115
-Receive: 10


« Reply #9 on: September 10, 2007, 04:09:46 16:09 »

BTW ... what is 'port change interrupt' (portB  4,5,6,7) means ... is it port change from low to high or high to low (digital signal) or  a changes of analog signal ie from 5V to 4.7V or ?

Logged
Ret12_12
Junior Member
**
Offline Offline

Posts: 75

Thank You
-Given: 104
-Receive: 32


« Reply #10 on: September 10, 2007, 09:57:38 21:57 »

I am glad we give you some help  Cheesy

wheal the change is digital from low to hi or hi to low depends what you will setup  Wink
Logged
foxelec
Newbie
*
Offline Offline

Posts: 22

Thank You
-Given: 11
-Receive: 12


« Reply #11 on: September 19, 2007, 10:25:29 10:25 »

hi friends,

iphone, you're welcome, and yes the interrupt on change is only on an I/O so digital level.
This interrupt is activated with setting RBIE then looking RBIF in the interrupt subroutine.

foxelec
Logged
engr.humair
Newbie
*
Offline Offline

Posts: 29

Thank You
-Given: 5
-Receive: 2


« Reply #12 on: December 13, 2007, 01:21:09 13:21 »

Read Tutorial of the respective language to get more information
Logged
caveman508
Junior Member
**
Offline Offline

Posts: 41

Thank You
-Given: 12
-Receive: 6

READ, REAd, REad, Read


« Reply #13 on: February 06, 2008, 04:11:19 04:11 »

iphone,

The "interrupt on change" of port B is an 'exclusive or' setup.  Let me explain:  There is an additional set of 4 flip-flops related to the RB7:RB4 that are exclusive ORed with the input pins (but ONLY if they are selected to be inputs, by the TRISB). This extra set of flip-flops are clocked on a Read of the Port B.  This means that you must read the port and then enable interrupts.  After that any change to the RB7:RB4 pins from the last read of RB will cause an interrupt.  REMEMBER to read the port B inside the interrupt to re-load the special latch doing the real time compare, otherwise, you will be in a constant interrupt situation.

Oh, it is only digital, nothing analog here.  You would need the comparators or the ADC for analog stuff.

For all of you PIC programmers, you need to get a copy of the "Mid-Range MCU Family Reference Manual.  It is way better than the datasheet.  It applies to all PICS and is a MUST READ

Cheers

Caveman
Logged
iphone
Active Member
***
Offline Offline

Posts: 168

Thank You
-Given: 115
-Receive: 10


« Reply #14 on: February 06, 2008, 07:14:25 07:14 »

you need to get a copy of the "Mid-Range MCU Family Reference Manual. 

Thanks a lot for the info. BTW, where to get "Mid-Range MCU Family Reference Manual", can you share over here ?
Logged
pickit2
Moderator
Hero Member
*****
Offline Offline

Posts: 4668

Thank You
-Given: 834
-Receive: 4322


There is no evidence that I muted SoNsIvRi


« Reply #15 on: February 06, 2008, 12:04:54 12:04 »

Thanks a lot for the info. BTW, where to get "Mid-Range MCU Family Reference Manual", can you share over here ?
microchip has loads of useful info.
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1956
Logged

Note: I stoped Muteing bad members OK I now put thier account in sleep mode
caveman508
Junior Member
**
Offline Offline

Posts: 41

Thank You
-Given: 12
-Receive: 6

READ, REAd, REad, Read


« Reply #16 on: February 08, 2008, 08:05:39 08:05 »

I have a copy in PDF, would be glad to share if I knew were to put it. 

Goto Microchip and look for :

PICmicro
Mid-Range MCU Family
Reference Manual

The stupid, senseless "document" number is DS33023A

"You know, , Just Gotta Love It"  , , , shit, why make it so easy?HuhHuhHuh?   Anyway, I have it, You can too.

Sorry guys, for the delay in reply.

I'm good at what I do and am willing to share, I love to teach the honest person.

I am not very good with forum management. 


Smiles,

Caveman
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC