Go Down

### Topic: Direct PORT programming is possible in arduino like PIC (Read 1 time)previous topic - next topic

#### rupeshkhatpe

##### Dec 17, 2014, 04:32 pm
/-----------------------------------------------------------------/

unsigned char R[]={50, 73, 73, 73, 38,0,14,17,17,17,10,0,31,8,16,
16,8,0,14,17,17,17,14,0,65,127,1,0,65,127,1,0,
46,1,1,0,31,8,16,16,15,0,9,21,21,21,30,0,0,0,0,127, 65, 65, 34};

m=sizeof(R);

for(i=0;i<=m;i++)
{
PORTD=R;
delay(500);
}
while(1);

/----------------------------------------------------------------/

this is my small program of pic uc
PlZ any one can tell me
How can i write this program in ardiuno IDE?

#### econjack

#1
##### Dec 17, 2014, 04:46 pm
See if this helps:

http://www.arduino.cc/en/Reference/PortManipulation

#### krupski

#2
##### Dec 17, 2014, 05:21 pmLast Edit: Dec 17, 2014, 05:28 pm by Krupski
How can i write this program in ardiuno IDE?
Yes you can. In fact, the syntax is virtually identical.

Code: [Select]
`    unsigned char R[] = { 50, 73, 73, 73, 38, 0 }; // left out a bunch for clarity    int m = (sizeof (R) / sizeof (*R)); // see note after this code block    int i;    DDRB = 0b11111111; // set port b data direction register all bits OUTPUT    for (i = 0; i < m; i++) {        PORTB = R[i]; // send element "i" of array "R" to port b    }`

Note it's better to do "sizeof (x) / sizeof(*x)" as a habit because if "x" were an integer or a float, it would be 2 or 4 bytes (or more in other systems) and "sizeof" would give you a value 2, 4, 8 or more times larger than the actual number of elements. For example, on Arduino a float is 4 bytes:

float x[] = { 1.0, 2.0, 3.0, 4.0, 5.0 );
sizeof (x) == 20 <--the number of bytes in the array
sizeof (*x) == 4 <-- the number of bytes each element uses
sizeof (x) / sizeof (*x) == 5 <--the number you really wanted!

See?

Also note that you need to check the schematic of the Arduino board you're using to know which port is associated with which pin(s).

What you need to do in any case is the following:

• Set the Data Direction Register appropriately. Use 1 for output and 0 for input.

Example:

Code: [Select]
`DDRC |= 0b00010000; // set port C, bit 4 as an outputDDRC &= ~0x20; // set port C, bit 4 as an input// note any data form can be used. 0b00010000 or 0x20 or 32 or _BV(4) are all the same.// "_BV(value)" is a macro which returns the BIT VALUE of the value. For example,// _BV(7) returns 128 (0x80 or 0b10000000)`

• Next to WRITE to a port, do this:

Code: [Select]
`PORTC |= _BV(4); // turn on port C, bit 4PORTC &= ~_BV(4); // turn off port C, bit 4`

• To READ the port, do this:

Code: [Select]
`int x = PINC; // x contains the entire contents (all 8 bits) of port cint x = (PINC | _BV(4)); // x contains "0x00" or "0x20" depending on if bit 4 was low or high`