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.

how to convert hi tech c code to mikroc code

Status
Not open for further replies.

srenjis

Member level 1
Joined
Jun 30, 2012
Messages
38
Helped
7
Reputation
14
Reaction score
6
Trophy points
1,288
Activity points
1,566
code of a FREQUENCY COUNTER .This is from

**broken link removed**

The pic used is 16f 628a


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//---------------------------------------------
// FREQUENCY COUNTER
//---------------------------------------------
 
 
 
#include <pic.h>
#include <delay.c>
 
__CONFIG(WDTDIS&PWRTEN&LVPDIS&XT);
 
unsigned char kontrol;
 
//---------------------------------------------
//       CCP1 INTERRUPT
//---------------------------------------------
void interrupt interrupt(void){
TMR1H=0; TMR1L=0;
GIE=0;
 
control=1; 
 
CCP1IF=0;
GIE=1;
}
 
//---------------------------------------------
//      MAIN PROGRAM
//---------------------------------------------
main(void)
{
unsigned const char number[10]={0x3F,0x06,0x5B,
        0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};    
unsigned char select[4]={1,2,4,8};
unsigned int counter,value,remainder1,remainder2;
float frekans;
unsigned char a,i,display[5],data;
 
TRISA=0x00;
TRISB=0x08;
CMCON=0x07;
 
cont=0;
PORTA=0; PORTB=0
 
CCP1IE=1;
 
CCP1CON=0b00000110; 
 
T1CON=0b00100001;
          
GIE=1;
PEIE=1;
 
for(;;){
 
 
counter=256*CCPR1H+CCPR1L;
 
if(control==1)frequency=100000000/counter;
if(kontrol==0)frequency=0;
 
if(counter<10000)frequency=0; 
 
control=0;
 
for(a=0;a<25;a++){ 
 
    value=(int)frequency;
 
    display[1]=value/1000;
    remainder1=value-display[1]*1000;
 
    display[2]=remainder1/100;
    remainder2=remainder1-display[2]*100;
 
    display[3]=remainder2/10;
    display[4]=remainder2-display[3]*10;
 
    
    for(i=0;i<4;i++){
        PORTB=0;
        PORTA=0;
 
        data=number[display[i+1]];
        PORTB=data&0x07;
        data=data<<1;
        PORTB=PORTB|(data&0xF0);
 
        PORTA=select[i];
        DelayMs(3);
    }
} 
}
}

 
Last edited by a moderator:

Which code details did you experience to be incompatbile with mikroc? Most PIC specific details (SFR, interrupt function coding) should be understood.
 

Load it into mikroc and see where the errors are when you compile it. You may need to change some compiler specific syntax but the program flow should be the same.
 

these are the errors


Code:
diagnostics: 0	126	All files Preprocessed in 16 ms	 
diagnostics: 0	122	Compilation Started	MyProject.c
error: 21	318	Assigning to non-lvalue 'GIE'	MyProject.c
error: 23	324	Undeclared identifier 'control' in expression	MyProject.c
error: 25	318	Assigning to non-lvalue 'CCP1IF'	MyProject.c
error: 26	318	Assigning to non-lvalue 'GIE'	MyProject.c
error: 39	396	Invalid declarator expected'(' or identifier	MyProject.c
error: 41	424	'}' expected ';' found	MyProject.c
hint: 34	1163	Variable 'number' has been declared, but not used	MyProject.c
hint: 36	1163	Variable 'select' has been declared, but not used	MyProject.c
hint: 37	1163	Variable 'counter' has been declared, but not used	MyProject.c
hint: 37	1163	Variable 'value' has been declared, but not used	MyProject.c
hint: 37	1163	Variable 'remainder1' has been declared, but not used	MyProject.c
hint: 37	1163	Variable 'remainder2' has been declared, but not used	MyProject.c
hint: 38	1163	Variable 'frekans' has been declared, but not used	MyProject.c
hint: 39	1163	Variable 'a' has been declared, but not used	MyProject.c
hint: 39	1163	Variable 'i' has been declared, but not used	MyProject.c
hint: 39	1163	Variable 'display' has been declared, but not used	MyProject.c
hint: 0	1163	Variable '' has been declared, but not used	MyProject.c
warning: 94	1503	Result is not defined in function: 'main'	MyProject.c
error: 95	371	Specifier needed	MyProject.c
error: 95	396	Invalid declarator expected'(' or identifier	MyProject.c
error: 96	402	; expected, but '}' found	MyProject.c
error: 96	312	Internal error ''	MyProject.c
error: 0	102	Finished (with errors): 12 May 2014, 19:13:10	MyProject.mcppi
 

Attachments

  • fre.zip
    6.4 KB · Views: 57

To start from the top. You'll notice that some SRF names are known by mikroc, e.g. TMR1H, TMR1L. For the other registers respectively bits, mikroc has apparently different names. Check the documentation how to correct it.
 

Bits like GIE INT0IE should be replaced by GIE_bit, INT0IE_bit.
 

but still lot of errors



Code:
//---------------------------------------------
// FREQUENCY COUNTER
// www.circuit-projects.com
// Y.Erol
//---------------------------------------------



//#include <pic.h>
//#include <delay.c>

///__CONFIG(WDTDIS&PWRTEN&LVPDIS&XT);

unsigned char kontrol,frequency;
unsigned char a,i,display[5],dat;
unsigned const char number[10]={0x3F,0x06,0x5B,
              0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned char select[4]={1,2,4,8};
unsigned int counter,control,value,remainder1,cont,remainder2;
float frekans;
//---------------------------------------------
//             CCP1 INTERRUPT
//---------------------------------------------
void interrupt (void){
TMR1H=0; TMR1L=0;

//GIE=0;
 GIE_bit=0;
control=1;

CCP1IF=0;
GIE=1;
}

//---------------------------------------------
//                MAIN PROGRAM
//---------------------------------------------
main(void)
{



TRISA=0x00;
TRISB=0x08;
CMCON=0x07;

cont=0;
PORTA=0; PORTB=0

CCP1IE=1;

CCP1CON=0b00000110;

T1CON=0b00100001;
 GIE_bit=1;
//GIE=1;
PEIE=1;

for(;;){


counter=256*CCPR1H+CCPR1L;

if(control==1)frequency=100000000/counter;
if(kontrol==0)frequency=0;

if(counter<10000)frequency=0;

control=0;

for(a=0;a<25;a++){

        value=(int)frequency;

        display[1]=value/1000;
        remainder1=value-display[1]*1000;

        display[2]=remainder1/100;
        remainder2=remainder1-display[2]*100;

        display[3]=remainder2/10;
        display[4]=remainder2-display[3]*10;


        for(i=0;i<4;i++){
                PORTB=0;
                PORTA=0;

                dat=number[display[i+1]];
                PORTB=daa&0x07;
                dat=dat<<1;
                PORTB=PORTB|(dat&0xF0);

                PORTA=select[i];
                DelayMs(3);
        }
}
}
}



errors are


HTML:
31 318 Assigning to non-lvalue 'CCP1IF' MyProject.c
32 318 Assigning to non-lvalue 'GIE' MyProject.c
48 402 ; expected, but 'CCP1IE' found MyProject.c
50 424 '}' expected ';' found MyProject.c
97 371 Specifier needed MyProject.c
97 396 Invalid declarator expected'(' or identifier MyProject.c
98 402 ; expected, but '}' found MyProject.c
\
 

It took 3 minutes to fix the code.
 

Attachments

  • Frequency Counter.rar
    31.3 KB · Views: 78
Hi, Mr.milan.rajik

Thanks..................
:thumbsup:
 

Hi srenjis,
Some time ago I also found this Y.Erol project, the only one I found that accurately measures low frequencies (about 30 hz for my purpose).
I am interested too to porting this program in MIKROBASIC.
Were you able to convert it? If so, does it work correctly?

Let me know.
Thanks....
 

He used mikroC not mikroBasic but I can help you port the code to mikroBasic.
 

He used mikroC not mikroBasic but I can help you port the code to mikroBasic.

I really appreciate your help if you can provide me with the code in MikroBasic.
Really great!

Carlo Alberto.
 

Here is the mikroBasic code. It compiles but I don't know whether it works or not. Test in hardware and reply. If it doesn't work then draw the circuit in Proteus and Zip and post it here. I will debug and test.
 

Attachments

  • Frequency Counter.rar
    6.3 KB · Views: 54

    V

    Points: 2
    Helpful Answer Positive Rating
Hi Milan
you've been very kind!
I ask you some questions about the logic of the program.
Since I'm trying to understand the operations, I renamed some variables, but only those ...

What I can not understand is the calculation of the frequency.
For example, the statement:

period = 256 * + CCPR1H CCPR1L;

is in microsecond because of 4Mhz crystal, 4Mhz/4=1Mhz (1us internal timer period).
Is right ?
Consequently get frequency (Hz) dividing 1 by the period:

frequency = 1000000 / period

So, in the original code I not understand this:

frequency = 100000000 / period

Why 100000000 ? What am I doing wrong ?

If I understand this will understand these dividing:

display[1] = value/1000;
remainder1 = value-display[1]*1000;

display[2] = remainder1/100;
remainder2 = remainder1-display[2]*100;

display[3] = remainder2/10;
display[4] = remainder2-display[3]*10;

Thank you very much !

Carlo Alberto
 

I have not analyzed the code. Maybe Prescalar is used for timer. Check T1CON value. Ask the original author regarding the doubts. I will analyze the code and will reply soon.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top