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.

PIC16F codes convert to 18F

Status
Not open for further replies.

nicoise

Newbie level 5
Joined
Jul 31, 2012
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,339
Can someone convert the codes from 16f to 18f ? I will use these 'Frequency Counter' codes in my PIC18F452. i will be very grateful

Microcontroller-Schematic.jpg


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



#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);
	}
} 
}
}
 

Better start with schematics. 18f452 is a 40 pins microcontroller. What you keep what you cange in pin assignment?
 

See if this works.


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
/* Main.c file generated by New Project wizard
 *
 * Created:   Fri May 10 2013
 * Processor: PIC18F452
 * Compiler:  MPLAB C18
 */
 
#include <p18f452.h>
#include <delays.h>
 
#pragma config WDT = OFF
#pragma config OSC = XT
#pragma config PWRT = ON
#pragma config BOR = OFF
#pragma config STVR = OFF
 
unsigned char kontrol;
unsigned int control;
 
//---------------------------------------------
//       CCP1 INTERRUPT
//---------------------------------------------
void interrupt(void){
    TMR1H=0; 
    TMR1L=0;
    INTCONbits.GIE=0;
    control = 1; 
    PIR1bits.CCP1IF=0;
    INTCONbits.GIE=1;
}
 
//---------------------------------------------
//      MAIN PROGRAM
//---------------------------------------------
void main()
{
    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 cont, counter,value,remainder1,remainder2;
    float frekans, frequency;
    unsigned char a,i,display[5],data;
 
    TRISA=0x00;
    TRISB=0x08;
    cont=0;
    PORTA=0; 
    PORTB=0;
    PIE1bits.CCP1IE = 1;
    CCP1CON=0b00000110; 
    T1CON=0b00100001;
          
    INTCONbits.GIE=1;
    INTCONbits.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];
                Delay10KTCYx(10);
            }
        } 
    }
}

 
Last edited:
See if this works.


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
/* Main.c file generated by New Project wizard
 *
 * Created:   Fri May 10 2013
 * Processor: PIC18F452
 * Compiler:  MPLAB C18
 */
 
#include <p18f452.h>
#include <delays.h>
 
#pragma config WDT = OFF
#pragma config OSC = XT
#pragma config PWRT = ON
#pragma config BOR = OFF
#pragma config STVR = OFF
 
unsigned char kontrol;
unsigned int control;
 
//---------------------------------------------
//       CCP1 INTERRUPT
//---------------------------------------------
void interrupt(void){
    TMR1H=0; 
    TMR1L=0;
    INTCONbits.GIE=0;
    control = 1; 
    PIR1bits.CCP1IF=0;
    INTCONbits.GIE=1;
}
 
//---------------------------------------------
//      MAIN PROGRAM
//---------------------------------------------
void main()
{
    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 cont, counter,value,remainder1,remainder2;
    float frekans, frequency;
    unsigned char a,i,display[5],data;
 
    TRISA=0x00;
    TRISB=0x08;
    cont=0;
    PORTA=0; 
    PORTB=0;
    PIE1bits.CCP1IE = 1;
    CCP1CON=0b00000110; 
    T1CON=0b00100001;
          
    INTCONbits.GIE=1;
    INTCONbits.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];
                Delay10KTCYx(10);
            }
        } 
    }
}


thank you very much:) i created a hex file. i will design a circuit and try it in isis proteus. i will write if i have a problem
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top