CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

PROPORTIONAL PRESCALER
Goto page Previous  1, 2, 3, 4  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

proportional prescaler
PostPosted: Sat Mar 29, 2008 2:25 pm     Reply with quote

Hi All,

I have moved along a bit and am stuck on an increment problem.

My issue is with the increment operation like i++
As far as I know this only increments in steps of one.
In my compilable code below I need to change it so that it increments in steps of 'n'.
Can this be done or is there a way around it. Any help as always is greatly appreciated.

code:
Code:
#include <18F4525.h>
#include <FLOAT.h>
#fuses NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP,NOPBADEN,XT
#use delay(clock=4000000) 

long int outs = 0;
#int_CCP1
CCP1_isr()
{
CCP_1 = CCP_1 + 120;
outs++;          
output_B(outs++ & 0xFF);
output_D(outs++ >> 8);
}
void main()
{
set_tris_b(0x00);
set_tris_d(0x00);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
setup_ccp1(CCP_COMPARE_INT);
enable_interrupts(INT_CCP1);
enable_interrupts(global);
while(1)
;
}


So instead of outs++, I want to increment by a higher value 'n'
Carl
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Mar 29, 2008 2:49 pm     Reply with quote

Add a number to it. Enclose the expression in parenthesis:
Code:
(i + 5)


Also, in your program, you don't have to set the TRIS.
The compiler will automatically set ports B and D to be
all outputs, when it executes the output_b() and output_d()
functions.
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Sat Mar 29, 2008 3:04 pm     Reply with quote

I tried it like this but no change






Code:
#include <18F4525.h>
#include <FLOAT.h>
#fuses NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP,NOPBADEN,XT
#use delay(clock=4000000) 

long int outs = 0;
#int_CCP1
CCP1_isr()
{
CCP_1 = CCP_1 + 120;
(outs++ +10);          
output_B((outs++ +10) & 0xFF);
output_D((outs++ +10) >> 8);
}
void main()
{
set_tris_b(0x00);
set_tris_d(0x00);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
setup_ccp1(CCP_COMPARE_INT);
enable_interrupts(INT_CCP1);
enable_interrupts(global);
while(1)
;
}
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Sat Mar 29, 2008 3:08 pm     Reply with quote

also trie it like this, but it just adds the number 10 and does not keep incrementing with steps of 10.

Code:
#int_CCP1
CCP1_isr()
{
CCP_1 = CCP_1 + 120;
(outs+10);          
output_B((outs+10) & 0xFF);
output_D((outs+10) >> 8);
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Mar 29, 2008 4:13 pm     Reply with quote

Sorry, I misunderstood what you wanted to do.

To increase a variable by more than 1, you can use the "+=" operator.
For example, to add 5 to the 'outs' variable, do it as shown in bold below:
Quote:
#int_CCP1
CCP1_isr()
{
CCP_1 = CCP_1 + 120;
outs += 5;
output_B(outs++ & 0xFF);
output_D(outs++ >> 8);
}
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Sun Mar 30, 2008 6:48 am     Reply with quote

That is superb. Thank-you very much.

And I know its cheeky but one last thing, is there a way that in the code
that I can caouse another interrupt when the timer rolls over. below it counts upto 65535 in increments of 120us. when 65535 is reached it starts the count again. i dont want this to happen, I want to go into another interrupt with compare when the roll-over occurs.

Code:
#include <18F4525.h>
#fuses NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP,NOPBADEN,XT
#use delay(clock=4000000) 

long int outs = 0;
#int_CCP1
CCP1_isr()
{
CCP_1 = CCP_1 + 120;
outs += 5;
output_B(outs++ & 0xFF);
output_D(outs++ >> 8);
}
void main()
{
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
setup_ccp1(CCP_COMPARE_INT);
enable_interrupts(INT_CCP1);
enable_interrupts(global);
while(1)
;
}


Thank-you again

Carl
crystal_lattice



Joined: 13 Jun 2006
Posts: 164

View user's profile Send private message

Another interrupt
PostPosted: Sun Mar 30, 2008 7:25 am     Reply with quote

Use the timer1 interrupt, this will occur when it rolls over. In the timer interrupt you can disable whatever function you want to stop.

Regards
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Sun Mar 30, 2008 8:27 am     Reply with quote

Thankyou Crystal. I thought this was the case and have been trying to incorporate it into the code. Have not worked ti out yet. The main idea si given in the code below which I want to be able to toggle between two compare interrupts on every roll over.

Code:
#include <18F4525.h>
#include <FLOAT.h>
#fuses NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP,NOPBADEN,XT
#use delay(clock=4000000)
long int outs = 0;
long int outs2 = 65535;
#int_CCP1
CCP1_isr()
{
CCP_1 = CCP_1 + 110;
outs += 5;       
output_B(outs++  & 0xFF);
output_D(outs++  >> 8);
}
#int_TIMER1                     //THIS IS WHAT NEEDS TO BE DONE.

//#int_CCP2
//CCP2_isr()               This is what I want to do in the second interrupt.      
//{                        But I doont know how to stop the first one  (CCP1)
//CCP_2 = CCP_2 + 110;         with the timer interrupt and then start this this one. 
//outs2 -= 5;                 THE PRIME OBJECTIVE IS TO TOGGLE BETWEEN BOTH INTERUPTS
//output_B(outs2--  & 0xFF);    (CCP1 AND CCP2) EVERYTIME THE TIMER ROLLS OVER.
//output_D(outs2--  >> 8);
//}
void main()
{
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
setup_ccp1(CCP_COMPARE_INT);
setup_ccp2(CCP_COMPARE_INT);
enable_interrupts(INT_CCP1);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_CCP2);
enable_interrupts(global);
while(1)
;
}

Thank you for your help

Carl
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Sun Mar 30, 2008 9:45 am     Reply with quote

Actually thinking about it (and trying it) that wont work because the timer interrupt willl interrupt every 1/2 second, but I want an interrupt every time the ccp1 value reaches 65535 and every time the ccp2 value reaches 1 (THESE TIMES WILL BE VARIABLE -IN THE ABOVE CODE IT IS 120us ALTHOUGH THE FINAL CODE WILL HAVE A VARIABLE). the timer interrupt therefore is not used - I think.

Any ideas

Carl
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Sun Mar 30, 2008 10:05 am     Reply with quote

I think I will need to use the special event trigger module which resets the timer1 in compare mode. But how can I use it to interrupt when CCP1 is 65535 and CCP2 is 1 - and toggling between the two. I h ave not seen any code examles for the special event trigger.

Carl
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Mon Mar 31, 2008 12:04 pm     Reply with quote

Nearly completed.

The code needed changing quite a bit. No CCP2 needed just did it all in CCP1. It now ramps up and then down continously - but only when the increment value is '1'. If the increment value is more than one then it ramps up for that amount of times and then ramps down for that amount of times. Just got to work that bit out and its easy sailing from here. Thankyou all for your help - I really like this site and the people on it - I will be mentioning you in my acknowledgements for my Beng degree project.

Code:
#include <18F4525.h>
#fuses NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP,NOPBADEN,XT
#use delay(clock=4000000)
long int outs = 0;         // 16 bit output value starts at 0     
int rampDir;             //A new varible that will determin the direction of the ramp
                           //1 = up, 0 = down
#int_CCP1       
CCP1_isr()
{
   CCP_1 = CCP_1 + 150;
     if (rampDir == 1){         //If rampDir = 1 then ramp up, or if = 0 then ramp down
         outs += 1;
         output_B(outs++ & 0xFF);
         output_D(outs++ >>8);}
else if (rampDir == 0){
         outs -= 1;
         output_B(outs-- & 0xFF);
         output_D(outs-- >>8);}
     if  (outs == 0){
         rampDir = 1;}     //must have ramped down to bottom value so change to ramp up
else if (outs == 65535){
         rampDir = 0; }    //must have ramped up to top value so change to ramp down                              
}
void main()
{
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(1)
  ;
}

Carl
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Fri Apr 04, 2008 2:10 pm     Reply with quote

Help!!

I am nearly there but am having trouble with interrupts. The compilable code below code1 works when it has been written on its own - no other functions are connected.

after I was happy with this and tested on bread board I then had to include it all as a function called rampup_init() (which has been called by another prgram, which has been called by another program, which has been called by main)that is part of the bigger program. the code2 below shows my best attempt at including it into the bigger program but a warning comes up saying "Interrupts disabled during call to prevent re-entrancy: (@MUL1616). this is not good.

I have read up on this and it previous replies state that the main and ISR should not be calling the same include files as this will cause trouble or have any delays in the interrupts. There is no delays in the ISR and is as simple as I can get it.

The fact remains that it works as code1 but not correctly in code2.

Ant suggestions please because I am stuck!!

code1
Code:
#include <18F4525.h>
#fuses NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP,NOPBADEN,XT
#use delay(clock=4000000)
void rampup_init(void);
void setup_rampy_array(void);
float time_init();
float b;
long int a,c;
long int outs;
long int rampDir =1;
long int rampdir2 =1;
long int rampy[23][3];               
//a=time_init();

void setup_rampy_array()
{
rampy[0][0] = 0;             
rampy[0][1] = 0;         
rampy[0][2] = 0;                       

rampy[1][0] = 200;             
rampy[1][1] = 25;         
rampy[1][2] = 65535;                         
     
rampy[2][0] = 255;
rampy[2][1] = 17;
rampy[2][2] = 65529;

rampy[3][0] = 207;
rampy[3][1] = 9;
rampy[3][2] = 65532;
 
rampy[4][0] = 217;
rampy[4][1] = 7;
rampy[4][2] = 65535;

rampy[5][0] = 228;
rampy[5][1] = 6;
rampy[5][2] = 65532;

rampy[6][0] = 230;
rampy[6][1] = 5;
rampy[6][2] = 65535;
 
rampy[7][0] = 212;
rampy[7][1] = 4;
rampy[7][2] = 65535;

rampy[8][0] = 244;
rampy[8][1] = 4;
rampy[8][2] = 65535;
 
rampy[9][0] = 207;
rampy[9][1] = 3;
rampy[9][2] = 65534;
 
rampy[10][0] = 228;
rampy[10][1] = 3;
rampy[10][2] = 65534;
 
rampy[11][0] = 252;
rampy[11][1] = 3;
rampy[11][2] = 65534;
 
rampy[12][0] = 276;
rampy[12][1] = 3;
rampy[12][2] = 65534;
 
rampy[13][0] = 198;
rampy[13][1] = 2;
rampy[13][2] = 65534;

rampy[14][0] = 214;
rampy[14][1] = 2;
rampy[14][2] = 65534;

rampy[15][0] = 228;
rampy[15][1] = 2;
rampy[15][2] = 65534;

rampy[16][0] = 244;
rampy[16][1] = 2;
rampy[16][2] = 65534;

rampy[17][0] = 260;
rampy[17][1] = 2;
rampy[17][2] = 65534;

rampy[18][0] = 274;
rampy[18][1] = 2;
rampy[18][2] = 65534;

rampy[19][0] = 290;
rampy[19][1] = 2;
rampy[19][2] = 65534;

rampy[20][0] = 306;
rampy[20][1] = 2;
rampy[20][2] = 65534;

rampy[21][0] = 320;
rampy[21][1] = 2;
rampy[21][2] = 65534;

rampy[22][0] = 338;
rampy[22][1] = 2;
rampy[22][2] = 65534;
}
#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(outs & 0xFF);
         output_D(outs >>8);}
 else if (rampDir == 0)     {                        //Ramp Down
         outs -= rampy[a][1];
         output_B(outs & 0xFF);
         output_D(outs >>8);}
     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(outs & 0xFF);
         output_D(outs >>8);}
else if (rampDir2 == 0){                           //Ramp Down
         outs -=1 ;
         output_B(outs & 0xFF);
         output_D(outs >>8); }
     if  (outs == 0){                                //Change to Ramp up next time
         rampDir2 = 1;}                                                       
else if (outs == 65535){                             //Change to Ramp down next time
         rampDir2 = 0; }                                                   
 }
}
void main()
{
a=23;
b=(float)1000000*a/131070;
c=(b+0.5);
setup_rampy_array();
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(1)
;
}


code2

Code:
#use delay(clock=4000000)
void rampup_init(void);
void setup_rampy_array(void);
float time_init();
float b;
long int a,c;
long int outs;
long int rampDir =1;
long int rampdir2 =1;
long int rampy[23][3];               


void setup_rampy_array()
{
rampy[0][0] = 0;             
rampy[0][1] = 0;         
rampy[0][2] = 0;                       

rampy[1][0] = 200;             
rampy[1][1] = 25;         
rampy[1][2] = 65535;                         
     
rampy[2][0] = 255;
rampy[2][1] = 17;
rampy[2][2] = 65529;

rampy[3][0] = 207;
rampy[3][1] = 9;
rampy[3][2] = 65532;
 
rampy[4][0] = 217;
rampy[4][1] = 7;
rampy[4][2] = 65535;

rampy[5][0] = 228;
rampy[5][1] = 6;
rampy[5][2] = 65532;

rampy[6][0] = 230;
rampy[6][1] = 5;
rampy[6][2] = 65535;
 
rampy[7][0] = 212;
rampy[7][1] = 4;
rampy[7][2] = 65535;

rampy[8][0] = 244;
rampy[8][1] = 4;
rampy[8][2] = 65535;
 
rampy[9][0] = 207;
rampy[9][1] = 3;
rampy[9][2] = 65534;
 
rampy[10][0] = 228;
rampy[10][1] = 3;
rampy[10][2] = 65534;
 
rampy[11][0] = 252;
rampy[11][1] = 3;
rampy[11][2] = 65534;
 
rampy[12][0] = 276;
rampy[12][1] = 3;
rampy[12][2] = 65534;
 
rampy[13][0] = 198;
rampy[13][1] = 2;
rampy[13][2] = 65534;

rampy[14][0] = 214;
rampy[14][1] = 2;
rampy[14][2] = 65534;

rampy[15][0] = 228;
rampy[15][1] = 2;
rampy[15][2] = 65534;

rampy[16][0] = 244;
rampy[16][1] = 2;
rampy[16][2] = 65534;

rampy[17][0] = 260;
rampy[17][1] = 2;
rampy[17][2] = 65534;

rampy[18][0] = 274;
rampy[18][1] = 2;
rampy[18][2] = 65534;

rampy[19][0] = 290;
rampy[19][1] = 2;
rampy[19][2] = 65534;

rampy[20][0] = 306;
rampy[20][1] = 2;
rampy[20][2] = 65534;

rampy[21][0] = 320;
rampy[21][1] = 2;
rampy[21][2] = 65534;

rampy[22][0] = 338;
rampy[22][1] = 2;
rampy[22][2] = 65534;
}
#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(outs & 0xFF);
         output_D(outs >>8);}
 else if (rampDir == 0)     {                        //Ramp Down
         outs -= rampy[a][1];
         output_B(outs & 0xFF);
         output_D(outs >>8);}
     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(outs & 0xFF);
         output_D(outs >>8);}
else if (rampDir2 == 0){                           //Ramp Down
         outs -=1 ;
         output_B(outs & 0xFF);
         output_D(outs >>8); }
     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()
{
a=6;  // time_init();
b=(float)1000000*a/131070;
c=(b+0.5);
setup_rampy_array();
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)
;
}



Carl
Ttelmah
Guest







PostPosted: Fri Apr 04, 2008 3:23 pm     Reply with quote

First, why not initialise the array when you declare it.
Second, look at the 'make_8' function.
Code:

#use delay(clock=4000000)
void rampup_init(void);
void setup_rampy_array(void);
float time_init();
float b;
long int a,c;
long int outs;
long int rampDir =1;
long int rampdir2 =1;
long int rampy[23][3] = {
{ 0,0,0 },              //row0
{200,25,65535},                       
{255,17,65529},
{207,9,65532},
{217,7,65535},
{228,6,65532},      //row5
{230,5,65535},
{212,4,65535},
{244,4,65535},
{207,3,65534},
{228,3,65534},    /row 10
{252,3,65534},
{276,3,65534},
{198,2,65534},
{214,2,65534},
{228,2,65534},   //row 15
{244,2,65534},
{260,2,65534},
{274,2,65534},
{290,2,65534},
{306,2,65534},  //row 20
{320,2,65534},
{338,2,65534}
}

#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() {
    a=6;  // 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) ;
}

The make8 function, can be matched by other methods, but basically does 8bit conversions as efficiently as possible, just selecting the individual byte requested.
If it still gives the error (which implies a 16bit multiplication is being performed in the INT handler somewhere), then split the array into three single dimensional ones. The only place I can think it is being used, is to multiply the row number, by the number of elements in an array line, to calculate the byte address needed in the array...

Best Wishes
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Sat Apr 05, 2008 8:07 am     Reply with quote

Thankyou Ttelmah for responding so quickly. I will give it a go today and tomorrow. Your idea looks really good and I will try your proposal if it still doesn't work.

The only suspect code that I can see in the ISR is the 'c':


Code:
CCP_1 = CCP_1 + c;


because 'c' is worked out using a float calculation(although it is not in the ISR it is in the ramp_init().:

Code:
a=6;  // time_init();
    b=(float)1000000*a/131070;
    c=(b+0.5);


I will try different things and let you know.

Thankyou again

Carl
Ttelmah
Guest







PostPosted: Sat Apr 05, 2008 8:44 am     Reply with quote

The error message, shows the 16*16bit multiply function (not the float multiply), being used both inside the interrupt, and outside. Now, nothing you directly show in the interrupt handler as posted, should use this function. The only thing I can think of, is the array indexing where the chip will have to calculate the row number, *6, plus the column number, to get the array location (since there are six bytes per row). Hence the comment about splitting the array, into three separate ones, one for each column (which gets rid of the need for the *6 multiply). It might even be worth as a really 'silly' experiment, increasing the number of columns to 4, since the compiler may well be smart enough to know that it can do this with a simple rotation, rather than a multiply!...

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3, 4  Next
Page 2 of 4

 
Jump to:  
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