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

How can I count 5 pulses and then active an output at 16f877

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



Joined: 29 Apr 2010
Posts: 11

View user's profile Send private message

How can I count 5 pulses and then active an output at 16f877
PostPosted: Mon May 17, 2010 7:29 am     Reply with quote

Hello,
How can I count 5 pulses ( T=1 sec, Duty cycle = 1/2 f= 1Hz) at input B0, and then make active B5 ?
And I'm use PIC 16F877A and PIC C program.
Thank you.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 17, 2010 11:26 am     Reply with quote

You can use the INT_EXT feature of Pin B0 to detect the edge of the
pulse. You can do it with, or without an #int_ext interrupt routine.
Just increment a count variable every time you detect an edge.
When the count = 5, the set pin B5 to a high level.

To do it with interrupts, create an #int_ext function and increment a
static int8 variable inside the function (which is initialized to 0).
Increment it every time an interrupt occurs. When the count = 5,
then set Pin B5 high.

To do it without interrupts, you can wait in a while() loop. Use the
interrupt_active() function to tell if the edge of the pulse has been
detected. Then increment a counter and call the clear_interrupt()
function (to clear the INT_EXT interrupt). When the count = 5, then
set pin B5 high.
rami_shama



Joined: 29 Apr 2010
Posts: 11

View user's profile Send private message

PostPosted: Mon May 17, 2010 12:00 pm     Reply with quote

This is my code
so what can i do??
and i need it to count the Pulses when the program at:
Code:
while (true)
{ "here"
.
.
.
}


Code:
#include "F:\cc\main.h"
int check_change();
void menu();
void end();
void cursor();
int which_MSG();
int sen1,sen2,sen3=0,i;
int x;
void standby();
int s1,s2,s3;
int which;


void main()
{


   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_CLOCK_DIV_2);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

output_low(PIN_B0);
output_low(PIN_B1);
output_low(PIN_B2);

set_tris_a(0x11);
set_tris_b(0xE8);
output_high(PIN_B5);




if(input(PIN_A0))
 sen1=1;

if(input(PIN_A1))
 sen2=1;

if(input(PIN_A2))
 sen3=1;


x=0;
while (true)
{
  if(input(PIN_A0))
      s1=1;
   else
   s1=0;
   if(input(PIN_A1))
       s2=1;
   else
   s2=0;
   if(input(PIN_A2))
      s3=1;
   else
   s3=0;
   
   standby();

//if (check_change()==1 && input(PIN_B3)==0)
  if(!(sen1==s1 && sen2==s2 && sen3==s3) && input(PIN_B3)==0)
   { 
 
 
end();
end();
menu();
menu();
menu();
cursor();
cursor();
cursor();
cursor();
menu();
which=which_MSG();
if(s1==1 && s2==1 && s3==1)
{
for(i=0; i<7; i++)
 cursor();
}
else
for(i=0; i<which; i++)
 cursor();

 
menu();
menu();
cursor();
menu();
menu();
cursor();
menu();
menu();
end();
delay_ms(1000);
 if(input(PIN_A0))
      sen1=1;
   else
   sen1=0;
   if(input(PIN_A1))
       sen2=1;
   else
   sen2=0;
   if(input(PIN_A2))
      sen3=1;
   else
   sen3=0;
   }

}
}
/*int check_change()
 {
 int s1,s2,s3;
 if(input(PIN_A0))
      s1=1;
   else
   s1=0;
   if(input(PIN_A1))
       s2=1;
   else
   s2=0;
   if(input(PIN_A2))
      s3=1;
   else
   s3=0;
 
 
  if(sen1!=s1 || sen2!=s2 || sen3!=s3)
      return 1;

  else
      return 0;
 }
 */

void menu()
 {
  output_high(PIN_B0);
  delay_ms(300);
  output_low(PIN_B0);
  delay_ms(300);
 }
void end()
 {
  output_high(PIN_B1);
  delay_ms(300);
  output_low(PIN_B1);
  delay_ms(300);
 }
void cursor()
 {
  output_high(PIN_B2);
  delay_ms(300);
  output_low(PIN_B2);
  delay_ms(300);
 }

int which_MSG()
 {
 delay_ms(3000);
   if(input(PIN_A0))
      s1=1;
   else
   s1=0;
   if(input(PIN_A1))
       s2=1;
   else
   s2=0;
   if(input(PIN_A2))
      s3=1;
   else
   s3=0;
 delay_ms(300);
  if(s1==0 && s2==0 && s3==0)
     return 0;
  else if(s1==0 && s2==0 && s3==1)
     return 1;
   else if(s1==0 && s2==1 && s3==0)
     return 2;
  else if(s1==0 && s2==1 && s3==1)
     return 3;
  else if(s1==1 && s2==0 && s3==0)
     return 4;
  else if(s1==1 && s2==0 && s3==1)
     return 5;
  else if(s1==1 && s2==1 && s3==0)
     return 6;
  else if(s1==1 && s2==1 && s3==1)
     return 7;

 }
 
 void standby()
 {
 output_low(PIN_B5);
delay_ms(90);
output_high(PIN_B5);
delay_ms(90);


 }


Please help me if you can!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 17, 2010 12:12 pm     Reply with quote

What is the purpose of your project ?

What signal are you trying to count ? Why ?
rami_shama



Joined: 29 Apr 2010
Posts: 11

View user's profile Send private message

PostPosted: Mon May 17, 2010 10:29 pm     Reply with quote

My project is to send status message when any input changed.
The Pulses is output of vibration of the mobile, so i wanna let the pic to open the call "Press send".
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 18, 2010 1:25 pm     Reply with quote

You have too much code, and I have no idea what it's doing.

My advice is to make a test program which has the purpose of detecting
the "ring" vibration on your cell phone. The only thing this program will
do, is to test routines for vibration detection.

Then, when you a reliable routine for "vibration detection", you can
combine it with your main program.
rami_shama



Joined: 29 Apr 2010
Posts: 11

View user's profile Send private message

PostPosted: Tue May 18, 2010 9:46 pm     Reply with quote

I thought it is too hard to do it, as I'm still beginner in CCS programing.
So I will take the Signal from vibration and enter it to a Opto Coupler.
and the opto will active SEND bottom..

Thanks all
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