C Rev counter- Need help with the Timer and Counter PIC18F252

Thread Starter

dmorey85

Joined Jul 22, 2015
3
I am currently trying to programme a Rev Counter running between 15Hz and 200Hz using a PIC18F252. I am unsure how to programme the timer port and the counter port so that it is measuring the input square wave from the rotating part over a second and counting the revolutions. I know there needs to be an interrupt to reset them both after 1 sec. The display will be on a 4 digit seven segment display.

I have no experience in and form of computer programming at all and I would be happy if anyone can give me advice on the calculations and the coding itself.

If there is any information you may need to help just ask and i will try my best.

Thanks

Dave
 

JohnInTX

Joined Jun 26, 2012
4,787
Well... If you haven't any programming experience, may I suggest an Arduino? I believe you would find it way easier to implement than starting with an out-of-date PIC, even in C.

If you do have some PIC code written, post it using CODE tags (mouseover the toolbar and find 'Insert') and a schematic and we'll take a look.
I don't want to discourage you but bringing up a PIC from scratch with interrupts and a display multiplexer would be a formidable first project, even with some programming experience. Most start with just flashing an LED.

BTW, the link just came up in a search. I don't know if the project is real or not but would give you and idea of how your project would come together. Arduino does have lots of canned routines that make things easier for a beginner. You are looking for some sort of frequency counter scaled to provide RPM or whatever and format the result for a display.

All of that said, we are here to help and welcome to AAC! Quote this, click 'Like' or tag @JohnInTX so we'll know if you reply.
 
Last edited:

MaxHeadRoom

Joined Jul 18, 2013
28,688
You might need 5 digits if displaying rpm. 200Hz is 12000rpm.
A 1 or 2 lineLCD display might be a better choice.
The PIC has capture/Timer1 (CCP) modules for this.
Max.
 

graham2550

Joined Jun 24, 2014
14
I am currently trying to programme a Rev Counter running between 15Hz and 200Hz using a PIC18F252. I am unsure how to programme the timer port and the counter port so that it is measuring the input square wave from the rotating part over a second and counting the revolutions. I know there needs to be an interrupt to reset them both after 1 sec. The display will be on a 4 digit seven segment display.

I have no experience in and form of computer programming at all and I would be happy if anyone can give me advice on the calculations and the coding itself.

If there is any information you may need to help just ask and i will try my best.

Thanks


Dave
This is some thing I wrote a long time ago for a power monitoring system.
This was the measurement of the mains frequency part, but the principle is the same, you are just counting pulses/second.
I pulled out the relevant code to show you setting up the timer and interrupt.
You will have to play around with the Timer numbers.
This code is probably not so good as I wrote it a long time ago, no criticism please.

Code:
bit Flag;

char *freq = "00";

void Display_Freq(unsigned int freq2write) {
   TMR1L = 0;                                   //reset Timer1, 8 bits only (low byte)
   freq[0] = (freq2write/10)%10 + 48;               // Extract tens digit
   freq[1] =  freq2write%10 + 48;               // Extract ones digit
   Glcd_Write_Text(freq,74, 0, 1);                    // Display Frequency on LCD     ******************************************
}

void Interrupt(){          //1 sec interrupt
  if (TMR0IF_bit){
    TMR0IF_bit = 0;
    TMR0H         = 0x67;
    TMR0L         = 0x69;
    Flag = 1;
  }
}

void InitTimer0(){
  T0CON         = 0x86;
  TMR0H         = 0x67;
  TMR0L         = 0x69;
  GIE_bit       = 1;
  TMR0IE_bit    = 1;
}

void main() {

  CMCON = 7;                           // 0x07 Disable comparators

  T1CON = 0B00010111;         // TMR1  assigned to count on T1CK1 pin (RC0), rising edge, 8 bit, 1:2 prescaler for Freq
  TMR1IE_bit = 0;             //disable the TM1 interupt
 
  InitTimer0();
 
  do {

    if(Flag){
       Flag = 0;
       Display_Freq(TMR1L);
      
     }
   
  } while(1);
}
 

Thread Starter

dmorey85

Joined Jul 22, 2015
3
Thanks so much for all the replies will read through them in depth to make sure extract what I need.

Unfortunately @JohnInTX it is a project that has to be written in C. I have added the outline of it below and also my schematic of the diagram to help with what I'm doing (I know its not the best)


Task


For this task, it is required to design a Microcontroller board interfaced to a 4-digit seven Segment LED display, which would be displaying number of revolutions per minute of a car Engine. Here a Pulse/Function generator is used to simulate the pulses coming from the Engine.

[ Note : range of 900 rev(15 Hz) – 12000 rev(200 Hz) ]


The components to be used

· Microchip PIC18F252 microcontroller

· 74HC4511 decoder/driver

· 4 digit 7 segment common cathode display

· Transistors/Drivers


Software and the language

· The environment is MPLAB

· The language to be used is ‘C’


Assessment:

· A complete circuit diagram showing all the interconnections between components

· Proper Use of External Interrupt in the working System

· Proper Use of Timer in the working System

· Proper Use of Timer Interrupt in the working System
 

Attachments

JohnInTX

Joined Jun 26, 2012
4,787
OK. There are some things to do to start: Get a project going in MPLAB and get the PIC up and running with some initial code.

PIC:
Is this on some sort of development board? If so it should be set up with power, oscillator etc. If not:
The 252 needs an oscillator to run. Use a crystal oscillator, ceramic resonator or external oscillator in a can. Pick a frequency - 10MHz might be good to start although faster is better.
Connect all of the power pins with 1uF tantalum || .1 ceramic between Vdd and GND. Don't forget this chip has two GND pins.
Pull up MCLR/ with a 3.3K - 10K resistor.
Presumably you have PICkit or some debugger. If so set up a header to pick up Vpp/MCLR, Vdd, GND, and the two ISCP pins PGD and PGC. MOVE the display decoder wires to another port to free up PGD and PGC. These can also be used to program the PIC on your bread board using PICkit.

MPLAB:
Use the project wizard to set up a project. Are you using MPLAB or MPLABX? Which C compiler?
Get it to compile a dummy main() with no errors and no warnings.
Visit the compiler help to see how to set the PIC's configuration bits (section 19.1). Different compilers use different syntax but setting the config is mandatory. Set the oscillator type and turn off the watchdog for now.

CODE:
Once you get a clean dummy main(), add code to init the IO. Set each TRIS register to the direction you need and each PORT register to initial values. Put a 0000 on the display data lines and a '1' on one of the transistor lines. If the PIC powers up, runs and inits the port, you should see an initial '0' on the display. Debug from there. Don't worry about timers / interrupts for now.

Post your updated circuit and code using CODE tags (see Insert on the toolbar) and let us know what you have.

Good luck.
 

takao21203

Joined Apr 28, 2012
3,702
all unneccessary work.

Ive done 4-digit LED with a plain 16F54

With larger 40pin PICs, you can simply connect the display 1:1, for instance directly on TQFP Adapter you need less wires, only about 1/2.

Then sort out in software.

With MPLABX you get code templates including interrupt. Setting up the PIC configuration is also a matter of copy&paste inside MPLABX.

All you need to do is set up a timer, and the usual interrupt enabling/AD disabling.

I have a multiplex/display drive source code on my blog, though not for 7seg, the IC doesnt really care, as a 7seg is nothing but also kindof a LED matrix.

You DO NOT need ANY extra components such as resistors, driver ICs or transistors, and you dont need any layout either. Its a complete waste of time.

http://aranna.altervista.org/dragon...ot-matrix-scrolling-message-14x5-source-code/

Could easily be changed for 7seg.
For 18F you need to use LATCHED IO.
Setting up the Peripherals is a little different.

https://en.wikipedia.org/wiki/Kaizen
https://en.wikipedia.org/wiki/Kawasaki,_Kanagawa
 
Last edited:
Top