Author Topic: Trying to understand a simple problem  (Read 3652 times)

0 Members and 1 Guest are viewing this topic.

Offline tahmodTopic starter

  • Contributor
  • Posts: 31
Trying to understand a simple problem
« on: March 30, 2013, 04:45:43 pm »
I am doing a tutorial on Assembly language and dont really understand the full functions of the program. I do not understand how the program work, so I tried to write some comment and search some code from the datasheet what does the code mean.

Question:
1.  Can you tell me if my understanding of the code is correct or wrong written in the comment of the code?
2.Can you tell me if each line are correct or wrong?
 
The code is
http://oi49.tinypic.com/nv7250.jpg
And the flowachart is
http://oi45.tinypic.com/bx278.jpg

org 0            ;2. force the program to start at init?
init: clrf TRISD      ;3.when you are using clrf, this initialize the variable? and make TRISD an output port?
            ;4.it is a variable or a memory location that we create in the RAM?
            ;5.A memory location take place in a register?
      movlw 0x55      ;6.This move the binary 0101 0101 into the w register?
      movwf LATD      ; 7.this move the binary 0101 0101 previously stored in w into LATD?
                               ;8.LATD is the register that contain the information for the output LED?
      bsf TRISB,0      ;bsf= Bit Set f  in the datasheet, this mean it set the TRISB , TRISB=1, the TRIS control the output             port of B to be either input or output, so a 1=input?

-9.I dont understand the code put a value of 0x55 into LATD then clrf the LATD




statel: clrf LATD   ;10.-I dont understand the code put a value of 0x55 into LATD then clrf the LATD, it is like a kind of test then?
   btfsc PORTB,0   ;11.btfsc= Bit Test f, Skip if Clear in the datasheet, so when it is high it keep on going 2             place backward and if the output go low it goes on btsff PORTB,0
         ; 12.So this line mean btfsc wait for the PORTB input? And what does the number 0 mean?
   Bra $-2     
   btfss PORTB,0   ;btfss=Bit Test f, Skip if Set, this is similar to the btfsc but it wait to be high to skip the function bra?
   bra $-2


state2: movlw 0xaa      ;13.set  the LED on 1010 1010?
   movwf LATD   ;14.LEDS 7,5,3,1 are on, they are on because the LATD tell them to be on? The LATD is the memory for the          LED on or off
   
   btfsc PORTB,0
   bra $-2
   btfss PORTB,0
   bra $-2

   bra statel   ;15.what does statel mean?
   end

-16.TRISB, LATD and state1 are fixed name? Can I change their name?
-17.We can change the labels name but the opcode and operant are fixed.?
-18.the “BIT-ORIENTED FILE REGISTER OPERATIONS” are if else function, input function?
For example btfss and btfsc can be used as if and else
-19. in the code bra-£2. $-2 make the file go 2 place backward, what does the bra mean?
« Last Edit: March 30, 2013, 04:53:45 pm by tahmod »
 

Offline ZeroStatic

  • Contributor
  • Posts: 29
Re: Trying to understand a simple problem
« Reply #1 on: March 30, 2013, 06:42:18 pm »
tahmod

org 0  puts the code at location zero in memory, this is where it will run from at power up
clrf TRISD  makes all the PORT D pins on the chip outputs, they are set as all inputs at reset
TRISD is a special memory location defined by the header files that controls the output drivers for the hardware port pins, 0 bit is output

LATD is another special memory location, this time it drives the pins that are set as outputs high to VCC or low to GND on the PORT D pins according to the value

bsf TRISB,0 sets just the 1 bit in the I/O control making PORT B pin 0 an input.

state1: Just a label we'll jump back to at the end, this can be any label

clrf LATD this seems a bit silly, yes. Set the value to 0x55 and then clear it! Just turn off the LEDs connected to the port.

Now the tricky bit.

btfsc PORTB,0 this tests the PORT B pin 0 for a LOW value and then if it's LOW it (skips) Jumps over the next instruction.
bra $-2 this is a fancy way of looping back to the btfsc instruction, sort of a while loop, while(PORTB,0 == 1){}

After finding a LOW they wait for a HIGH.  So if you pressed a button, wait for the button to be released.

btfss PORTB,0
bra $-2


Now turn on the LEDs or whatever is connected to the port on bits 1,3,5,7

movlw 0xaa
movwf LATD


Now wait for the button to be pressed and released again.

btfsc PORTB,0
bra $-2
btfss PORTB,0
bra $-2

 
And the jump or branch, back to the label state1:  bra is branch always

I hope this helps,  Brenden
« Last Edit: March 30, 2013, 07:03:38 pm by ZeroStatic »
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 11880
  • Country: us
Re: Trying to understand a simple problem
« Reply #2 on: March 30, 2013, 06:54:06 pm »
 

Offline tahmodTopic starter

  • Contributor
  • Posts: 31
Re: Trying to understand a simple problem
« Reply #3 on: April 03, 2013, 06:54:24 pm »
Thanks, now I think I understand what is assembly language and how it works,
you made my day,

The problem was that I did not know that the pins in a port were able to be changed and some other problem.
 

Offline amyk

  • Super Contributor
  • ***
  • Posts: 8269
Re: Trying to understand a simple problem
« Reply #4 on: April 03, 2013, 07:58:36 pm »
Always read the datasheet... I'm surprised how many beginners never even think of doing that.
 

Offline DavidDLC

  • Frequent Contributor
  • **
  • Posts: 755
  • Country: us
Re: Trying to understand a simple problem
« Reply #5 on: April 03, 2013, 08:09:10 pm »
I am doing a tutorial on Assembly language and dont really understand the full functions of the program. I do not understand how the program work, so I tried to write some comment and search some code from the datasheet what does the code mean....


Why are you doing a tutorial if you do not understand how things work ?
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf