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

Need help sending 16-bit serial data

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



Joined: 29 Sep 2010
Posts: 73
Location: Brazil

View user's profile Send private message MSN Messenger

Need help sending 16-bit serial data
PostPosted: Fri Nov 04, 2011 6:05 pm     Reply with quote

8-bit shift error:
http://www.ccsinfo.com/forum/viewtopic.php?t=29118
The tip above works!

But I am having some trouble.

I need to read a potentiometer on PIC1 with ADC=10bits.

Send it over serial.

For test, i did a little code.

If I push a button, the PIC 1 reads the ad value, shift it 8 bit and send the high part and low part to the serial.

When I push the button on the first time, this works very well.

The second PIC get the to values and convert it again to a 10 bit value with the tip above by Ttelmah.

But, if I let the pot in the same position and push the button again, I am getting the value of 771 on the second PIC.

Look a small code:

Transmitter:

Code:
#include <16F877A.h>
#DEVICE ADC=10

#define WireTX PIN_c6
#define WireRX PIN_c7

#FUSES XT
#FUSES NOWDT                 
#FUSES PUT
#FUSES NOPROTECT

#use delay(clock=4000000)

#use rs232(baud=2400, xmit=WireTX, rcv=WireRX, ERRORS, STREAM=Wireless)

int8 c;
int8 valor_alto;
int8 valor_baixo;

void main()
{   
   setup_adc_ports(AN0);                             
   setup_adc(ADC_CLOCK_DIV_64);
   
   set_adc_channel(0);
   delay_us(20);
   
   while(1)
   {
      if(input(PIN_B0))
      {                     
         valor_alto = (read_adc()>>8);
         fprintf(Wireless,"%c", valor_alto);
         delay_ms(10);
         valor_baixo = (int)(read_adc());         
         fprintf(Wireless,"%c", valor_baixo);
         
         do
         {
         } while (input(pin_B0));
      }
   }   
}


Receiver:

Code:
#include <16F877A.h>

#define WireTX PIN_c6
#define WireRX PIN_c7

#FUSES XT
#FUSES NOWDT   
#FUSES PUT         
#FUSES NOPROTECT     

#use delay(clock=4000000)                 

#use rs232(baud=2400, xmit=WireTX, rcv=WireRX, ERRORS, STREAM=Wireless)

int8 n;
int8 vetor[3];
int16 resultado;
int1 data;
int8 termo_1;
int8 termo_2;
int16 resultado_1;

#int_RDA
void RDA_isr(void)
{
   vetor[n] = getc();
   if (n == 1)
   {
      data = 1;
      n = 0;
   }
   n++;
   
}

void main()

   enable_interrupts(INT_RDA);
   
   enable_interrupts(global);
   while(TRUE)
   { 
      if (data == 1)
      {           
         resultado = (((int16)vetor[0]<<8) | vetor[1]);         
         fprintf (Wireless, "\n\r%lu", resultado);         
         data++;
      }
   }
}


Output:

http://i.imgur.com/4ahJn.jpg

What is wrong?
temtronic



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

View user's profile Send private message

PostPosted: Fri Nov 04, 2011 8:36 pm     Reply with quote

two things I would do

1) remove the wireless units and go direct PIC to PIC, just to confirm it's not in the wireless hardware units.

2) setup a loop in the transmitter to send data from 0000 to ffff, incrementally, say every 1/2 second, to verify it's not something in your code or maybe the input to the ADC. Saying you get 771 the second time is not enough info. What was the first reading? How stable is the ADC Vref, the ADC power supply, EMI ? etc. One possible reason for the 'bad' reading is a lowering of the ADC supply due to insufficient capacity of the power supply to recover from the wireless transmitter being active. It could be a bad ground, not enough decoupling, etc. Lots of areas so you need to breakdown the project to find the cause.

By sending known data at a regular rate PIC to PIC you can eliminate most of the hardware and any 'funny' ADC problems. If it fails now, then it's code issues.
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