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 CCS Technical Support

Timer1 in RC0 and RC1
Goto page Previous  1, 2, 3, 4, 5, 6
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jan 28, 2015 10:35 am     Reply with quote

This post is on a new page. Make sure you read Ttelmah's post just before this one, on page 5 of the thread.
Quote:

I did not include the whole code here for the purpose of saving space, but the interrupt routine is included:

void main()
{
enable_interrupts(global);
enable_interrupts(int_rb);

ext_int_edge( H_TO_L );
}

This code above is not an interrupt routine. This enables PortB "interrupt-
on-change" interrupts on pins B4 to B7. If you have these lines without
an interrupt routine, your program will crash. Examples of how to declare
an interrupt routine are shown further below.

This line below has nothing to do with PortB interrupt-on-change interrupts:
Quote:
ext_int_edge( H_TO_L );

It's associated with the External Interrupt (INT_EXT) which is on Pin B0.
External interrupts are enabled with:
Code:
enable_interrupts(INT_EXT);

The interrupt routine for this interrupt is prefixed with #int_ext. Example:
Code:

#int_ext
void int_ext_isr(void)
{
// user code goes here.
}

If you were using interrupt-on-change interrupts (on pins B4 to B7)
the routine would be like this:
Code:

#int_rb
void int_rb_isr(void)
{
// user code goes here.
}

This is all in the CCS manual.

A warning. Just sticking in the above code, will NOT make your program
work. Do not do it, unless you completely understand what you are doing.

You still haven't told us where 'signal' comes from or shown the line of
code where it's declared. I don't know if 'signal' is a variable or if it's
something else (reading an i/o pin).
picman62



Joined: 12 Dec 2014
Posts: 77

View user's profile Send private message

PostPosted: Thu Jan 29, 2015 3:36 am     Reply with quote

Thanks for your inputs.

Ttelmah wrote:
There are all sorts of potential problems, that we can't be sure about, because we don't know the types of the numbers. For instance:
Code:

((value>=counter-6 && value<counter-2)


Will give completely unexpected results, if 'counter' is an int, and is less than 5.

For an int8, 'counter-6', will solve for counter==2, to 252.... Not what you expect or want at all.
For an int16, for the same values, it'll solve to 65532.

Problem is that maths 'wraps', if an unsigned int, goes -ve, it wraps to '(max+1)- -ve result'.


Counter is an INT16 variable that relates to a specific frequency that will be 'read' at pin_C0. Three 'cont' sets or 'pages' are displayed in the LCD each time pinB4 is pressed.

So first set displays counter=1000. Counter=9000 in the second set and counter=15000 in the third one.

This is the way it was written:
Code:

 if ( (SW1 && !ISPRESSED_KEY1) ) // redefine valores padrĂ£o: 1000,2500,15000

   {
   
    counter=1000;
     
   if(cont >= 1) counter=9000;
   if(cont >= 2) counter=15000;
   if(cont >= 4) counter=1000;
 
   if(++cont >= 5) cont = 1;
   
 
   ISPRESSED_KEY1=TRUE ;


Actually there are 4 sets. The last one is the battery indicator display. When this last display is shown, one more press of B4 goes back to first page.

For some reason if I simply set:
If(cont==1) counter=1000
if(cont==2) counter=9000
if(cont==3) counter=15000
etc.

This gives wrong results in CCS. So the first code above is what I came up with and what works without mistakes.
I had to set the value for initial counter as 1000, because CCS understands cont==1 as the second press of B4. I would have to write cont==0 for this case and it would mess all the other conts.

Quote:

Then the problem PCM_programmer has already pointed out, applies 'in spades':
Code:

if(value>=counter-3 && value<=counter+2) //This statement if true
               
         output_bit(pin_c1(1));  //executes this statement _only_
         delay_ms(10);
         output_bit(pin_c1(0));
         delay_ms(10);


Try to use some logic in your indentation to actually 'show' how things are executed/intended. Currently your indentation jumps around like a Mexican bean.


In order to write the code to save space in memory, instead of writing the same instructions 3 times for each different conts, I did the way that you see in my last post.

This is what I meant with it:
In any of the three sets of frequencies for each display page, if the 'value' read is three 'units' less than counter, then I wrote 'counter-3', if it is three units more, 'counter+3'.
You are saying that this ends up problematic. Please suggest an instruction that I can write and which covers the intention above.
Thanks.
picman62



Joined: 12 Dec 2014
Posts: 77

View user's profile Send private message

PostPosted: Thu Jan 29, 2015 4:08 am     Reply with quote

PCM programmer wrote:
This post is on a new page. Make sure you read Ttelmah's post just before this one, on page 5 of the thread.
Quote:

I did not include the whole code here for the purpose of saving space, but the interrupt routine is included:

void main()
{
enable_interrupts(global);
enable_interrupts(int_rb);

ext_int_edge( H_TO_L );
}

This code above is not an interrupt routine. This enables PortB "interrupt-
on-change" interrupts on pins B4 to B7. If you have these lines without
an interrupt routine, your program will crash. Examples of how to declare
an interrupt routine are shown further below.

This line below has nothing to do with PortB interrupt-on-change interrupts:
Quote:
ext_int_edge( H_TO_L );

It's associated with the External Interrupt (INT_EXT) which is on Pin B0.
External interrupts are enabled with:
Code:
enable_interrupts(INT_EXT);

The interrupt routine for this interrupt is prefixed with #int_ext. Example:
Code:

#int_ext
void int_ext_isr(void)
{
// user code goes here.
}

If you were using interrupt-on-change interrupts (on pins B4 to B7)
the routine would be like this:
Code:

#int_rb
void int_rb_isr(void)
{
// user code goes here.
}

This is all in the CCS manual.

A warning. Just sticking in the above code, will NOT make your program
work. Do not do it, unless you completely understand what you are doing.

You still haven't told us where 'signal' comes from or shown the line of
code where it's declared. I don't know if 'signal' is a variable or if it's
something else (reading an i/o pin).


Hi, PCM.
This is the interrupt routine that was written:
Code:

void button_isr()
{
   
   if( !input(PIN_B7) && !signal )
      signal = 1;
   else
   
   
   if( !input(PIN_B7) && signal )
      signal = 0;   
}

It covers only pinB7 which is the pin pressed for display change and to enable PWM.
Along with the interrupt enables shown, is the routine above correct for my purposes?

According to what you say, "ext_int_edge( H_TO_L )" relates only to B0. So as I do not use B0 at all, I don't need this instruction. Is that it?

Regarding the signal. The PWM signal output from the PIC will be sent to a receiver that once a specific frequency is 'sensed' transmits back that frequecy which is 'read' by the PIC and lights the led accordingly.
picman62



Joined: 12 Dec 2014
Posts: 77

View user's profile Send private message

PostPosted: Thu Jan 29, 2015 4:35 am     Reply with quote

Ttelmah,

In order to check the problem of the LED being blocked from blinking, I made a reduction in the specific parts related to the counter statements, excluding them.
This is what resulted.

Code:

if ( (SW4 && !ISPRESSED_KEY4) )
   
  {
            ISPRESSED_KEY4=TRUE;
         
   do // interrupts to loop state of PWM and out of it
     
  {
   
    if(signal) // if PWM is sent through C2
 {
         set_timer1(0);
         setup_timer_1(t1_external | T1_DIV_BY_1);
         delay_ms(1000);
         setup_timer_1(T1_DISABLED);
         value=get_timer1();       
  }
 {
         
          output_bit(pin_c1(1));
          delay_ms(10);
          output_bit(pin_c1(0));
          delay_ms(10);
  }
 
   {     
         setup_timer_2(T2_DIV_BY_4,249,1);
         set_pwm1_duty(748);
         setup_ccp1(CCP_PWM); //enable PWM
         delay_ms(100);
         setup_ccp1(CCP_OFF);//disable PWM
         delay_ms(1000);
         
   }
if(!signal) // if PWM is not sent through C2 pin

   {         
           
            printf(lcd_putc,"\f");
            lcd_gotoxy(1,1);
            if(cont==1)
            lcd_putc("text1");
            if(cont==2)
            lcd_putc("text2");
            if(cont==3)
            lcd_putc("text3");
            lcd_gotoxy(7,2);
            printf(lcd_putc," %LU ",counter);
            break;         
    }
         
    }
   
   while(input(pin_b4)==1); //if SW4 is pressed while in SW1   
   
   }

Still, the problem remains. LED does not light or blink. Only the PWM is started and turned off. So I believe it's not related to the counter statements. It seems it's related only to the PWM and LED routines. Maybe the PWM interrupt on B7?
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Thu Jan 29, 2015 8:58 am     Reply with quote

picman62 wrote:
Ttelmah,

In order to check the problem of the LED being blocked from blinking, I made a reduction in the specific parts related to the counter statements, excluding them.
This is what resulted.

Code:
.......
.......
.......

Still, the problem remains. LED does not light or blink. Only the PWM is started and turned off. So I believe it's not related to the counter statements. It seems it's related only to the PWM and LED routines. Maybe the PWM interrupt on B7?

I'm surprised this thread has now reached 6 pages.
It's about time you looked at the forum guidelines.
In particular post SHORT COMPLETE COMPILABLE code which illustrates your issue.
Then we can copy and paste to test.
Makes life easier for all of us.

Mike
picman62



Joined: 12 Dec 2014
Posts: 77

View user's profile Send private message

PostPosted: Thu Jan 29, 2015 8:13 pm     Reply with quote

I think I might have found something that I believe it's not included in the guidelines. It relates to Proteus.
I changed the delays of the LEDs making them longer and the PWM pulses shorter. The LEds finally started to blink and the PWM was not blocked.
I also have noticed that certain delays of on and off goes exactly 'along' with the PWM on and off also. Initially I thought it was a problem regarding the timers, wrong configuration of the pins output or a bug in CCS. But it might be a problem of how Proteus deals with PWM pulses and led delays happening at the same time.
I am away from the hardware, so I cannot say for sure if the code is working in a real test, but I will soon.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Fri Jan 30, 2015 4:23 am     Reply with quote

picman62 wrote:
I think I might have found something that I believe it's not included in the guidelines. It relates to Proteus..................

Read again Item 6 on this link.

http://www.ccsinfo.com/forum/viewtopic.php?t=26245&start=0&postdays=0&postorder=asc&highlight=guidelines

Quote:
This forum's sole purpose is to help people with programming Microchip's in CCS PIC C........

In other words Proteus/ISIS is "Off topic".

You're making wild guesses. You've already been told you're approach is back to front. And you need to learn to debug.

Mike
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, 5, 6
Page 6 of 6

 
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