xpress_embedo
Advanced Member level 4
- Joined
- Jul 5, 2011
- Messages
- 1,154
- Helped
- 161
- Reputation
- 396
- Reaction score
- 189
- Trophy points
- 1,353
- Location
- India
- Activity points
- 10,591
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 // PIC32MX534F064H Configuration Bit Settings #include <p32xxxx.h> // DEVCFG3 // USERID = No Setting #pragma config FSRSSEL = PRIORITY_7 // SRS Select (SRS Priority 7) #pragma config FCANIO = ON // CAN I/O Pin Select (Default CAN I/O) #pragma config FUSBIDIO = ON // USB USID Selection (Controlled by the USB Module) #pragma config FVBUSONIO = OFF // USB VBUS ON Selection (Controlled by Port Function) // DEVCFG2 #pragma config FPLLIDIV = DIV_2 // PLL Input Divider (2x Divider) #pragma config FPLLMUL = MUL_20 // PLL Multiplier (20x Multiplier) #pragma config UPLLIDIV = DIV_2 // USB PLL Input Divider (2x Divider) #pragma config UPLLEN = ON // USB PLL Enable (Enabled) #pragma config FPLLODIV = DIV_1 // System PLL Output Clock Divider (PLL Divide by 1) // DEVCFG1 #pragma config FNOSC = PRIPLL // Oscillator Selection Bits (Primary Osc w/PLL (XT+,HS+,EC+PLL)) #pragma config FSOSCEN = OFF // Secondary Oscillator Enable (Disabled) #pragma config IESO = OFF // Internal/External Switch Over (Disabled) #pragma config POSCMOD = XT // Primary Oscillator Configuration (XT osc mode) #pragma config OSCIOFNC = OFF // CLKO Output Signal Active on the OSCO Pin (Disabled) #pragma config FPBDIV = DIV_1 // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/1) #pragma config FCKSM = CSECME // Clock Switching and Monitor Selection (Clock Switch Enable, FSCM Enabled) #pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576) #pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls)) // DEVCFG0 #pragma config DEBUG = OFF // Background Debugger Enable (Debugger is disabled) #pragma config ICESEL = ICS_PGx2 // ICE/ICD Comm Channel Select (ICE EMUC2/EMUD2 pins shared with PGC2/PGD2) #pragma config PWP = OFF // Program Flash Write Protect (Disable) #pragma config BWP = OFF // Boot Flash Write Protect bit (Protection Disabled) #pragma config CP = OFF // Code Protect (Protection Disabled) //////////////////////////////// #define DATA LATDbits.LATD6 #define STAT LATGbits.LATG6 //////////////////////////////// void Delay_ms(unsigned int time) { unsigned int first,second; for(first=0;first<time;first++) for(second=0;second<11500;second++) ; } main() { [B]unsigned int time = 500;[/B] TRISD = 0; TRISG = 0; LATD = 0; LATG = 0; while(1) { DATA = ~DATA; Delay_ms(time); STAT = ~STAT; Delay_ms(time); } }
The call stack is just the contents of the stack in the order the data was put on it. It gives you some tracability of which routine called another because the return address of each is stored there. As a routine is called, either with a CALL instruction or because an interrupt occurred, the place it has to return to when the routine has finished has to be saved somewhere, this is called the stack. As successive calls or returns (or end of a function) is reached, the last stack entry is where the program has to resume from. It works the opposite way around to normal memory, a register called the "stack pointer" (which cannot be directly accessed in some PICs) holds the currently active position in the stack. As you enter a subroutine/ISR, the program counter value is 'pushed' on to the stack and the pointer is adjusted to it's position, as you return from a subroutine/ISR, the address in the stack being pointed to is copied or 'popped' to the program counter so execution continues from there and the stack pointer is adjusted to the previous entry in the stack. It allows routines to be nested one inside another as many times as there are spaces in the stack to hold the return addresses.
I think Batang is warning you that the stack can be used to carry variables into and out of functions as well so the stack can fill up quickly. Certainly in conventional C/C++, all function parameters are carried on the stack but in the case of PIC microcontrollers I'm not sure that's true. To save space and time. I would guess the stack only holds addresses and data is held in a reserved block of conventional memory instead. Someone with better knowledge of the inner workings of C32 can advise you better than me.
Brian.
Hi Arun,
Read this link for details https://en.wikipedia.org/wiki/Call_stack
Cheers.
@Brian, Likewise I have never used Pics and yes that was the warning.
Stack use in C and C++
C and C++ both use the stack intensively. For example, the stack holds:
The return address of functions.
Registers that must be preserved, for instance, when register contents are saved on entry into subroutines.
Local variables, including local arrays, structures, unions, and in C++, classes.
Some stack usage is not obvious, such as:
Local integer or floating point variables are allocated stack memory if they are spilled (that is, not allocated to a register).
Structures are normally allocated to the stack.
Arrays are always allocated to the stack.
Optimizations can introduce new temporary variables to hold intermediate results.
Stack use is difficult to estimate because it is code dependent, and can vary between runs depending on the code path that the program takes on execution.
In general, you can lower the stack requirements of your program by:
Writing small functions that only require a small number of variables.
Avoiding the use of large local structures or arrays.
Avoiding recursion, for example, by using an alternative algorithm.
Minimizing the number of variables that are in use at any given time at each point in a function.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?