Menu

Port problem?

Theo
2014-09-11
2014-09-13
  • Theo

    Theo - 2014-09-11

    It's not my lucky day.

    PORTB.0 is connected to VCC with a resistor of 10K and a pushbutton to GND, so default pin PORTB.0 is ON(1).
    The program shows the value of PORTB; 1 time represented as value of the complete port and the second time from Pin PORTB.0
    It should show both times a 1, but it doesn't.
    By pressing the pushbutton you only see the value of PORTB.0 switching from 1 to 0.
    So the value of a complete PORT is not correct, but is has nothing to do with printing.
    I am pretty shure that this program worked with a 16F648A a few months ago.

    See listing:

    ;Chip Settings

    chip mega8, 8

    ;#define LCD_IO 4

    define LCD_DB4 PORTD.0 ; 11

    define LCD_DB5 PORTD.1 ; 12

    define LCD_DB6 PORTD.2 ; 13

    define LCD_DB7 PORTD.3 ; 14

    define LCD_RS PORTD.4 ; 4

    define LCD_Enable PORTD.5 ; 6

    define LCD_NO_RW

    define LCD_SPEED fast

    define Led PORTC.5

    dir PORTB In
    cls

    Do
    locate 0,0:print PORTB
    locate 1,0:print PORTB.0
    PulseOut Led, 1 ms
    loop


    GCBasic version (0.9 11/5/2014)
    lcd.h 08-17-2014

    Kind Regards.

    Theo.

     
  • Anobium

    Anobium - 2014-09-11

    Try adding set DDRB = 0b00000001 rather than dir PORTB In

    Something it not right. The ASM looks odd when you use dir PORTB In, so try set DDRB = [value].

    Let us know the result.

    Anobium

     
  • Anobium

    Anobium - 2014-09-11

    PS. I do not have a Mega chip... I am just looking at the ASM.

    So, I could be totally wrong!

     
  • Theo

    Theo - 2014-09-11

    Adding set DDRB = 0b00000001 does not solve the problem,
    the only thing that's changed is the output; both lines print a zero, so it looks to me that PORTB.0 is switched to output and the rest are inputs.

    setting the port in 3 diff ways gives the same result (for AVR) after compiling

    ;dir PORTB in
    ldi SysValueCopy,0
    out DDRB,SysValueCopy
    ;Dir PORTB 0b00000000
    ldi SysValueCopy,0
    out DDRB,SysValueCopy
    ;SET DDRB = 0b00000000
    ldi SysValueCopy,0
    out DDRB,SysValueCopy

    I believe we have 1 problem here:

    • resolving the value of a port (byte)

    Theo.

     

    Last edit: Theo 2014-09-11
  • Anobium

    Anobium - 2014-09-11

    As odd as this sounds... try dir PORTB OUT

    What happens?

     
  • Theo

    Theo - 2014-09-11

    By using DIR PORTB OUT:

    in asm file:

    ldi SysValueCopy,255
    out DDRB,SysValueCopy

    The whole port has been switched to output, this behavior is correct for AVR.

    See GCB Help, Command Reference, Misc Commands, Dir:
    WARNING: Entire port form will work differently on AVR when a value other than IN or OUT is used! AVR chips use 0 to indicate in and 1 to indicate out, whereas PICs use 0 for out and 1 for in. When IN and OUT are used there are no compatibility issues.

    By using "dir PORTB IN" all pins switch to input, that's correct.

    The only problem is the printing of the value of a port; if portB.0 is 1 then printing PortB.0 shows a 1 but at the same time printing PORTB shows a 0.

     
  • Anobium

    Anobium - 2014-09-11

    I get it sorry.

    I guess if you print 'portb and 1' all is ok?

     
  • Theo

    Theo - 2014-09-11

    Do you mean like this?

    dir PORTB in

    Do

    locate 0,0:print PORTB and 1
    locate 1,0:print PORTB.0
    PulseOut Led, 1 ms
    loop

     
  • Anobium

    Anobium - 2014-09-11

    Yes.

    Can you also try assigning portb.0 to a byte variable and then a word variable. And, print those variables. This is three tests.

     
  • Theo

    Theo - 2014-09-11

    Original behavior:

    locate 0,0:print PORTB shows: 0
    locate 1,0:print PORTB.0 shows: 1, by pressing button 0


    3 diff Tests:

    locate 0,0:print PORTB and 1 shows: 0
    locate 1,0:print PORTB.0 shows: 1, by pressing button 0


    "dim PORTB as byte"
    locate 0,0:print PORTB shows: 0
    locate 1,0:print PORTB.0 shows: 1, by pressing button 0


    "dim PORTB as word"
    locate 0,0:print PORTB shows: 0
    locate 1,0:print PORTB.0 shows: 1, by pressing button 0


    So there's no change.

    I am sorry, i changed PORTB and not PORTB.0
    I will test again.

     

    Last edit: Theo 2014-09-11
  • Theo

    Theo - 2014-09-11

    Dim PORTB.0 as Byte: No diff against original behavior
    Dim PORTB.0 as word: No diff against original behavior

     
  • Anobium

    Anobium - 2014-09-11

    Thanks for trying. I am trying to rule out an 'overloading' issue.

    However, I have just tried changing one line.

    Using 'print PORTB and 1' shows 1 and 0 when I press and release the port button respectively...... in the simulator. I do not have that chipset here.

    So, I am getting puzzled.

     

    Last edit: Anobium 2014-09-11
  • Theo

    Theo - 2014-09-11

    Thanks for your patience!

    I will try to find tomorrow a ATtiny2313 somewhere in my "Junkyard" for some tests.
    For now it's almost time to go to bed.

    Thanks again.

    Theo.

     
  • Anobium

    Anobium - 2014-09-11

    Let us look at the ASM also. This works ok on Microchip so this is most odd.

     
  • Marcoos

    Marcoos - 2014-09-12

    hello
    If I understand correctly, it seems to me still the issue of reading in the Atmel port, you can see this tread:
    http://sourceforge.net/p/gcbasic/discussion/596084/thread/3af12d36/
    In the next version will correct ??
    To clarify, that's going to read an input port as you have to use your PIN.(X) And not the PORT.(X) register
    I hope I explained.
    Marco

     
  • Anobium

    Anobium - 2014-09-12

    Please try the following. Marco has provided me the datasheet and I believe this is the correct method.

    To read the port use PINxn. The datasheet 14.2.4 is the section I am using as the information source.

    dir PORTB In
    
    cls
    Do
      locate 0,0
      print  PORTB
    
      locate 1,0
      print  PINB0
    
      PulseOut Led, 1 ms
    loop
    

    Let us know. Thanks go to Marco!!

     
  • Theo

    Theo - 2014-09-12

    Anobium and Marco, you are have been a great help to me.
    Already a long time ago they told me RTFM, but i did not!

    So the code should be like this:

    dir PORTB in ; this is the DDR
    set PORTB = 0 ; pullup not active (PUD should be 0)

    Do
    locate 0,0:print PINB ; this shows all pins of portB
    locate 1,0:print PINB.0 ; this shows pin 0 (the first bit) of portB
    PulseOut Led, 1 ms
    loop


    Reading the datasheet i showed to me that in this case (Atmega8)
    I could also use print peek(0x36) (typical only Atmega8).

    Thanks again for your support and have a nice weekend.

    Theo.

     
  • Anobium

    Anobium - 2014-09-13

    The compiler is meant to change any reads of PORTx to reads of PINx for compatibility with PIC code. This is the design intent of Great Cow Basic. Generally, this is the case, but, there are a few cases where reading of PORTx does not work as expected. You have found one of the cases where PINx is required.

    This will be corrected in a future release of the compiler.

    Meanwhile... PINxn is the answer.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.