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.

user defined functions in ccs c

Status
Not open for further replies.

john120

Banned
Joined
Aug 13, 2011
Messages
257
Helped
11
Reputation
22
Reaction score
10
Trophy points
1,298
Activity points
0
Hello can u plz help me to display message and temperature and voltage using different functions,meanwhile not all those parameters defined in main function,I want to display on the lcd using ccs c compiler.I am using PIC 16F877A.


I did an example but the problem comes from calling the function which I called



float tempdisplay()
{
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
adc_value = read_adc();
volts = ((float)(adc_value * 500)/1023.0);
delay_ms(10);
volts2 = ((float)(adc_value * 500)/1023.0);
delay_ms(10);
volts3 = ((float)(adc_value * 500)/1023.0);
delay_ms(10);
volts4 = ((float)(adc_value * 500)/1023.0);
delay_ms(10);
volts5 = ((float)(adc_value * 500)/1023.0);
delay_ms(10);
volts1=(volts+volts2+volts3+volts4+volts5)/5;
voltsf=(volts*1.8)+32;


}

this temperature is must be displayed when I push a button on RC4 and then the next function is to display :
void float messagedisplay()
{
delay_us(200);
printf(lcd_putc,"\fWELCOME");
delay_ms(500);
printf(lcd_putc,"\fTO");
delay_ms(500);
printf(lcd_putc, "\fPIC 16F877A", );
delay_ms(500);
printf(lcd_putc, "\fPROGRAMMING");
}

I am trying to call those functions in main function but the compiler gives me the error:
undefined identifier:tempdisplay.

Thanks.
 

You need to put a function prototype at the beginning of the file so that main knows what parameters the function takes.

/*--- Function prototypes ---*/

void tempdisplay(void);
void messagedisplay(void);

/*--- Start of program ---*/

void main(void)
{
//code
}


If you wrote the code for main after the other functions it would be ok as well.
Main can't see the other functions that come after it. It can't forward reference.

Also
void float messagedisplay() ???

You cant return void and float.
 

You need to put a function prototype at the beginning of the file so that main knows what parameters the function takes.

/*--- Function prototypes ---*/

void tempdisplay(void);
void messagedisplay(void);

/*--- Start of program ---*/

void main(void)
{
//code
}


If you wrote the code for main after the other functions it would be ok as well.
Main can't see the other functions that come after it. It can't forward reference.

Also
void float messagedisplay() ???

You cant return void and float.



can you plz help me by collecting my codes and put that codes of a push button ,how may I separate the codes of those two different functions?
Thanks
 
Last edited:

Visit Microchip and take a look at some of their code examples.
There is a lot of sample code to look at that deals with the A/D.
 

Post your full code and I will fix it.

Dear see my codes here jayanth.devarayanadurga


#include <16F877A.h>
#DEVICE ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include "lcd.c"


int1 flag;
long rise1,rise2;

float voltsf;
float MR;
//long period;
float period;
unsigned int volts1, adc_value,
volts,volts2,volts3,volts4, volts5;


#int_ccp1
void isr()
{
if (flag==0)
{
rise1 = CCP_1;
flag=1;
}
else

if (flag==1)
{
rise2 = CCP_1;
period=(rise2-rise1);
flag=0;
}



float tempdisplay()
{
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
adc_value = read_adc();
volts = ((float)(adc_value * 500)/1023.0);
delay_ms(10);
volts2 = ((float)(adc_value * 500)/1023.0);
delay_ms(10);
volts3 = ((float)(adc_value * 500)/1023.0);
delay_ms(10);
volts4 = ((float)(adc_value * 500)/1023.0);
delay_ms(10);
volts5 = ((float)(adc_value * 500)/1023.0);
delay_ms(10);
volts1=(volts+volts2+volts3+volts4+volts5)/5;
voltsf=(volts*1.8)+32;


}


void float messagedisplay()
{
delay_us(200);
printf(lcd_putc,"\fWELCOME");
delay_ms(500);
printf(lcd_putc,"\fTO");
delay_ms(500);
printf(lcd_putc, "\fPIC 16F877A", );
delay_ms(500);
printf(lcd_putc, "\fPROGRAMMING");
}


void main(void)
{

set_tris_c(0xff);
set_tris_b(0x00);
set_tris_a(0xff);
flag=0;
lcd_init();
lcd_gotoxy(1,1);
delay_us(200);
goto message;

test1:
setup_ccp1(CCP_CAPTURE_RE|CCP_CAPTURE_DIV_16); // Configure CCP1 to capture rise
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); // Start timer 1
enable_interrupts(INT_CCP1); // Setup interrupt on risining edge
enable_interrupts(GLOBAL);
disable_interrupts(global);
delay_ms(300);
delay_ms(150);

if(input(PIN_B4)!=0)
{
display temperature
display period of a square wave signal read on CCP1;
}

if(input(PIN_B3)!=0)
{
display other message for example:"How are you"
}
in case no any button pushud
the lcd remain displaying only"

delay_ms(500);
printf(lcd_putc, "\fPIC 16F877A", );
delay_ms(500);
printf(lcd_putc, "\fPROGRAMMING");

"

Thanks for the help.!!
 

What is your new problem?

button code.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if(input(PIN_B4)==1)
{
    delay_ms(30);
    if(input(PIN_B4)==1)
    {
        display temperature
        display period of a square wave signal read on CCP1;
    }
}
 
if(input(PIN_B3)==1)
{
    delay_ms(30);
    if(input(PIN_B3)==1)
    {
        display other message for example:"How are you"
    }
}




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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <16F877A.h>
#DEVICE ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include "lcd.c"
 
void tempdisplay();
void messagedisplay();
 
int1 flag;
float voltsf, MR;
long rise1, rise2, period;
unsigned int adc_value, volts;
 
 
#int_ccp1
void isr()
{
    if (flag==0)
    {
        rise1 = CCP_1;
        flag=1;
    }
    else if (flag==1)
    {
        rise2 = CCP_1;
        period=(rise2-rise1);
        flag=0;
    }
}
 
 
 
void tempdisplay()
{
    volts = 0;
    adc_value = read_adc();
    volts += ((float)(adc_value * 500)/1023.0);
    delay_ms(10);
    volts += ((float)(adc_value * 500)/1023.0);
    delay_ms(10);
    volts += ((float)(adc_value * 500)/1023.0);
    delay_ms(10);
    volts += ((float)(adc_value * 500)/1023.0);
    delay_ms(10);
    volts += ((float)(adc_value * 500)/1023.0);
    delay_ms(10);
    volts = volts/5;
    voltsf=(volts*1.8)+32;
}
 
 
void messagedisplay()
{
    delay_us(200);
    printf(lcd_putc,"\fWELCOME");
    delay_ms(500);
    printf(lcd_putc,"\fTO");
    delay_ms(500);
    printf(lcd_putc, "\fPIC 16F877A", );
    delay_ms(500);
    printf(lcd_putc, "\fPROGRAMMING");
}
 
 
void main(void)
{
 
    flag=0;
    lcd_init();
    lcd_gotoxy(1,1);
    delay_us(200);
    printf(lcd_putc, "\fPIC 16F877A", );
    delay_ms(500);
    printf(lcd_putc, "\fPROGRAMMING");
    
    setup_adc_ports(AN0);
    setup_adc(ADC_CLOCK_INTERNAL);
    set_adc_channel(0);
    
    setup_ccp1(CCP_CAPTURE_RE|CCP_CAPTURE_DIV_16); // Configure CCP1 to capture rise 
    setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); // Start timer 1
    enable_interrupts(INT_CCP1); // Setup interrupt on risining edge
    enable_interrupts(GLOBAL); 
    disable_interrupts(global);
    delay_ms(300); 
    
 
    while(1){
    
        tempdisplay();
        
        if(input(PIN_B4)==1)
        {
            delay_ms(30);
            if(input(PIN_B4)==1)
            {
                //display temperature
                printf(lcd_putc, "\f%7.4f\r\n", voltsf);
                //display period of a square wave signal read on CCP1;
                printf(lcd_putc, "\f%5lu\r\n", period);
            }
        }
 
        if(input(PIN_B3)==1)
        {
            delay_ms(30);
            if(input(PIN_B3)==1)
            {
                messagedisplay();
            }
            
        }
            
    }
}

 
Last edited:

What is your new problem?

button code.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if(input(PIN_B4)==1)
{
    delay_ms(30);
    if(input(PIN_B4)==1)
    {
        display temperature
        display period of a square wave signal read on CCP1;
    }
}
 
if(input(PIN_B3)==1)
{
    delay_ms(30);
    if(input(PIN_B3)==1)
    {
        display other message for example:"How are you"
    }
}




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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <16F877A.h>
#DEVICE ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include "lcd.c"
 
void tempdisplay();
void messagedisplay();
 
int1 flag;
float voltsf, MR;
long rise1, rise2, period;
unsigned int adc_value, volts;
 
 
#int_ccp1
void isr()
{
    if (flag==0)
    {
        rise1 = CCP_1;
        flag=1;
    }
    else if (flag==1)
    {
        rise2 = CCP_1;
        period=(rise2-rise1);
        flag=0;
    }
}
 
 
 
void tempdisplay()
{
    volts = 0;
    adc_value = read_adc();
    volts += ((float)(adc_value * 500)/1023.0);
    delay_ms(10);
    volts += ((float)(adc_value * 500)/1023.0);
    delay_ms(10);
    volts += ((float)(adc_value * 500)/1023.0);
    delay_ms(10);
    volts += ((float)(adc_value * 500)/1023.0);
    delay_ms(10);
    volts += ((float)(adc_value * 500)/1023.0);
    delay_ms(10);
    volts = volts/5;
    voltsf=(volts*1.8)+32;
}
 
 
void messagedisplay()
{
    delay_us(200);
    printf(lcd_putc,"\fWELCOME");
    delay_ms(500);
    printf(lcd_putc,"\fTO");
    delay_ms(500);
    printf(lcd_putc, "\fPIC 16F877A", );
    delay_ms(500);
    printf(lcd_putc, "\fPROGRAMMING");
}
 
 
void main(void)
{
 
    flag=0;
    lcd_init();
    lcd_gotoxy(1,1);
    delay_us(200);
    printf(lcd_putc, "\fPIC 16F877A", );
    delay_ms(500);
    printf(lcd_putc, "\fPROGRAMMING");
    
    setup_adc_ports(AN0);
    setup_adc(ADC_CLOCK_INTERNAL);
    set_adc_channel(0);
    
    setup_ccp1(CCP_CAPTURE_RE|CCP_CAPTURE_DIV_16); // Configure CCP1 to capture rise 
    setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); // Start timer 1
    enable_interrupts(INT_CCP1); // Setup interrupt on risining edge
    enable_interrupts(GLOBAL); 
    disable_interrupts(global);
    delay_ms(300); 
    
 
    while(1){
    
        tempdisplay();
        
        if(input(PIN_B4)==1)
        {
            delay_ms(30);
            if(input(PIN_B4)==1)
            {
                //display temperature
                printf(lcd_putc, "\f%7.4f\r\n", voltsf);
                //display period of a square wave signal read on CCP1;
                printf(lcd_putc, "\f%5lu\r\n", period);
            }
        }
 
        if(input(PIN_B3)==1)
        {
            delay_ms(30);
            if(input(PIN_B3)==1)
            {
                messagedisplay();
            }
            
        }
            
    }
}



My problem is that:
when I push B4,the LCD displays two things:
first is the period then it delays 3000ms,then it test the level of voltsf and then display one default value defined example below
example:if 5<voltsf<10;lcd displays 15
if 10<volts<12;lcd displays 18
if 12<voltsf<15;lcd displays 65

when pressing B3,the Lcd displays the above but it first shows:
delay_us(200);
printf(lcd_putc,"\fWELCOME");
delay_ms(500);
printf(lcd_putc,"\fTO");
delay_ms(500);
printf(lcd_putc, "\fPIC 16F877A", );
delay_ms(500);
printf(lcd_putc, "\fPROGRAMMING");
and also the values of period and voltsf are displayed only once after pushing the button,I think the while loop has to be omitted;Help plz.

Thanks,
 

Your CCPx capture code to get the pulse period is wrong. You have disabled the interrupts. You won't get the value of Period. I don't know CCS C much, So, you have to find solution for period thing yourself.


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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <16F877A.h>
#DEVICE ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include "lcd.c"
 
void tempdisplay();
void messagedisplay();
 
int1 flag;
float voltsf, MR;
long rise1, rise2, period;
unsigned int adc_value, volts;
 
 
#int_ccp1
void isr()
{
    if (flag==0)
    {
        rise1 = CCP_1;
        flag=1;
    }
    else if (flag==1)
    {
        rise2 = CCP_1;
        period=(rise2-rise1);
        flag=0;
    }
}
 
 
 
void tempdisplay()
{
    volts = 0;
    adc_value = read_adc();
    volts += ((float)(adc_value * 500)/1023.0);
    delay_ms(10);
    volts += ((float)(adc_value * 500)/1023.0);
    delay_ms(10);
    volts += ((float)(adc_value * 500)/1023.0);
    delay_ms(10);
    volts += ((float)(adc_value * 500)/1023.0);
    delay_ms(10);
    volts += ((float)(adc_value * 500)/1023.0);
    delay_ms(10);
    volts = volts/5;
    voltsf=(volts*1.8)+32;
}
 
 
void messagedisplay()
{
    delay_us(200);
    printf(lcd_putc,"\fWELCOME");
    delay_ms(500);
    printf(lcd_putc,"\fTO");
    delay_ms(500);
    printf(lcd_putc, "\fPIC 16F877A", );
    delay_ms(500);
    printf(lcd_putc, "\fPROGRAMMING");
}
 
 
void main(void)
{
 
    flag=0;
    lcd_init();
    lcd_gotoxy(1,1);
    delay_us(200);
    printf(lcd_putc, "\fPIC 16F877A", );
    delay_ms(500);
    printf(lcd_putc, "\fPROGRAMMING");
    
    setup_adc_ports(AN0);
    setup_adc(ADC_CLOCK_INTERNAL);
    set_adc_channel(0);
    
    setup_ccp1(CCP_CAPTURE_RE|CCP_CAPTURE_DIV_16); // Configure CCP1 to capture rise 
    setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); // Start timer 1
    enable_interrupts(INT_CCP1); // Setup interrupt on risining edge
    enable_interrupts(GLOBAL); 
    disable_interrupts(global);
    delay_ms(300); 
    
 
    while(1){
    
        tempdisplay();
        
        if(input(PIN_B4)==1)
        {
            delay_ms(30);
            if(input(PIN_B4)==1)
            {
                //display period of a square wave signal read on CCP1;
                printf(lcd_putc, "\f%5lu\r\n", period);
                delay_ms(3000);
                //display temperature
                if((voltsf > 5) && (voltsf < 10)){
                    printf(lcd_putc, "\f15\r\n");
                }
                else if((voltsf > 10) && (voltsf < 12)){
                    printf(lcd_putc, "\f18\r\n");
                }
                else if((voltsf > 12) && (voltsf < 15)){
                    printf(lcd_putc, "\f65\r\n");
                }
                
                
            }
        }
 
        if(input(PIN_B3)==1)
        {
            delay_ms(30);
            if(input(PIN_B3)==1)
            {
                messagedisplay();
                delay_ms(3000);
                //display period of a square wave signal read on CCP1;
                printf(lcd_putc, "\f%5lu\r\n", period);
                delay_ms(3000);
                //display temperature
                if((voltsf > 5) && (voltsf < 10)){
                    printf(lcd_putc, "\f15\r\n");
                }
                else if((voltsf > 10) && (voltsf < 12)){
                    printf(lcd_putc, "\f18\r\n");
                }
                else if((voltsf > 12) && (voltsf < 15)){
                    printf(lcd_putc, "\f65\r\n");
                }
            }
            
        }
            
    }
}

 

Your CCPx capture code to get the pulse period is wrong. You have disabled the interrupts. You won't get the value of Period. I don't know CCS C much, So, you have to find solution for period thing yourself.


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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <16F877A.h>
#DEVICE ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include "lcd.c"
 
void tempdisplay();
void messagedisplay();
 
int1 flag;
float voltsf, MR;
long rise1, rise2, period;
unsigned int adc_value, volts;
 
 
#int_ccp1
void isr()
{
    if (flag==0)
    {
        rise1 = CCP_1;
        flag=1;
    }
    else if (flag==1)
    {
        rise2 = CCP_1;
        period=(rise2-rise1);
        flag=0;
    }
}
 
 
 
void tempdisplay()
{
    volts = 0;
    adc_value = read_adc();
    volts += ((float)(adc_value * 500)/1023.0);
    delay_ms(10);
    volts += ((float)(adc_value * 500)/1023.0);
    delay_ms(10);
    volts += ((float)(adc_value * 500)/1023.0);
    delay_ms(10);
    volts += ((float)(adc_value * 500)/1023.0);
    delay_ms(10);
    volts += ((float)(adc_value * 500)/1023.0);
    delay_ms(10);
    volts = volts/5;
    voltsf=(volts*1.8)+32;
}
 
 
void messagedisplay()
{
    delay_us(200);
    printf(lcd_putc,"\fWELCOME");
    delay_ms(500);
    printf(lcd_putc,"\fTO");
    delay_ms(500);
    printf(lcd_putc, "\fPIC 16F877A", );
    delay_ms(500);
    printf(lcd_putc, "\fPROGRAMMING");
}
 
 
void main(void)
{
 
    flag=0;
    lcd_init();
    lcd_gotoxy(1,1);
    delay_us(200);
    printf(lcd_putc, "\fPIC 16F877A", );
    delay_ms(500);
    printf(lcd_putc, "\fPROGRAMMING");
    
    setup_adc_ports(AN0);
    setup_adc(ADC_CLOCK_INTERNAL);
    set_adc_channel(0);
    
    setup_ccp1(CCP_CAPTURE_RE|CCP_CAPTURE_DIV_16); // Configure CCP1 to capture rise 
    setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); // Start timer 1
    enable_interrupts(INT_CCP1); // Setup interrupt on risining edge
    enable_interrupts(GLOBAL); 
    disable_interrupts(global);
    delay_ms(300); 
    
 
    while(1){
    
        tempdisplay();
        
        if(input(PIN_B4)==1)
        {
            delay_ms(30);
            if(input(PIN_B4)==1)
            {
                //display period of a square wave signal read on CCP1;
                printf(lcd_putc, "\f%5lu\r\n", period);
                delay_ms(3000);
                //display temperature
                if((voltsf > 5) && (voltsf < 10)){
                    printf(lcd_putc, "\f15\r\n");
                }
                else if((voltsf > 10) && (voltsf < 12)){
                    printf(lcd_putc, "\f18\r\n");
                }
                else if((voltsf > 12) && (voltsf < 15)){
                    printf(lcd_putc, "\f65\r\n");
                }
                
                
            }
        }
 
        if(input(PIN_B3)==1)
        {
            delay_ms(30);
            if(input(PIN_B3)==1)
            {
                messagedisplay();
                delay_ms(3000);
                //display period of a square wave signal read on CCP1;
                printf(lcd_putc, "\f%5lu\r\n", period);
                delay_ms(3000);
                //display temperature
                if((voltsf > 5) && (voltsf < 10)){
                    printf(lcd_putc, "\f15\r\n");
                }
                else if((voltsf > 10) && (voltsf < 12)){
                    printf(lcd_putc, "\f18\r\n");
                }
                else if((voltsf > 12) && (voltsf < 15)){
                    printf(lcd_putc, "\f65\r\n");
                }
            }
            
        }
            
    }
}


Why Am I not getting the real value of period,I am always getting period =0;
is it possible to declare as much functions as you can?
Because I declared the third function for displaying another message
like:

messagedisplay1()
{
delay_ms(1000);
printf(lcd_putc,"Morning");
delay_ms(1000);
}


and associate it to PIN B2,but it is not running.

Thanks for helping.
 

You are not getting the value of period because of this line disable_interrupts(global);

You have disabled glogal interrupts, so, no interrupts occur and your isr code is of no use. Yes, you can declare function like that and call it in your code but remember to add function prototype. Declaration will depend upon how much memory you have for code. i.e., program memory.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top