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

pic16f876a pin c3 coding bug when with real time interrupt

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
codeboy2016



Joined: 15 Dec 2016
Posts: 2

View user's profile Send private message

pic16f876a pin c3 coding bug when with real time interrupt
PostPosted: Thu Dec 15, 2016 2:55 am     Reply with quote

Hi all, i wrote a program and use pcwh to compile and download program into pic16f876a. I found strange output on pin_C3. Maybe it is compiler bug.

Here is what i wrote:
Code:

#include <16f876A.h> //use pic16f876A
#device adc=10  //use 10bit adc
#use delay(clock=20000000) //20mhz
#fuses hs,protect,nowdt //fuse setting
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=N)

long int motor1_cnt=10;
long int timer_cnt=0;

#int_rtcc
clock_isr()

   timer_cnt++;
   if(timer_cnt>500) 
   { timer_cnt=0;
       // disable this line:   output_high(pin_c5); //servo1 
      }
   if(timer_cnt==motor1_cnt)
   { 
     //disable this line: output_low(pin_c5);   
   }
}

void main()
{  //set i/o for each pin
   set_tris_a(0b00111111);
   set_tris_b(0b00000111);
   set_tris_c(0b10000000);
   setup_port_a(ALL_ANALOG);
   setup_adc(ADC_CLOCK_INTERNAL);
   
   set_rtcc(0);      //turn on real time clock
   setup_counters(RTCC_INTERNAL, RTCC_DIV_1);     
   enable_interrupts(int_rtcc);
   enable_interrupts(GLOBAL);

   output_high(pin_b4); //off led1
   output_high(pin_c3); //off led2
   delay_ms(3000);
   output_low(pin_b4); //off led1
   output_low(pin_c3); //off led2
   
}

In example above, I use pic16f876a and a starter kit to test it.

When i disable 'output_high(pin_C5' and 'output_low(pin_C5)' I found both pin B4 and C3 turn on for 3 sec then turn off. works perfectly.

Now if i enable 'output_high(pin_c5);' or 'output_low(pin_c5);' and i found only pin b4 is turn on 3 sec then turn off.
And i found c3 will turn on and in less than 1 sec it will turn off.

So i am wondering what is happening.

Because the command in the interrupt is nothing deal with pin_c3. and why pin C3 is affected and pin B4 not affected?

** i have checked the board wiring it is good. Because when i test program without interrupt. the c3 is functioning perfectly.

This is really a strange case. Please guide on how to solve it.
Ttelmah



Joined: 11 Mar 2010
Posts: 19506

View user's profile Send private message

PostPosted: Thu Dec 15, 2016 3:38 am     Reply with quote

1) When asking questions like this always tell us your compiler version number. There are now something over 300 CCS versions. We have no hope of knowing if you may have a faulty version if you don't tell us what one you are using....
However this is not actually likely to be the problem unless you have one of the 'beta' versions that some peopel seem to have.

2) ADC_CLOCK_INTERNAL is _not_ recommended above 1MHz CPU clock rate. If you intend to use the ADC in the future, worth reading the data sheet and correcting.

3) If using the hardware UART, you _must_ always have 'ERRORS' in your RS232 setup, unless you are adding the code to handle errors yourself.

4) When developing code, don't use 'PROTECT'. Only enable this when your code is finished. Having it enabled, increases the wear and tear on the chip's flash memory, meaning that the programmer has to erase every cell in the chip, each time even one byte is changed.

5) Bit problem is what happens at the end. Your code is dropping off the end, which means the chip will go to sleep. Now the exact timing in the code sequence of this I'd have to sit down and calculate, but my guess is that the extra time involved in the interrupt code, is letting it just drop off the end at the wrong time. CCS code is 'standalone'. There is no OS for the code to go 'back' to. Your code should always ensure that it doesn't do this.
Mike Walne



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

View user's profile Send private message

PostPosted: Thu Dec 15, 2016 3:47 am     Reply with quote

Learn to use the code button, preserves your formatting, makes code easier to read.

What happens if you skip (comment out) these lines?
Quote:
set_tris_a(0b00111111);
set_tris_b(0b00000111);
set_tris_c(0b10000000);
In CCS 'C' they should not be necessary.

Have you got a motor actually connected and well decoupled?

Mike

Edit Mr T. got in whilst I was typing, always follow his advice.
codeboy2016



Joined: 15 Dec 2016
Posts: 2

View user's profile Send private message

new coding
PostPosted: Thu Dec 15, 2016 4:25 am     Reply with quote

I am using version 4.023

Sorry for confuse you.

Basically the question of the project has nothing deal with uart or analog input. But problem is on the real time clock interrupt.

Please read on the line 14 and line 18.
You will see 'output_high(pin_C5);' and also'output_low(pin_c5);'.
when these 2 lines are DISABLED, the led at pin_c3 and pin_b4 works perfectly.

But when either of the lines enabled, then only b4 works perfectly (follow 3 sec on 3 sec off). But pin c3 is only turned on less than 0.5sec then auto turn off.

I only connect to a led and through resistor 1kr. So nothing with reset or unstable. Because b4 works perfectly, and also c3 works perfectly when line 14 and 18 disabled.

Here is the new coding, please read again.
Code:

#include <16f876A.h> //use pic16f876A
#device adc=10 //use 10bit adc
#use delay(clock=20000000) //20mhz
#fuses hs,protect,nowdt //fuse setting
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=N)

long int motor1_cnt=10;
long int timer_cnt=0;

#int_rtcc
clock_isr()
{
timer_cnt++;

if(timer_cnt>500)
  {
   timer_cnt=0;
   // disable this line: output_high(pin_c5); 
  }

if(timer_cnt==motor1_cnt)
  {
   //disable this line: output_low(pin_c5);
  }
}

void main()
{ //set i/o for each pin
set_tris_b(0b00000000);
set_tris_c(0b00000000);

set_rtcc(0); //turn on real time clock
setup_counters(RTCC_INTERNAL, RTCC_DIV_1);
enable_interrupts(int_rtcc);
enable_interrupts(GLOBAL);

do
  {
   output_high(pin_b4); //off led1
   output_high(pin_c3); //off led2
   delay_ms(3000);
   output_low(pin_b4); //off led1
   output_low(pin_c3); //off led2
   delay_ms(3000);
  }while(1);

}
Ttelmah



Joined: 11 Mar 2010
Posts: 19506

View user's profile Send private message

PostPosted: Thu Dec 15, 2016 6:26 am     Reply with quote

Ouch....
Do you really mean 4.023.

That was at best an 'early beta' version.
I don't actually have a V4 version this low. I only keep versions once they are proven to be nearly working. At this time I was still using 3.249, since V4 wasn't yet running. The earliest version I kept was 4.027, which still had major problems, then versions starting a little later....

Big problem is you have LVP enabled. This will cause problems unless the LVP pins are biased off.

A test with 4.027 (with LVP off), shows it running fine, except for the LED being about 20% slow (since it is spending so much time in the interrupt - remember you are calling this every 256 instructions and it uses about 70 instruction)....
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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