|
|
View previous topic :: View next topic |
Author |
Message |
MAKInnovation
Joined: 16 Nov 2010 Posts: 61
|
#int_ccp1 problem |
Posted: Mon May 27, 2013 4:55 am |
|
|
Dear Friends;
I will add some part of my code at the end my messages.
My code runs fine once it is started normally but when power fluctuate the microcontroller does not restart from beginning & it actually runs #int_ccp1
interrupts
This adds some value in TPulses.Tval
i.e: "TPulses.Tval = Tpulses.Tval + 0.1;"
I used timer0 and timer1 for generating interrupts about 76 times per second. (This precise value is helping me to run some calculation of my code)
How could i resolve this issue.
Code: |
#define USE_WITH_PC 1
#include <18F452.h>
#fuses HS, NOWDT, NOLVP, PROTECT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, errors)
#define FAST_GLCD
//RTC variables
#define XTAL_FREQUENCY 20000000
#define TIMER1_FREQUENCY (XTAL_FREQUENCY / 4) // 1 clock tick = 1 instr. cycle = crystal frequency / 4
////////////////////////////////////////////////////////////////////////////////
// Initialize RTC
////////////////////////////////////////////////////////////////////////////////
void Initialize_RTC(void)
{
Ticker = TIMER1_FREQUENCY; // initialize clock counter to number of clocks per second
setup_timer_1( T1_INTERNAL | T1_DIV_BY_1 ); // initialize 16-bit Timer1 to interrupt
// exactly every 65536 clock cycles
// (about 76 times per second)
enable_interrupts( INT_TIMER1 ); // Start RTC
}
#org DEFAULT
// Timer 1
#org 0x1000, 0x1FFF DEFAULT
#int_TIMER1
void TIMER1_isr()
{
Ticker -= 65536;// Decrement ticker by clocks per interrupt
if ( Ticker < 65536 ) // If second has expired
{ Ticker += TIMER1_FREQUENCY; // Increment ticker by clocks per second
seconds++; // Increment number of seconds
}
IPulses.Ival = Pulses1/360;
if (Seconds == 30) {Seconds=0;}
}
#org DEFAULT
#int_ccp1
void isr()
{
Pulses1 = Pulses1 + 1;
TPulseCount = TPulseCount + 1;
if (TPulseCount > 3)
{
TPulseCount = 0;
TPulses.Tval = Tpulses.Tval + 0.1;
}
}
////////////////////////////////////////////////////////////////////////////////
// MAIN Program
////////////////////////////////////////////////////////////////////////////////
void main()
{
SET_TRIS_C( 0x8F );
IPulses.Ival = 0;
setup_ccp1(CCP_CAPTURE_FE); // Configure CCP1 to capture rise
Initialize_RTC();
enable_interrupts(INT_CCP1); // Setup interrupt on falling edge
enable_interrupts( GLOBAL );
while(1)
{
if (seconds != prev_second)
{
prev_second = seconds;
sprintf(IFlow,"%3.0f",IPulses.Ival);
sprintf(TFlow,"%10.1f",TPulses.Tval);
delay_ms(10);
}
}
}
|
Regards;
Ashraf |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Mon May 27, 2013 6:35 am |
|
|
there are several issues with your code.
the first is it won't compile.
at a minimum, you omitted the data definitions for things like 'ticker'
how about a post that WILL compile ?? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 27, 2013 11:59 am |
|
|
Quote: | but when power fluctuate the microcontroller does not restart from beginning |
How to solve this problem ? Read the 18F452 data sheet:
Quote: |
3.5 Brown-out Reset (BOR)
A configuration bit, BOREN, can disable (if clear/
programmed), or enable (if set) the Brown-out Reset
circuitry. If VDD falls below parameter D005 for greater
than parameter 35, the brown-out situation will reset
the chip. A RESET may not occur if VDD falls below
parameter D005 for less than parameter 35. The chip
will remain in Brown-out Reset until VDD rises above
BVDD. |
Parameter D005 is the selected Brownout reset trigger voltage.
In the 18F452.h file, it lists the Brownout fuses:
Code: | BROWNOUT,BORV45,BORV42,BORV27,BORV20 |
Check your 18F452.h file to find the fuses. Those are the settings
available for compiler vs. 4.141.
See the program below. I have added the fuse for Brownout at 4.5v:
Code: |
#include <18F452.h>
#fuses XT,NOWDT,PUT,NOLVP,BORV45
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)
//======================================
void main(void)
{
while(1);
} |
The 18F452 (non-LF version) can only operate at a Vdd voltage of 4.2v
or above, so the BORV45 fuse is the only one you can use. |
|
|
MAKInnovation
Joined: 16 Nov 2010 Posts: 61
|
|
Posted: Mon May 27, 2013 11:03 pm |
|
|
I have added data definition in the code and now it can compile.
My problem is the "#int_ccp1", it executes when power fluctuate or 5V adapter lose connection.
On power fluctuation, the totalizer value increment automatically without having pulses on ccp1 pin.
This one: TPulses.Tval = Tpulses.Tval + 0.1;
I also pull up the RC2/CCP1 pin with 1K resistor.
My compiler version is PCH Compiler 4.057.
Code: | #define USE_WITH_PC 1
#include <18F452.h>
#fuses HS, NOWDT, NOLVP, PROTECT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, errors)
#define FAST_GLCD
//RTC variables
#define XTAL_FREQUENCY 20000000
#define TIMER1_FREQUENCY (XTAL_FREQUENCY / 4) // 1 clock tick = 1 instr. cycle = crystal frequency / 4
int32 Ticker;
int8 Seconds=0;
union {
int8 Ibytes[4];
float32 Ival;
} IPulses;
int32 Pulses1 = 0;
int32 TPulseCount = 0;
union {
int8 Tbytes[4];
float32 Tval;
} TPulses;
////////////////////////////////////////////////////////////////////////////////
// Initialize RTC
////////////////////////////////////////////////////////////////////////////////
void Initialize_RTC(void)
{
Ticker = TIMER1_FREQUENCY; // initialize clock counter to number of clocks per second
setup_timer_1( T1_INTERNAL | T1_DIV_BY_1 ); // initialize 16-bit Timer1 to interrupt
// exactly every 65536 clock cycles
// (about 76 times per second)
enable_interrupts( INT_TIMER1 ); // Start RTC
}
#org DEFAULT
// Timer 1
#org 0x1000, 0x1FFF DEFAULT
#int_TIMER1
void TIMER1_isr()
{
Ticker -= 65536;// Decrement ticker by clocks per interrupt
if ( Ticker < 65536 ) // If second has expired
{ Ticker += TIMER1_FREQUENCY; // Increment ticker by clocks per second
seconds++; // Increment number of seconds
}
IPulses.Ival = Pulses1/360;
if (Seconds == 30) {Seconds=0;}
}
#org DEFAULT
#int_ccp1
void isr()
{
Pulses1 = Pulses1 + 1;
TPulseCount = TPulseCount + 1;
if (TPulseCount > 3)
{
TPulseCount = 0;
TPulses.Tval = Tpulses.Tval + 0.1;
}
}
////////////////////////////////////////////////////////////////////////////////
// MAIN Program
////////////////////////////////////////////////////////////////////////////////
void main()
{
int8 prev_second;
SET_TRIS_C( 0x8F );
IPulses.Ival = 0;
setup_ccp1(CCP_CAPTURE_FE); // Configure CCP1 to capture rise
Initialize_RTC();
enable_interrupts(INT_CCP1); // Setup interrupt on falling edge
enable_interrupts( GLOBAL );
while(1)
{
if (seconds != prev_second)
{
prev_second = seconds;
printf("%3.0f",IPulses.Ival);
printf("%10.1f",TPulses.Tval);
delay_ms(10);
}
}
}
|
|
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue May 28, 2013 8:35 am |
|
|
a couple of thoughts:
1-
CCS says all INT types are by default UNSIGNED
and thusly - the handling of 'ticker' in the timer ISR makes my head spin.
The comparison is not reliable in the event of an underflow - for instance ...
I don't think your timer ISR is doing what you hope it is ( whatever that might actually be ). there are FAR simpler ways to convert timer ticks into seconds than the 32 bit tap dance you are attempting. And without that sluggish division by 360 as well.
2- you need to quantify "voltage fluctuations" to some form of measured
data that would allow reasonable interpretation of what is happening to you.
do you have good PSU decoupling in your circuit? |
|
|
MAKInnovation
Joined: 16 Nov 2010 Posts: 61
|
|
Posted: Tue May 28, 2013 11:46 pm |
|
|
Dear Friends;
I modify my code with PCM Programmer suggestion and observe an improvement but there was still a problem.
Then i use a 9V battery instead of a DC adapter at the input of my regulator circuit and it works fine.
So there must be something wrong with my regulator circuit or with my decoupling capacitor selection.
I am using 7805 regulator with 4.7uf electrolytic at input and 1uf(elecrolytic ) with 100nf(film) at the output.
Need your suggestion for a suitable regulator circuit. The question is irrelevant for this forum but still asking for your kind opinions.
Regards;
Ashraf |
|
|
|
|
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
|