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.

embedded C programming

Status
Not open for further replies.

7ezhil7

Junior Member level 2
Joined
Mar 31, 2016
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
214
Hi,
I need to reuse a function to use sensors at different pins of pic16f877a. For that I assigned binary value to a variable.I passed that variable as argument to the function which has to be reused.Then I shifted that variable and passed it as argument to same function.
I don't know what data type to use for that binary variable.Also I need to know whether this method works good for code reusablity..Please help .....Thanks in advance...
Code:
v=0b11;
temp(v);
v=v<<1;
temp(v);
....
void temp(v)
{
TRISE=v;
PORTE=1;
.....}
 

Just a one small example
Code:
unsigned char DS1822_Start_Conversion_by_ROM (char * GPIOx, unsigned char PINx, unsigned char (*Serial_Num)[DS1822_SERIAL_NUM_SIZE])
{
        unsigned char cnt;
        cnt=One_Wire_Reset(GPIOx, PINx);
        if (cnt!=One_Wire_Success) return cnt;
        One_Wire_Write_Byte(One_Wire_Match_ROM,GPIOx, PINx);
        for (cnt=0;cnt!=8;cnt++) One_Wire_Write_Byte((*Serial_Num)[cnt],GPIOx, PINx);
        One_Wire_Write_Byte(DS1822_CONVERT_T_CMD,GPIOx, PINx);
        return One_Wire_Success;
}
As you can see, pin of port is a parameter of function.
 

Sir,Thank you for your reply... Will you please send the entire code...So that I could understand clearly...Again Thank you sir.
 

Do your job not included in my planing list for today. I can guide in a correct direction, but I will never give you any code for your task. It is your task and you should solve it, not me.
 

You need to be careful to separate two things here: the first is that we generally refer to the PORT pins by number such as 0, 1, 2, 3 etc.. This makes it easy for us to specify which pin we are dealing with.
However the PIC device you are using can only read or write the PORT SFR as a whole. (Even if you try to use a bit set/clear instruction, the underlying hardware will read the whole PORT, manipulate the required bit and write the whole value back to the PORT. IT is for this reason that these devices are susceptible to RMW problems.)
Therefore, to read the value of a particular bit, you need to mask off all of the other bits and then test the result. For example, to get the value of PORTA bit 3, you could use something like
Code:
    if( PORTA & 0x08)  // will be true of the bit is set
(Be careful NOT to use "if( (PORTA & 0x08) == 1)' and the like.)
You can pass the mask (the 0x08 in this example) as a parameter and return the boolean result as required.
An alternative is to also pass the index number for the bit. In that case you need to convert the index to a mask such as:
Code:
   if( PORTA & (1 << bitIndex))   // will be true if the specified bit is set
where 'bitIndex' can be from 0 to 7.
Susan
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top