packet streaming

General discussion on mikroC.
Author
Message
atommic22
Posts: 24
Joined: 17 May 2008 12:39

packet streaming

#1 Post by atommic22 » 17 May 2008 13:49

I'm trying to make a packet streaming program where the state of the buttons attached to port A are converted into a serial pulse train that is emitted through port B. However, when i test the output only the start and stop bits are expressed. I think that I am assigning the individual PORTA bits incorrectly.

Code: Select all

unsigned short i;

void main() {

  // Initialize USART module (8 bit, 2400 baud rate, no parity bit..)
  Usart_Init(2400);
  TRISA = 0xFF;          // set PORTB to be input
  TRISB = 0;             // set PORTD to be output
  PORTB = 0xFF;          // initialize PORTB  do {
     PORTB = 1;                 //start bit
     Delay_ms(100);              //delay

     PORTB = PORTA.F0;
     Delay_ms(100);
     PORTB = PORTA.F1;
     Delay_ms(100);
     PORTB = PORTA.F6;
     Delay_ms(100);
     PORTB = PORTA.F7;
     Delay_ms(100) ;

     PORTB = 1;               //stop bit
     Delay_ms(100);            //delay
     PORTB = (0);             //zero state for long pause
     Delay_ms(1000);           //long pause
  } while (1);
}//~!

pwdixon
Posts: 1431
Joined: 13 Apr 2005 11:14
Location: UK

#2 Post by pwdixon » 17 May 2008 14:39

I think you have misunderstood how the serial library works or I have not understood what you are trying to do.

It looks like you are trying to send a serial port character based on the value of porta.

The library can serialise the value from porta directly, see the help file.



void main() {

// Initialize USART module (8 bit, 2400 baud rate, no parity bit..)
Usart_Init(2400);

do {
Usart_Write(PortA); // Send data via USART
}
} while (1);
}//~!

atommic22
Posts: 24
Joined: 17 May 2008 12:39

#3 Post by atommic22 » 17 May 2008 15:39

does Usart_Write(PortA) output to PORTB? or do i have to write

Code: Select all

Usart_read(PortB)
?

Jan Rune
Posts: 416
Joined: 21 Oct 2005 23:04
Location: Oslo, Norway

#4 Post by Jan Rune » 17 May 2008 16:26

Usart_write() writes a byte to the PIC's hardware USART module. Usart_read() reads a incoming byte from the hardware USART module.

atommic22
Posts: 24
Joined: 17 May 2008 12:39

#5 Post by atommic22 » 18 May 2008 03:49

I'm trying to construct this
Image
If the encoder chip uses Usart_Write(PORTA) how do i transmit the usart serial data to port B?

thenoble66
Posts: 243
Joined: 28 Sep 2005 19:43
Location: Romania

#6 Post by thenoble66 » 18 May 2008 14:36

Hi, atommic22,

If you take a look at the datasheet, you'll see, that:

RB1 / Rx
RB2 / Tx

This means, there is no other choice for hardware USART.
According to your schematic, which is generally correct, one of PICs will be ONLY transmitter, and WILL use TX (RB2), the other MUST be receiver, and WILL use Rx (RB1).
By the way, your code has nothing to do with your schematic. :shock:
Regards,
thenoble66
--------------------------------------------------------------------

atommic22
Posts: 24
Joined: 17 May 2008 12:39

#7 Post by atommic22 » 18 May 2008 15:08

What do you mean nothing to do with my schematic? I have to send serial information. Is there a better method that USART?

thenoble66
Posts: 243
Joined: 28 Sep 2005 19:43
Location: Romania

#8 Post by thenoble66 » 18 May 2008 15:47

Now really, there is no better way. But your code does not contain anything related to hardware USART, except Usart_Init...
Regards,
thenoble66
--------------------------------------------------------------------

atommic22
Posts: 24
Joined: 17 May 2008 12:39

#9 Post by atommic22 » 19 May 2008 00:19

Oh that was my original code when i tried to set up some kind of streaming packet system. :P
Now I'm using USART and my respective codes look like this:
Encoder:

Code: Select all

void main() {

  TRISA = 0xFF;          // set PORTA to be input
  TRISB = 0;             // set PORTB to be output
  PORTB = 0xFF;          // initialize PORTB

// Initialize USART module
Usart_Init(600);

do {
Usart_Write(PortA); // Send data via USART
PORTB = Usart_Read();
} while (1);
}//~!
And the Decoder:

Code: Select all

unsigned short i;

void main() {

  TRISA = 0;          // set PORTA to be output
  TRISB = 0xFF;       // set PORTB to be intput
  PORTA = 0xFF;       // initialize PORTB



  // Initialize USART module
  Usart_Init(600);

  do {
    if (Usart_Data_Ready()) {   // If data is received
      i = Usart_Read();         // Read the received data
      Usart_Write(i);           // Send data via USART
    }
  } while (1);
}//~!
But i dont know how to get the decoder to write the decoded USART signal to PORTA....

???

thenoble66
Posts: 243
Joined: 28 Sep 2005 19:43
Location: Romania

#10 Post by thenoble66 » 19 May 2008 07:06

This fragment was taken from your encoder:

Code: Select all

do { 
   Usart_Write(PortA); // Send data via USART 
   PORTB = Usart_Read(); 
} while (1); 
Why do you keep using the encoder for receiving?

Code: Select all

  do { 
    if (Usart_Data_Ready()) {   // If data is received 
      i = Usart_Read();         // Read the received data 
      Usart_Write(i);           // Send data via USART 
    } 
  } while (1); 
Why do you keep using the decoder for transmitting?

1. CHECK DATASHEET of 16F628A!
2. Complete your schematic with pinouts.
3. ENCODER IS ONLY TRANSMITTING, so forget Rx on encoder
4. DECODER IS ONLY RECEIVING, so forget Tx on decoder
Regards,
thenoble66
--------------------------------------------------------------------

thenoble66
Posts: 243
Joined: 28 Sep 2005 19:43
Location: Romania

#11 Post by thenoble66 » 19 May 2008 20:36

http://picasaweb.google.ro/nemesgyozo/T ... 2485853842

It is a working test circuit.
If you like it, I'll post the code also.
I have to rewrite it from mBasic to mC.
Regards,
thenoble66
--------------------------------------------------------------------

atommic22
Posts: 24
Joined: 17 May 2008 12:39

#12 Post by atommic22 » 20 May 2008 05:35

Ahh yes that's exactly what i need. If you could post the assembly code or the mikroC code I'd be very grateful.

Thanx, Atommic

thenoble66
Posts: 243
Joined: 28 Sep 2005 19:43
Location: Romania

#13 Post by thenoble66 » 20 May 2008 13:50

If you could post the assembly code or the mikroC code I'd be very grateful.
I bet you would.
But I am too ashamed to post the code :oops: :lol: , because the difference between your code and my code is so minor, that you should figure out yourself 8)
Instead I give you some hints:
Encoder: too much lines in do{..} loop
Decoder: change second line in do{..} loop
Regards,
thenoble66
--------------------------------------------------------------------

atommic22
Posts: 24
Joined: 17 May 2008 12:39

#14 Post by atommic22 » 20 May 2008 13:59

Encoder: too much lines in do{..} loop
Decoder: change second line in do{..} loop
Well I read through what you said and the data sheet. The Port B is allocated by default. I just seem to remember when testing it last time it wouldn't work unless i set it to read to PORTB. anyway I have altered the code however I havnt yet tested it's functionality.

Encoder:

Code: Select all

void main() {

  TRISA = 0xFF;          // set PORTA to be input
  TRISB = 0;             // set PORTB to be output
  PORTB = 0xFF;          // initialize PORTB

// Initialize USART module
Usart_Init(600);

  do {
    Usart_Write(PortA); // Send data via USART
  } while (1);
}//~!
Decoder:

Code: Select all

unsigned short i;

void main() {

  TRISA = 0;          // set PORTA to be output
  TRISB = 0xFF;       // set PORTB to be intput
  PORTA = 0xFF;       // initialize PORTB



  // Initialize USART module
  Usart_Init(600);

  do {
    if (Usart_Data_Ready()) {   // If data is received
      PORTA = Usart_Read();         // Read the received data
    }
  } while (1);
}//~!
I just dont know if i should incoperate a Usart_Write(PORTA) command in the decoder after PORTA = Usart_Read();
I'll try and test the codes tomorrow, I need access to a project board.

thenoble66
Posts: 243
Joined: 28 Sep 2005 19:43
Location: Romania

#15 Post by thenoble66 » 20 May 2008 14:22

You're on the good way :)
Regards,
thenoble66
--------------------------------------------------------------------

Post Reply

Return to “mikroC General”