 |
 |
| View previous topic :: View next topic |
| Author |
Message |
carl
Joined: 06 Feb 2008 Posts: 240 Location: Chester
|
|
Posted: Sat Apr 05, 2008 8:56 am |
|
|
Hi Ttelmah, Again thankyou for the quick reply.
I will try everything you have suggested. Could you explain though why this works in the single program on its own (code1) - but not not as (code2)
Carl |
|
 |
Ttelmah Guest
|
|
Posted: Sat Apr 05, 2008 9:09 am |
|
|
Code2, as posted, is not complete, and couldn't compile, so it is impossible to answer.
Remember that it isn't just functions in the bit you are currently executing, but functions anywhere 'in' the external code that may be called at any time, versus functions anywhere in the any interrupt routines, that lead to the error message.
Best Wishes |
|
 |
carl
Joined: 06 Feb 2008 Posts: 240 Location: Chester
|
|
Posted: Sat Apr 05, 2008 10:00 am |
|
|
Wow! Ttelmah - you are good.
I made it an array of 4 and it compiled correctly. I will now test it fully and let you know how it works out.
Thankyou so much
Carl |
|
 |
carl
Joined: 06 Feb 2008 Posts: 240 Location: Chester
|
|
Posted: Sat Apr 05, 2008 11:22 am |
|
|
It Worked!!!!!!
And one last question - rampup_init() is just one branch (out of a possible 4) of the program - and this uses the ISR with CCP1.
for the other 3 branches I also need to use an ISR with CCP - but can I just keep using CCP1 for all the branches - they will not be called at the same time or is there another way because I dont think it likes it whan I have the same ISR in different branches.
I know that the PIC has 2 CCP's - CCP-1 and CCP-2 - but I need 4 for all the branches. |
|
 |
carl
Joined: 06 Feb 2008 Posts: 240 Location: Chester
|
|
Posted: Sat Apr 05, 2008 2:08 pm |
|
|
done it.
Integrated the programs together so that i can use all four brancehes in two programs with 2 CCP's
Rampup/down and Rampdown/up:
| Code: | #use delay(clock=4000000)
void rampup_init(void);
void rampdown_init(void);
void ramp_init(void);
long int rampDir;
long int rampDir2;
float time_init();
float b;
long int a,c,outs;
long int rampy[23][4] = {
{ 0,0,0,0 }, //row0
{200,25,65535,0},
{255,17,65529,0},
{207,9,65532,0},
{217,7,65535,0},
{228,6,65532,0}, //row5
{230,5,65535,0},
{212,4,65535,0},
{244,4,65535,0},
{207,3,65534,0},
{228,3,65534,0}, //row 10
{252,3,65534,0},
{276,3,65534,0},
{198,2,65534,0},
{214,2,65534,0},
{228,2,65534,0}, //row 15
{244,2,65534,0},
{260,2,65534,0},
{274,2,65534,0},
{290,2,65534,0},
{306,2,65534,0}, //row 20
{320,2,65534,0},
{338,2,65534,0}
};
#int_CCP1
CCP1_isr(){
if (a <= 22){
CCP_1 = CCP_1 + rampy[a][0]; //a between 1 and 16
if (rampDir == 1){ //Ramp Up
outs += rampy[a][1];
output_B(make8(outs,0));
output_D(make8(outs,1));
}
else if (rampDir == 0) { //Ramp Down
outs -= rampy[a][1];
output_B(make8(outs,0));
output_D(make8(outs,1)); }
if (outs == 0){ //Change to Ramp up next time
rampDir = 1; }
else if (outs >= 65525){ //Change to Ramp down next time
rampDir = 0;
}
}
else {
CCP_1 = CCP_1 + c; //a is greater than 22
if (rampDir2 == 1) { //Ramp Up
outs +=1 ;
output_B(make8(outs,0));
output_D(make8(outs,1)); }
else if (rampDir2 == 0){ //Ramp Down
outs -=1 ;
output_B(make8(outs,0));
output_D(make8(outs,1)); }
if (outs == 0){ //Change to Ramp up next time
rampDir2 = 1;}
else if (outs == 65535){ //Change to Ramp down next time
rampDir2 = 0; }
}
}
void rampup_init() {
rampDir =1;
rampDir2 =1;
ramp_init();}
void rampdown_init() {
rampDir =0;
rampDir2 =0;
ramp_init();}
void ramp_init() {
a=time_init();
b=(float)1000000*a/131070;
c=(b+0.5);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1); // enable all interupts
setup_ccp1(CCP_COMPARE_INT);
enable_interrupts(INT_CCP1);
enable_interrupts(global);
while (TRUE) ;
} |
Stepup/down and stepdown/up:
| Code: | #use delay(clock=4000000)
void stepup_init(void);
void stepdown_init(void);
void step_init(void);
long int stepDir3;
long int stepDir4;
float time_init();
float v;
long int w,z,outs2;
long int rampy2[23][4] = { //THESE VALUES STILL NEED TO BE CHANGED
{ 0,0,0,0 }, //row0
{200,25,65535,0},
{255,17,65529,0},
{207,9,65532,0},
{217,7,65535,0},
{228,6,65532,0}, //row5
{230,5,65535,0},
{212,4,65535,0},
{244,4,65535,0},
{207,3,65534,0},
{228,3,65534,0}, //row 10
{252,3,65534,0},
{276,3,65534,0},
{198,2,65534,0},
{214,2,65534,0},
{228,2,65534,0}, //row 15
{244,2,65534,0},
{260,2,65534,0},
{274,2,65534,0},
{290,2,65534,0},
{306,2,65534,0}, //row 20
{320,2,65534,0},
{338,2,65534,0}
};
#int_CCP2
CCP2_isr(){
if (w <= 22){
CCP_2 = CCP_2 + rampy2[w][0]; //a between 1 and 16
if (stepDir3 == 1){ //Ramp Up
outs2 += rampy2[w][1];
output_B(make8(outs2,0));
output_D(make8(outs2,1));
}
else if (stepDir3 == 0) { //Ramp Down
outs2 -= rampy2[w][1];
output_B(make8(outs2,0));
output_D(make8(outs2,1)); }
if (outs == 0){ //Change to Ramp up next time
stepDir3 = 1; }
else if (outs2 >= 65525){ //Change to Ramp down next time
stepDir3 = 0;
}
}
else {
CCP_2 = CCP_2 + w; //a is greater than 22
if (stepDir4 == 1) { //Ramp Up
outs2 +=1 ;
output_B(make8(outs2,0));
output_D(make8(outs2,1)); }
else if (stepDir4 == 0){ //Ramp Down
outs -=1 ;
output_B(make8(outs2,0));
output_D(make8(outs2,1)); }
if (outs2 == 0){ //Change to Ramp up next time
stepDir4 = 1;}
else if (outs2 == 65535){ //Change to Ramp down next time
stepDir4 = 0; }
}
}
void stepup_init() {
stepDir3 =1;
stepDir4 =1;
step_init();}
void stepdown_init() {
stepDir3 =0;
stepDir4 =0;
step_init();}
void step_init() {
z=time_init();
v=(float)1000000*z/131070;
w=(v+0.5);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1); // enable all interupts
setup_ccp2(CCP_COMPARE_INT);
enable_interrupts(INT_CCP2);
enable_interrupts(global);
while (TRUE) ;
} |
THANKYOU SO MUCH TTELMAH - I REALLY APPRECIATE YOUR HELP
Carl |
|
 |
Ttelmah Guest
|
|
Posted: Sat Apr 05, 2008 3:01 pm |
|
|
Well done.
Best Wishes |
|
 |
carl
Joined: 06 Feb 2008 Posts: 240 Location: Chester
|
|
Posted: Sun Apr 06, 2008 5:33 am |
|
|
Hi Ttelmah,
Sorry for bothering you again but i have just come across a snag with the stepup/down and stepdown/up prgram withh ccp2. I need a really slow tick time because there will be only be eight steps in this program (4 up and 4 down OR 4 down and 4 up) whereas before there was 65535 steps.
And I am using CCP compare - which has to utilise timer1 or timer3 - even with the prescale of maximum 8 it is far too fast. the roll-over would occur at 524ms.
i.e. but the time span chosen by the user (to stepup/down or stepdown/up) is anywhere between 1 second and 300seconds which means that the slowest rollover would be:
tick time = 524ms =
8ticks (=complete span) x 524ms = 4.19 seconds (this is with the prescale of 8)
This value is no good because I need the the tick time to be for the slowest roll-over:
300/'n' = 37.5seconds (so 8 x 37.5seconds =300)
Is there any way I can combine two timers to give a slower tick time -the two would have to be snychronised though - as in when the rollover from one timer occurs it starts the 'other' and the 'other' would be used as normal with the CCP compare ISR.
Again sorry for bothering you
Carl |
|
 |
carl
Joined: 06 Feb 2008 Posts: 240 Location: Chester
|
|
Posted: Sun Apr 06, 2008 7:41 am |
|
|
Or another thought is, can I use two different clock sources - so the 4MHz crystal for most of the program (which is what i'm using now) and somehow utilise an internal oscillator with a much lower time base for the last bit of the program - therefore the tick rate would be a lot longer (max tick is 37.5 seconds and min tick rate is 0.125seconds)
I'm Baffled though how to do this and configure it.
Carl |
|
 |
Ttelmah Guest
|
|
Posted: Sun Apr 06, 2008 8:21 am |
|
|
If you are performing the pin updates yourself, you don't have to use a CCP interrupt. You could then look at Timer2.
Alternatively, you can count timer1 or Timer3 off the timer1 external oscillator or clock input. Feed this with a much lower frequency crystal, and you can have one CCP running off the master oscillator, and the other off this.
Best Wishes |
|
 |
carl
Joined: 06 Feb 2008 Posts: 240 Location: Chester
|
|
Posted: Mon Apr 07, 2008 1:50 am |
|
|
HI Ttelmah,
The first part I dont understand:
| Quote: | | If you are performing the pin updates yourself, you don't have to use a CCP interrupt. You could then look at Timer2. |
from below:
And the second part:
| Quote: | | Alternatively, you can count timer1 or Timer3 off the timer1 external oscillator or clock input. Feed this with a much lower frequency crystal, and you can have one CCP running off the master oscillator, and the other off this. |
I take it you mean by this that I have to have another actual physical oscillator external to the IC (As well as my 4MHz). This option would be no good because my hardware is completed - I can only play with the software. What I really need is to set a different system clock - (For instance what is the 31KHz speed referenced in the datasheet - does it mean I need to have an external oscillator to use this).
Or run the timer at a much slower rate.
Thanks
Carl |
|
 |
Ttelmah Guest
|
|
Posted: Mon Apr 07, 2008 2:47 am |
|
|
You don't seem to actually be using the CCP outputs or inputs, instead just using them as timers. Your CCP interrupt outputs the pin patterns you want, not using the hardware ability to do things on the CCP completion. Hence you don't need to be using a CCP interrupt at all. As such, you could just use a timer interrupt. Timer2 (for example), supports prescalers of 1:1, 1:4, and 1:16. Division by 1 to 256, and postscale to the interrupt of 1:1, to 1:16, giving possible divisions (from the master clock), between the interrupts,of 1:4, to 1:262144, with over 11000 possible selections between these!.
Best Wishes |
|
 |
carl
Joined: 06 Feb 2008 Posts: 240 Location: Chester
|
|
Posted: Mon Apr 07, 2008 6:25 am |
|
|
Hi Ttelmah,
Your right I'm only using the CCP as a timer interrupt so that it compares the value of timer1 with the CCP + 'n' value. No hardware effects take place.
Could I just use timer 2 for the same purpose? - but how would it do the comparing bit? when 'n' is a variable.
Carl |
|
 |
RLScott
Joined: 10 Jul 2007 Posts: 465
|
|
Posted: Mon Apr 07, 2008 11:00 am |
|
|
| carl wrote: | ...Could I just use timer 2 for the same purpose? - but how would it do the comparing bit? when 'n' is a variable...
|
Yes, you could. Timer 2 always resets to 00 one clock after it matches PR2. Note that PR2 is initialized to 0xFF on RESET, so if you do nothing to PR2, you get the full 256 counts from Timer 2. So just set PR2 to one count less than the number of Timer 2 ticks you want in a cycle, select 1:1 for the post-scaler in T2CON, and you will get a TMR2IF interrupt at the required rate.
However the CCP method using Output Compare has the advantage of being able to use a full 16 bits of timer and comparator.
Robert Scott
Real-Time Specialties |
|
 |
carl
Joined: 06 Feb 2008 Posts: 240 Location: Chester
|
|
Posted: Mon Apr 07, 2008 12:17 pm |
|
|
Sorry Robert and Ttelmah I'm confused.
I understand that I will use timer2 instead of CCP. But I dont fully understand how to set it up from the quote below:
| Quote: | | Timer2 (for example), supports prescalers of 1:1, 1:4, and 1:16. Division by 1 to 256, and postscale to the interrupt of 1:1, to 1:16, giving possible divisions (from the master clock), between the interrupts,of 1:4, to 1:262144, with over 11000 possible selections between these!. |
My tick times will be 300 possibilities:
For 1 second span = 0.125s tick time (because 8ticks x 0.125 = 1s)
For 2 second span = 0.25s tick time (because 8ticks x 0.25 = 2s)
etc
etc
etc
For 300 second span = 37.5s tick time (because 8ticks x37.5s = 300s)
Now this is where I get stuck.
If I use a prescaler of 1:16 then:
System clock = 1us
Timer 1 roll-over = 256us x 16 = 4.096ms (I need a minimum increment of 125ms - but also it shouldn't roll-over untill after 37.5s )
How then do I make this slower - What does the postscaler to the above time period taken from the datasheet do:
| Code: | A range of 16 postscale options (from 1:1 through 1:16
inclusive) can be selected with the postscaler control
bits, T2OUTPS3:T2OUTPS0 (T2CON<6:3>). |
Also could you please explain:
| Quote: | | Division by 1 to 256, |
Sorry I'm not very good at this and am taking up lots of your time.
Thanks
Carl |
|
 |
carl
Joined: 06 Feb 2008 Posts: 240 Location: Chester
|
|
Posted: Mon Apr 07, 2008 1:05 pm |
|
|
setup_timer_2(T2_INTERNAL|T2_DIV_BY_16|N|16)
Ok I think I understand but in the setup above I dont want the value N incorporated into the setup because I want to use something like shown below. So I have my divide by, postscale but how do I include the prescale into the setup - whilst also removing the N - or would N = rampy[a][0];
THereofre:
setup_timer_2(T2_INTERNAL|T2_DIV_BY_16|rampy[a][0]|16)
#int_TIMER2
TIMER2_isr()
{
Timer_2 = Timer_2 + rampy[a][0];
//code to go here
}
THanks Carl |
|
 |
|
|
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
|