Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] passing microcontroller port to function

Status
Not open for further replies.

pravin b

Member level 5
Member level 5
Joined
May 20, 2012
Messages
85
Helped
2
Reputation
4
Reaction score
1
Trophy points
1,288
Location
Mumbai, India
Activity points
2,083
How can I pass the the microcontroller port to a function? for example my task is to flash PORTC with some delay using PIC16F1939.

Code:
#include <htc.h>

__CONFIG(FOSC_INTOSC & WDTE_OFF & PWRTE_OFF & MCLRE_ON & CP_ON & CPD_OFF & BOREN_ON & CLKOUTEN_OFF & IESO_OFF & FCMEN_OFF);
__CONFIG(WRT_OFF & VCAPEN_OFF & PLLEN_ON & STVREN_ON & BORV_HI & LVP_ON);

#define LEDPORT PORTC

void systeminit()
{
    OSCCON  = 0b01110000;               // 32 MHz Fosc w/ PLLEN_ON (config bit)  
    TRISC=0x00;
}
void delay(unsigned int count)
{
    unsigned int i,j;
    for(i=0;i<=count;i++)
        for(j=0;j<=548;j++);
}
void flashport(unsigned char oport)
{
    oport=0x00;
    delay(100);

    oport=0xFF;
    delay(100);
}

void main() 
{
    systeminit();
    while(1)
         flashport(LEDPORT);
}

However with the above written code I am not able to get the results. Is there any method to do so? Any further reading?
 
Last edited:

Try this method (parameter passed by its address, not by its value):

Code:
void flashport(char *oport)
{
    *oport=0x00;
    delay(100);

    *oport=0xFF;
    delay(100);
}

#define LEDPORT PORTC

void main() 
{
  //.....
         flashport(&LEDPORT);
}

Hope this helps you
zuisti
 
Not that it will matter in this situation (where you are writing to the whole port) but where the MCU has a LAT register it is better to write to that instead of writing to the PORT. (The rule is "read from the PORT, write to the LAT.)
The LAT registers are introduced to get around a nasty problem referred to as "read-modify-write" (RMW) and it is a good habit to get in to using them.
Susan
 
Hey Thanks Zuisti. It worked like a charm....I used the above method to pass the uc port and with addition to that used "mask" to play with the uc port pins. Many many thanks.
Hey Suasn really valid point raised!!!:-?
By the way have you guys worked on 16*32 LED matrix display? something like this...(http://g01.a.alicdn.com/kf/HTB1.N6Z...-b-font-font-b-16x32-b-font-red-color-dot.jpg)
I am badly looking for some kind of guide on how to interface these displays with controllers, any help?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top