Jump to content
Sign in to follow this  
paulgreeny83

Urgent Problem With 16f88 Interrupt!

Recommended Posts

Hi guys.. I am using the 16F88 PIC as part of my honours project and have come across a major snag!! :)

 

 

My problem is that I can't get my program to enter the interrupt. I am using the standard 'void interrupt (void)' function that is recognisable in C2C. I am using the first two timers as counters that increment upon a pulse on their pins (RA4, and RB6). I am using the 3rd timer, timer 2 as a timer that sets the interrupt flag once it has counted to the value set in the PR2 register (255 in my case). I am using the matrix multimedia board which has a 3.2768MHz crystal. It takes four clock pulses to complete one instruction cycle:

 

3276800/4

=819200

 

I am using a 16:1 pre-scaler:

 

819200/16

=51200

 

and interrupting once the count in timer 2 reaches 255:

 

51200/256

=200

 

I am also using software inside the interrupt which says:

 

count = count +1;

 

if (count>1000)

do interrupt

 

This should give me an interrupt that executes its code every 5 seconds?

 

The interrupt should take the values stored in the timer0 and timer1 registers, and store them in variables (called speed and beats). The program should then perform calculations on these values inside function main before outputting the values to the LCD, I get nothing.

 

I have tried just putting dummy values into variables inside the interrupt, then performing calculations on them in function main before displaying them onto the LCD and nothing happens. It works fine however when I put these dummy values into variables outside the interrupt, so I know the maths and the display routine to the LCD is fine, it just doesn't seem to be entering the interrupt.

 

I have set all hardware I can see that is relevant, global interrupts is set, TMR2 is on and interrupt enabled etc, it just will not enter the interrupt! I have looked at examples on the picant site and some use the interrupt as the standard void interrupt (void) function, where as others use it as normal declared function such as 'void interrupt ();'

 

Is there anything obvious you can think of that I am missing?

 

I only have two weeks left of projcet and fear a big fat ZERO if I can't get this working! :o

 

Any ideas?

 

Cheers

 

Paul

Share this post


Link to post
Share on other sites

Paul,

 

Your calculations and the things you are doing look correct.

 

I would suggest having a look at the sample code that is part of the SourceBoost installation, there is a timer interrupt example in there for timer0. Maybe you can get this code to work with your hardware and work out what is wrong.

 

Regards

Dave

Share this post


Link to post
Share on other sites
Check count declaration. Must be short (for a two bytes short integer) or (count > 1000) will never be true.

 

 

Hi Paul,

 

 

I am not familiar wiht the particular chip, but ...

 

Did you?

Have the Global interrupt enable flag set to on?

Have the individual peripheral interrupt enable set to on?

 

Do not forget to clear the individual peripheral interrupt flag just before leaving the interrupt code, when you have dealt with the interrupt code.

Depending on the chip, sometimes peripherals wont work with their interrupt flag still set. In these cases you have to clear the flags outside the interrupt loop.

 

I hope this helps.

 

 

Frans.

Share this post


Link to post
Share on other sites

[/code]/* Honours Project 3 - A Bicycle Data Logger */

/* Paul Greenhalgh 2005 */

 

/* HARDWARE CONFIGURATION */

 

// REGISTER FILES

 

char _INDF@0x00;

char _TMR0@0x01;

char _PCL@0x02;

char _STATUS@0x03;

char _FSR@0x04;

char _PORTA@0x05;

char _PORTB@0x06;

char _PORTC@0x07;

char _PORTD@0x08;

char _PORTE@0x09;

char _PCLATH@0x0A;

char _INTCON@0x0B;

char _PIR1@0x0C;

char _PIR2@0x0D;

char _TMR1L@0x0E;

char _TMR1H@0x0F;

char _T1CON@0x10;

char _TMR2@0x11;

char _T2CON@0x12;

char _SSPBUF@0x13;

char _SSPCON@0x14;

char _CCPR1L@0x15;

char _CCPR1H@0x16;

char _CCP1CON@0x17;

char _RCSTA@0x18;

char _TXREG@0x19;

char _RCREG@0x1A;

char _CCPR2L@0x1B;

char _CCPR2H@0x1C;

char _CCP2CON@0x1D;

char _ADRESH@0x1E;

char _ADCON0@0x1F;

char _OPTION_REG@0x81;

char _TRISA@0x85;

char _TRISB@0x86;

char _TRISC@0x87;

char _TRISD@0x88;

char _TRISE@0x89;

char _PIE1@0x8C;

char _PIE2@0x8D;

char _PCON@0x8E;

char _SSPCON2@0x91;

char _PR2@0x92;

char _SSPADD@0x93;

char _SSPSTAT@0x94;

char _TXSTA@0x98;

char _SPBRG@0x99;

//char _ANSEL@0x9B;

char _ADRESL@0x9E;

char _ADCON1@0x9F;

char _EEDATA@0x10C;

char _EEADR@0x10D;

char _EEDATH@0x10E;

char _EEADRH@0x10F;

char _EECON1@0x18C;

char _EECON2@0x18D;

 

// STATUS Bits Masks

 

#define _IRP 0x80

#define _RP1 0x40

#define _RP0 0x20

#define _NOT_TO 0x10

#define _NOT_PD 0x08

#define _Z 0x04

#define _DC 0x02

#define _C 0x01

 

// INTCON Bits Masks

 

#define _GIE 0x80

#define _PEIE 0x40

#define _TMR0IE 0x20

#define _INT0IE 0x10

#define _RBIE 0x08

#define _TMR0IF 0x04

#define _INTF 0x02

#define _RBIF 0x01

 

// PIR1 Bits Masks

 

#define _PSPIF 0x80

#define _ADIF 0x40

#define _RCIF 0x20

#define _TXIF 0x10

#define _SSPIF 0x08

#define _CCP1IF 0x04

#define _TMR2IF 0x02

#define _TMR1IF 0x01

 

// PIR2 Bits Masks

 

#define _EEIF 0x10

#define _BCLIF 0x08

#define _CCP2IF 0x01

 

// T1CON Bits Masks

 

#define _T1CKPS1 0x20

#define _T1CKPS0 0x10

#define _T1OSCEN 0x08

#define _NOT_T1SYNC 0x04

#define _T1INSYNC 0x04 // Backward compatibility only

#define _T1SYNC 0x04

#define _TMR1CS 0x02

#define _TMR1ON 0x01

 

// T2CON Bits Masks

 

#define _TOUTPS3 0x40

#define _TOUTPS2 0x20

#define _TOUTPS1 0x10

#define _TOUTPS0 0x08

#define _TMR2ON 0x04

#define _T2CKPS1 0x02

#define _T2CKPS0 0x01

 

// SSPCON Bits Masks

 

#define _WCOL 0x80

#define _SSPOV 0x40

#define _SSPEN 0x20

#define _CKP 0x10

#define _SSPM3 0x08

#define _SSPM2 0x04

#define _SSPM1 0x02

#define _SSPM0 0x01

 

// CCP1CON Bits Masks

 

#define _CCP1X 0x20

#define _CCP1Y 0x10

#define _CCP1M3 0x08

#define _CCP1M2 0x04

#define _CCP1M1 0x02

#define _CCP1M0 0x01

 

// RCSTA Bits Masks

 

#define _SPEN 0x80

#define _RX9 0x40

#define _RC9 0x40 // Backward compatibility only

#define _NOT_RC8 0x40 // Backward compatibility only

#define _RC8_9 0x40 // Backward compatibility only

#define _SREN 0x20

#define _CREN 0x10

#define _ADDEN 0x08

#define _FERR 0x04

#define _OERR 0x02

#define _RX9D 0x01

#define _RCD8 0x01 // Backward compatibility only

 

// CCP2CON Bits Masks

 

#define _CCP2X 0x20

#define _CCP2Y 0x10

#define _CCP2M3 0x08

#define _CCP2M2 0x04

#define _CCP2M1 0x02

#define _CCP2M0 0x01

 

// ADCON0 Bits Masks

 

#define _ADCS1 0x80

#define _ADCS0 0x40

#define _CHS2 0x20

#define _CHS1 0x10

#define _CHS0 0x08

#define _GO 0x04

#define _NOT_DONE 0x04

#define _GO_DONE 0x04

#define _ADON 0x01

 

// OPTION_REG Bits Masks

 

#define _NOT_RBPU 0x80

#define _INTEDG 0x40

#define _T0CS 0x20

#define _T0SE 0x10

#define _PSA 0x08

#define _PS2 0x04

#define _PS1 0x02

#define _PS0 0x01

 

// TRISE Bits Masks

 

#define _IBF 0x80

#define _OBF 0x40

#define _IBOV 0x20

#define _PSPMODE 0x10

#define _TRISE2 0x04

#define _TRISE1 0x02

#define _TRISE0 0x01

 

// PIE1 Bits Masks

 

#define _PSPIE 0x80

#define _ADIE 0x40

#define _RCIE 0x20

#define _TXIE 0x10

#define _SSPIE 0x08

#define _CCP1IE 0x04

#define _TMR2IE 0x02

#define _TMR1IE 0x01

 

// PIE2 Bits Masks

 

#define _EEIE 0x10

#define _BCLIE 0x08

#define _CCP2IE 0x01

 

// PCON Bits Masks

 

#define _NOT_POR 0x02

#define _NOT_BO 0x01

#define _NOT_BOR 0x01

 

// SSPCON2 Bits Masks

 

#define _GCEN 0x80

#define _ACKSTAT 0x40

#define _ACKDT 0x20

#define _ACKEN 0x10

#define _RCEN 0x08

#define _PEN 0x04

#define _RSEN 0x02

#define _SEN 0x01

 

// SSPSTAT Bits Masks

 

#define _SMP 0x80

#define _CKE 0x40

#define _D 0x20

#define _I2C_DATA 0x20

#define _NOT_A 0x20

#define _NOT_ADDRESS 0x20

#define _D_A 0x20

#define _DATA_ADDRESS 0x20

#define _P 0x10

#define _I2C_STOP 0x10

#define _S 0x08

#define _I2C_START 0x08

#define _R 0x04

#define _I2C_READ 0x04

#define _NOT_W 0x04

#define _NOT_WRITE 0x04

#define _R_W 0x04

#define _READ_WRITE 0x04

#define _UA 0x02

#define _BF 0x01

 

// TXSTA Bits Masks

 

#define _CSRC 0x80

#define _TX9 0x40

#define _NOT_TX8 0x40 // Backward compatibility only

#define _TX8_9 0x40 // Backward compatibility only

#define _TXEN 0x20

#define _SYNC 0x10

#define _BRGH 0x04

#define _TRMT 0x02

#define _TX9D 0x01

#define _TXD8 0x01 // Backward compatibility only

 

// ADCON1 Bits Masks

 

#define _ADFM 0x80

#define _PCFG3 0x08

#define _PCFG2 0x04

#define _PCFG1 0x02

#define _PCFG0 0x01

 

// ANSEL Bits Masks

 

#define _ANS6 0x40

#define _ANS5 0x20

#define _ANS4 0x10

#define _ANS3 0x08

#define _ANS2 0x04

#define _ANS1 0x02

#define _ANS0 0x01

 

 

// EECON1 Bits Masks

 

#define _EEPGD 0x80

#define _WRERR 0x08

#define _WREN 0x04

#define _WR 0x02

#define _RD 0x01

 

void setup_hardware ( void )

{

clear_bit ( _STATUS,_RP1 );

set_bit ( _STATUS,_RP0 ); /* Selects register bank 1 */

 

clear_bit ( _OPTION_REG,_NOT_RBPU ); /* Enables RB pull-ups */

set_bit ( _OPTION_REG,_T0CS ); /* Counter mode for external input - sensor 1 */

clear_bit ( _OPTION_REG,_T0SE ); /* Increments counter on low-to-high trasition */

clear_bit ( _OPTION_REG,_PSA ) ; /* Assigns prescaler to timer0 module */

//_OPTION_REG = 32;

 

clear_bit ( _INTCON,_TMR0IE ); /* Disables timer 0 interrupt */

set_bit ( _INTCON,_GIE ); /* Enables global interrupts */

set_bit ( _INTCON,_PEIE ); /* Enables peripheral interrupts */

clear_bit ( _INTCON,_INT0IE ); /* Disables RB0 external interrupt */

//_INTCON = 192;

 

clear_bit ( _PIE1,_ADIE );

set_bit ( _PIE1,_TMR2IE ); /* Enables timer 2 interrupt */

//_PIE1 = 2;

 

clear_bit ( _ANSEL,_ANS6 );

clear_bit ( _ANSEL,_ANS5 );

clear_bit ( _ANSEL,_ANS4 );

clear_bit ( _ANSEL,_ANS3 );

clear_bit ( _ANSEL,_ANS2 );

clear_bit ( _ANSEL,_ANS1 );

clear_bit ( _ANSEL,_ANS0 ); /* Declares input pins as digital */

//_ANSEL = 0;

 

_PR2 = 10; /* Setting the number timer 2 has to count to to cause interrupt */

_ADCON1 = 0x06;

 

clear_bit ( _STATUS,_RP1 );

clear_bit ( _STATUS,_RP0 ); /* Selects register bank 0 */

 

clear_bit ( _T1CON,_T1OSCEN ); /* Uses RB6 as external input pin */

set_bit ( _T1CON,_NOT_T1SYNC); /* Do not synchronize external input to clock */

set_bit ( _T1CON,_TMR1CS ); /* Counter mode for external input - sensor 1 */

set_bit ( _T1CON,_TMR1ON ); /* Turns timer 1 on */

//_T1CON = 3;

 

clear_bit ( _T2CON,_TOUTPS3 );

clear_bit ( _T2CON,_TOUTPS2 );

clear_bit ( _T2CON,_TOUTPS1 );

clear_bit ( _T2CON,_TOUTPS0 ); /* Sets post-scaler to 1:1 */

set_bit ( _T2CON,_TMR2ON ); /* Turns timer 2 on */

set_bit ( _T2CON,_T2CKPS1 );

clear_bit ( _T2CON,_T2CKPS0 ); /* Sets pre-scaler to 16:1 */

//_T2CON = 256;

}

int speed = 0; /* Variables used in interrupt rountine */

int beats = 0;

int speedresult = 0;

int beatsresult = 0;

short count = 0;

 

 

 

/* FUNCTION PROTOTYPES */

 

char lcd_start ( void ) ;

 

char lcd_clear ( void ) ;

 

char lcd_print_ch ( char ch ) ;

 

char lcd_print (

const char * message ) ;

 

char lcd_cursor ( char x, char y ) ;

 

#define RSMASK 0x10

#define EBIT 0x05 /* Masks for control bits */

 

// defined values for delays tested up to 20Mhz PICmicro

 

#define PUTCH_DELAY 3000 /* Standard write delay */

 

#define CLEAR_DELAY 20000 /* Clear and cursor home take longer: special delay for them */

 

#define POWER_UP_DELAY 10000 /* Delay to let the LCD settle */

 

#define BIT_DELAY 2 /* Delay to let the ports settle */

 

void lcd_delay ( int size )

{

int i ;

 

for ( i = 0 ; i < size ; i++ ) ;

}

 

/* sends a byte out to the LCD */

/* the byte is given by in, the */

/* mask is used to allow the */

/* state of RS to be set as well */

 

void lcd_raw_send (

unsigned char in,

unsigned char mask )

{

unsigned char pb ;

 

/* use a PICmicro assembler */

/* to swap the nibbles in the */

/* input */

/* puts high nibble at the */

/* bottom of the byte */

 

asm swapf param00_lcd_raw_send,F

 

/* OR in the mask */

 

pb = (in & 0x0f ) | mask ;

 

/* OR in the other bits */

/* PORTB */

 

pb = pb | (PORTB & 0xc0) ;

 

/* send the data */

/* don't disturb the other */

/* bits in PORTB */

 

PORTB = pb ;

 

/* let the bits settle */

lcd_delay ( BIT_DELAY ) ;

 

/* now clock the bit out */

/* by raising and lowering E */

 

set_bit ( PORTB, EBIT ) ;

 

/* let the bits settle */

lcd_delay ( BIT_DELAY ) ;

 

clear_bit ( PORTB, EBIT ) ;

 

/* put the low nibble back */

/* into in */

 

asm swapf param00_lcd_raw_send,F

 

/* OR in the mask */

 

pb = (in & 0x0f ) | mask ;

 

/* OR in the other bits */

/* PORTB */

 

pb = pb | (PORTB & 0xc0) ;

 

/* send the data */

/* don't disturb the other */

/* bits in PORTB */

 

PORTB = pb ;

 

/* let the bits settle */

lcd_delay ( BIT_DELAY ) ;

 

/* now clock the bit out */

/* by raising and lowering E */

 

set_bit ( PORTB, EBIT ) ;

 

/* let the bits settle */

lcd_delay ( BIT_DELAY ) ;

 

clear_bit ( PORTB, EBIT ) ;

 

/* do the delay here */

lcd_delay (PUTCH_DELAY) ;

}

 

/* puts a character at the */

/* cursor position */

 

char lcd_print_ch ( char in )

{

/* use raw send with RS set */

lcd_raw_send ( in, RSMASK ) ;

return 1 ;

}

 

/* sends a command to the LCD */

 

void lcd_command (

unsigned char in )

{

lcd_raw_send ( in, 0 ) ;

}

 

/* clear the display */

/* and home the cursor */

 

char lcd_clear ( void )

{

lcd_command ( 0x01 ) ;

/* do extra delay here */

lcd_delay (CLEAR_DELAY) ;

lcd_command ( 0x02 ) ;

/* do extra delay here */

lcd_delay (CLEAR_DELAY) ;

return 1 ;

}

 

/* position the cursor */

/* 0,0 is the top left corner */

 

char lcd_cursor ( unsigned char x,

unsigned char y )

{

 

if ( y==0 )

{

/* position for line 0 */

y=0x80 ;

}

else

{

/* position for line 1 */

y=0xc0 ;

}

 

lcd_command ( y+x ) ;

return 1 ;

}

 

const unsigned char lcd_init [5] =

{

/* LCD initialize */

0x33,

/* Set for 4 bit operation */

0x32,

/* Set for 2 line LCD */

0x2c,

/* Select move after write */

0x06,

/* disp. on cursor off blink off*/

0x0c

} ;

 

void bpm_display_value ( int value )

{

unsigned char hunds, tens, units ;

 

/* first get the digits */

 

units = value % 10 ;

value = value / 10 ;

tens = value % 10 ;

value = value / 10 ;

hunds = value % 10 ;

 

/* now display them */

 

lcd_print_ch ( '0' + hunds ) ;

lcd_print_ch ( '0' + tens ) ;

lcd_print_ch ( '0' + units ) ;

}

 

void mph_display_value ( int value )

{

unsigned char hunds, tens, units ;

 

/* first get the digits */

 

units = value % 10 ;

value = value / 10 ;

tens = value % 10 ;

value = value / 10 ;

hunds = value % 10 ;

 

/* now display them */

 

lcd_print_ch ( '0' + hunds ) ;

lcd_print_ch ( '0' + tens ) ;

lcd_print_ch ( '.' ) ;

lcd_print_ch ( '0' + units ) ;

}

 

char lcd_start (void)

{

unsigned char i, t ;

 

clear_bit ( _STATUS,_RP1 );

set_bit ( _STATUS,_RP0 ); /* switch to register bank 1 */

 

_TRISA = 255; /* sets all port a pins as inputs */

_TRISB = 192;

 

/* sets port b pins 0 to 5 as outputs (LCD), and 6 to 7 as inputs (TMR1) */

 

clear_bit ( _STATUS,_RP1 ); /* switch to register bank 0 */

clear_bit ( _STATUS,_RP0 );

 

/* give the LCD time to settle */

/* from power up */

 

lcd_delay ( POWER_UP_DELAY ) ;

 

for ( i=0 ; i < 5 ; i++ )

{

lcd_command ( lcd_init ) ;

}

lcd_clear () ;

return 1 ;

}

 

/* lcd_print accepts a pointer */

/* parameter which points at the*/

/* string to be printed */

 

char lcd_print (

const unsigned char * mess )

{

unsigned char i = 0 ;

 

/* treat the pointer as an */

/* array and send bytes until*/

/* we find an element with 0 */

/* in it */

 

while ( mess != 0 )

{

lcd_print_ch ( mess ) ;

i++ ;

}

return 1 ;

}

 

const unsigned char MPH [4] =

{

'M','P','H',0x00

} ;

 

const unsigned char BPM [4] =

{

'B','P','M',0x00

} ;

 

unsigned char fsr;

unsigned char indf;

 

void tmrHandler ( void )

{

//clear_bit (_STATUS,_RP0);

fsr = _FSR;

indf = _INDF;

 

speed = _TMR0;

beats = _TMR1L;

 

//speed = 40;

// beats = 5;

 

_TMR0 = 0;

_TMR1L = 0;

 

_FSR = fsr;

_INDF = indf;

 

count = 0;

}

 

 

 

void interrupt (void)

 

{

if (_PIR1 & 2)

{

clear_bit(_PIR1,1);

count = count + 1;

}

 

 

if (count>1000)

{

tmrHandler();

}

}

 

void main ( void )

{

 

setup_hardware ();

clear_bit(_PIR1,1);

lcd_start ();

 

_TMR0 = 0;

_TMR1L = 0;

 

 

while(1)

{

 

 

/* 96 comes from wheel circumference x 12 x 60 / 63360 (=0.96)*/

/* Need to then multiply this number by 100 then divide by 10 */

/* To avoid using decimal points. A '.' character is placed */

/* on the display in the mph_display_value function to give */

/*the impression of a decimal point */

 

beatsresult = beats * 12;

speedresult = speed * 96 / 10;

 

unsigned char x, y;

unsigned char i = 0;

 

 

 

lcd_cursor ( 4, 0 );

lcd_print ( MPH );

 

lcd_cursor ( 4, 1 );

lcd_print ( BPM );

 

lcd_cursor ( 0, 0 );

lcd_print_ch ( 'x' );

lcd_cursor ( 1, 0 );

lcd_print_ch ( 'x' );

 

lcd_cursor ( 7, 0 );

lcd_print_ch ( ':' );

 

lcd_cursor ( 14, 0 );

lcd_print_ch ( 'x' );

lcd_cursor ( 15, 0 );

lcd_print_ch ( 'x' );

 

lcd_cursor ( 0, 1 );

lcd_print_ch ( 'x' );

lcd_cursor ( 1, 1 );

lcd_print_ch ( 'x' );

 

lcd_cursor ( 7, 1 );

lcd_print_ch ( ':' );

 

lcd_cursor ( 14, 1 );

lcd_print_ch ( 'x' );

lcd_cursor ( 15, 1 );

lcd_print_ch ( 'x' );

 

lcd_cursor ( 8, 0 );

mph_display_value ( speedresult );

 

lcd_cursor ( 8, 1 );

bpm_display_value ( beatsresult );

 

}

}




			
		

Share this post


Link to post
Share on other sites

Hi Paul,

 

 

Try this in your interrupt.

 

if ( (_PIR1 & 2) != 0 )

 

 

I hope this is in time and helps.

 

 

With best regards,

 

 

Frans.

 

Note: I edited it to your naming of registers.

 

Frans.

Edited by fransp

Share this post


Link to post
Share on other sites

Hi Paul,

 

 

Forget about my previous post.

 

I made a mistake there, 2 changes at once leaves the error syndrome.

 

but you forgot to turn on timer 2

 

Put:

 

set_bit ( _T2CON , TMR2ON ) ;

 

in the setup.

 

 

With best regards.

 

 

Frans.

Share this post


Link to post
Share on other sites

Sorry guys forgot to say this is my code that i'm having a nightmare with!

 

Every register I can see that is required is set up as required.. I can only think it's a syntax error that compiles but does not do what is should.

 

Any ideas? I'm at me wits end!! ;)

 

Cheers

 

Paul

Share this post


Link to post
Share on other sites

Hi Fran

 

Thanks for your reply.

 

The _T2CON,TMR2ON bit is actually set in the code if you look carefully, there are so many it's hard to spot! ;)

 

There's nothing else I can think of, i've tested everything.. maybe it's staring me in the face but can't spot it?!

 

Cheers

 

Paul

Share this post


Link to post
Share on other sites

paul,

 

Timer 2 is not like timer 0.

It keeps timing until its value matches that of Timer2s preset register (PR2).

 

I can't see you loading up PR2 in your code, so the timer will match the PR2 value.

 

you need:

 

....
volatile char pr2 @ PR2;
....
pr2 = 255;
....

Hope that helps.

 

Regards

Dave

Share this post


Link to post
Share on other sites
paul,

 

Timer 2 is not like timer 0.

It keeps timing until its value matches that of Timer2s preset register (PR2).

 

I can't see you loading up PR2 in your code, so the timer will match the PR2 value.

 

you need:

 

....
volatile char pr2 @ PR2;
....
pr2 = 255;
....

Hope that helps.

 

Regards

Dave

 

Hi Paul,

 

You are right I found it.

 

Maybe Dave is right, normally PR is 255 at start-up as the data sheet says.

But in your code I found a

 

_PR2 = 10

 

It is just after the clear_bit (_ANSEL, _ANS6 ).....

 

I also noticed you commented out the adres of _ANSEL.

 

 

With best regards,

 

 

Frans.

Share this post


Link to post
Share on other sites
paul,

 

Timer 2 is not like timer 0.

It keeps timing until its value matches that of Timer2s preset register (PR2).

 

I can't see you loading up PR2 in your code, so the timer will match the PR2 value.

 

you need:

 

....
volatile char pr2 @ PR2;
....
pr2 = 255;
....

Hope that helps.

 

Regards

Dave

 

Hi Dave

 

Thanks for your reply. PR2 register is actually in there in the hardware setup function. In the post I have it equal to 10, this was when I was trouble shooting, I normally have it set to 255, although from what I can gather PR2 defaults to 255 at reset.. if no value is written to it..

 

I'm sure it's something like that that I have missed though!!

 

Any more ideas??

 

Cheers

Paul

Share this post


Link to post
Share on other sites

Hi Frans,

 

Cheers for the post. You're right the ANSEL address is quoted out, this was done during troubleshooting, it has been included before and timer 2 still not worked. PR2 was also changed to 10 during troubleshooting, and is normally 255.

 

I can only think that there is a register some where that is connected to timer 2 that requires configuring correctly for timer 2 to work correctly, either that or when a timer 2 to PR2 match occurs and sets PIR1,TMR2IF, the interrupt routine i.e. if (PIR & 2) is not correct format, although I have changed this into several possible alternatives.

 

There may be some registers that are defaulted to a certain condition, whereas I want the opposite for my code to work (such as the ANSEL reg which sets pins to analogue as default)....

 

Any more ideas guys?

 

Thanks for your help

 

Paul

Share this post


Link to post
Share on other sites
Hi Frans,

 

Cheers for the post.  You're right the ANSEL address is quoted out, this was done during troubleshooting, it has been included before and timer 2 still not worked.  PR2 was also changed to 10 during troubleshooting, and is normally 255.

 

I can only think that there is a register some where that is connected to timer 2 that requires configuring correctly for timer 2 to work correctly, either that or when a timer 2 to PR2 match occurs and sets PIR1,TMR2IF, the interrupt routine i.e. if (PIR & 2) is not correct format, although I have changed this into several possible alternatives.

 

There may be some registers that are defaulted to a certain condition, whereas I want the opposite for my code to work (such as the ANSEL reg which sets pins to analogue as default)....

 

Any more ideas guys?

 

Thanks for your help

 

Paul

 

 

Hi Paul,

 

 

How sure are you that the PIC is really running. Why don't you put a blinking character on the display, not too fast, not timed by interrupt :-) but software in your main/while(1)

 

So then when this character is blinking you now the PIC is running.

 

In every project I always try to have a free pin, so I can put a LED/ or scope on it and watch the activity.

 

I will search further because in my current project Timer2 is on the menu too, although the interrupt loopt is running here on a F874 [ That is all I have at the moment. ]

 

Late note: In thr tmrhandler you have this FSR, INDF saving and restoring. Any particular reason to do that?. Or just a left over from troubleshoot!

 

With best regards,

 

Frans.

Share this post


Link to post
Share on other sites
Hi Paul,

 

 

How sure are you that the PIC is really running. Why don't you put a blinking character on the display, not too fast, not timed by interrupt :-) but software in your main/while(1)

 

So then when this character is blinking you now the PIC is running.

 

In every project I always try to have a free pin, so I can put a LED/ or scope on it and watch the activity.

 

I will search further because in my current project Timer2 is on the menu too, although the interrupt loopt is running here on a F874  [ That is all I have at the moment. ]

 

Late note: In thr tmrhandler you have this FSR, INDF saving and restoring. Any particular reason to do that?. Or just a left over from troubleshoot!

 

With best regards,

 

Frans.

 

Hi Fran.

 

The PIC is actually running as I have LCD characters on the display I.E. 'MPH:' and 'BPM:'. I also get to see the contents of my timer0 and timer1 counters when I put:

 

speed = tmr0;

beats = tmr1l;

 

inside function main. Obviously the numbers I get are not valid as the values change every time the program cycles through main.

 

It just seems that timer2 is not working correctly.

 

FSR and INDF are the working registers that get stored one I enter my interrupt into variables, they get put back at the end of the interrupt. This seems common practice in interrupts...

 

Cheers

 

Paul

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...