|
|
View previous topic :: View next topic |
Author |
Message |
jmaurin
Joined: 29 Mar 2008 Posts: 26 Location: Ribeirão Preto, SP, Brasil
|
18F4550 stack overflow |
Posted: Sat Oct 18, 2008 9:40 am |
|
|
Hi all. Again, i need some help!
I'm build a device for my car and i'm having problems with timer2.
Enabling interrupt for timer2, i got stack overflow always! I can see this on simulator. What's wrong? This is the code that i'm using:
Interrupt:
Code: |
#INT_TIMER2
void timer2_interrupt()
{
timer_ms++;
if (timer_ms>=500) {
timer_ms=0;
meio_seg=true;
meio_seg2=true;
portb5 =! portb5;
}
timer_ms++;
portb5 =! portb5;
}
|
Main:
Code: |
int8 iBuff[1]; //buffers
int8 oBuff[20];
timer_ms=0; //zera o contador de tempo
setup_adc_ports(AN0_TO_AN5|VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_EXT_L_TO_H | RTCC_DIV_1);
setup_timer_1(T1_EXTERNAL | T1_DIV_BY_1);
setup_timer_2(T2_DIV_BY_16,1,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
while...
|
Fuses:
Code: |
#include <18F4550.h>
#device adc=10 PASS_STRINGS=IN_RAM
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT32768 //Watch Dog Timer uses 1:128 Postscale
#FUSES HSPLL //High speed Osc (> 4mhz)
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES NOPUT //Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES NOSTVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOFCMEN //Fail-safe clock monitor enabled
#FUSES NOPBADEN //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC //configuration not registers write protected
#FUSES WRTB //Boot block write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block protected from table reads
#FUSES NOCPB //Boot Block Code Protected
#FUSES MCLR //Master Clear pin enabled
#FUSES NOLPT1OSC //Timer1 configured for low-power operation
#FUSES NOXINST //Extended set extension and Indexed Addressing mode enabled
#FUSES PLL5 //Divide By 5(20MHz oscillator input)
#FUSES CPUDIV1 //System Clock by 4
#FUSES USBDIV //USB clock source comes from PLL divide by 2
#FUSES VREGEN //USB voltage regulator enabled
#FUSES NOICPRT //ICPRT enabled
#FUSES NOIESO
#FUSES CCP2C1
#FUSES NOPBADEN
#use delay(clock=48000000)
//#use I2C(master, sda=PIN_B0, scl=PIN_B1, slow)
#use rs232(baud=19200,parity=N,xmit=PIN_B1,rcv=PIN_B0,bits=8,STREAM=rs1)
|
And this is the error:
Looks like the error is in internal oscilator. I've already tried to change my ADC to ADC_CLOCK_DIV_2, but the problem still.
PS: I'll need to use my A/D module AND USB, but USB isn't used in this simulation. |
|
|
jmaurin
Joined: 29 Mar 2008 Posts: 26 Location: Ribeirão Preto, SP, Brasil
|
|
Posted: Sat Oct 18, 2008 10:13 am |
|
|
Well, i found the problem but not the solution.
Code: |
#build(reset=0x800, interrupt=0x808)
#org 0x000, 0x7ff { }
|
I use this code in my program because i'm using boot loader on my project.
This work very well, except by now, enabling interrupt for timer2. Why this? I'm setting only the start of interrupt address, not the size. Why this overflow? How to solve it?
Tks! |
|
|
MicroManiac
Joined: 21 Aug 2008 Posts: 34
|
|
Posted: Sat Oct 18, 2008 11:08 am |
|
|
I actually never tried Timer2 in 18F4550, but this kind of error happens usually when you jump between function without the assembly "Return" command. Try checking the Listing at the timer and see if it is closing correctly _________________ "Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction."
Albert Einstein |
|
|
jmaurin
Joined: 29 Mar 2008 Posts: 26 Location: Ribeirão Preto, SP, Brasil
|
|
Posted: Sat Oct 18, 2008 11:30 am |
|
|
Still not working :(
Here is the full code:
Code: |
#include "C:\_PIC\Vectrac2xx\v2xx.h"
#build(reset=0x800, interrupt=0x808)
#org 0x000, 0x7ff { }
// Pre configurações
#rom 0xF00000={0x0C,0xF2,0x01,0x1A,0x02,0x77,0x02,0x9F,0x02,0xFA,0x02,0x22,0x03,0xFF,0x00,0x27,0x01,0x8D,0x00,0xB5,0x00,0x78,0x01,0xA0,0x01,0x60,0x00,0x6F,0x03,0x0A,0x12,0x05,0x01,0x00,0x00}
#define LED PIN_B5
long int timer_c=0;
#int_timer2
void inttmr2()
{
timer_c++;
if (timer_c>=500) {
portb5 =! portb5;
timer_c=0;
}
return;
}
void main()
{
setup_adc_ports(AN0_TO_AN5|VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_EXT_L_TO_H | RTCC_DIV_1);
setup_timer_1(T1_EXTERNAL | T1_DIV_BY_1);
setup_timer_2(T2_DIV_BY_16,82,10);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
disable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
while (true) {
;
}
}
|
v2xx.h:
Code: |
#include <18F4550.h>
#device adc=10 PASS_STRINGS=IN_RAM
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT32768 //Watch Dog Timer uses 1:128 Postscale
#FUSES HSPLL //High speed Osc (> 4mhz)
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES NOPUT //Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES NOSTVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOFCMEN //Fail-safe clock monitor enabled
#FUSES NOPBADEN //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC //configuration not registers write protected
#FUSES WRTB //Boot block write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block protected from table reads
#FUSES NOCPB //Boot Block Code Protected
#FUSES MCLR //Master Clear pin enabled
#FUSES NOLPT1OSC //Timer1 configured for low-power operation
#FUSES NOXINST //Extended set extension and Indexed Addressing mode enabled
#FUSES PLL5 //Divide By 5(20MHz oscillator input)
#FUSES CPUDIV1 //System Clock by 4
#FUSES USBDIV //USB clock source comes from PLL divide by 2
#FUSES VREGEN //USB voltage regulator enabled
#FUSES NOICPRT //ICPRT enabled
#FUSES NOIESO
#FUSES CCP2C1
#FUSES NOPBADEN
#use delay(clock=48000000)
//#use I2C(master, sda=PIN_B0, scl=PIN_B1, slow)
#use rs232(baud=19200,parity=N,xmit=PIN_B1,rcv=PIN_B0,bits=8,STREAM=rs1)
// Definições
#nolist
#byte PORTA = 0x0f80
#byte PORTB = 0x0f81
#byte PORTC = 0x0f82
#byte PORTD = 0x0f83
#byte PORTE = 0x0f84
#byte TRISA = 0x0f92
#byte TRISB = 0x0f93
#byte TRISC = 0x0f94
#byte TRISD = 0x0f95
#byte TRISE = 0x0f96
// PORTA bits
#bit porta5 = PORTA.5
#bit porta4 = PORTA.4
#bit porta3 = PORTA.3
#bit porta2 = PORTA.2
#bit porta1 = PORTA.1
#bit porta0 = PORTA.0
#define RA5 5
#define RA4 4
#define RA3 3
#define RA2 2
#define RA1 1
#define RA0 0
// PORTB bits
#bit portb7 = PORTB.7
#bit portb6 = PORTB.6
#bit portb5 = PORTB.5
#bit portb4 = PORTB.4
#bit portb3 = PORTB.3
#bit portb2 = PORTB.2
#bit portb1 = PORTB.1
#bit portb0 = PORTB.0
#define RB7 7
#define RB6 6
#define RB5 5
#define RB4 4
#define RB3 3
#define RB2 2
#define RB1 1
#define RB0 0
// PORTC bits
#bit portc7 = PORTC.7
#bit portc6 = PORTC.6
#bit portc5 = PORTC.5
#bit portc4 = PORTC.4
#bit portc3 = PORTC.3
#bit portc2 = PORTC.2
#bit portc1 = PORTC.1
#bit portc0 = PORTC.0
#define RC7 7
#define RC6 6
#define RC5 5
#define RC4 4
#define RC3 3
#define RC2 2
#define RC1 1
#define RC0 0
// PORTD bits
#bit portd7 = PORTD.7
#bit portd6 = PORTD.6
#bit portd5 = PORTD.5
#bit portd4 = PORTD.4
#bit portd3 = PORTD.3
#bit portd2 = PORTD.2
#bit portd1 = PORTD.1
#bit portd0 = PORTD.0
#define RD7 7
#define RD6 6
#define RD5 5
#define RD4 4
#define RD3 3
#define RD2 2
#define RD1 1
#define RD0 0
// PORTE bits
#bit porte2 = PORTE.2
#bit porte1 = PORTE.1
#bit porte0 = PORTE.0
#define RE2 2
#define RE1 1
#define RE0 0
// TRISA bits
#bit trisa5 = TRISA.5
#bit trisa4 = TRISA.4
#bit trisa3 = TRISA.3
#bit trisa2 = TRISA.2
#bit trisa1 = TRISA.1
#bit trisa0 = TRISA.0
// TRISB bits
#bit trisb7 = TRISB.7
#bit trisb6 = TRISB.6
#bit trisb5 = TRISB.5
#bit trisb4 = TRISB.4
#bit trisb3 = TRISB.3
#bit trisb2 = TRISB.2
#bit trisb1 = TRISB.1
#bit trisb0 = TRISB.0
// TRISC bits
#bit trisc7 = TRISC.7
#bit trisc6 = TRISC.6
#bit trisc5 = TRISC.5
#bit trisc4 = TRISC.4
#bit trisc3 = TRISC.3
#bit trisc2 = TRISC.2
#bit trisc1 = TRISC.1
#bit trisc0 = TRISC.0
// TRISD bits
#bit trisd7 = TRISD.7
#bit trisd6 = TRISD.6
#bit trisd5 = TRISD.5
#bit trisd4 = TRISD.4
#bit trisd3 = TRISD.3
#bit trisd2 = TRISD.2
#bit trisd1 = TRISD.1
#bit trisd0 = TRISD.0
// TRISE bits
#bit ibf = TRISE.7
#bit obf = TRISE.6
#bit ibov = TRISE.5
#bit pspmode = TRISE.4
#bit trise2 = TRISE.2
#bit trise1 = TRISE.1
#bit trise0 = TRISE.0
|
|
|
|
MicroManiac
Joined: 21 Aug 2008 Posts: 34
|
|
Posted: Sat Oct 18, 2008 10:51 pm |
|
|
adding "Return" in the interrupt can cause more problems than before.
What i meant was to check the assembly listing of the compiled program and check if the interrupt is exiting with "Retfie". But Probably this is not the case.
Remove the ";" from inside the while because it has no purpose. i'm not at my working laptop now, so i can't check your code, when i have time i'll review it and reply if i can find a solution _________________ "Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction."
Albert Einstein |
|
|
jmaurin
Joined: 29 Mar 2008 Posts: 26 Location: Ribeirão Preto, SP, Brasil
|
|
Posted: Sat Oct 18, 2008 11:31 pm |
|
|
MicroManiac, thank you!
The ; is just to do nothing.
About the problem, i think it's a bug from compiler....but i'm not sure.
Is in the 'C/Asm list' that i can see the final assembler code? if yes, i'm not sure it's ok.....this is the code:
Code: |
CCS PCH C Compiler, Version 4.057, 17935 19-out-08 03:15
Filename: c:\_pic\vectrac2xx\v2xx.lst
ROM used: 348 bytes (1%)
Largest free fragment is 30368
RAM used: 26 (1%) at main() level
26 (1%) worst case
Stack: 1 worst case (0 in main + 1 for interrupts)
*
0800: GOTO 08C8
*
0808: MOVWF 05
080A: MOVFF FD8,06
080E: MOVFF FE0,07
0812: MOVLB 0
0814: MOVFF FE9,0D
0818: MOVFF FEA,08
081C: MOVFF FE1,09
0820: MOVFF FE2,0A
0824: MOVFF FD9,0B
0828: MOVFF FDA,0C
082C: MOVFF FF3,14
0830: MOVFF FF4,15
0834: MOVFF FFA,16
0838: MOVFF 00,0F
083C: MOVFF 01,10
0840: MOVFF 02,11
0844: MOVFF 03,12
0848: MOVFF 04,13
084C: BTFSS FF2.5
084E: GOTO 0858
0852: BTFSC FF2.2
0854: GOTO 089E
0858: MOVFF 0F,00
085C: MOVFF 10,01
0860: MOVFF 11,02
0864: MOVFF 12,03
0868: MOVFF 13,04
086C: BSF 0E.7
086E: MOVFF 0D,FE9
0872: MOVFF 08,FEA
0876: MOVFF 09,FE1
087A: MOVFF 0A,FE2
087E: MOVFF 0B,FD9
0882: MOVFF 0C,FDA
0886: MOVFF 14,FF3
088A: MOVFF 15,FF4
088E: MOVFF 16,FFA
0892: MOVF 05,W
0894: MOVFF 07,FE0
0898: MOVFF 06,FD8
089C: RETFIE 0
....................
// My coments at beginning of program
/*****************************************************************************
.................... * Interface de controle para veiculos GM Vectra
.................... * e compativeis.
.................... *
.................... *****************************************************************************/
.................... #include "C:\_PIC\Vectrac2xx\v2xx.h"
.................... #include <18F4550.h>
.................... //////// Standard Header file for the PIC18F4550 device ////////////////
.................... #device PIC18F4550
.................... #list
....................
.................... #device adc=10 PASS_STRINGS=IN_RAM
.................... #FUSES NOWDT //No Watch Dog Timer
.................... #FUSES WDT32768 //Watch Dog Timer uses 1:128 Postscale
.................... #FUSES HSPLL //High speed Osc (> 4mhz)
.................... #FUSES NOPROTECT //Code not protected from reading
.................... #FUSES BROWNOUT //Reset when brownout detected
.................... #FUSES BORV20 //Brownout reset at 2.0V
.................... #FUSES NOPUT //Power Up Timer
.................... #FUSES NOCPD //No EE protection
.................... #FUSES NOSTVREN //Stack full/underflow will cause reset
.................... #FUSES NODEBUG //No Debug mode for ICD
.................... #FUSES NOLVP //Low Voltage Programming on B3(PIC16) or B5(PIC18)
.................... #FUSES NOWRT //Program memory not write protected
.................... #FUSES NOWRTD //Data EEPROM not write protected
.................... #FUSES NOFCMEN //Fail-safe clock monitor enabled
.................... #FUSES NOPBADEN //PORTB pins are configured as analog input channels on RESET
.................... #FUSES NOWRTC //configuration not registers write protected
.................... #FUSES WRTB //Boot block write protected
.................... #FUSES NOEBTR //Memory not protected from table reads
.................... #FUSES NOEBTRB //Boot block protected from table reads
.................... #FUSES NOCPB //Boot Block Code Protected
.................... #FUSES NOMCLR //Master Clear pin enabled
.................... #FUSES NOLPT1OSC //Timer1 configured for low-power operation
.................... #FUSES NOXINST //Extended set extension and Indexed Addressing mode enabled
.................... #FUSES PLL5 //Divide By 5(20MHz oscillator input)
.................... #FUSES CPUDIV1 //System Clock by 4
.................... #FUSES USBDIV //USB clock source comes from PLL divide by 2
.................... #FUSES VREGEN //USB voltage regulator enabled
.................... #FUSES NOICPRT //ICPRT enabled
.................... #FUSES NOIESO
.................... #FUSES CCP2C1
.................... #FUSES NOPBADEN
....................
.................... #use delay(clock=48000000)
.................... //#use I2C(master, sda=PIN_B0, scl=PIN_B1, slow)
.................... #use rs232(baud=19200,parity=N,xmit=PIN_B1,rcv=PIN_B0,bits=8,STREAM=rs1)
....................
....................
.................... // Definições
095E: SLEEP
Configuration Fuses:
Word 1: 0E24 NOIESO NOFCMEN HSPLL PLL5 CPUDIV1 USBDIV
Word 2: 1E3F BROWNOUT NOWDT BORV20 NOPUT WDT32768 VREGEN
Word 3: 0100 NOPBADEN CCP2C1 NOMCLR NOLPT1OSC RESERVED
Word 4: 0080 NOSTVREN NODEBUG NOLVP NOXINST NOICPRT RESERVED
Word 5: C00F NOPROTECT NOCPD NOCPB
Word 6: A00F NOWRT NOWRTD NOWRTC WRTB
Word 7: 400F NOEBTR NOEBTRB
|
As i can see, the programm starts going to adrress 08C8, but is not listed at the ASM code. This is not right......right?
Is i'm right about this error, it's probably a compiler bug. |
|
|
MicroManiac
Joined: 21 Aug 2008 Posts: 34
|
|
Posted: Sat Oct 18, 2008 11:42 pm |
|
|
Quote: | 0852: BTFSC FF2.2
0854: GOTO 089E
0858: MOVFF 0F,00
085C: MOVFF 10,01
0860: MOVFF 11,02
0864: MOVFF 12,03
0868: MOVFF 13,04
086C: BSF 0E.7
086E: MOVFF 0D,FE9
0872: MOVFF 08,FEA
0876: MOVFF 09,FE1
087A: MOVFF 0A,FE2
087E: MOVFF 0B,FD9
0882: MOVFF 0C,FDA
0886: MOVFF 14,FF3
088A: MOVFF 15,FF4
088E: MOVFF 16,FFA
0892: MOVF 05,W
0894: MOVFF 07,FE0
0898: MOVFF 06,FD8
089C: RETFIE 0 |
I'm not at my working computer so I don't have access to the datasheet and I have no clue about the registers, but you see the "GOTO 089E" if it enters this state the 089E is outside the "Retfie" this can cause a stack overflow if it happens continuously. Also it might be a compiler bug, these things happen. _________________ "Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction."
Albert Einstein |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sun Oct 19, 2008 3:10 am |
|
|
Hello,
the said goto 089e in interrupt code is completely correct. Unfortunately, the CCS listing is showing only
the interrupt stub, but not the inttmr2() specific part. It is usually executed by a goto, but also
has a goto back to 0858, see the below MPLAB disassembly listing. You can see the code also through
CCS disassembler tool, but without the resepctive C-code and symbols.
I didn't check for other possible coding errors, but the basic interrupt code can be expected to work
correct in PCH.
Code: | 12: #int_timer2
13: void inttmr2()
14: {
15: timer_c++;
089E 2A18 INCF 0x18, F, ACCESS
08A0 B4D8 BTFSC 0xfd8, 0x2, ACCESS
08A2 2A19 INCF 0x19, F, ACCESS
16: if (timer_c>=500) {
08A4 5019 MOVF 0x19, W, ACCESS
08A6 0800 SUBLW 0
08A8 E208 BC 0x8ba
08AA 0AFF XORLW 0xff
08AC E103 BNZ 0x8b4
08AE 5018 MOVF 0x18, W, ACCESS
08B0 08F3 SUBLW 0xf3
08B2 E203 BC 0x8ba
17: portb5 =! portb5;
08B4 7A81 BTG 0xf81, 0x5, ACCESS
18: timer_c=0;
08B6 6A19 CLRF 0x19, ACCESS
08B8 6A18 CLRF 0x18, ACCESS
19: }
20: return;
21: }
22:
08BA 929E BCF 0xf9e, 0x1, ACCESS
08BC EF2C GOTO 0x858 |
Regards,
Frank
P.S.:
To ask a possibly silly question. Do you operate your code with a valid bootloader, that redirects the interrupt to 0x808?
I tried your code in simulator with the below dummy bootloader without any stack overflows.
Code: | #org 0,0x7
void boot(void)
{
#asm
goto 0x800
#endasm
}
#org 8, 0x7ff
void isr(void)
{
#asm
goto 0x808
#endasm
}
//#org 0x000, 0x7ff { } |
|
|
|
jmaurin
Joined: 29 Mar 2008 Posts: 26 Location: Ribeirão Preto, SP, Brasil
|
|
Posted: Sun Oct 19, 2008 7:12 am |
|
|
FvM, you said an important think: bootloader. My bootloader is working fine on my board, but i'm not using on simulator...
I'm not sure, but the interrupts redirect are made on my main program...isn't? (#build)
If not, this could be the problem. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sun Oct 19, 2008 10:15 am |
|
|
The bootloader must contain a dummy interrupt function with a goto 0x808. The CCS bootloader examples have. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|