Jump to content
Sign in to follow this  
stevenlkz

Normal Programming Question

Recommended Posts

I got two questions about the programming of PIC16F877.

1. Normally, when we rotate 8 time with 8 bit data, we will get back the same data right? But if I rotate one time, then call a subroutine or goto other place, the carry flag already change. Then back to here and rotate again. This loop will go on for 8 time, is it finally still can get back the same data?

2. If I add 8 bit data with another 8 bit data and the result is is overload to 9 or 10 bit, then how a result will store?

Share this post


Link to post
Share on other sites

I have another 6 question with MPlab assembler programming.

1. Is the PIC can use to check the value whether it is a positive or negative value?

2. Is the IF/ELSE statement available in MPlab?

3. How to compare and get the smallest number between three number?

4. After I use the fixed point division of the application note, AN617 in microchip, but the output is in floating point, then how to read the result?

5. If the maximum number of floating point is 0.125 in decimal number, then how many bits should I use for math operation?

6. In application note, AN575 in microchip, it said that floating point 0X823C5198, but how to read it? What decimal value is it?

Share this post


Link to post
Share on other sites
I got two questions about the programming of PIC16F877.

1. Normally, when we rotate 8 time with 8 bit data, we will get back the same data right? But if I rotate one time, then call a subroutine or goto other place, the carry flag already change. Then back to here and rotate again. This loop will go on for 8 time, is it finally still can get back the same data?

 

I don't know why people use the PIC16F877 family of processers any more. The PIC18F452 is pin for pin compatible, has far more peripherals, better peripherals, better clocking options, more RAM, better instruction set, faster, and about the same price. Why start a project with a dinosaur?

 

Anyway back to your question. The brain dead PIC16F877 supports only a rotate through carry. Consider the case of Rotate Left using the RLF instruction. We will rotate the hex value of 0x5A which is binary 01011010. Lets assume that the initial carry value is "unknown" and call it 'c'

 

Here is the result of each stage of shifting

 

Value Carry

start condition 01011010 c

1 RRL 1011010c 0

2 RRL 011010c0 1

3 RRL 11010c01 0

4 RRL 1010c010 1

5 RRL 010c0101 1

6 RRL 10c01011 0

7 RRL 0c010110 1

8 RRL c0101101 0

9 RRL 01011010 c

 

From this you can see you have to go through the loop nine times to get back to the start condition. Obviously if you change the state of the carry flag as a result of any other instruction then the pattern will be corrupted.

 

2. If I add 8 bit data with another 8 bit data and the result is is overload to 9 or 10 bit, then how a result will store?

 

Lets assume you are using unsigned maths. In which case 0 = 0 and 0xff = 255.

When you add two eight bit numbers together then the maximum size of the result will be nine bits. The 9th bit being the carry bit. So if the carry bit is set then the result of the addition = 256 + the 8 bit result. If the carry bit i clear then the result of the addition - the 8 bit result

Share this post


Link to post
Share on other sites
I have another 6 question with MPlab assembler programming.

1. Is the PIC can use to check the value whether it is a positive or negative value?

2. Is the IF/ELSE statement available in MPlab?

3. How to compare and get the smallest number between three number?

4. After I use the fixed point division of the application note, AN617 in microchip, but the output is in floating point, then how to read the result?

5. If the maximum number of floating point is 0.125 in decimal number, then how many bits should I use for math operation?

6. In application note, AN575 in microchip, it said that floating point 0X823C5198, but how to read it? What decimal value is it?

 

Hi Steven,

 

From your questions I assume you are new to PICs and new to assembly programming. Why not write in C instead? It will be a lot less painful. Also do you need to use floating point? Most applications can get away with fixed point arithmatic. If you need to deal in say numbers of 0.01 resolution then just conceptually multiply everything by 100. When you come to output your results then you output a decimal place before the last two digits.

Share this post


Link to post
Share on other sites
1. Normally, when we rotate 8 time with 8 bit data, we will get back the same data right? But if I rotate one time, then call a subroutine or goto other place, the carry flag already change. Then back to here and rotate again. This loop will go on for 8 time, is it finally still can get back the same data?

You could set the new bit immediately after the shift if the carry is set, effectively doing a byte rotate.

e.g. (and I've not tested this)

 

RLF myreg,1

BNC noset

BSF myreg,0

noset:

 

Alternatively, check the bottom bit first, and set the carry flag so it gets shifted in correctly. Both would use a similar number of cycles.

 

Alternatively, (and better still), use a PIC18 instead and you have the RRNCF and RLNCF functions that don't go via the carry flag.

 

2. If I add 8 bit data with another 8 bit data and the result is is overload to 9 or 10 bit, then how a result will store?

You must use two bytes. If the carry is set, you have an extra bit to set in the lowest bit of the next byte.

The result can only ever be a max of 9 bits, as 0xFF+0xFF=0x1FE.

 

Cheers,

Jason

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...