Issues with operators

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
bradsprojects
Posts: 28
Joined: Thu Nov 29, 2012 12:29 am
Location: Australia

Issues with operators

Post by bradsprojects » Fri Mar 01, 2013 1:20 pm

Hi all,

I am trying to do something quite basic in that I have eight toggle switches connected to PORTB and eight connected to PORTC of an 18f452 microcontroller. I then have eight LED's on PORTD.

I can run some simple code to add PORTB and PORTC together and show the Answer on PORTD:

Code: Select all

Sub DoAddition()
    PORTD = PORTB + PORTC      ' add the two eight bit words together
End Sub
The above code works just fine.

However when I try to OR the ports or AND the ports to display the result on PORTD (the LED's) nothing happens on the output I.E. the LED's don't light up no matter what I input on the switches.

Here's the code for ANDing the ports:

Code: Select all

Sub DoAND()
    PORTD = PORTB AND PORTC      ' and the two eight bit words together
End Sub
I have also tried creating variables to store the data and then output to PORTD to make the code look like this:

Code: Select all

Sub DoAND()
    PORTBBuffer = PORTB
    PORTCBuffer = PORTC
    PORTD = PORTBBuffer AND PORTCBuffer      ' and the two eight bit words together
End Sub
This also does not work. Could someone please shed some light on this issue?

Thankyou in advance.

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Fri Mar 01, 2013 1:43 pm

I just tried the following

Code: Select all

include "usart.bas"
include "convert.bas"

function DoAND() as byte
    dim PORTBBuffer as byte
    dim PORTCBuffer as byte
    PORTBBuffer = %11001100
    PORTCBuffer = %10101010
    result = PORTBBuffer AND PORTCBuffer      
End function

function DoOr() as byte
    dim PORTBBuffer as byte
    dim PORTCBuffer as byte
    PORTBBuffer = %11001100
    PORTCBuffer = %10101010
    result = PORTBBuffer or PORTCBuffer      
End function

SetBaudrate(br19200)
Write(BinToStr(DoAnd,8),13,10)
Write(BinToStr(DoOR,8),13,10)
output:

Code: Select all

10001000
11101110
which looks correct. I can only assume what you think is on your ports isn't. Just for reference, Swordfish writes to LATCH and reads from PORT.

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Fri Mar 01, 2013 1:47 pm

Two other things come to mind...

Make sure the ports are set for digital mode (otherwise they read 0), and try

PORTD = LATB + LATC

bradsprojects
Posts: 28
Joined: Thu Nov 29, 2012 12:29 am
Location: Australia

Post by bradsprojects » Fri Mar 01, 2013 9:25 pm

Thankyou very much for the replies guys.

It seems as though ISIS simulator was the problem after all that.

So now this piece of code does work:

Code: Select all

Sub DoAND
    PortBBuffer = PORTB
    PortCBuffer = PORTC
    PORTD = PortBBuffer and PortCBuffer
End Sub
Thankyou very much again for the help!

Post Reply