Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

counter using pic uC in microc

Status
Not open for further replies.

mailus

Full Member level 4
Full Member level 4
Joined
Mar 6, 2012
Messages
234
Helped
7
Reputation
16
Reaction score
7
Trophy points
1,308
Location
-
Activity points
2,706
i want to design a counter that counts the no of pulses applied to the input pin.i write a program for this but it does not count the signal at all time due to display program.
how to operate these simultaneous operation at a same time in microcontroller.i show my program please help me....
my program written in microc for pic16f877a microcontroller at 1.000MHz clock for proteus simulation i use delay for 100ms
Code:
sbit read at rc4_bit;

unsigned int count;
short int ds1,ds2,ds3,ds4,i;

short int mask(short int get)
{
switch (get)
{
 case 0: return 0xc0;
 case 1: return 0xf9;
 case 2: return 0xa4;
 case 3: return 0xb0;
 case 4: return 0x99;
 case 5: return 0x92;
 case 6: return 0x82;
 case 7: return 0xf8;
 case 8: return 0x80;
 case 9: return 0x98;
 }
}

void display()
{
  ds1= (count%10);
  ds1= mask(ds1);
  ds2=(count/10)%10;
  ds2= mask(ds2);
  ds3=(count/100)%10;
  ds3= mask(ds3);
  ds4=(count/1000);
  ds4= mask(ds4);
  for(i=0;i<=5;i++)
  {
   portb=(ds4);
   portc.f0=1;
   portc.f1=0;
   portc.f2=0;
   portc.f3=0;
   delay_ms(100);
   portb=(ds3);
   portc.f0=0;
   portc.f1=1;
   portc.f2=0;
   portc.f3=0;
   delay_ms(100);
   portb=(ds2);
   portc.f0=0;
   portc.f1=0;
   portc.f2=1;
   portc.f3=0;
   delay_ms(100);
   portb=(ds1);
   portc.f0=0;
   portc.f1=0;
   portc.f2=0;
   portc.f3=1;
   delay_ms(100);
  }
}


void main()
{
 cmcon=0x07;
 adcon1=0x06;
 trisd=0x00;
 portd=0xff;
 trisb=0x00;
 trisc=0x10;
 portc=0xff;
 portb=0xff;
 count=0;
 do
 {
 if(button(&portc,4,.1,1))
 {
 count++;
 if(count>9999) count=0;
 }while(read=~read);
 display();

 }while(1);
}
 

use interrupts, you can count the pulses using RB0 int pin. increment your counter in the interrupt routine.
unsigned short counter; // Define variable cnt

void interrupt()
{
if(portB.F1 == 1)
counter++;
}
void main()
{
INTCON.GIE = 1; // this enables all interrupt
INTCON.INTE = 1; //this will enable RB0 interrupt

//put your display program here.

}

Read datasheet too
 
  • Like
Reactions: mailus

    mailus

    Points: 2
    Helpful Answer Positive Rating
for information,
how to read two or more inputs simultaneously and display it.
(or)
is it possible without using interrupt pin?
 
Last edited:

Forgot to tell that you have to clear the flags before you leave the routine. Otherwise it will not count anymore. I'll edit the code.

void interrupt()
{
INTCON.GIE = 0; //disable all interupts

if(INTCON.INTF)
{
counter++;
INTCON.INTF = 0; //clear flag
}

INTCON.GIE = 1; //enable all interrupts
}



If you want read more inputs, You can use RB port change interrupt. It will intterupt if RB4 to RB7 pins changed state. Then in interrupt routine you have to check which pin changed using if conditions.
enable register
INTCON.RBIE
flag
INTCON.RBIF

- - - Updated - - -

for information,
how to read two or more inputs simultaneously and display it.
(or)
is it possible without using interrupt pin?

No, every other time when the micro-controller is not polling your port pin, it will miss the pulse.
 

Ya,i used it. Now it's working as expected. Some times real time application need this facility.how to handle this situation.
 

Ya,i used it. Now it's working as expected. Some times real time application need this facility.how to handle this situation.
which facility ? and what situation?
 

sorry for the mistake,
how to read two or more inputs
simultaneously and display it.
(or)
is it possible without using
interrupt pin?
Some times real time
application need this facility.how
to handle this situation.
 

how to read two or more inputs
simultaneously and display it.

If you want read more inputs, You can use RB port change interrupt. It will intterupt if RB4 to RB7 pins changed state. Then in interrupt routine you have to check which pin changed using if conditions.
enable register
INTCON.RBIE
flag
INTCON.RBIF


Also another way, you can connect all inputs RB0 INT pin and again connect them separately to a port so that if any of them gets logic high, interrupt will occur and you can check which port pin/pins are logic high in the interrupt routine. Or as i said above use the RB port change interrupt.
 

ya,ok.thank you verymuch.

- - - Updated - - -

ya,ok.thank you verymuch.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top