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

PIC18F46K22 reading ADC value

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



Joined: 06 Nov 2021
Posts: 92

View user's profile Send private message

PIC18F46K22 reading ADC value
PostPosted: Thu Jan 20, 2022 1:31 pm     Reply with quote

Code:
#include <TT.h>

#use delay (clock=4000000)
#use fast_io(a)
#use fast_io(c)
#include <HDM64GS12.c>
#include <graphics.c>
#define display_1 pin_B6
#define display_2 pin_B7
#define button pin_A1
#define secme pin_B3
#define slayt pin_A2
int32 c;
const char imagine[]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0xC0, 0xC0, 0xC0, 0xC0, 0xE0, 0xE0, 0xF0,
0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF0, 0xF0, 0xF0,
0xF8, 0xF0, 0xF0, 0xF8, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xE0, 0xE0, 0xE0,
0xE0, 0xE0, 0xE0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8,
0xF8, 0xFC, 0x3E, 0x3E, 0x3F, 0x3F, 0x1F, 0x1F, 0x1F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F,
0x0F, 0x0F, 0x07, 0x07, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x1F, 0x0F, 0x1F, 0x1F, 0x1F,
0x1F, 0x1F, 0x1F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F,
0x0F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x3F, 0x1F, 0x1F, 0x3F, 0x3F, 0x3E, 0x7E, 0xFE, 0xFC, 0xF8,
0xF8, 0xF0, 0xE0, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x3F, 0x7F, 0xFF, 0xFF, 0xFF,
0x3F, 0x0C, 0x00, 0x00, 0x00, 0xC0, 0x40, 0x40, 0x40, 0x60, 0x60, 0x40, 0x60, 0x60, 0x60, 0x60,
0x60, 0x60, 0x60, 0x60, 0xC0, 0xC0, 0xC0, 0x40, 0xC0, 0xC0, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x60, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0,
0xE0, 0xE0, 0x60, 0x20, 0x60, 0x60, 0x60, 0x60, 0x60, 0x40, 0xC0, 0x80, 0x00, 0x07, 0x07, 0x1F,
0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x9E, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x0D, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x0F, 0x03,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 0x03,
0x03, 0x03, 0x03, 0x01, 0x00, 0x01, 0x01, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03,
0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x03, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x43, 0x07, 0x0D, 0x08, 0x28, 0x10, 0x00, 0x00, 0x00, 0x01, 0x01, 0x06, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x48, 0x68, 0x6A, 0x6A, 0x6E, 0x2C, 0x28, 0x00, 0x54,
0x0C, 0x58, 0x68, 0x6A, 0x6E, 0x6E, 0x68, 0x68, 0x48, 0x48, 0x50, 0x10, 0x10, 0x00, 0x00, 0x00,
0x00, 0x00, 0x80, 0x80, 0x40, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x02, 0x02, 0x03, 0x06, 0x0E, 0x0E, 0x0E,
0x1E, 0x1C, 0x3C, 0x3C, 0x3C, 0x78, 0x78, 0x78, 0x7C, 0x78, 0xF8, 0xF0, 0xE0, 0xF1, 0xE1, 0xE1,
0xE3, 0xE1, 0xF1, 0x71, 0x61, 0x70, 0x68, 0x70, 0x78, 0x78, 0x78, 0x78, 0x38, 0x3C, 0x3C, 0x1E,
0x1E, 0x0E, 0x0E, 0x06, 0x07, 0x02, 0x01, 0x00, 0x01, 0x00, 0x80, 0x70, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x00, 0x40,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x80, 0xC0, 0x60, 0x20, 0x30, 0x1C, 0x06, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0E, 0x0C,
0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x07, 0x0F, 0x0F, 0x0F, 0x0E, 0x0C,
0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
int a=0;
int i,j,k;
int32 sayac;
char birler=0, onlar=0;
const int digit[10]={0x7E,0x0C,0xB6,0x9E,0xCC,0xDA,0xFA,0x0F,0xFE,0xDE};
long int adc_value;
#int_timer1
void timer()
{
   if(read_adc()>=4369 && read_adc()<8738){{
   set_timer1(3036);
    a++;}
    if(a>=2){
    int x=0;
      int y=0;

glcd_init(on);
   for(i=0;i<64;i+=8)
      {   
         for(j=0;j<128;j++)
         {
            for(k=0;k<8;k++)
            {
               if(bit_test(imagine[sayac],k))
               {
                  glcd_pixel(x+j,y+i+k,ON);
               }
            }
            sayac++;
         }
      }
 sayac=0;
   a=0;
    }
   
   }
}
void main()
{
    set_tris_c(0x01);
    set_tris_a(0xFF);

        while(1){
        while(!input(button)){
 
 setup_adc(ADC_CLOCK_DIV_32); 
            setup_adc_ports(sAN0);   
            c=read_adc();        // Here I read "c'' value
//!             delay_us(20);   
             output_low(display_1);
             output_low(display_2);
   
    adc_value=read_adc();   // 0-1023
     adc_value=(adc_value/(4360)); //1023/255*2.5=10
     
//!     
     birler=(adc_value%10);
     onlar=(adc_value/10);
     
   
          output_high(display_2);  // 2. display aktif durumda
         output_c(digit[birler]); // displaye bilgi gönderiliyor
         delay_ms(5);             // gecikme veriliyor
         output_low(display_2);   // 2. display pasif durumda
         output_high(display_1);  // 1. display aktif durumda
         output_c(digit[onlar]);  // displaye bilgi gönderiliyor
         delay_ms(5);             // gecikme veriliyor
         output_low(display_1);   // 1. display pasif durumda
   

        }
       
        if(input(button)){

 setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
 set_timer1(read_adc());//0.5s tek kesme
 enable_interrupts(int_timer1);
 enable_interrupts(GLOBAL);
 
        }
       
        }

 


}


Greetings, why I haven't measured max adc value(16 bit) as 65535 properly? I see 65472.When it is turned in 10 bit, I see '0' and'1023'.
gaugeguy



Joined: 05 Apr 2011
Posts: 303

View user's profile Send private message

PostPosted: Thu Jan 20, 2022 4:04 pm     Reply with quote

Because you do not have a 16 bit A/D. You have a 10 bit A/D that can be left justified or right justified.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jan 20, 2022 9:52 pm     Reply with quote

In other words, 1023 x 64 = 65472.
Khansokhua



Joined: 06 Nov 2021
Posts: 92

View user's profile Send private message

PostPosted: Sat Jan 22, 2022 6:00 pm     Reply with quote

1111111111000000=65472
0000001111111111=1023
temtronic



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

View user's profile Send private message

PostPosted: Sun Jan 23, 2022 6:52 am     Reply with quote

right..
this code..
adc_value=(adc_value/(4360));

probably won't work as expected when the ADC result is 'right justified'.
Khansokhua



Joined: 06 Nov 2021
Posts: 92

View user's profile Send private message

timer1
PostPosted: Sun Jan 23, 2022 7:16 pm     Reply with quote

Code:

#include <TT.h>

#use delay (clock=4000000)
#use fast_io(a)
#use fast_io(c)
#include <HDM64GS12.c>
#include <graphics.c>
#define display_1 pin_B6
#define display_2 pin_B7
#define button pin_A1
#define secme pin_B3
#define slayt pin_A2
int32 c;
const char imagine[]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0xC0, 0xC0, 0xC0, 0xC0, 0xE0, 0xE0, 0xF0,
0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF0, 0xF0, 0xF0,
0xF8, 0xF0, 0xF0, 0xF8, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xE0, 0xE0, 0xE0,
0xE0, 0xE0, 0xE0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8,
0xF8, 0xFC, 0x3E, 0x3E, 0x3F, 0x3F, 0x1F, 0x1F, 0x1F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F,
0x0F, 0x0F, 0x07, 0x07, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x1F, 0x0F, 0x1F, 0x1F, 0x1F,
0x1F, 0x1F, 0x1F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F,
0x0F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x3F, 0x1F, 0x1F, 0x3F, 0x3F, 0x3E, 0x7E, 0xFE, 0xFC, 0xF8,
0xF8, 0xF0, 0xE0, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x3F, 0x7F, 0xFF, 0xFF, 0xFF,
0x3F, 0x0C, 0x00, 0x00, 0x00, 0xC0, 0x40, 0x40, 0x40, 0x60, 0x60, 0x40, 0x60, 0x60, 0x60, 0x60,
0x60, 0x60, 0x60, 0x60, 0xC0, 0xC0, 0xC0, 0x40, 0xC0, 0xC0, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x60, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0,
0xE0, 0xE0, 0x60, 0x20, 0x60, 0x60, 0x60, 0x60, 0x60, 0x40, 0xC0, 0x80, 0x00, 0x07, 0x07, 0x1F,
0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x9E, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x0D, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x0F, 0x03,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 0x03,
0x03, 0x03, 0x03, 0x01, 0x00, 0x01, 0x01, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03,
0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x03, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x43, 0x07, 0x0D, 0x08, 0x28, 0x10, 0x00, 0x00, 0x00, 0x01, 0x01, 0x06, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x48, 0x68, 0x6A, 0x6A, 0x6E, 0x2C, 0x28, 0x00, 0x54,
0x0C, 0x58, 0x68, 0x6A, 0x6E, 0x6E, 0x68, 0x68, 0x48, 0x48, 0x50, 0x10, 0x10, 0x00, 0x00, 0x00,
0x00, 0x00, 0x80, 0x80, 0x40, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x02, 0x02, 0x03, 0x06, 0x0E, 0x0E, 0x0E,
0x1E, 0x1C, 0x3C, 0x3C, 0x3C, 0x78, 0x78, 0x78, 0x7C, 0x78, 0xF8, 0xF0, 0xE0, 0xF1, 0xE1, 0xE1,
0xE3, 0xE1, 0xF1, 0x71, 0x61, 0x70, 0x68, 0x70, 0x78, 0x78, 0x78, 0x78, 0x38, 0x3C, 0x3C, 0x1E,
0x1E, 0x0E, 0x0E, 0x06, 0x07, 0x02, 0x01, 0x00, 0x01, 0x00, 0x80, 0x70, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x00, 0x40,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x80, 0xC0, 0x60, 0x20, 0x30, 0x1C, 0x06, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0E, 0x0C,
0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x07, 0x0F, 0x0F, 0x0F, 0x0E, 0x0C,
0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
int a=0;
int i,j,k;
int32 sayac;
char birler=0, onlar=0;
const int digit[10]={0x7E,0x0C,0xB6,0x9E,0xCC,0xDA,0xFA,0x0F,0xFE,0xDE};
long int adc_value;
#int_timer1
void timer()
{
   if(read_adc()>=4369 && read_adc()<8738){{
   set_timer1(3036);
    a++;}
    if(a>=30){
    int x=0;
      int y=0;

glcd_init(on);
   for(i=0;i<64;i+=8)
      {   
         for(j=0;j<128;j++)
         {
            for(k=0;k<8;k++)
            {
               if(bit_test(imagine[sayac],k))
               {
                  glcd_pixel(x+j,y+i+k,ON);
               }
            }
            sayac++;
         }
      }
 sayac=0;
   a=0;
    }
   
   }
}
void main()
{
    set_tris_c(0x01);
    set_tris_a(0xFF);

        while(1){
         if(input(button)){

 setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
 set_timer1(3036);//0.5s tek kesme
 enable_interrupts(int_timer1);
 enable_interrupts(GLOBAL);
 
        }
        while(!input(button)){
           
 setup_adc(ADC_CLOCK_DIV_32); 
            setup_adc_ports(sAN0);   
            c=read_adc();
             delay_us(20);   
             output_low(display_1);
             output_low(display_2);
   
    adc_value=read_adc(); 
     adc_value=(adc_value/(4360));
     
//!     
     birler=(adc_value%10);
     onlar=(adc_value/10);
     
   
          output_high(display_2);  // 2. display aktif durumda
         output_c(digit[birler]); // displaye bilgi gönderiliyor
         delay_ms(5);             // gecikme veriliyor
         output_low(display_2);   // 2. display pasif durumda
         output_high(display_1);  // 1. display aktif durumda
         output_c(digit[onlar]);  // displaye bilgi gönderiliyor
         delay_ms(5);             // gecikme veriliyor
         output_low(display_1);   // 1. display pasif durumda
   

        }
       
       
    }

}

Why the interrupt timing is 15.532 second.It supposed to be 15.032
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: timer1
PostPosted: Sun Jan 23, 2022 7:54 pm     Reply with quote

Khansokhua wrote:

Why the interrupt timing is 15.532 second. It supposed to be 15.032

I told you the answer in your previous thread. It's because your
glcd code in the Timer1 interrupt routine takes a long time.
See the link below:
http://www.ccsinfo.com/forum/viewtopic.php?t=59619&start=1
Ttelmah



Joined: 11 Mar 2010
Posts: 19535

View user's profile Send private message

PostPosted: Sun Jan 23, 2022 11:19 pm     Reply with quote

Also, the two reads of the ADC. It would take half the time to read this
once. Since these happen _before_ the timer value is set, they add
a lot of time. The ADC takes 32 cycles of the CPU clock per ADC cycle.
It takes 13 cycles of this to read the ADC. So 104 instruction times
for the read. Two reads, gives over 200 cycles of delay here. In fact it
will give wrong values by reading twice in the same line of code. There
needs to be Tacq between successive reads of the ADC. This is badly
wrong...

Several things can be done to help here.
First at 4MHz, the optimum ADC divisor is FOSC/4, not FOSC/32. This
will make the readings 8* faster., so CLK_DIV_4.
If you look in the data sheet at 4MHz, the /32 selection has the note '3',
which says "for faster conversion times the selection of another clock
source is recommended'...
Then, you really should not be reading the ADC both inside and outside the
interrupt. Take the reading outside the interrupt only, and store the readings
when they are done into a global variable. Use this in the interrupt.
Also set the ADC to automatically perform the acquisition.
ADC_TAD_MUL_8. in the setup. This then ensured that Tacq is correctly
allowed whenever an acquisition is done. You may well find with these
changes the ADC is a lot more reliable.
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