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 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Delfinss



Joined: 05 Apr 2022
Posts: 21

View user's profile Send private message

TIMER0 and ADC with 16F1509
PostPosted: Thu Apr 28, 2022 1:22 am     Reply with quote

Hello everyone
ccs c version 5.007
I haven't been able to get the timer0 interrupt working with adc.
I think I have a problem with the timings.
Can you help me where do you need to pay attention?

I know that the chip can only flash the led with the timer0 interrupt.
Code:

#include <16f1509.h>
#device ADC = 10    // 0 ile 1023

#include <math.h>

#use delay(internal=4000000)

#fuses INTRC_IO ,NOWDT ,NOPUT ,NOLVP ,NOPROTECT ,NODEBUG ,NOBROWNOUT ,NOWRT

#use fast_io(c)
#use fast_io(a)

#use rs232 (baud=9600, xmit=pin_B7,rcv=pin_B5, parity=N, stop=1)

#int_timer0

int count_NTC;
int sayi=0;


unsigned int  onuf_co; 

unsigned int avg_NTC,value,sum_NTC,sum_CO,avg_CO;

float R1 = 22000;
float logR2, R2, T;
float c1 = 0.5699030023e-03, c2 = 2.830523628e-04, c3 = -1.761701742e-07;


void timer0_test()
 {

set_timer0(60);
//delay_ms(100);
sayi++;
if(sayi==10)
{
 
  sum_NTC=0;
    for (count_NTC=0; count_NTC<8; count_NTC++)
   
   {
    value=read_adc();
    delay_us(100);
    sum_NTC=value + sum_NTC;
    }

      avg_NTC= sum_NTC/=8;



  output_high(pin_a2);
 // delay_us(20);
}

if(sayi==20)
{
 
sum_CO=0;
  for (count_NTC=0; count_NTC<8; count_NTC++)
   
   {
      onuf_co=read_adc();
      delay_us(100);
     sum_CO=onuf_co + sum_CO;
    }

     avg_CO= sum_CO/=8;

output_low(pin_a2);
//delay_us(20);
sayi=0;

}

}

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);
   
while (true)
{
 
  //delay_ms(1000);
 //======================NTC============================
  set_adc_channel(4);  //AN4 analog kanal
  delay_us(20);
 
//========================ONUF_CO=======================

  set_adc_channel(7); 
  delay_us(20);
 
//===================================================
   set_timer0(60);
 
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);
 
//==================red lED==========================

if (( avg_CO > 0) || ( avg_CO < 240) ) {

   output_low(pin_a5);
   delay_us(20);
 
}

else if (avg_CO > 245){

output_high(pin_a5);
delay_us(20);
}

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

 //==================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);
       delay_ms(1000);
   
  }

}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 28, 2022 1:47 am     Reply with quote

Quote:

#int_timer0

int count_NTC;
int sayi=0;


unsigned int onuf_co;

unsigned int avg_NTC,value,sum_NTC,sum_CO,avg_CO;

float R1 = 22000;
float logR2, R2, T;
float c1 = 0.5699030023e-03, c2 = 2.830523628e-04, c3 = -1.761701742e-07;


void timer0_test()
{




The #int_timer0 line must go above the interrupt function, with
no other lines of code between the two lines. Example:
Code:
#int_timer0
void timer0_test()
 {
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Thu Apr 28, 2022 2:19 am     Reply with quote

There are also rather a lot of other problems.

He loops, changing the ADC channel. After the first pass, the interrupt
will be enabled, so the ADC will be reading different ADC channels on
some calls than others, with no synchronisation between this and the timer
calls. The values won't have any relationship to these selections... Sad
Now he is also delaying for an enormous time inside the interrupt on the
10th and 20th calls. Why does he delay 100uSec?.
Tacq is only 7.37uSec, so 8uSec would be plenty.
The sum maths is odd:
avg_CO= sum_CO/=8;

That is not the normal syntax for the operation. It seems to be dividing
sum_CO by 8, writing this into sum_CO, and then copying this into
avg_CO. Probably will work, but an unneeded extra operation.

In the main loop, he reads the two average values, but not even one
timer interrupt will have triggered at this point. He set the count to 60, so
the timer interrupt period will be about 50mSec. He does long delays that
would allow things to happen, after having worked on the readings.
However even these are not enough to allow the 20 cycles needed to
fully generate a reading. It will take three seconds for a pass to complete.

So the first couple of times round the loop, given the avg values are
not initialised, the values will probably be overrange.

Then there is a huge problem with maths types. He has adc=10, but
uses an int for the adc value. 10bit does not go into 8bit.... Sad
He then sums this 8 times, so potentially 13bits. This doesn't fit by an
even larger margin.

Then the problem with fast_io. The ADC channels are on port A, but
he sets the whole of this port to be digital outputs.
Get rid of fast_io. It is miles safer to use standard_io (the default).
Avoids this sort of error.

I gave up at this point....
temtronic



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

View user's profile Send private message

PostPosted: Thu Apr 28, 2022 5:53 am     Reply with quote

gee..and here I was thinking...

... hmm 'James Bond' version of compiler...

well know to have a few 'bugs'...
Delfinss



Joined: 05 Apr 2022
Posts: 21

View user's profile Send private message

TIMER0 and ADC with 16F1509
PostPosted: Fri Apr 29, 2022 3:15 am     Reply with quote

Thank you very much for your valuable suggestions.
I tried to implement your warnings as far as I can understand.
I used Timer1 because I didn't know how to solve the synchronization problem. But now I can't get the adc values.
avg_CO= sum_CO/=8; I used this to smooth the adc value. If you have different suggestions, I'm listening carefully.
Code:

#include <16f1509.h>
#device ADC = 10   // 0 ile 1023

#include <math.h>

#use delay(internal=4000000)

#fuses INTRC_IO ,NOWDT ,NOPUT ,NOLVP ,NOPROTECT ,NODEBUG ,NOBROWNOUT ,NOWRT

#use standard_io(c)
#use standard_io(a)

#use rs232 (baud=9600, xmit=pin_B7,rcv=pin_B5, parity=N, stop=1)

int count_NTC;
int sayi=0;
int mayi=0;


unsigned short int  onuf_co;   //input_co,opamp_co,

unsigned short int avg_NTC,value,sum_NTC,sum_CO,avg_CO;

float R1 = 22000;
float logR2, R2, T;
float c1 = 0.5699030023e-03, c2 = 2.830523628e-04, c3 = -1.761701742e-07;

#int_timer0
void timer0_test()
 {
// clear_interrupt(INT_TIMER0);
set_timer0(60);
//delay_ms(100);
sayi++;
if(sayi==20)
{
 
  sum_NTC=0;
    for (count_NTC=0; count_NTC<8; count_NTC++)
   
   {
    value=read_adc();
    delay_us(8);
    sum_NTC=value + sum_NTC;
    }

      avg_NTC= sum_NTC/=8;

  output_high(pin_a2);
 // delay_us(20);
 sayi=0;
}
 }
#int_timer1
 void timer1_test()
 {
 set_timer1(60536);
 mayi++;
 
if(mayi==100)
{
 
sum_CO=0;
  for (count_NTC=0; count_NTC<8; count_NTC++)
   
   {
      onuf_co=read_adc();
      delay_us(8);
     sum_CO=onuf_co + sum_CO;
    }

     avg_CO= sum_CO/=8;

output_low(pin_a2);
//delay_us(20);
mayi=0;

}
 }


void main()
{

 //delay_ms(300);


   //setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   
   
   set_tris_c(0b00001111);// C0,C1,C2,C3 pini input yapıldı.// C4 output
   set_tris_a(0x00); // a2,a4,a5 pini output //red,green,yellow leds
   
   
    output_a(0x00); //red led
    output_b(0x00);
    output_c(0x00);
   
     setup_adc(adc_clock_div_8);
   
     setup_adc_ports(sAN4 |sAN7, VSS_VDD );
     
    setup_timer_0(T0_INTERNAL | T0_DIV_256);
   
    setup_timer_1(T1_INTERNAL | T1_DIV_BY_4);
   
while (true)
{
 
  delay_ms(1000);
 //======================NTC============================
  set_adc_channel(4);  //AN4 analog kanal
  delay_us(20);
  //====================================================
 
   set_timer0(60);
 
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);

//========================ONUF_CO=======================

   set_adc_channel(7); 
   delay_us(20);
 
//===================================================
   set_timer1(60536);
 
   enable_interrupts(INT_TIMER1);
   enable_interrupts(GLOBAL);
   
   //setup_adc_ports(NO_ANALOGS);
   
 
//==================red lED==========================

if (( avg_CO >= 0) && ( avg_CO < 245) ) {

   output_low(pin_a5);
   delay_us(20);
 
}

else {

output_high(pin_a5);
delay_us(20);
}

 //==================green LED ==========================
  output_toggle(pin_a4);
   delay_ms(500);
 
 //==================yellow LED ==========================
   
 

 //==================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("ONUF_CO-ADC= %u\n\r  ",onuf_co);
    // delay_ms(1000);
   
  }
}
temtronic



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

View user's profile Send private message

PostPosted: Fri Apr 29, 2022 5:50 am     Reply with quote

I had a quick look
saw these two lines...
Quote:

unsigned short int onuf_co; //input_co,opamp_co,
this makes onuf_co a one bit variable...
Quote:

unsigned short int avg_NTC,value,sum_NTC,sum_CO,avg_CO;
again variables are ONE bit wide.
Quote:
onuf_co=read_adc();
this tries to put 8 or 10 but adc result into the ONE bit !
Quote:
sum_CO=onuf_co + sum_CO;
this tries to add them up...BUT they're one bit wide...

also
you'll need to add 'errors' to the#USE RS232(...options...)
Delfinss



Joined: 05 Apr 2022
Posts: 21

View user's profile Send private message

PostPosted: Thu May 05, 2022 6:28 am     Reply with quote

temtronic wrote:
I had a quick look
saw these two lines...
Quote:

unsigned short int onuf_co; //input_co,opamp_co,
this makes onuf_co a one bit variable...
Quote:

unsigned short int avg_NTC,value,sum_NTC,sum_CO,avg_CO;
again variables are ONE bit wide.
Quote:
onuf_co=read_adc();
this tries to put 8 or 10 but adc result into the ONE bit !
Quote:
sum_CO=onuf_co + sum_CO;
this tries to add them up...BUT they're one bit wide...

also
you'll need to add 'errors' to the#USE RS232(...options...)





I think you are right but why is this happening and how can I fix it.

NTC-ADC=1 ONUF_CO-ADC= 0

NTC-ADC=1 ONUF_CO-ADC= 1
Delfinss



Joined: 05 Apr 2022
Posts: 21

View user's profile Send private message

PostPosted: Thu May 05, 2022 6:38 am     Reply with quote

Ttelmah wrote:
There are also rather a lot of other problems.

He loops, changing the ADC channel. After the first pass, the interrupt
will be enabled, so the ADC will be reading different ADC channels on
some calls than others, with no synchronisation between this and the timer
calls. The values won't have any relationship to these selections... Sad
Now he is also delaying for an enormous time inside the interrupt on the
10th and 20th calls. Why does he delay 100uSec?.
Tacq is only 7.37uSec, so 8uSec would be plenty.
The sum maths is odd:
avg_CO= sum_CO/=8;

That is not the normal syntax for the operation. It seems to be dividing
sum_CO by 8, writing this into sum_CO, and then copying this into
avg_CO. Probably will work, but an unneeded extra operation.

In the main loop, he reads the two average values, but not even one
timer interrupt will have triggered at this point. He set the count to 60, so
the timer interrupt period will be about 50mSec. He does long delays that
would allow things to happen, after having worked on the readings.
However even these are not enough to allow the 20 cycles needed to
fully generate a reading. It will take three seconds for a pass to complete.

So the first couple of times round the loop, given the avg values are
not initialised, the values will probably be overrange.

Then there is a huge problem with maths types. He has adc=10, but
uses an int for the adc value. 10bit does not go into 8bit.... Sad
He then sums this 8 times, so potentially 13bits. This doesn't fit by an
even larger margin.

Then the problem with fast_io. The ADC channels are on port A, but
he sets the whole of this port to be digital outputs.
Get rid of fast_io. It is miles safer to use standard_io (the default).
Avoids this sort of error.

I gave up at this point....






Code:
#include <16f1509.h>
#device ADC = 10  // 0 ile 1023

#include <math.h>

#use delay(internal=4000000)

#fuses INTRC_IO ,NOWDT ,NOPUT ,NOLVP ,NOPROTECT ,NODEBUG ,NOBROWNOUT ,NOWRT

#use standard_io(c)
#use standard_io(a)

#use rs232 (baud=9600, xmit=pin_B7,rcv=pin_B5, parity=N, stop=1)

int count_NTC;
int sayi=0;
int mayi=0;

unsigned short int avg_NTC,sum_NTC,sum_CO,avg_CO;

unsigned int onuf_co,value;

float R1 = 22000;
float logR2, R2, T;
float c1 = 0.5699030023e-03, c2 = 2.830523628e-04, c3 = -1.761701742e-07;

#int_timer0
void timer0_test()
 {
// clear_interrupt(INT_TIMER0);
set_timer0(60);
//delay_ms(100);
sayi++;
if(sayi==60)
{
 
  sum_NTC=0;
    for (count_NTC=0; count_NTC<8; count_NTC++)
   
   {
    value=read_adc();
    delay_us(8);
    sum_NTC=value + sum_NTC;
   }

     avg_NTC= sum_NTC/=8;
     
    printf("NTC-ADC=%u    ",  value);
    delay_us(10);

    output_high(pin_a2);
 // delay_us(20);
 sayi=0;
}
 }
#int_timer1
 void timer1_test()
 {
 set_timer1(60536);
 mayi++;
 
if(mayi==150)
{
 
sum_CO=0;
  for (count_NTC=0; count_NTC<8; count_NTC++)
   
   {
      onuf_co=read_adc();
      delay_us(8);
    sum_CO=onuf_co + sum_CO;
    }

     avg_CO= sum_CO/=8;
     
    printf("ONUF_CO-ADC= %u\n\r  ",onuf_co);
    delay_us(10);

output_low(pin_a2);
//delay_us(20);
mayi=0;

}
 }


void main()
{

 //delay_ms(300);


   //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_b(0x00);
    output_c(0x00);
   
     setup_adc(adc_clock_div_8);
   
     setup_adc_ports(sAN4 |sAN7, VSS_VDD );
     
    setup_timer_0(T0_INTERNAL | T0_DIV_256);
   
    setup_timer_1(T1_INTERNAL | T1_DIV_BY_4);
   
while (true)
{
 
  delay_ms(1000);
 //======================NTC============================
  set_adc_channel(4);  //AN4 analog kanal
  delay_us(20);
  //====================================================
 
   set_timer0(60);
 
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);
   
    setup_adc_ports(NO_ANALOGS);
   
//========================ONUF_CO=======================
   delay_ms(1000);
 
   set_adc_channel(7); 
   delay_us(20);
 
//===================================================
   
    set_timer1(60536);
 
   enable_interrupts(INT_TIMER1);
   enable_interrupts(GLOBAL);
   
   setup_adc_ports(NO_ANALOGS);



I used timer0 and timer1 for the sequencing problem.
as a result i am not getting the ADC values.
temtronic



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

View user's profile Send private message

PostPosted: Thu May 05, 2022 7:33 am     Reply with quote

Here's part of your problem...
Quote:
unsigned short int avg_NTC,sum_NTC,sum_CO,avg_CO;
You've declared ALL these variables to be ONE bit ( '1' or '0' )

Quote:
unsigned int onuf_co,value;
These are 8 bit values, yet ADC is declared as 10 bit.

Until you change these to the proper declarations you'll never get to read and do 'math' properly.
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Thu May 05, 2022 11:47 am     Reply with quote

Yes, I pointed out that his variable sizes were too small at the start, and
instead of making them bigger, he has made them smaller!... Sad

int1
int
int16
int32

Is the order of sizes. He started at int, and went to int1, instead of
to int16 or int32.
Delfinss



Joined: 05 Apr 2022
Posts: 21

View user's profile Send private message

TİMER0 and ADC with 16F1509
PostPosted: Tue May 10, 2022 12:37 am     Reply with quote

Hello again everyone

I tried to simplify the code. but tell me i ran into a problem.
When I use standard_io I get the ADC value of 1.
When I use fast_io I read the ADC value normally.
where is the problem? can you help me?


Code:
#include <16f1509.h>
#device ADC = 10  // 0 ile 1023

#include <math.h>

#use delay(internal=4000000)

#fuses INTRC_IO ,NOWDT ,NOPUT ,NOLVP ,NOPROTECT ,NODEBUG ,NOBROWNOUT ,NOWRT

#use  standard_io(C)
#use  standard_io(A)

#use rs232 (baud=9600, xmit=pin_B7,rcv=pin_B5, parity=N, stop=1)

int sayi=0;

int16 value;


float R1 = 22000;
float logR2, R2, T;
float c1 = 0.5699030023e-03, c2 = 2.830523628e-04, c3 = -1.761701742e-07;

#int_timer0
void timer0_test()
 {
set_timer0(60);        // 50 ms 
sayi++;
if(sayi==40)     // 50*40 =2000 ms(2sn)   
  {
 
    value=read_adc();
    delay_us(8);
   
    printf("NTC-ADC= %lu\n\r ", value);
    delay_us(10);

    output_toggle(pin_a2);
   
     sayi=0;
  }
}

void main()
{

    setup_timer_1(T1_DISABLED);
    setup_timer_2(T2_DISABLED,0,1);
   
    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_tris_a(0x00);
    //set_tris_c(0b00001111);

while (true)
{
   
  //======================NTC============================
  set_adc_channel(4);  //AN4 analog kanal
  delay_us(20);
 // read_adc(ADC_START_ONLY);
 //=========================================================
 
   set_timer0(60);
 
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);

 //==================green LED ==========================
  output_high(pin_a4);
  // delay_ms(500);
 
 //==================Stein Hart for NTC ==========================
  R2 = R1 * (1023.0 / (float) value- 1.0);
  logR2 = log(R2);
  T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
  T = T - 273.15;
//==============================================================

 //==================  printf  ==========================
    delay_ms(2000);
    printf ("Temperature:%f\n\r ",T);
     
  }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 10, 2022 12:47 am     Reply with quote

Quote:
output_c(0x00);

When using standard i/o the statement above will set the TRIS to
be all outputs on PortC.

You want pins C0 and C3 to be inputs for the ADC function.

You can do this with:
Code:
 
output_c(0x00);
set_tris_c(0x09);  // Set pins C0 and C3 to be inputs
or
Code:

output_c(0x00);
output_float(PIN_C0);  // Set pin C0 as input
output_float(PIN_C3);  // Set pin C3 as input
Delfinss



Joined: 05 Apr 2022
Posts: 21

View user's profile Send private message

PostPosted: Tue May 10, 2022 2:03 am     Reply with quote

PCM programmer thank you for some previous info
now i get 1 value when reading 2 adc values
where is the problem? can you help me?

Code:
#include <16f1509.h>
#device ADC = 10  // 0 ile 1023

#include <math.h>

#use delay(internal=4000000)

#fuses INTRC_IO ,NOWDT ,NOPUT ,NOLVP ,NOPROTECT ,NODEBUG ,NOBROWNOUT ,NOWRT

#use  standard_io(C)
#use  standard_io(A)

#use rs232 (baud=9600, xmit=pin_B7,rcv=pin_B5, parity=N, stop=1)


int sayi=0;

unsigned int16 value,value2;


float R1 = 22000;
float logR2, R2, T;
float c1 = 0.5699030023e-03, c2 = 2.830523628e-04, c3 = -1.761701742e-07;

#int_timer0
void timer0_test()
 {
set_timer0(60);        // 50 ms   }

sayi++;
 if(sayi==40)     // 50*40 =2000 ms(2sn)
  {

    value=read_adc();
    delay_us(8);
   
   
    printf("NTC-ADC= %lu\n\r ", value);
    delay_us(10);

    output_high(pin_a2);
   }   
 if(sayi==80)  // 50*80 =4000 ms(4sn)
 {
     

    value2=read_adc();
    delay_us(8);
   
    printf("CO-ADC= %lu\n\r ", value2);
    delay_us(10);

    output_low(pin_a2);
    sayi=0;
 
}
}

void main()
{

    setup_timer_1(T1_DISABLED);
    setup_timer_2(T2_DISABLED,0,1);
   
   
    output_a(0x00); //red led
    output_c(0x00);
   
    set_tris_a(0x00);
    set_tris_c(0x09);  // C0 and C3 input
   
//output_float(PIN_C0);  // Set pin C0 as input
//output_float(PIN_C3);  // Set pin C3 as input
   
     setup_adc(adc_clock_div_8);
   
     setup_adc_ports(sAN4 |sAN7, VSS_VDD );
   
     setup_timer_0(T0_INTERNAL | T0_DIV_256);
     
   

while (true)
{
  //===================================
   set_adc_channel(4);  //AN4
   delay_us(20);
 // read_adc(ADC_START_ONLY);

 //=========================================================
 
   set_timer0(60);
 
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);
   
  //===========================================
 
   set_adc_channel(7);   //AN7 
   delay_us(20);
 // read_adc(ADC_START_ONLY);
 
 //=====================================
    set_timer0(60);
 
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);


 //==================green LED ==========================
  output_high(pin_a4);
  // delay_ms(500);
 
 //==================Stein Haavg_NTC rt for NTC ==========================
  R2 = R1 * (1023.0 / (float) value- 1.0);
  logR2 = log(R2);
  T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
  T = T - 273.15;
//==============================================================
//
 //==================  printf  ==========================
    delay_ms(2000);
    printf ("Temperature:%f\n\r ",T);
     
  }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 10, 2022 2:43 am     Reply with quote

Quote:
set_adc_channel(4); //AN4
delay_us(20);
// read_adc(ADC_START_ONLY);

//===========================================

set_timer0(60);

enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);

//===========================================

set_adc_channel(7); //AN7
delay_us(20);
// read_adc(ADC_START_ONLY);

In the code above you are setting the adc channel before reading
each channel, but you're not actually reading the channels.

In the code below, you have two adc reads, but you are not
setting the channel before each read. Because of this you're
going to read the same thing for each read.

Before you read the ADC you should set the desired channel and
wait at least 5 usec. Then read the ADC.
Quote:

if(sayi==10)
{

sum_NTC=0;
for (count_NTC=0; count_NTC<8; count_NTC++)

{
value=read_adc();
delay_us(100); // This delay does nothing. Delete it.
sum_NTC=value + sum_NTC;
}

avg_NTC= sum_NTC/=8;



output_high(pin_a2);
// delay_us(20);
}

if(sayi==20)
{

sum_CO=0;
for (count_NTC=0; count_NTC<8; count_NTC++)

{
onuf_co=read_adc();
delay_us(100); // This delay does nothing. Delete it.
sum_CO=onuf_co + sum_CO;
}

avg_CO= sum_CO/=8;

output_low(pin_a2);
//delay_us(20);
sayi=0;

}
Delfinss



Joined: 05 Apr 2022
Posts: 21

View user's profile Send private message

PostPosted: Tue May 10, 2022 3:24 am     Reply with quote

temperature:22.45

NTC-ADC = 339

CO-ADC = 1

I couldn't read channel 7 again


Quote:
#int_timer0
void timer0_test()
{
set_timer0(60); // 50 ms }

sayi++;
if(sayi==20) // 50*40 =2000 ms(2sn)
{
set_adc_channel(4); //AN4
delay_us(5);
value=read_adc();
delay_us(8); // for Tacq
// setup_adc( ADC_OFF );

printf("NTC-ADC= %lu\n\r ", value);
delay_us(10);

output_high(pin_a2);
}
if(sayi==40) // 50*80 =4000 ms(4sn)
{

set_adc_channel(7); //AN7
delay_us(5);
value2=read_adc();
delay_us(8); // for Tacq

printf("CO-ADC= %lu\n\r ", value2);
delay_us(10);

output_low(pin_a2);
sayi=0;
}
}
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 1, 2  Next
Page 1 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