Author Topic: PC 18f2550 timer0 problem  (Read 549 times)

0 Members and 1 Guest are viewing this topic.

Offline khatusTopic starter

  • Regular Contributor
  • *
  • Posts: 146
  • Country: gl
PC 18f2550 timer0 problem
« on: June 23, 2019, 05:48:09 am »
https://www.electronicwings.com/pic/pic18f4550-timer-capture

/*
   Frequency Measurement using Input Capture Mode in PIC18F4550
   http://www.electronicwings.com
*/
#include <stdio.h>
#include <stdlib.h>
#include <p18f4550.h>
#include "osc_config.h"
#include "LCD_8bit_file.h"
#include <string.h>

#define f_timer 2000000

void main()
{
    unsigned long signal_period,data1,data2;
    unsigned long frequency_Hz[20];
    float Frequency;
    TRISCbits.TRISC2=1;
    OSCCON=0x72;        /* set internal clock to 8MHz */
    LCD_Init();
    memset(frequency_Hz,0,20);
    LCD_String_xy(0,1,"Pulse");
 
    PIE1bits.CCP1IE=1;
    PIR1bits.CCP1IF=0;     
    CCP1CON=0x05;       /* Capture mode is selected for detecting Rising edge */
    CCPR1=0x00;         /*CCPR1 is capture count Register which is cleared initially*/
    TMR1IF=0;           
    T1CON=0x80;         /* Enable 16-bit TMR1 Register,No pre-scale,use internal clock,Timer OFF */
    TMR1=0;
   TMR1ON=1; /* Turn-On Timer1 */   
    while(1)       
    {   
        while(!(PIR1bits.CCP1IF));  /*Wait for Interrupt flag which is generated when edge is detected*/
        PIR1bits.CCP1IF=0;
        data1 = CCPR1;              /*Copy count of 1st edge detected*/       
           
        while(!(PIR1bits.CCP1IF));  /*Wait for Interrupt flag which is generated when edge is detected*/
        PIR1bits.CCP1IF=0;
        data2 = CCPR1;              /*Copy count of 2nd edge detected*/

        if(data1 < data2)
           {

            /*Calculation for Frequency Measurement*/
            signal_period = data2 - data1;
            Frequency = ((float)f_timer / (float)signal_period); /*Count for 1 cycle*0.5us gives period */
            sprintf(frequency_Hz,"%.3f  ",Frequency);

            LCD_String_xy(2,0,frequency_Hz);
           
            }
        TMR1=0;
        memset(frequency_Hz,0,20);
    }                             
}

this is the code which measure Frequency of the input signal and display it on 16x2 LCD. I want to convert this code from mplab to mikroc .But i mikroc when i declare TMR1=0; it shows error.How i can overcome this problem??
« Last Edit: June 23, 2019, 05:52:18 am by khatus »
 

Offline ggchab

  • Frequent Contributor
  • **
  • Posts: 276
  • Country: be
Re: PC 18f2550 timer0 problem
« Reply #1 on: June 23, 2019, 08:13:57 am »
And check the doc: TMRxH might have to be initialized before TMRxL
The high value is stored in a temporary register and really loaded in TMRxH when TMRxL is written.
 

Online MarkF

  • Super Contributor
  • ***
  • Posts: 2542
  • Country: us
Re: PC 18f2550 timer0 problem
« Reply #2 on: June 23, 2019, 02:11:24 pm »
And please use the code tag "#" button while editing in the future.
It places the code in a small frame using a mono spaced font.   :-+
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf