Trying to understand a simple problem - Page 1
Forum section: electronics / general

author: tahmod
author_rank: Contributor
timestamp: March 30, 2013, 04:45:43 pm
content: 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 ishttp://oi49.tinypic.com/nv7250.jpgAnd the flowachart ishttp://oi45.tinypic.com/bx278.jpgorg 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 LATDstatel: 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 $-2btfss 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 $-2state2: 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 offbtfsc PORTB,0bra $-2btfss PORTB,0bra $-2bra 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?

author: ZeroStatic
author_rank: Contributor
timestamp: March 30, 2013, 06:42:18 pm
content: tahmodorg 0puts the code at location zero in memory, this is where it will run from at power upclrf TRISDmakes all the PORT D pins on the chip outputs, they are set as all inputs at resetTRISD is a special memory location defined by the header files that controls the output drivers for the hardware port pins, 0 bit is outputLATD 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 valuebsf TRISB,0sets 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 labelclrf LATDthis 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,0this tests the PORT B pin 0 for a LOW value and then if it's LOW it (skips) Jumps over the next instruction.bra $-2this is a fancy way of looping back to thebtfscinstruction, 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,0bra $-2Now turn on the LEDs or whatever is connected to the port on bits 1,3,5,7movlw 0xaamovwf LATDNow wait for the button to be pressed and released again.btfsc PORTB,0bra $-2btfss PORTB,0bra $-2And the jump or branch, back to the label state1:brais branch alwaysI hope this helps, Brenden

author: IanB
author_rank: Super Contributor
timestamp: March 30, 2013, 06:54:06 pm
content: You asked this exact same question on March 11:https://www.eevblog.com/forum/microcontrollers/some-few-error-in-assembly-language/msg199888/#msg199888

author: tahmod
author_rank: Contributor
timestamp: April 03, 2013, 06:54:24 pm
content: 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.

author: amyk
author_rank: Super Contributor
timestamp: April 03, 2013, 07:58:36 pm
content: Always read the datasheet... I'm surprised how many beginners never even think of doing that.

author: DavidDLC
author_rank: Frequent Contributor
timestamp: April 03, 2013, 08:09:10 pm
content: Quote from: tahmod on March 30, 2013, 04:45:43 pmWhy are you doing a tutorial if you do not understand how things work ?