beginner need help w/ language

I am a student working on a very basic project using the Rabbit 2000 dev kit. I am required to hook the board up and run the sample programs – which I have done quite easily.

I am trying to understand the code in a sample program demotcp1.c
I can obviously read the comments included but I am trying to understand the syntax. I am using the Dynamic C manual.

I am not a complete novice in programming though you can still consider me an unexperienced programmer.

Can someone please explain these lines of code? Or point me to documentation I can access? I am interested in knowing what WrPortI and BitWrPortI is here. They look like functions with 3 parameters but the program doesn’t appear to be linked to any other files.

// set lowest 2 bits of port D as outputs
WrPortI(PDDDR, &PDDDRShadow, 0x03);

and this one:
// turn led on output 0 on
BitWrPortI(PDDR, &PDDRShadow, 0xFF, 0);

Thanks.

WrPortI - Writes an internal I/O register with 8 bits and updates shadow for that register

BitWrPortI - Updates shadow register at position bitcode with value (0 or 1); copies shadow to register.

take a gander in the “Function Reference Manual” it will explain these.

basically the “BitWrPortI” is used to change just one bit and “WrPortI” is used when you need to modify more that one bit in a register.

For example, if you have a led connected to A0 and you want to turn it on.
(depending how its wired of course, lets say a logic 1 turns the led on)

(Led ON)
BitWrPortI(PADR, &PADRShadow, 1, 0) the “1” means make the pin high and the “0” means the bit position of the register.

(Led Off)
BitWrPortI(PADR, &PADRShadow, 0, 0) the “1” means make the pin high and the “0” means the bit position of the register.

(Just another example)
BitWrPortI(PADR, &PADRShadow, 1, 7) the “1” means make the pin high and the “7” means the bit position of the register.

the same thing can be done using WrPortI as well, although its a bit more tricky to modify just one bit and not affect the others so be careful using this one.

WrPortI(PADR, &PADRShadow, 0x01) is similar to "BitWrPortI(PADR, &PADRShadow, 1, 0) "

and

WrPortI(PADR, &PADRShadow, 0x80) is similar to “BitWrPortI(PADR, &PADRShadow, 1, 7)”

Hope this helps.

Thank you, this is helping…

The more I look into this the more I realize I have to learn.

I am still a little fuzzy on the 1st and 2nd parameters though… Let me see if I can get this… For parameter 2 - OK I know (I think I know) this is the shadow register but is this a pointer? And this is a shadow register to parameter 1, right?

Also, the names/syntax for parameters 1 & 2 come from where? I am trying to understand this.

I am currently reading about slave/master I/O registers in the Dynamic C manual. I got here by trying to understand port registers.

Thanks so much.

your getting it…

PADR is #defined to be 0x30.

take a look in the \lib\bioslib for the file SYSIO.LIB

you will find the complete functions for these calls there and i believe when you see them it will explain everything to you.
if not just ask…

OK, yep I see it now…

The “PDDDR” and “PDDCR” are variable names defined for the address of the data and control registers for that specific port (port D). And now I know the syntax of param 2 is based off that variable.

Ok, this is a great start. I can’t say I understand the code fully just yet but I’m understanding each piece so I can put it together :slight_smile:

Thanks Again! Woo Hoo!

little by little you will get it. Th good thing is that rabbit has lots of examples and pretty good documentation.

Now that they got this forum going it will greatly help all.

I am working on my project again and understanding more and more (many thanks to you)… I still need to learn about the different ports (a whole other topic for another time) but lets stick with the LED ports - port D. I think I’m really getting BitWrPortI() but I am now trying to ‘get’ WrPortI(). I didn’t quite understand your example of it’s use in your previous post.

Breaking this code (in my sample program) down as far as possible…

I think I understand it that the data register for port D for the Rabbit 2000 is a Read/Write register. So when creating/using it, I need (or “should”) to tell [the 8 bits of the] it whether it will be specifically one or the other (input or output). Hence the purpose of this line of code:

// set lowest 2 bits of port D as outputs
WrPortI(PDDDR, &PDDDRShadow, 0x03);

Now the manual (you pointed me to - thanks) for the function doesn’t give enough detail for me, but I know it’s for I/O from the product manual. The code comment says it sets the TWO lowest bits to be output. My confusion/question is where the value 0x03 came from:

  1. If I understand this right, the WrPortI() creates (and thus resets) an I/O register. And upon reset, it sets each bit for INPUTby default. So, this code line sets the lower 2 bits (starting at least significant by default?) to be output. Does param 3 indicate OUTPUT or does it mean ‘the last 2 bits’? Again I am not sure where this value came from.

The code comment says it sets the TWO lowest bits to be output. My confusion/question is where the value 0x03 came from:

ok, the 0x03 is hex for the last 2 bits i will try to explain.

on an 8-bit port you have 8 I/O lines. they are in binary weights as follows.

PORT - Bit Position - Weight in Hex - Weight in Decimal
PD0 0 0x01 1
PD1 1 0x02 2
PD2 2 0x04 4
PD3 3 0x08 8
PD4 4 0x10 16
PD5 5 0x20 32
PD6 6 0x40 64
PD7 7 0x80 128

So, if you wanted to set PD0 & PD1 high you would write 0x03 (add 1 & 2 together) to WrPortI register. likewise, if you wanted to set bits PD6 & PD3 high you would write 0x48 to that register.

So, this code line sets the lower 2 bits (starting at least significant by default?) to be output. Does param 3 indicate OUTPUT or does it mean ‘the last 2 bits’? Again I am not sure where this value came from.

actually, both…

PORTD is set to inputs on reset, so if you need any of the pins an output you need to configure it as such.

PDDDR–Parallel port D data direction register. A “1” makes the corresponding pin an output. Write only.

there are a few registers that you need to deal with for PORTD, they are:
PDDDR, PDDCR, PDFR, PDCR. upon default they may work just fine for you but its always best to make sure the bit settings are correct for your application. Take a look at the Rabbit 2000 users manual, for the HTML version go to section 9.4, this talks about PORTD.

Hope this helps.

It was a BIG help!

I’m buried in documentation and have learned a lot, but spinning in circles in other areas… I’m amazed on how much I was able to truly pick up in only a couple days.

Thank you again!

I’m amazed on how much I was able to truly pick up in only a couple days.

I am a BIG believer in examples. documentation is necessary, however documentation does not hold a candle to what a few good examples can teach someone.