PIC 18f2550 timer0 problem

Thread Starter

khatus

Joined Jul 2, 2018
68
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??
 

AlbertHall

Joined Jun 4, 2014
8,385
You need to look up what mikroc calls timer 1 and whether you can write all 16 bits at once or whether you need to split into two 8 bit writes.
I don't know mikroc so I can't comment on the correct solution.
 

Ian Rogers

Joined Dec 12, 2012
666
MikroC isn't an ANCI standard C compiler You'll need to get rid of all the includes and rename all registers that are not in "their" format.

TMR1 as Albert said is a 16 bit write... MikroC will have their own way of doing this.

NOTE** Your title mentions timer0??
 
Top