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 support@ccsinfo.com

Remote data transfer TWS-434 and RWS-434
Goto page Previous  1, 2, 3
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
sergio3214



Joined: 15 Mar 2017
Posts: 25
Location: brasil

View user's profile Send private message

Re: TWS434 16F628
PostPosted: Thu Mar 30, 2017 4:56 pm     Reply with quote

ze.vana wrote:
Hello people I was also looking for a code in C that could translated that such Basic Stamps.
And I also wanted to leave my contribution, maybe this serves as base for others.
Then based on what was published here I created other code using PICS 2 16F628. I noticed that the largest problem in a serial communication is to synchronize the RECEIVER with the transmitter. This code uses the Interruption RDA (RS232 Interruption ) in the Receiver and a hardware Reset: a 1uF CAPACITOR connected from pin6 RB0 to pin4 MCLR and 10K resistor from MCLR to Vdd (Note: soft reset "resetcpu()" does not work) . When e Transmitter or Receiver is turned off and ON, data can come aleatory; CPU restarts and get normal sequence, it takes milliseconds. I've tested this on Proteus and also by hardware, it really works!
Transmitter
Code:
 
////////////434trans628.c///////////////////////////
#include <16f628a.h>
#fuses INTRC_IO, NOLVP, NOWDT, PUT, BROWNOUT
#use delay (clock=4M)// No crystal is needed
#use rs232(baud=2400,XMIT=PIN_B2,RCV=PIN_B1,bits=8)
#define a2 (pin_a0)//pin 17
#define a3 (pin_a1)//pin 18
  char junk;
  char synch;
  int dat,dat2;
byte send[4];
 // int8 div;
void send_dat(void)
{
  int i;
  for (i=0; i<4; ++i)//send 4 data
  {                 //0 is the first 3 the last
  putc(send[i]);
  }
}
void main(void)
{
  set_tris_a(0x00);// All output 
  set_tris_b(0b00000010); //RB1 RB2  Rx/tx)
  junk = 126;
  synch = 'A';//decimal 65
  dat   = 50; dat2=60;
  while(true)
   {
   send[0]=junk; send[1]=synch; send[2]=dat; send[3]=dat2;
   output_toggle a2;
   send_dat();
   // delay_ms(100);//no delays
   }
  }
Receiver

//////////////434receiv628.c//////////////////////
#include <16f628a.h> //<16F876a.h>
#fuses INTRC_IO, NOLVP, NOWDT, PUT, BROWNOUT
#use delay (clock=4M)// No crystal is needed
#use rs232(baud=2400,XMIT=PIN_b2,RCV=PIN_b1,bits=8)
#define a2 (pin_a2)
#define a3 (pin_a3)
#define reset (pin_b0)
char junk;
char synch;
int dat,dat2;
byte receiv[4];
#INT_RDA // RS232 Interrupt
void rda_isr()
{
int i;
for (i=0; i<4; ++i)//receive 4 data
{ //0 is the first
receiv[i]=getc();
OUTPUT_TOGGLE A2;//knowing data are receiving
}
junk=receiv[0]; synch=receiv[1]; dat=receiv[2];dat2=receiv[3] ;
}

void main()
{
enable_interrupts(global);
enable_interrupts(INT_rda);// RS232 Interrpt
set_tris_a(0x00);// All output
set_tris_b(0b00000010);//reset (pin_b0)/ XMIT=PIN_B2,RCV=PIN_B1
output_high reset;//---__----
while (true)
{
if(synch== 'A') {//led A3 lights up when data are Ok then you don't need
//using Hyperterminal if you want.
output_high a3; printf("J:%u S1:%c D:%u D%u OK\n\r",junk,synch,dat,dat2);
}
else {
output_low a3; printf("J:%u S1:%u D:%u D%u ER\n\r",junk,synch,dat,dat2);
}//If data come aleatory
if(dat==126 || dat==65 ||dat2==126|| junk==65 ) output_low reset;//65 is the same 'A'.
//connect a 1uF capacitor between B0 and MCLR. If there are //incorrect data
}//reset CPU
}
Hyperterminal

    J:223 S1:218 D:250 D239 ER
    J:223 S1:218 D:250 D239 ER
    J:223 S1:218 D:250 D255 ER
    J:60 S1
    :65 D:50 D60 ER
    J:126 S1:A D:50 D60 OK
    J:126 S1:A D:50 D60 OK
    J:126 S1:A D:50 D60 OK
    J:126 S1:A D:50 D60 OK
    J:126 S1:A D:50 D60 OK
    J:126 S1:A D:50 D60 OK
    J:126 S1:A D:50 D60 OK
    J:126 S1:A D:50 D60 OK
    J:126 S1:A D:50 D60 OK
    J:126 S1:A D:50 D60 OK
    J:126 S1:A D:50 D60 OK
    J:126 S1:A D:50 D60 OK
    J:126 S1:A D:50 D60 OK
    J:126 S1:A D:50 D60 OK
    J:126 S1:A D:50 D60 OK
    J:126 S1:A D:50 D60 OK


I do not understand your code. Could you send me the connection schematic ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19255

View user's profile Send private message

PostPosted: Fri Mar 31, 2017 12:14 am     Reply with quote

Don't bother. It's not code you should use. It's foul....

INT_RDA, means _one_ character is ready to be received. Not four.
The code will hang if there is any lost data.

The examples from the manufacturer use an encoder/decoder chip, that adds a 'pre-amble' byte, sync, address, data, and a checksum, all designed to ensure that the data will get through reliably, and can be rejected if there is any noise in the link. The radios you have _require_ this to be anything approaching reliable.

If you insist on using your radios, then you are going to have to add a similar design. The code is not going to just be able to use putc/getc, but needs to wait for the carrier to be established, and only then start the RS232 receive. Then will need to add error checking to the data sent and test for this at the receive. Complex to do even remotely well.
This is why we have said if you want to just do a simple RS232 radio link, then you _need_ to get better radios that have this built into the hardware. Otherwise the code needs to be a lot more complex....

Basic sequence for your radios, is something like:

1) Wait for the receive input pin to go high.
2) Only once high, enable UART on receiver (soft or hardware).
3) Transmitter needs to send a couple of pre-amble bytes with a short pause between to ensure UART is synced. Perhaps 0xAA, pause, 0x55.
4) Receiver waits till it receives 0x55, and then moves on.
5) Transmitter sends something like data, inverted data, checksum. However many bytes are needed. Then pauses.
6) Receiver accepts the 'packet', and tests first the data against the checksum. If this is OK advance to '8'. Otherwise '7'.
7) Assumes data is invalid, so tests instead the inverted data with the checksum. If this is OK advance to '8', otherwise back to '1'. Perhaps indicate an LED to say 'error'.
8) Now using either the data or inverted data (whichever matched the checksum), carry out the required operation.
9) Back to '1'.
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, 3
Page 3 of 3

 
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