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

Discussion in 'Embedded Systems and Microcontrollers' started by dmorey85, Jul 22, 2015.

  1. dmorey85

    Thread Starter New Member

    Jul 22, 2015
    3
    0
    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
     
  2. JohnInTX

    Moderator

    Jun 26, 2012
    2,340
    1,022
    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: Jul 22, 2015
    dmorey85 likes this.
  3. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,507
    2,367
    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.
     
  4. graham2550

    New Member

    Jun 24, 2014
    11
    1
    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 (Text):
    1. bit Flag;
    2.  
    3. char *freq = "00";
    4.  
    5. void Display_Freq(unsigned int freq2write) {
    6.    TMR1L = 0;                                   //reset Timer1, 8 bits only (low byte)
    7.    freq[0] = (freq2write/10)%10 + 48;               // Extract tens digit
    8.    freq[1] =  freq2write%10 + 48;               // Extract ones digit
    9.    Glcd_Write_Text(freq,74, 0, 1);                    // Display Frequency on LCD     ******************************************
    10. }
    11.  
    12. void Interrupt(){          //1 sec interrupt
    13.   if (TMR0IF_bit){
    14.     TMR0IF_bit = 0;
    15.     TMR0H         = 0x67;
    16.     TMR0L         = 0x69;
    17.     Flag = 1;
    18.   }
    19. }
    20.  
    21. void InitTimer0(){
    22.   T0CON         = 0x86;
    23.   TMR0H         = 0x67;
    24.   TMR0L         = 0x69;
    25.   GIE_bit       = 1;
    26.   TMR0IE_bit    = 1;
    27. }
    28.  
    29. void main() {
    30.  
    31.   CMCON = 7;                           // 0x07 Disable comparators
    32.  
    33.   T1CON = 0B00010111;         // TMR1  assigned to count on T1CK1 pin (RC0), rising edge, 8 bit, 1:2 prescaler for Freq
    34.   TMR1IE_bit = 0;             //disable the TM1 interupt
    35.  
    36.   InitTimer0();
    37.  
    38.   do {
    39.  
    40.     if(Flag){
    41.        Flag = 0;
    42.        Display_Freq(TMR1L);
    43.      
    44.      }
    45.    
    46.   } while(1);
    47. }
     
    dmorey85 likes this.
  5. dmorey85

    Thread Starter New Member

    Jul 22, 2015
    3
    0
    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
     
  6. JohnInTX

    Moderator

    Jun 26, 2012
    2,340
    1,022
    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.
     
  7. theonewho

    New Member

    Jul 9, 2015
    17
    2
  8. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    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: Aug 3, 2015
Loading...