View previous topic :: View next topic |
Author |
Message |
NAVID
Joined: 14 Jun 2017 Posts: 28
|
adc [Solved] |
Posted: Fri Mar 02, 2018 7:56 am |
|
|
hi
I want to use adc on 16f877a and write a code include timer 1 and 2 and adc....
When i compile the code its compile with no error but in simulation i have many errors like this:
[PIC16 ADC] PC=0x02D4. ADC conversion started before 'wait' time has expired following previous conversion or channel change. [U1]
and its repeat every second....i dont know why....this is my code...pls help and tnx
Code: |
#include <main2.h>
#include "lcd_flex.c"
int8 period,puls_forward=0,puls_back,step;
int16 sec2;
unsigned int32 left=0,right=0;
#INT_RTCC
void RTCC_isr(void)
{
if(period==100){
period=0;
//sec++;
sec2++;
}
else period++;
set_timer0(60);
}
#INT_TIMER1
void TIMER1_isr(void)
{
}
void main()
{
setup_adc_ports(AN0_AN1_AN3);
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)
{
set_adc_channel(0);
delay_us(20);
a0=read_adc();
set_adc_channel(1);
delay_us(20);
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);
///////////////////////////////////////////////////////////// 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
}
} |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Fri Mar 02, 2018 9:27 am |
|
|
Which 'simulator'? If 'Proteus/ISIS', please read PIC101 sticky. It is well known to NOT simulate PICs 100% properly.
Now as a general comment, you should add comments ( //what this line does...) to almost every line of code. While you may know what variables are for and how the 'math' works, we don't.
Also check the ADC section of the datasheet. Selecting setup_adc(ADC_CLOCK_INTERNAL); is usually reserved for when SOME PICs are put to sleep. |
|
|
NAVID
Joined: 14 Jun 2017 Posts: 28
|
|
Posted: Fri Mar 02, 2018 10:56 am |
|
|
temtronic wrote: | Which 'simulator'? If 'Proteus/ISIS', please read PIC101 sticky. It is well known to NOT simulate PICs 100% properly.
Now as a general comment, you should add comments ( //what this line does...) to almost every line of code. While you may know what variables are for and how the 'math' works, we don't.
Also check the ADC section of the datasheet. Selecting setup_adc(ADC_CLOCK_INTERNAL); is usually reserved for when SOME PICs are put to sleep. |
hi
yes its Proteus and I test also in real hardware and it has problem....its not simulation problem....
and what is PIC101 sticky??? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Fri Mar 02, 2018 11:50 am |
|
|
At the top of the discussion list, there are a few 'stickies'. These are postings that everyone should read. They are 'answers' to very common questions.
As for the hardware not working, does it run a '1HZ LED ' program properly ?
This program should be the first one you code/compile and run. It should flash an LED at a 1Hz rate. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Mar 02, 2018 12:00 pm |
|
|
Quote: |
set_adc_channel(0);
delay_us(20);
a0=read_adc();
|
You have the correct 20 usec delay for the 16F877A. Therefore, the
most likely reason is your simulator clock is not set to the same
frequency as your #use delay(clock=xxxx).
You didn't post your main2.h file, so we don't know what your #use delay() is. |
|
|
NAVID
Joined: 14 Jun 2017 Posts: 28
|
|
Posted: Fri Mar 02, 2018 1:45 pm |
|
|
PCM programmer wrote: | Quote: |
set_adc_channel(0);
delay_us(20);
a0=read_adc();
|
You have the correct 20 usec delay for the 16F877A. Therefore, the
most likely reason is your simulator clock is not set to the same
frequency as your #use delay(clock=xxxx).
You didn't post your main2.h file, so we don't know what your #use delay() is. |
oh sorry once i think maybe if i increase delay it should be OK but its not then i change it to 10....like ccs help
and its header:
Code: | #include <16F877A.h>
#device ADC=10
#use delay(crystal=20000000) |
|
|
|
NAVID
Joined: 14 Jun 2017 Posts: 28
|
|
Posted: Fri Mar 02, 2018 2:09 pm |
|
|
temtronic wrote: | At the top of the discussion list, there are a few 'stickies'. These are postings that everyone should read.they are 'answers' to very common questions.
As for the hardware not working, does it run a '1HZ LED ' program properly ?
This program should be the first one you code/compile and run. It should flash an LED at a 1Hz rate. |
aha you mean i write a code 1hz led and if it flashing so the program is correct???
i am sure program have some problem with adc....cause when i disable it everything is ok.... |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Fri Mar 02, 2018 7:58 pm |
|
|
The testing using a 1Hz LED program will confirm that your PCB and PIC are operating. You should see the LED flash at a nominal 1 Hz rate. If it does, you KNOW the PIC is good and can proceed with more complicated programs. If it doesn't, you need to find the error (hardware or software) and correct as required.
re:
your code...
Quote: |
setup_adc(ADC_CLOCK_INTERNAL); |
Please see section 11 (adc) of the data sheet, specifically Table 11-1, under section 11.2.
ADC_CLOCK_INTERNAL is NOT a valid selection for a PIC running at 20 MHz clock speed.
Jay |
|
|
NAVID
Joined: 14 Jun 2017 Posts: 28
|
|
Posted: Sat Mar 03, 2018 12:57 pm |
|
|
temtronic wrote: | The testing using a 1Hz LED program will confirm that your PCB and PIC are operating. You should see the LED flash at a nominal 1 Hz rate. If it does, you KNOW the PIC is good and can proceed with more complicated programs. If it doesn't, you need to find the error (hardware or software) and correct as required.
re:
your code...
Quote: |
setup_adc(ADC_CLOCK_INTERNAL); |
Please see section 11 (adc) of the data sheet, specifically Table 11-1, under section 11.2.
ADC_CLOCK_INTERNAL is NOT a valid selection for a PIC running at 20 MHz clock speed.
Jay |
hi....i write " output_toggle(pin_a5);" in #int_rtcc so led flashing by 1 hz...and its work correctly
and also i change the "setup_adc(ADC_CLOCK_INTERNAL);" to " setup_adc(ADC_CLOCK_DIV_64);" but still i have same problem...
what is the problem?? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Sat Mar 03, 2018 3:01 pm |
|
|
please post your current program that fails... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Sun Mar 04, 2018 1:50 am |
|
|
and post the main.h (which is vital), and tell us what crystal is on the actual chip. |
|
|
NAVID
Joined: 14 Jun 2017 Posts: 28
|
|
Posted: Sun Mar 04, 2018 3:31 am |
|
|
temtronic wrote: | please post your current program that fails... |
hi and tnx for your helping
Code: | #include <main2.h>
#include "lcd_flex.c"
int8 period,puls_forward=0,puls_back,step;
int16 sec2;
unsigned int32 left=0,right=0;
// int16 a0,a1;
#INT_RTCC
void RTCC_isr(void)
{
if(period==100){
/* set_adc_channel(0);
delay_us(0);
a0=read_adc(20);
set_adc_channel(1);
delay_us(20);
a1=read_adc(); */
period=0;
//sec++;
sec2++;
output_toggle(pin_a5);
}
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)
{
set_adc_channel(0); //
delay_us(10); //
a0=read_adc(); // i have problem here
//
set_adc_channel(1); //
delay_us(10); //
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);
///////////////////////////////////////////////////////////// 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
}
} |
Last edited by NAVID on Sun Mar 04, 2018 3:35 am; edited 1 time in total |
|
|
NAVID
Joined: 14 Jun 2017 Posts: 28
|
|
Posted: Sun Mar 04, 2018 3:32 am |
|
|
Ttelmah wrote: | and post the main.h (which is vital), and tell us what crystal is on the actual chip. |
#include <16F877A.h>
#device ADC=10
#use delay(crystal=20000000)
and i use 20mhz crystal |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Sun Mar 04, 2018 4:41 am |
|
|
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. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Mar 04, 2018 7:26 am |
|
|
Right, but he's not even following the 16F877A data sheet.
Here, he's tossing numbers around and not even putting them in the
correct place:
Quote: |
/* set_adc_channel(0);
delay_us(0);
a0=read_adc(20);
set_adc_channel(1);
delay_us(20);
a1=read_adc(); */ |
Here, he is violating the delay specification given in the 16F877A data sheet:
Quote: | set_adc_channel(0); //
delay_us(10); //
a0=read_adc(); // i have problem here
//
set_adc_channel(1); //
delay_us(10); //
a1=read_adc(); //
|
The 16F877A data sheet says the delay should be 20 usec:
Quote: |
EQUATION 11-1: ACQUISITION TIME
TACQ =
Amplifier Settling Time + Hold Capacitor Charging Time + Temperature Coefficient
= TAMP + TC + TCOFF
= 2us + TC + [(Temperature – 25°C)(0.05 us/°C)]
TC = CHOLD (RIC + RSS + RS) In(1/2047)
= - 120 pF (1k + 7k + 10k) In(0.0004885)
= 16.47us
TACQ = 2us + 16.47us + [(50°C – 25°C)(0.05 us/°C)
= 19.47us |
|
|
|
|