CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Request for help in C coding

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
H
Guest







Request for help in C coding
PostPosted: Wed Apr 02, 2008 10:36 pm     Reply with quote

Hi, i m Helen.

Need some help in my project.

I need to design a circuit with coding which voltage as input to tell the pic16f877a to record the time. Then 3 timing is record and calculation will be do after that.

In the simulation i can run the code correctly. However, in pratical i can't get what i want.


The code below:

#include <16F877A.h>
#device ADC=10
#device icd=true
#fuses NOWDT,HS, PUT, NOPROTECT, BROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG
#use delay(clock=20000000)
#include <STDLIB.H>
#include <math.h>
#include <lcd.c>

#define Full 500 //in mili litre

#define INTS_PER_SECOND 76 // (20000000/(4*256*256))

BYTE seconds; // A running seconds counter
BYTE int_count; // Number of interrupts left before a second has elapsed


#int_rtcc // This function is called every time
void clock_isr() { // the RTCC (timer0) overflows (255->0).
// For this program this is apx 76 times
if(--int_count==0) { // per second.
++seconds;
int_count=INTS_PER_SECOND;
}
}

void main(){

float input_num;
float conv_num;
int i;
BYTE T1;
BYTE T3;
BYTE T4;
BYTE Time_full;
float Cap_per_Sec;
BYTE Total_time;
float Total_capacity;


port_b_pullups(TRUE);
set_tris_b(0xFF);

setup_adc_ports(RA0_RA1_ANALOG_RA3_REF);//AN0);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);

setup_psp(PSP_DISABLED);
setup_spi(FALSE);

int_count=INTS_PER_SECOND;
set_timer0(0);
setup_counters( RTCC_INTERNAL, RTCC_DIV_256 | RTCC_8_BIT);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);

lcd_init();

i=3;
T1=0;
T3=0;
T4=0;

do {

input_num=Read_ADC();
conv_num=input_num/204.599;

if (conv_num<1.5 && conv_num>1.0 && T1==0){
T1=seconds;
i=i-1;
printf(lcd_putc,"%u", T1);
}

if (conv_num<3.0 && conv_num>2.5 && T3==0){
T3=seconds;
i=i-1;
printf(lcd_putc," %u", T3);
}

if (conv_num<4.5 && conv_num>4.0 && T4==0){
T4=seconds;
i=i-1;
printf(lcd_putc," %u Cap:\n", T4);
}

}while(i>0);

// printf(lcd_putc,"%u %u %u\n", T1, T3, T4);
Time_full=T4-T3;
Cap_per_Sec=Full/Time_full;
Total_time=T4-T1;
Total_capacity=Cap_per_Sec*Total_time/1000;
printf(lcd_putc,"%1.4f liter", Total_capacity);

}

Can anyone solve this problem or give some advise, please.

Thank You.


Sincerely,
Helen
[/code]
Matro
Guest







PostPosted: Thu Apr 03, 2008 1:28 am     Reply with quote

First, notice that your code is not endless. So if i <= 0 then your program will exit and the PIC will stop.
My second advise is : indent and comment your code (in english).
That is definitely "unreadable" for us, but maybe for you too...

To debug this program you can comment everything except the first part of the main and test it. If it works fine, uncomment the second part and test again. And so on...

Matro.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Apr 03, 2008 1:32 am     Reply with quote

You included the /code tag but forgot the first code tag Smile

Anyway, do you get any results ?
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Apr 03, 2008 1:51 am     Reply with quote

You forgot to set she seconds variable to zero at program start.
Ttelmah
Guest







PostPosted: Thu Apr 03, 2008 2:37 am     Reply with quote

Also, read the data sheet about ADC clocks.
ADC_CLOCK_INTERNAL, should _not_ be used on this chip, above 1MHz, unless the processor is put to sleep for the conversion. It is in the notes under table 11-1. You need to be using the /32 ADC clock instead. Generally simulators, won't realise that this is wrong, and will give good results, but on the real chip, it results in really wayward ADC readings.

Best Wishes
Guest








Re: Request for help in C coding
PostPosted: Thu Apr 03, 2008 4:20 am     Reply with quote

H wrote:
Hi, i m Helen.

Need some help in my project.

I need to design a circuit with coding which voltage as input to tell the pic16f877a to record the time. Then 3 timing is record and calculation will be do after that.

In the simulation i can run the code correctly. However, in pratical i can't get what i want.


The code below:

#include <16F877A.h>
#device ADC=10
#device icd=true
#fuses NOWDT,HS, PUT, NOPROTECT, BROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG
#use delay(clock=20000000)
#include <STDLIB.H>
#include <math.h>
#include <lcd.c>

#define Full 500 //in mili litre

#define INTS_PER_SECOND 76 // (20000000/(4*256*256))

BYTE seconds; // A running seconds counter
BYTE int_count; // Number of interrupts left before a second has elapsed


#int_rtcc // This function is called every time
void clock_isr() { // the RTCC (timer0) overflows (255->0).
// For this program this is apx 76 times
if(--int_count==0) { // per second.
++seconds;
int_count=INTS_PER_SECOND;
}
}

void main(){

float input_num;
float conv_num;
int i;
BYTE T1;
BYTE T3;
BYTE T4;
BYTE Time_full;
float Cap_per_Sec;
BYTE Total_time;
float Total_capacity;


port_b_pullups(TRUE);
set_tris_b(0xFF);

setup_adc_ports(RA0_RA1_ANALOG_RA3_REF);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);

setup_psp(PSP_DISABLED);
setup_spi(FALSE);

int_count=INTS_PER_SECOND;
set_timer0(0);
setup_counters( RTCC_INTERNAL, RTCC_DIV_256 | RTCC_8_BIT);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);

lcd_init();

i=3;
T1=0;
T3=0;
T4=0;

do {

input_num=Read_ADC();
conv_num=input_num/204.599;

if (conv_num<1.5 && conv_num>1.0 && T1==0){ //if volt i/p in this
range
T1=seconds; // T1 second is record
i=i-1;
printf(lcd_putc,"%u", T1);
}

if (conv_num<3.0 && conv_num>2.5 && T3==0){ //if volt i/p in this
range
T3=seconds; // T3 second is record
i=i-1;
printf(lcd_putc," %u", T3);
}

if (conv_num<4.5 && conv_num>4.0 && T4==0){ //if volt i/p in this
range
T4=seconds; // T4 second is record
i=i-1;
printf(lcd_putc," %u Cap:\n", T4);
}

}while(i>0); //for a process, i jz need sense 3 time voltage
change. then if will that the 3 timer to do
calculation

Time_full=T4-T3;
Cap_per_Sec=Full/Time_full;
Total_time=T4-T1;
Total_capacity=Cap_per_Sec*Total_time/1000;
printf(lcd_putc,"%1.4f liter", Total_capacity);

}

Can anyone solve this problem or give some advise, please.

Thank You.


Sincerely,
Helen
[/code]
Guest








Re: Request for help in C coding
PostPosted: Thu Apr 03, 2008 4:36 am     Reply with quote

H wrote:


Sorry I till not sure how to sent a post.
Really thanks for fellow friends suggestion. I will try every one to find the solution.

However, i try to write the comment nicely so it can be read but it till change after I post it.

Actually which seconds variable that you that forget to set to zero?

What mean by first code tag?

Sorry for my poor English and understanding.

For the ADC_CLOCK_INTERNAL problem, I really not notice about it. Give some time to understand it.

Really thank you all.


Sincerely,
Helen
[/code]
[/quote]
Matro
Guest







PostPosted: Thu Apr 03, 2008 5:11 am     Reply with quote

The variable called "seconds" is never initialized.

in your first post, you place a "[/code]" at the end but forgot to place the "[code]" tag at the beginning.

Matro.
tyhon
Guest







PostPosted: Sat Apr 12, 2008 7:23 am     Reply with quote

you can post circuit?
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Sat Apr 12, 2008 8:11 am     Reply with quote

Quote:

In the simulation i can run the code correctly. However, in pratical i can't get what i want.

If you can simulate the preceding code successfully but not with a real hardware, it is
a good practice for a beginner to start with a tested hardware. You can arrange a basic
hardware with a running MCU + 1 blinking LED. This will give you a close aproach that
the MCU is running doing its basics functions and the whole hardware is OK.
Then you should add a new function in each step, doing some tests and debugging until
it run as expected and so on.
Regarding your posted messages, before click in the Submit button, pls try the Preview
button to see how it will look once posted.

Humberto
Guest








Interrupt problem??
PostPosted: Wed Apr 16, 2008 3:35 am     Reply with quote

Hi!

I still dealling with the same problem. I trying to post the circuit on but I till finding the way to do so.
However, I test the coding part by part. Till the same situation where the timing till display zero. Even I write in most simple coding already.

Code:

do{

  input_num=Read_ADC();
   conv_num=input_num/204.599;

   if(conv_num<1.5 && conv_num>1){
        T1=seconds;
        printf(lcd_putc," %u", T1);
   }
   }while(i>0);


Can I ask? I am not using RS232, then can I till using the interrupt function - #INT_RTCC? Since, the PIC can't sense the seconds and always zero. Or it is my circuit problem?


Sincerley,
helen
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group