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

TIMER0 and ADC with 16F1509
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ttelmah



Joined: 11 Mar 2010
Posts: 19553

View user's profile Send private message

PostPosted: Tue May 10, 2022 6:49 am     Reply with quote

You still have the problem that your timer will not remain synchronised to
your external loop.
As a comment try to indent your code to help readability.
Do the work inside the interrupt. Don't change channels outside. Now you
seem to be trying to interrupt about 20* per second, and do about one
second each for CO and NTC. I've done it so the entire cycle is one
second. Something like:
Code:

//Have a global int1 done_flag=FALSE;

void timer0_test()
{
   set_timer0(60);
   switch (sayi++) {
   case 0:
      set_adc_channel(4);
      sum_NTC=0;
      break;
   case 1:
   case 2:
   case 3:
   case 4:
   case 5:
   case 6:
   case 7:
   case 8:
      sum_NTC+=read_adc();
      break; //sum 8 readings
   case 9:
      avg_NTC=sum_ntc/8;
      break;
   case 10:
      set_adc_channel(7);
      sum_CO=0;
      break;
   case 11:
   case 12:
   case 13:
   case 14:
   case 15:
   case 16:
   case 17:
   case 18:
      sum_CO+=read_adc()
      break;
   case 19:
      avg_CO=sum_CO/8;
      sayi=0;
      done_flag=TRUE;
      break;
   }
}


Depending on the age of your compiler you may get a warning about
switch statements without breaks. Either just ignore this or use the
'nobreak' fix in the compiler's 'readme'.

Then in your main:
Code:

//Then have a main like:
void main()
{

   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   
   
   set_tris_c(0b00001111);
   set_tris_a(0x00);
   
   
   output_a(0x00); //red led
   output_c(0x00);
   
   setup_adc(adc_clock_div_8);
   
   setup_adc_ports(sAN4 |sAN7, VSS_VDD );
     
   setup_timer_0(T0_INTERNAL | T0_DIV_256);
   set_timer0(60);
 
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);
   
   while (true)
   {
      //wait for the timer to do a set of samples
      while (done_flag==FALSE)
         ;
      done_flag=FALSE;

      //==================red lED================

      if (( avg_CO > 0) || ( avg_CO < 240) )
      {
         output_low(pin_a5);
      }

      else if (avg_CO > 245)
      {
         output_high(pin_a5);
      }

      //==================green LED ==============
      output_toggle(pin_a4);

 
      //==================Stein Hart for NTC ==========
      R2 = R1 * (1023.0 / (float)avg_NTC - 1.0);
      logR2 = log(R2);
      T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
      T = T - 273.15;
      //========================================

 
      //==================  printf  =================
 
      printf ("Temperature:%f ",T);
      printf("AVG_CO-ADC= %lu  ",avg_CO);
   }
}
Delfinss



Joined: 05 Apr 2022
Posts: 21

View user's profile Send private message

TİMER0 and ADC with 16F1509
PostPosted: Fri May 13, 2022 5:06 am     Reply with quote

Hi again
Unfortunately this is the result

Temperature:-51.26 AVG CO-ADC= 1

Please share your suggestions. I also try a lot before writing to the forum, but I have not been successful yet.

Quote:
//Have a global int1 done_flag=FALSE;

void timer0_test()
{
set_timer0(60);
switch (sayi++) {
case 0:
set_adc_channel(4);
sum_NTC=0;
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
sum_NTC+=read_adc();
break; //sum 8 readings
case 9:
avg_NTC=sum_ntc/8;
break;
case 10:
set_adc_channel(7);
sum_CO=0;
break;
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
sum_CO+=read_adc()
break;
case 19:
avg_CO=sum_CO/8;
sayi=0;
done_flag=TRUE;
break;
}
}
//Then have a main like:
void main()
{

setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);


set_tris_c(0b00001111);
set_tris_a(0x00);


output_a(0x00); //red led
output_c(0x00);

setup_adc(adc_clock_div_8);

setup_adc_ports(sAN4 |sAN7, VSS_VDD );

setup_timer_0(T0_INTERNAL | T0_DIV_256);
set_timer0(60);

enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);

while (true)
{
//wait for the timer to do a set of samples
while (done_flag==FALSE)
;
done_flag=FALSE;

//==================red lED================

if (( avg_CO > 0) || ( avg_CO < 240) )
{
output_low(pin_a5);
}

else if (avg_CO > 245)
{
output_high(pin_a5);
}

//==================green LED ==============
output_toggle(pin_a4);


//==================Stein Hart for NTC ==========
R2 = R1 * (1023.0 / (float)avg_NTC - 1.0);
logR2 = log(R2);
T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
T = T - 273.15;
//========================================


//================== printf =================

printf ("Temperature:%f ",T);
printf("AVG_CO-ADC= %lu ",avg_CO);
}
}


Last edited by Delfinss on Fri May 13, 2022 5:55 am; edited 1 time in total
temtronic



Joined: 01 Jul 2010
Posts: 9247
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri May 13, 2022 5:41 am     Reply with quote

you need to show us your current code,

this...
AVG CO-ADC= 1

could be the result was 0 or 1... OR defined as a short OR some 'weird math'.

also when doing 'math', you should printout intermediate results of complicated calculations and compare to YOU doing the math with pencil and paper. having brackets in the wrong spot can cause all sorts of 'funny' numbers....

it's impossible to KNOW what's happening until we SEE your code.
Delfinss



Joined: 05 Apr 2022
Posts: 21

View user's profile Send private message

TİMER0 and ADC with 16F1509
PostPosted: Fri May 13, 2022 8:42 am     Reply with quote

I think it worked Smile

I was able to get values with fast_io and not standard_io
Will this cause any problems for me?

Temperature:26.09 AVG_CO-ADC= 208

Temperature:26.18 AVG_CO-ADC= 207

thank you everyone.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri May 13, 2022 11:33 am     Reply with quote

That's because of the position of the two lines shown in bold.
In Standard I/O mode, they will over-write the TRIS settings of the
previous two lines.

If you want the program to work with Standard I/O, you need to
move the two lines in bold so they are above the set_tris lines.
Quote:

void main()
{
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);

set_tris_c(0b00001111);
set_tris_a(0x00);

output_a(0x00); //red led
output_c(0x00);

temtronic



Joined: 01 Jul 2010
Posts: 9247
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri May 13, 2022 4:04 pm     Reply with quote

I've only needed fast-IO() 2-3 times in 25 years...
Save yourself a LOT of headaches, and just use standard_io ( the default).
If, IF, you really, really speed, then and ONLY then use fast-io.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19553

View user's profile Send private message

PostPosted: Sat May 14, 2022 7:18 am     Reply with quote

and the key point as PCM points out is 'position'. To use standard_io,
change the order of the commands to:
Code:

void main()
{
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);

output_a(0x00); //red led
output_c(0x00);

set_tris_c(0b00001111);
set_tris_a(0x00);


The point is that with a port wide output, using standard I/O, this includes
inside the command setting the tris on the whole port to output.
So set the TRIS after this, not before.
Delfinss



Joined: 05 Apr 2022
Posts: 21

View user's profile Send private message

TİMER0 and ADC with 16F1509
PostPosted: Wed May 18, 2022 8:58 am     Reply with quote

thank you all very much, you did a great job
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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