PIC18f4550

Post
mayur05_damania
Junior Member
2007/12/17 08:58:48
Hi,
 
   Has anyone worked on PIC18f4550 USART? Does anyone know how to accept the two bytes of data from Rx pin and How to transmit data from Tx pin? Any sourse codes?
 
Regards,
bob_barr
Super Member
RE: PIC18f4550 2007/12/17 09:07:15
Two bytes can be read from the UART by reading the RCREG register twice. The bit RCIF in the PIR1 register indicates that a byte is available.

Transmitting is done by writing the data byte to be sent to the TXREG register.
<added>
The bit TXIF in the PIR1 register indicates that the UART is able to accept a byte.
</added>

Datasheet Section 20.2.1 describes the process of transmitting data bytes. Section 20.3.1 describes the receive process.
post edited by bob_barr - 2007/12/17 09:09:45
vloki
always on da run
RE: PIC18f4550 2007/12/17 09:08:47
Does anyone know how to accept the two bytes of data from Rx pin
what two bytes you're talking about ?

Any sourse codes?
The MCHPFSUSB/fw/CDC demo is USB<->USART
Marctwo
Super Member
RE: PIC18f4550 2007/12/17 09:32:19
The bit TXIF in the PIR1 register indicates that the UART is able to accept a byte.
From what I gather, it takes 1 instruction cycle for TXIF to clear after writing to TXREG so you can't test it immediately after writing.  You can, however, test TXSTAbits.TRMT which is set when TSR is empty.
Olin Lathrop
Super Member
RE: PIC18f4550 2007/12/17 12:40:39
Has anyone worked on PIC18f4550 USART?

<sarcastic>No, why would anyone do that?</sarcastic> (Duh)
 
Does anyone know how to accept the two bytes of data from Rx pin and How to transmit data from Tx pin?

Yes, of course.
 
Any sourse codes?

My PIC UART code is available, and supports that PIC among many others.  The main code is in the template module QQQ_UART.ASPIC with some hooks in the interrupt module QQQ_INTR.ASPIC of my PIC development environment at http://www.embedinc.com/pic.  This code uses interrupts and FIFOs for both transmit and receive.  Beware of the NULL byte bug in the transmit part of that UART.
BMD
Super Member
RE: PIC18f4550 2007/12/17 13:02:37
Hi,
 
I have noticed recently a few remarks concerning a "Null" byte on serial transmission. I have got into the habit of using 0x03 (which is ascii 'end of text' I believe), apart from a 'Null' byte being easily recognised (BZ etc.), whereas I have to test each byte, am I missing something else? or is it purely choice what to use?
corvette321
Super Member
RE: PIC18f4550 2007/12/18 02:29:11
here are rewrites of the library routines.... (dont use library routines unless u want the null to be sent!!)
 
this code is tested and works and is part of my soon to be released Xbee (zigbee / 802.15.4 ) framework which u can read about in the posts here...
 
for circular buffa's checkout my other post for complete working code...
 

// this sends a string to USART WITHOUT SENDING THE NULL!!!!!
void putsuart( char *data)
{
  do
  {  // Transmit a byte
    while(fulluart());
 if(*data!=0)
 {
     Writeuart(*data);
 }
  } while( *data++ );
}
 
 
//this sends a string to USART WITHOUT SENDING THE NULL!!
void putrsuart(const rom char *data)
{
  do
  {  // Transmit a byte
    while(BusyUSART());
 if(*data!=0)
 {
     putcUSART(*data);
 }
  } while( *data++ );
}
 
 
 
remember a zero in ascii is 0x30 not 0x00!! 0x00 is a null/....
 
so obviously nulls are just terminations as are string terminations....
 
[8|][8|][8|][8|][8|]winkwinkwinkwinkwinkwink
corvette321
Super Member
RE: PIC18f4550 2007/12/18 02:32:12
heres another routine u need for above code..
 
//check for a full UART
int fulluart(void)
{
  if(!TXSTAbits.TRMT)  // Is the transmit shift register empty
    return 1;          // no, return true (register full!!)
  return 0;            // Return false register empty
}
corvette321
Super Member
RE: PIC18f4550 2007/12/18 02:35:53
heres another good USART routine :)
 
a getstringUSART that doesnt loop forever!!! nope it has a delay in it..
 
so if nothing shows up then perhaps nothing is there!!
 
 
this is good for testing for a response if u dont know its comming to you...
 
I use it to test for an xbee module in the socket and also to test for a valid response at a given baud rate..
 
if no response, then either the socket is empty or wrong baud rate!!...
 
 
//this is a delayed test getsuart, does not loop forever waiting to get a rcv_buffa
//use this one if you have an unknown working module state...(module in socket??)
void getdelayuart(char *buffer, unsigned char len)
{
  char i;    // Length counter
  unsigned char data;
  unsigned char good;
  good=0x66;
for(j=0;j<30000;j++)
 {
 Delay100TCYx(2);  //like 50 uS
    if(dataready())
 {
  j=30000;
  good=0x33;
 }
 }

if (good==0x33)
{
  for(i=0;i<len;i++)  // Only retrieve len characters
  {
  while(!dataready());// Wait for data to be received 
    data = readusart();    // Get a character from the USART
                           // and save in the string
    *buffer = data;
    buffer++;              // Increment the string pointer
  }
}
}
 
 
 
obviously you adjust the delay length on the baudrate you are working with... and how long the module / line takes to response.
mayur05_damania
Junior Member
RE: PIC18f4550 2007/12/18 04:33:20
thanks for your reply.
 
  the codes you have given me its for one byte or two bytes? if I want for three bytes then what should |I do?
 
 
vloki
always on da run
RE: PIC18f4550 2007/12/18 05:03:27
the codes you have given me its for one byte or two bytes? if I want for three bytes then what should |I do?

you should recognize that the size of Tx and Rc registers are 1 byte each.
so if you want to receive or transmit 3 bytes
you must read or write three times !

(or use the functions that will do this for u,
i assume u have the ability to find out which functions)
 

mayur05_damania
Junior Member
RE: PIC18f4550 2007/12/18 08:02:19
Hi volki,
 
   I am new to using USART. so i am on the learning curve. and learning is always better. let me know the functions. 
corvette321
Super Member
RE: PIC18f4550 2007/12/18 10:11:20
just call the function i put there..


u can send bytes in a string like this..


char [2] thisismybytes="AA";

putrsuart(thisismybytes);



this will send the ascii codes for AA out as 2 bytes out the USART.

see how easy it is??


1st you have to initialize your USART.
corvette321
Super Member
RE: PIC18f4550 2007/12/18 10:15:08
another way you could do it is send 1 byte at a time.
 
this one uses the usart library..... so put that in your includes
 
 
#includes <usart.h>
 
 
void Writeuart_1byte(char data)
{
 TXREG=data;
 while(BusyUSART());
}
 
 
this routine is slightly modified from the library function of the same thing...
 
so to send 2 bytes separately and if you dont wanna deal with ascii values... then do this.. just call it
 
 
Writeuart_1byte(data1);
Writeuart_1byte(data2);
 
 
that will send your 2 bytes out in succession.
 
gringrin[8|][8|]
mayur05_damania
Junior Member
RE: PIC18f4550 2007/12/18 10:54:12
Thanks corvette321.
 
  what to do for receiveing data from USART? Actually Whatever data I receiver from USART I want to send over USB. So do you have any IDEA? Have you got any codes for transmitting and Receiveing data from USART?
corvette321
Super Member
RE: PIC18f4550 2007/12/18 12:01:04
hahaha
 
i have all kinds of codes...
 
if you want USART with USB then try using the microchip CDC firmware...
 
i wrote my own implementation using the full speed api firmware....
 
but i havent released it yet...
 
so checkout the CDC firmware :)
 
mayur05_damania
Junior Member
RE: PIC18f4550 2007/12/18 12:08:07
hi corvette321,
 
Thanks for that.
 
with CDC firmware they are using Transmitting and receiving data from USB. they are actuallt using Tx and Rx pins of the PIC for Receiving data.
 
 let me explainn you what I want to do is receive the data from Rx pin and sen it over USB Bus and at the same time send that data through Tx pin. please help me corvette321
corvette321
Super Member
RE: PIC18f4550 2007/12/18 15:51:31
:)
 
i do consulting work if you want me to write all your code for you.
 
$35 dollars an hour.
 
gringringringringrin
 
if you have no clue how to do this kind of thing you should poke around 1st.
 
1. get knowledgable about the full speed USB framework from microchip. learn how commands are received and sent.
 
2. learn how to do USART.
 
3. merge them together with USB commands and a windows application.
 
 
vloki
always on da run
RE: PIC18f4550 2007/12/19 01:05:23
let me explainn you what I want to do is receive the data from Rx pin and sen it over USB Bus and at the same time send that data through Tx pin. please help me corvette321
ok, you are not able to do the exercises in the CDC demo by your self
The following (modified) "user.c" may help you with that.
Please note that it's only a basic code to start with (without error handling)
but i think it is enough to understand what you have to do.
/*********************************************************************
*
*                Microchip USB C18 Firmware Version 1.0
*
*********************************************************************
* FileName:        user.c
* Dependencies:    See INCLUDES section below

...*/

#define DBG_toggle(port,pin)  {_asm BTG port, pin,0 _endasm}
#define DBG_puls(port,pin)  {_asm BTG port, pin,0 BTG port, pin,0 _endasm}


/** I N C L U D E S **********************************************************/
#include <p18cxxx.h>
#include <usart.h>
#include "system\typedefs.h"

#include "system\usb\usb.h"

#include "io_cfg.h"             // I/O pin mapping
#include "user\user.h"


/** V A R I A B L E S ********************************************************/
#pragma udata access ACCESSRAM
near char i, mask, my_errors;
near unsigned char new_out_bytes, out_bytes, in_bytes;

#pragma udata
char out_buffer[64];
char in_buffer[255];



/** P R I V A T E  P R O T O T Y P E S ***************************************/
void InitializeUSART(void);

void RX(void);            // data RS232 -> PIC
void usbIN(void);        // data PIC -> USB (PC)

void usbOUT(void);        // data USB (PC) -> PIC
void TX(void);            // data PIC -> RS232


/** D E C L A R A T I O N S **************************************************/
#pragma code
void UserInit(void)
{
  TRISA = 0;        // use PORTA for DEBUG signals

  InitializeUSART();
  in_bytes = 0;
  new_out_bytes = 0;
  out_bytes = 0;
  my_errors = 0;  

}//end UserInit

void InitializeUSART(void)
{
  TRISCbits.TRISC7 = 1; // RX
  TRISCbits.TRISC6 = 1; // TX [color="#ff0000"][color="#000000"]edit: 0" may work but was wrong here[/color][/color][color="#000000"] [/color]
  SPBRG = 0xCF;        // 0xCF -> 57600 baud   ((0x01A0 for 48MHz -> 28800 baud))
  SPBRGH = 0x00;      //                 ((0x0271 for 48MHz -> 19200 baud))
  TXSTA = 0x24;       // TX enable BRGH=1
  RCSTA = 0x90;       // continuous RX
  BAUDCON = 0x08;     // BRG16 = 1
}//end InitializeUSART

/******************************************************************************
* Function:        void ProcessIO(void)
*
* PreCondition:    None
* Input:           None
* Output:          None
* Side Effects:    None
*
* Overview:        This function is a place holder for other user routines.
*                  It is a mixture of both USB and non-USB tasks.
*
* Note:            None
*****************************************************************************/
void ProcessIO(void)
{  
  // User Application USB tasks
  if((usb_device_state < CONFIGURED_STATE)||(UCONbits.SUSPND==1))
  {
       return;
  }
  RX();        // data RS232 -> PIC
  usbIN();    // data PIC -> USB (PC)

  usbOUT();    // data USB -> PIC
  TX();        // data PIC -> RS232

}//end ProcessIO


/******************************************************************************
* Function:        void RX(void)
*****************************************************************************/
void RX(void)
{
  if (RCSTAbits.FERR || RCSTAbits.OERR)
  {
      RCSTAbits.CREN = 0;        // error handling
      RCSTAbits.CREN = 1;
  }   
  if (DataRdyUSART())
  {   
      in_buffer[in_bytes] = ReadUSART();
      if (in_bytes < 235)
      {
//            DBG_puls(PORTA,1);    //##### Debug-Signal #####
          in_bytes++;
      }
      else
      {
//            DBG_puls(PORTA,2);    //##### Debug-Signal #####
          my_errors++;
      }
  }
}

/******************************************************************************
* Function:        void USBin(void)
*****************************************************************************/
void usbIN(void)
{
    if (in_bytes)
//    if ((in_bytes > 100)||(in_buffer[in_bytes-1]== 0xAB))
  {
      if (mUSBUSARTIsTxTrfReady())
      {
//            DBG_puls(PORTA,4);    //##### Debug-Signal #####
          mUSBUSARTTxRam((byte*)in_buffer,in_bytes);
           in_bytes = 0;
      }
      else
      {
//            DBG_puls(PORTA,5);    //##### Debug-Signal #####
      }
  }
}

/******************************************************************************
* Function:        void usbOUT(void)
*****************************************************************************/
void usbOUT(void)
{
  new_out_bytes = getsUSBUSART((out_buffer + out_bytes), CDC_BULK_OUT_EP_SIZE);   

  if (new_out_bytes)
  {
      out_bytes += new_out_bytes;
  }
}

/******************************************************************************
* Function:        void TX(void)
*****************************************************************************/
void TX(void)
{
  if (out_bytes)
  {
      if(!BusyUSART())
      {
//            DBG_toggle(PORTA,3);    //##### Debug-Signal #####
          WriteUSART( out_buffer[0] );
          out_bytes--;
          for (i  = 0; i < out_bytes; i++)
              out_buffer[i] = out_buffer[i+1];
//            DBG_toggle(PORTA,3);    //##### Debug-Signal #####
      }
  }
}



edit:2008/05/29 removed some errors (some out_bytes -> in_bytes)
post edited by vloki - 2008/05/28 06:59:01
corvette321
Super Member
RE: PIC18f4550 2007/12/19 21:02:38
be careful theres some erros in the CDC stuff although i havent checked out the latest release....
 
i knew when i last looked at it that windows vista needed a different .inf file... theres one floating around the web somewhere...
 
buts its a great framework to use!!
 
:)