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.

Work with functions on PIC16F76

nexus_1975

Newbie
Newbie level 2
Joined
Oct 4, 2024
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
25
I am using PIC16F76 I am trying to write my own code.

When I use the following code it compiles and runs on hardware with 1 error.
If RC4_bit == 0 and i == 10 program will work well since 2nd iteration, i want it work with 1st iteration.
ChecFunc( ) not working during 1st iteration!

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * Test configuration:
     MCU:             PIC16F76
 */
 
unsigned int i, count, cnt, time_in_ms;
void Vdelay_ms(time_in_ms);
CheckFunc(time_in_ms);
 
CheckFunc(time_in_ms)
       {
        if((RC4_bit == 0) && (i == 10)) //continue;
                    {
                    i=0;
                    PORTA = 0x00;
                    time_in_ms = 150;//continue;
                    }
                    else if((RC3_bit == 0) && (i == 20)) //continue;
                         {
                         i=0;
                         PORTA = 0x00;
                         time_in_ms = 150;//continue;
                         }
                         else if(RC2_bit == 0)
                               {
                               time_in_ms = 120; //continue;
                               }
                               else if(RC1_bit == 0)
                                    {
                                    time_in_ms = 200;//continue;
                                    }
                                    else if(RC0_bit == 0)
                                         {
                                         time_in_ms = 1000; //continue;
                                         }
                                               else if(PORTC == 0xFF)
                                               {
                                               time_in_ms = 500; //continue;
                                               }
         return time_in_ms;
        }
 
void main()
{
//
// initialize system
//
        ADCON0 = 0;
        ADCON1 = 0x07;               // all digital port pins
        TRISC = 0xFF;                // port C, bit 1, is input
        TRISB = 0x00;                // port B, bit 0, is output
        TRISA = 0x00;                // port A is output
        PORTC = 0xFF;
        PORTB = 0x00;                // all LEDs on
        PORTA = 0x00;
        cnt = 0;             // Initialize cnt
        INTCON = 0x00;       // Set GIE, PEIE
        time_in_ms = 150;            // start count at zero
 
        while ( 1 )                // the one and only program loop
        {
        for(count=0;count<=7;count++)
        {
 
               PORTB = 0x10;
               CheckFunc();
               Vdelay_ms(time_in_ms);  /* One second pause */
               PORTB = 0x01;
               CheckFunc();
               Vdelay_ms(time_in_ms);  /* One second pause */
               PORTB = 0x02;
               CheckFunc();
               Vdelay_ms(time_in_ms);  /* One second pause */
               PORTB = 0x04;
               CheckFunc();
               Vdelay_ms(time_in_ms);  /* One second pause */
               PORTB = 0x08;
               CheckFunc();
               Vdelay_ms(time_in_ms);  /* One second pause */
 
               PORTA = PORTA++;         //
               i=i+5;
 
        }
        PORTA = 0x00;
        PORTB = 0x00;
        count=0;
        i=0;
        }
}

Added code tags
 
Last edited by a moderator:
You are talking about "error" in code behaviour but don't tell what expected versus observed behaviour is.

My first observation, you are calling CheckFunc() without actual argument. I expect that inside CheckFunc() time_in_ms is treated as local variable, it's never read anywhere in the code, neither inside the function nor as return value.

Calling a function without defined arguments would be normally flagged as error unless the argument has a default value (an option provided by some compilers).
 
You don't initialise 'i' so it will start with some random value.
As @FvM indicates, your code is full of bad coding practices which also need to be cleaned up.
Susan
 
Hi,

What´s the idea of:
increasing "i" by 5
then check against 10 and 20

isn´t it the same as
increasing "i" by 1
and checking it against 2 and 4

****
I don´t recommend to make "i" as a public/global variable at all
The better way is to make it local to main() and pass it to the fucntion via a pointer.
But indeed the even better way is to not modify "i" in the fucntion at all. You could use a rrtutn value of the funtion... to modify "i" in main() only. This way "i" is not used in the function at all.

***

As far as I can see ... CheckFunc leaves a lot of "unchecked" conditions.

Klaus
 
I am using PIC16F76 I am trying to write my own code.

When I use the following code it compiles and runs on hardware with 1 error.
If RC4_bit == 0 and i == 10 program will work well since 2nd iteration, i want it work with 1st iteration.
ChecFunc( ) not working during 1st iteration!

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * Test configuration:
     MCU:             PIC16F76
 */
 
unsigned int i, count, cnt, time_in_ms;
void Vdelay_ms(time_in_ms);
CheckFunc(time_in_ms);
 
CheckFunc(time_in_ms)
       {
        if((RC4_bit == 0) && (i == 10)) //continue;
                    {
                    i=0;
                    PORTA = 0x00;
                    time_in_ms = 150;//continue;
                    }
                    else if((RC3_bit == 0) && (i == 20)) //continue;
                         {
                         i=0;
                         PORTA = 0x00;
                         time_in_ms = 150;//continue;
                         }
                         else if(RC2_bit == 0)
                               {
                               time_in_ms = 120; //continue;
                               }
                               else if(RC1_bit == 0)
                                    {
                                    time_in_ms = 200;//continue;
                                    }
                                    else if(RC0_bit == 0)
                                         {
                                         time_in_ms = 1000; //continue;
                                         }
                                               else if(PORTC == 0xFF)
                                               {
                                               time_in_ms = 500; //continue;
                                               }
         return time_in_ms;
        }
 
void main()
{
//
// initialize system
//
        ADCON0 = 0;
        ADCON1 = 0x07;               // all digital port pins
        TRISC = 0xFF;                // port C, bit 1, is input
        TRISB = 0x00;                // port B, bit 0, is output
        TRISA = 0x00;                // port A is output
        PORTC = 0xFF;
        PORTB = 0x00;                // all LEDs on
        PORTA = 0x00;
        cnt = 0;             // Initialize cnt
        INTCON = 0x00;       // Set GIE, PEIE
        time_in_ms = 150;            // start count at zero
 
        while ( 1 )                // the one and only program loop
        {
        for(count=0;count<=7;count++)
        {
 
               PORTB = 0x10;
               CheckFunc();
               Vdelay_ms(time_in_ms);  /* One second pause */
               PORTB = 0x01;
               CheckFunc();
               Vdelay_ms(time_in_ms);  /* One second pause */
               PORTB = 0x02;
               CheckFunc();
               Vdelay_ms(time_in_ms);  /* One second pause */
               PORTB = 0x04;
               CheckFunc();
               Vdelay_ms(time_in_ms);  /* One second pause */
               PORTB = 0x08;
               CheckFunc();
               Vdelay_ms(time_in_ms);  /* One second pause */
 
               PORTA = PORTA++;         //
               i=i+5;
 
        }
        PORTA = 0x00;
        PORTB = 0x00;
        count=0;
        i=0;
        }
}

Added code tags
To ensure CheckFunc() behaves as expected from the first iteration, there are a few modifications and improvements that can be made to your code:

  1. Function Prototypes: Define function prototypes for Vdelay_ms() and CheckFunc() at the beginning to ensure proper recognition by the compiler.
  2. Parameter Handling: CheckFunc is designed to take a parameter time_in_ms, but in main() it’s called without arguments, causing a mismatch.
  3. Initialization: Ensure i is properly initialized at the start of the main loop, so it correctly counts from the beginning.
C:
/*
 * Test configuration:
     MCU:             PIC16F76
 */

unsigned int i, count, cnt, time_in_ms;

// Function prototypes
void Vdelay_ms(unsigned int time);
unsigned int CheckFunc(void);

unsigned int CheckFunc() {
    if ((RC4_bit == 0) && (i == 10)) {
        i = 0;
        PORTA = 0x00;
        return 150;
    } else if ((RC3_bit == 0) && (i == 20)) {
        i = 0;
        PORTA = 0x00;
        return 150;
    } else if (RC2_bit == 0) {
        return 120;
    } else if (RC1_bit == 0) {
        return 200;
    } else if (RC0_bit == 0) {
        return 1000;
    } else if (PORTC == 0xFF) {
        return 500;
    }
    return time_in_ms;  // Return current delay if no conditions match
}

void Vdelay_ms(unsigned int time) {
    // Implement your delay function here based on time parameter
}

void main() {
    // Initialize system
    ADCON0 = 0;
    ADCON1 = 0x07;  // All digital port pins
    TRISC = 0xFF;   // Port C is input
    TRISB = 0x00;   // Port B is output
    TRISA = 0x00;   // Port A is output
    PORTC = 0xFF;
    PORTB = 0x00;   // All LEDs off
    PORTA = 0x00;

    i = 0;                // Initialize i for use in CheckFunc
    time_in_ms = 150;     // Initial delay time

    while (1) {           // Main program loop
        for (count = 0; count <= 7; count++) {
            PORTB = 0x10;
            time_in_ms = CheckFunc(); // Call CheckFunc and update delay time
            Vdelay_ms(time_in_ms);

            PORTB = 0x01;
            time_in_ms = CheckFunc();
            Vdelay_ms(time_in_ms);

            PORTB = 0x02;
            time_in_ms = CheckFunc();
            Vdelay_ms(time_in_ms);

            PORTB = 0x04;
            time_in_ms = CheckFunc();
            Vdelay_ms(time_in_ms);

            PORTB = 0x08;
            time_in_ms = CheckFunc();
            Vdelay_ms(time_in_ms);

            PORTA++;
            i += 5;
        }
        PORTA = 0x00;
        PORTB = 0x00;
        count = 0;
        i = 0;  // Reset i at the end of each outer loop iteration
    }
}

Explanation of Modifications​

  1. CheckFunc Return Type and Calls: CheckFunc now returns time_in_ms based on the condition, so it can be used directly to update time_in_ms in the loop.
  2. Vdelay_ms Parameter: Vdelay_ms() is now defined to accept time_in_ms as a parameter, allowing more flexible timing.
  3. Initialization and Reset: i is reset to 0 after each outer loop iteration to ensure the function behaves as expected on each loop start
 

LaTeX Commands Quick-Menu:

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top