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

adc [Solved]
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: 19538

View user's profile Send private message

PostPosted: Sun Mar 04, 2018 9:27 am     Reply with quote

I must admit I was assuming he was using his original code. The new code as you say has introduced a whole handful of new idiocy.... Sad
temtronic



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

View user's profile Send private message

PostPosted: Sun Mar 04, 2018 12:30 pm     Reply with quote

what should be done is a SIMPLE 5 line program to test the adc.

main()
start:
..select an adc iput
..read that input
..send data to PC
..delay for 1 second
..loop back to start:

This needs to be done BEFORE any 'fancy' program is tried.

Jay
NAVID



Joined: 14 Jun 2017
Posts: 28

View user's profile Send private message Send e-mail

PostPosted: Tue Mar 06, 2018 8:45 am     Reply with quote

Ttelmah wrote:
OK. I'd say that the error is just a fault with Proteus.
I've seen exactly the same on another chip, where the acquisition time is hardware programmable, and was set to MicroChip's recommendations, directly from the data sheet, yet Proteus perpetually complained about inadequate acquisition time.
This is a typical example of the sort of problem that Proteus has.....

However this does assume you have set the clock in Proteus that the simulation is using to 20MHz.


hi tnx for helping...but its not working correctly in real hardware....
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 06, 2018 8:49 am     Reply with quote

What is the problem when you run real hardware ?
You have never told us.

You told us the problem with Proteus is the error message:
"ADC conversion started before 'wait' time has expired following previous
conversion or channel change."

But real hardware won't give that message.

What is the problem when you run real hardware ?
NAVID



Joined: 14 Jun 2017
Posts: 28

View user's profile Send private message Send e-mail

PostPosted: Tue Mar 06, 2018 9:04 am     Reply with quote

PCM programmer wrote:
What is the problem when you run real hardware ?
You have never told us.

You told us the problem with Proteus is the error message:
"ADC conversion started before 'wait' time has expired following previous
conversion or channel change."

But real hardware won't give that message.

What is the problem when you run real hardware ?


i dont know how to explain!!
the problem is when i change adc input its not change on lcd
and some time (mostly) its like the micro is locked...
i write the code to show me power up time (sec2) but its not changing...
but i put led for flashing every second and its flashing correctly!!!
im confused....
temtronic



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

View user's profile Send private message

PostPosted: Tue Mar 06, 2018 11:02 am     Reply with quote

please post your current program...
maybe we can see the error.
NAVID



Joined: 14 Jun 2017
Posts: 28

View user's profile Send private message Send e-mail

PostPosted: Tue Mar 06, 2018 2:40 pm     Reply with quote

temtronic wrote:
please post your current program...
maybe we can see the error.


sure...tnx for help

Code:

#include <main2.h>
#include "lcd_flex.c"
unsigned int8 period,puls_forward=0,puls_back,step;
unsigned int16 sec2;
unsigned  int32 left=0,right=0;
 // int16 a0,a1;
#INT_RTCC
void  RTCC_isr(void)  //for 1 sec
{
if(period==100){

period=0;
//sec++;
sec2++;
output_toggle(pin_a5); // led for test
}
else period++;
set_timer0(60);
}

#INT_TIMER1
void  TIMER1_isr(void)
{

}

void main()
{
   setup_adc_ports(AN0_AN1_AN3);
   setup_adc(ADC_CLOCK_DIV_64);
 //  setup_adc(ADC_CLOCK_INTERNAL);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256|RTCC_8_bit);      //13.1 ms overflow
   setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);      //13.1 ms overflow


   enable_interrupts(INT_RTCC);
   enable_interrupts(INT_TIMER1);
   enable_interrupts(GLOBAL);
   int16 a0,a1;
   set_timer1(0);
   lcd_init();
   while(TRUE)
   {
   delay_ms(1);
    set_adc_channel(0);   //
    delay_ms(10);         //
    a0=read_adc();        //  i have problem here
                          //
    set_adc_channel(1);   //
    delay_ms(10);         //
    a1=read_adc();        //
   lcd_clear();
   lcd_gotoxy(1,1);
   printf(lcd_putc,"%d   %lu   %d   ",period,sec2,puls_forward);
   puls_forward=get_timer1();
   lcd_gotoxy(1,2);
   printf(lcd_putc,"%lu     %lu   ",a0,a1);
   
   ///////////////////////////////////////////////////////////// forward
    if(a0>500 && a1>500){
     if(sec2==570){
       for(step=0;step<30;step++){
       if(a0+20>a1){output_high(pin_d0); //left
                    output_low(pin_d1);
                    left++;}
       if(a1+20>a0){output_high(pin_d1); //right
                   output_low(pin_d0);}
                   right++;   }
       step=0;
       sec2=0;
    }}
    /////////////////////////////////////////////////////////// back
     else{
    puls_forward=get_timer1();
    output_low(pin_d0);
    output_low(pin_d1);
    set_timer1(0);
    do{
    if(right>left){output_high(pin_d0); //left
                   output_low(pin_d1);}
    if(right<left){output_high(pin_d1); //right
                   output_low(pin_d0);}             
    puls_back=get_timer1();
   
    }while(puls_back <= puls_forward);
    if(puls_back == puls_forward){
    left=0;
    right=0;
    set_timer1(0);
    puls_back =0;
    puls_forward=0;}
    } 
   
   
   
   
   
   
   
      //TODO: User Code
   }

}


and its .h:
Code:
#include <16F877A.h>
#device ADC=10
#use delay(crystal=20000000)


also i tried on Difference delay and Difference ADC clock source.
temtronic



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

View user's profile Send private message

PostPosted: Tue Mar 06, 2018 3:48 pm     Reply with quote

I'd try something like this....
Code:

void main()
{
   setup_adc_ports(AN0_AN1_AN3);
   setup_adc(ADC_CLOCK_DIV_64);
   int16 a0,a1;
   delay_ms(500); ///hardware delay to be sure LCD module is running
   lcd_init();
   while(TRUE)
   {
    set_adc_channel(0);   //
    delay_ms(1);         /// smaller delay !
    a0=read_adc();        //  i have problem here
                          //
    set_adc_channel(1);   //
    delay_ms(1);         /// smaller delay !
    a1=read_adc();        //
    lcd_clear();
    lcd_gotoxy(1,2);
    printf(lcd_putc,"%lu     %lu   ",a0,a1);
    delay_ms(1000); // delay for 1 second, so main 'loops' at 1hz rate
    }


Notice I've deleted EVERYTHING except 'read the adc and display results'

Also why bother with the 'header' ? Those 3 lines of code could be at the top of your program.

There may be a typo but it should work.
Jay
NAVID



Joined: 14 Jun 2017
Posts: 28

View user's profile Send private message Send e-mail

PostPosted: Wed Mar 07, 2018 8:56 am     Reply with quote

temtronic wrote:
I'd try something like this....
Code:

void main()
{
   setup_adc_ports(AN0_AN1_AN3);
   setup_adc(ADC_CLOCK_DIV_64);
   int16 a0,a1;
   delay_ms(500); ///hardware delay to be sure LCD module is running
   lcd_init();
   while(TRUE)
   {
    set_adc_channel(0);   //
    delay_ms(1);         /// smaller delay !
    a0=read_adc();        //  i have problem here
                          //
    set_adc_channel(1);   //
    delay_ms(1);         /// smaller delay !
    a1=read_adc();        //
    lcd_clear();
    lcd_gotoxy(1,2);
    printf(lcd_putc,"%lu     %lu   ",a0,a1);
    delay_ms(1000); // delay for 1 second, so main 'loops' at 1hz rate
    }


Notice I've deleted EVERYTHING except 'read the adc and display results'

Also why bother with the 'header' ? Those 3 lines of code could be at the top of your program.

There may be a typo but it should work.
Jay


ok tnx....i check it and notify the results
NAVID



Joined: 14 Jun 2017
Posts: 28

View user's profile Send private message Send e-mail

PostPosted: Fri Mar 09, 2018 2:45 am     Reply with quote

hi...i solved it!!!
the adc was work correctly when i increased the delay...but its not showed on lcd!!
so i wrote a function named "display" and call it every where in program to
refresh led every time... and its work correctly in simulation and real hardware...tnx everyone for help (:
this is my final code:
Code:

#include <main2.h>
#include "lcd_flex.c"
unsigned int8 period,puls_forward=0,puls_back,step;
unsigned int16 sec2=0;
unsigned  int32 left=0,right=0;
int16 a0,a1;
void display(void);
 // int16 a0,a1;
#INT_RTCC
void  RTCC_isr(void)  //for 1 sec
{
if(period==100){

period=0;
//sec++;
sec2++;
output_toggle(pin_a5); // led for test
}
else period++;
set_timer0(60);
}

#INT_TIMER1
void  TIMER1_isr(void)
{

}

void main()
{
   setup_adc_ports(AN0_AN1_AN3);
   setup_adc(ADC_CLOCK_DIV_64);
 //  setup_adc(ADC_CLOCK_INTERNAL);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256|RTCC_8_bit);      //13.1 ms overflow
   setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);      //13.1 ms overflow


   enable_interrupts(INT_RTCC);
   enable_interrupts(INT_TIMER1);
   enable_interrupts(GLOBAL);
   set_timer0(0);

   set_timer1(0);
   delay_ms(500);
   lcd_init();
 
   while(TRUE)
   {
   delay_ms(1);
    set_adc_channel(0);   
    delay_us(300);         
    a0=read_adc();       
                         
    set_adc_channel(1);   
    delay_us(300);         
    a1=read_adc();       
    display();

   ///////////////////////////////////////////////////////////// forward
    if(a0>500 && a1>500){
     if(sec2==570){
       for(step=0;step<30;step++){
       if(a0+20>a1){output_high(pin_d0); //left
                    output_low(pin_d1);
                    left++;
                    display();}
       if(a1+20>a0){output_high(pin_d1); //right
                   output_low(pin_d0);}
                   right++;
                   display();}
       step=0;
       sec2=0;
    }}
    /////////////////////////////////////////////////////////// back
     else{
     display();
    puls_forward=get_timer1();
    output_low(pin_d0);
    output_low(pin_d1);
    set_timer1(0);
    do{
    if(right>left){output_high(pin_d0); //left
                   output_low(pin_d1);}
    if(right<left){output_high(pin_d1); //right
                   output_low(pin_d0);}             
    puls_back=get_timer1();
     display();
    }while(puls_back <= puls_forward);
    if(puls_back == puls_forward){
    left=0;
    right=0;
    set_timer1(0);
    puls_back =0;
    puls_forward=0;
     display();}
    } 
   
   
   
   
   
   
   
      //TODO: User Code
   }

}

void display(void){
 
  set_adc_channel(0);   
    delay_us(30);         
    a0=read_adc();       
                         
    set_adc_channel(1);   
    delay_us(30);         
    a1=read_adc(); 
   lcd_gotoxy(1,1);
   printf(lcd_putc,"%d   %lu   %d   ",period,sec2,puls_forward);
   puls_forward=get_timer1();
   lcd_gotoxy(1,2);
   printf(lcd_putc,"%lu     %lu   ",a0,a1);
}
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