|
|
View previous topic :: View next topic |
Author |
Message |
lychanhuy
Joined: 14 Oct 2011 Posts: 2
|
Transmit adc data from PIC to PIC |
Posted: Fri Oct 14, 2011 11:22 pm |
|
|
First, I'd like to say hello to all the members in this forum. I really need your help.
I have a project, in this one I must control 2 RC servo with 1 PIC16F877A. I'd like to read 2 channel ADC, and use it to create PWM pulses for controlling Servo. But when I simulate it on Proteus, the warning of adc happens: "adc conversion started before wait time has expired following previous conversion or channel exchange. This is the code CCS
Code: |
#include <16F877A.h>
#device *=16 adc=10
#fuses NOWDT, HS, NOPUT, NOPROTECT
#use delay(clock=20000000)
int16 value1,value2;
void adc_init1()
{
setup_adc(ADC_CLOCK_INTERNAL );
setup_adc_ports(ALL_ANALOG);
set_ADC_channel( 0 );
delay_us(100);
set_ADC_channel( 1 );
delay_us(100);
}
void adc_init2()
{
setup_adc(ADC_CLOCK_INTERNAL );
setup_adc_ports(ALL_ANALOG);
set_ADC_channel( 1 );
delay_us(100);
}
void servo1()
{
value1=Read_ADC();
delay_us(10);
value1 = value1 + 1000;
output_high(pin_B0);
delay_us(value1);
output_low(pin_B0);
delay_us(value1);
}
void servo2()
{
value2=Read_ADC();
delay_us(10);
value2 = value2 + 1000;
output_high(pin_B1);
delay_us(value2);
output_low(pin_B1);
delay_us(value2);
}
void main()
{
while(1)
{
adc_init1();
servo1();
adc_init2();
delay_us(10);
}
}
|
The next problem is when I try to transmit the adc data from this PIC to the other one, using UART Tx-Rx Pin RC6-RC7, the data is missing and not exact. Example: when I control one servo with 1 PIC, it's ok, but when I use slave PIC to control through UART, the control is wrong like minimize the width transmitted from Master PIC, and this causes wrong control signal.
I just use putc(),and getc() to Transceive.
This is the code I wrote:
Code: |
//Master
#include <16F877A.h>
#device *=16 adc=8
#fuses NOWDT, HS, NOPUT, NOPROTECT
#use delay(clock=20000000)
#use rs232(baud = 9600, parity = N , Xmit = Pin_c6,rcv = pin_c7)
int16 value,goi;
void main()
{
setup_adc(ADC_CLOCK_INTERNAL );
setup_adc_ports( ALL_ANALOG));
set_ADC_channel( 0 );
delay_us(10);
set_tris_C(255);
while(true)
{
value=Read_ADC();
delay_us(10);
goi=value*4+1000;
putc(goi);
}
}
|
Code: |
//Slave
#include <16F877A.h>
#device *=16 adc=8
#fuses NOWDT, HS, NOPUT, NOPROTECT
#use delay(clock=20000000)
#use rs232(baud = 9600, parity = N , Xmit = Pin_c6,rcv = pin_c7) //khoi tao usart
int16 a,b;
void main()
{
set_tris_C(255);
set_tris_B(0);
while(true)
{
a=getch();
b=a;
//if (kbhit())
output_high(pin_B0);
delay_us(b);
output_low(pin_B0);
delay_us(b);}
}
|
And Sorry for my bad english.
Thanks for reading my topic, and I'll be grateful if you guys can help me. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Sat Oct 15, 2011 5:12 am |
|
|
second problem first...
You've taken the 8 bit value( a BYTE) from the ADC result ,multiplied by 4 and added 1000 to it.Playing computer with a pen and pencil makes the result 'goi' = (255*4)+1000 = (1020+1000)= 2020.
Standard serial communications(aka RS232,aka comports in PC) including the PIC UART deal with 8 bit data( BYTEs).You're attempting to send 16 bit data, can't be done! You have to break it down into 2 bytes and send them..on the receive PIC you'll have to make the 2 bytes into a word(16 bits).
Also be sure to add 'errors' to the use rs232(....) options, otherwise you'll have the UART 'stall' out.
As for your error in Proteus, since I only work in the real world and not simulations, I can only tell you that Proteus is known to be full of bugs,errors, and bad DRCs. You should look at the fine example code that CCS provides in the 'examples' folders. They show how to use almost every function of PICs ! |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Sat Oct 15, 2011 6:08 am |
|
|
Also you don't need to go through all that to simply set up and read the ADCs. Do it something like this:
Code: |
setup_adc(ADC_CLOCK_INTERNAL );
setup_adc_ports(ALL_ANALOG);
....
while (1)
{
set_ADC_channel( 0 );
delay_us(20);
value0=Read_ADC();
...
set_ADC_channel( 1 );
delay_us(20);
value1=Read_ADC();
value1 = value1 + 1000
...
}
|
_________________ Google and Forum Search are some of your best tools!!!! |
|
|
lychanhuy
Joined: 14 Oct 2011 Posts: 2
|
|
Posted: Fri Oct 21, 2011 8:52 pm |
|
|
Thank you very much. I am sorry for not checking my topic. I was busy. |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Sat Oct 22, 2011 6:23 am |
|
|
Code: | void adc_init1()
{
setup_adc(ADC_CLOCK_INTERNAL );
setup_adc_ports(ALL_ANALOG);
set_ADC_channel( 0 ); <-------------- ok so far....
delay_us(100);
set_ADC_channel( 1 ); <---------------------you are overwriting the first one!
delay_us(100);
} |
Both your setup functions set up the same channel....
G _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
|
|
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
|