Menu

Button or Switch topic

Help
JB
2024-03-14
2024-04-04
  • JB

    JB - 2024-03-14

    Hello everyone,
    just a simple question:

    I've got a chip with BP3 as input this input is connected via a 4.7k resistor to 5Vdc (high)
    and a tact switch is connected from BP3 to 0V (Low) or GND

    when I press the tact SW, BP3 to Low the led stop pulsing
    any clue,
    Thank's for all reply

    #define button portb.3
    
    dir portb.0 out
    dir portb.1 out
    dir portb.2 out
    dir button in
    
    #chip tiny10, 8
    
    Do while button = 0 ; led are pulsing when BP3 = high or 1
    Wait 200 ms
    PulseOut PortB.0, 500 ms
    Wait 200 ms
    PulseOut PortB.1, 500 ms
    Wait 200 ms
    PulseOut PortB.2, 500 ms
    Loop
    
     
  • Anobium

    Anobium - 2024-03-14

    Is the programmer attached?
    All LEDS?

    That is a very strange loop. If the button is high then the loop will not be executed and the chip will go to end (a continuous loop),or sleep.

    Rewrite the loop.

     
  • Anobium

    Anobium - 2024-03-14

    If you want to see what your program is doing there a number of simulators but I would recommend ATMEL Studio 6 for this chip.

    See here for the installers. https://sourceforge.net/projects/gcbasic/files/Support%20Files/ATMELCompilers/

    You can create a project and use the ASM produced by GCBASIC as your source. Then, debug in the AS6 simulator.

    For your program the LEDS flash whilst the switch is Low, then, the program ends when the switch goes High. If the switch is High at the start... then, the LEDs do not flash.

    The program as written is odd.

    Maybe you where thinking of this ?

    Do Forever
    Wait While button = 0
    Flash LEDs
    Loop

     
  • JB

    JB - 2024-03-14

    Many thank's I'll make changes

     
  • JB

    JB - 2024-03-14

    Hello, I edited the code and still when PB3 = 1 or high the led is flashing when I close the switch to low then the led stop flashing.

    I re-wrote the code
    and if pb3=1 or high led is flashing, when pb3=0 or low the led stop flashing

    #define button portb.3
    dir portb.0 out
    dir button in
    
    #chip tiny10, 8
    
    MAIN:
    if button = 0 then ; pb3 = 1 led are flashing
      portb.0 = 0
      pause 500
      portb.0 = 1
      pause 500
    end if
    GOTO MAIN
    
     
  • JB

    JB - 2024-03-14

    here's the scope traces.

     
  • Anobium

    Anobium - 2024-03-14

    The scope is doing what the program is told to do.

    Go back to your first post.

    #define button portb.3
    
    dir portb.0 out
    dir portb.1 out
    dir portb.2 out
    dir button in
    
    #chip tiny10, 8
    
    Do 
        Wait While button = 0 
        Wait 200 ms
        PulseOut PortB.0, 500 ms
        Wait 200 ms
        PulseOut PortB.1, 500 ms
        Wait 200 ms
        PulseOut PortB.2, 500 ms
    Loop
    
     
  • JB

    JB - 2024-03-14

    I'm sorry to ask again but this make no sense to me
    (I'm use to 1=high or VCC and 0=low or gnd)

    that's mean , if button = 0 (zero will mean = high) as the scope show, then led are flashing
    and when button=1 (will mean low) then no flashing led.

    is this correct?

     
    • Anobium

      Anobium - 2024-03-14

      1 is the same as On equates to High.
      0 is the same Off equate to Low


      You are used to using PICs.
      This an AVR.

      Read the page in the Help. This is the way the AVR architecture is, not, an GCBASIC thing.

      https://gcbasic.sourceforge.io/help/_inputs_outputs.html specially the ATMEL specifics for read/write operations section ... then, change your code.

       

      Last edit: Anobium 2024-03-14
  • Chris Roper

    Chris Roper - 2024-03-14

    It depends on your hardware but most switches are connected via a Resistor to Vcc and the switch is open circuit. Then when the button or switch is activated it connects to ground this diverts the Vcc on the switch to ground, hence when you press the button the signal on the port pin goes from high to low, not low to high.

     
  • JB

    JB - 2024-03-14

    Hello, everyone and many thank's for the reply.

    Here's the Good the Bad and the Ugly:

    The Good
    Yes Anobium,* Chris are 100% right 0=low and 1=high*

    and Yes here's my mistake:

    First the button was connected to PB3 (Reset) pin, according to Atmel datasheet the reset pin can also be used as a (weak) I/O pin.
    As inputs, the port pins that are externally pulled low will source current *if pull-up resistors
    are activated.

    The Bad

    cannot find any ways to setup the pull-up resistors even with AVRdude GUI.
    my programmer USBtinyISP work fine to program the chip, but cannot detect
    the chip from AVRdudeGUI.

    The Ugly

    I have left 3 out of 4 I/O pins.

    Is there a way to get PB3 working, let me know.
    Here's my code and circuit test below.
    Thank's again

    #define button portb.0
    dir portb.1 out
    dir portb.2 out
    dir button in
    
    #chip tiny10, 8
    
    MAIN:
    if button = 0 then
      portb.1 = 1
      portb.2 = 1
      else
      portb.1 = 0
      portb.2 = 0
    end if
    GOTO MAIN
    
     
  • JB

    JB - 2024-03-14

    also ATmel datasheet

     
  • Anobium

    Anobium - 2024-03-14

    I have memory that you have to change the fuses and they are write once.

    The ATTINY10 is a very odd chip along with the others in this chip family.

    There is not a lot of experience across the internet.

    But, look at changing the fuse setting.

     
  • JB

    JB - 2024-03-14

    Thank's Anobium

     
  • JB

    JB - 2024-03-15

    Hello,

    finaly I was able to get 4 port from attiny10 ( PB0,1,2 as I/O, and PB3 as Input only)
    switches circuit was modified to access all ports.

    So PB0,1,2 can be access as 1=high, 0 =low PB3 as input can be read as high only with the line of code as : if portb.3=0 (0 is high from the switch).

    (see circuit)

     
    • Anobium

      Anobium - 2024-03-15

      Excellent. If you have a working program please post. I would like to add to the Demonstrations with your diagram.

       
  • JB

    JB - 2024-03-17

    Hello,
    here's a simple PB3 high input test code.

    ;attiny10 PORTB.3 input high test
    dir portb.0,1,2 out
    dir portb.3 in
    
    #chip tiny10, 8
    
    Do while portb.3 = 0 
      PulseOut PortB.0, 500 ms
      PulseOut PortB.1, 500 ms
      PulseOut PortB.2, 500 ms
    Loop
    
     
    • Anobium

      Anobium - 2024-03-17

      @JB

      Thank you. I will add to the demonstrations. See https://github.com/GreatCowBASIC/Demonstration_Sources/tree/main/ChipFamily121_AVRrc_Specific_Solutions/Tiny4_5_9_10/Multiple_LEDs_and_Switch for the folder.

      Some advice:

      Always add `#option explicit' This will ensure you dimension all your variables. Variables like WORD, INTEGER and other need to be dimensioned but by using #option explicit it will ensure your registers all exist ( a register a chip byte/bit). It is easy to have a typo and get frustrated when not using the #option.

      As a newbee to GCBASIC, another you should always set the DIR on line per port.

      Below is the code I have posted to the GitHub

      '''A demonstration program for GCBASIC.
      '''--------------------------------------------------------------------------------
      '''This program controls three LEDs via a switch.
      '''See the photo and the schematic in the same folder as this source program.
      '''
      '''@author    JB   
      '''@license   GPL
      '''@version   1.00   
      '''@date      2024-03-17
      '''********************************************************************************
      
      #chip tiny10, 8
      #option Explicit
      
      /*
                  ------------PORTB----------------
          Bit#:  -7---6---5---4---3---2---1---0---
          IO:    ----------------SW--LED-LED_LED--
          IO:    ---------------------------------
      */
      
      
      // Define ports output state
      dir portb.0 out
      dir portb.1 out
      dir portb.2 out
      
      // Define ports in state
      dir portb.3 in
      
      // Repeat while switch state is Low, else exit loop and enter sleep state 
      Do while portb.3 = 0 
      
          // PulseOut will set the port as an output, set the state and then delay
          PulseOut PortB.0, 500 ms
          PulseOut PortB.1, 500 ms
          PulseOut PortB.2, 500 ms
      
      Loop
      
       
  • JB

    JB - 2024-03-17

    You've been a great help

     
    • Anobium

      Anobium - 2024-03-17

      Pleasure.

      My advice regarding DIR on a separate line .... the command you use dir portb.0,1,2 out would have actually not set the direction but would have done something else.

      However, dir portb.0,1,2 out is a actually what I called a 'silent failure' where the compiler accepts commands and generates ASM but not what you expected. I will update the compiler to trap this syntax.

       
  • JB

    JB - 2024-04-04

    I found out about how many I/O port a attiny10 has.

    3 I/O PB0,1,2

    PB3 (RESET/PCINT3,ADC3)

    PB3 is mostly use as a Reset and can be use as INT3 or ADC3

    see reference at:

    http://www.technoblogy.com/show?1YQY

     
    👍
    1

Log in to post a comment.