PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Feb 01, 2007 11:21 pm |
|
|
When the reset_cpu() function is called in a 16F chip, it jumps to
address 0x0000. The stack is 8-deep and it's circular. The stack
pointer is a modulo-8 counter. The initial value of the stack pointer
at the start of a program doesn't matter.
In the program shown below, a Timer1 interrupt occurs about 524 ms
after the start of the program. Inside the isr, the reset_cpu() function
is called. This function will jump to address 0x0000 and thus restart
the program, while leaving the return address on the stack. No hardware
reset is performed by the PIC.
The program runs OK. It restarts every time and there's no problem
with the stack. Here's the output of the program:
Quote: |
ABCDEFT
ABCDEFT
ABCDEFT
ABCDEFT
ABCDEFT
ABCDEFT
ABCDEFT
ABCDEFT
ABCDEFT
ABCDEFT
ABCDEFT
ABCDEFT
ABCDEFT
ABCDEFT
ABCDEFT
etc.
|
Here's the test program:
Code: |
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#int_timer1
void timer1_isr(void)
{
putc('T');
putc('\n');
putc('\r');
reset_cpu();
}
//===============================
void main()
{
int8 value;
// Setup Timer1 to interrupt every 524 ms (with 4 MHz crystal).
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
set_timer1(0x0000);
clear_interrupt(INT_TIMER1);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
value = 'A';
while(1)
{
putc(value);
value++;
delay_ms(100);
}
} |
|
|