Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

cant get uart to display port values :(

Status
Not open for further replies.

large_ghostman

Well-Known Member
Most Helpful Member
hi *waves* i am trying to get port values displayed over the rs232 i have the rs232 working :D but not getting far with the c18 lib for usart i have the following code wich is messy at the mo as i am only testing stuff
Code:
/*********************************************************************
* NOTES:
* Code uses the Peripheral library support available with MCC18 Compiler
* Code Tested on:
* PicDem2+ demo board with PIC18F4685 controller
*
* Uses Tx pin for transmission and Rx pin for reception.
* Baud rate of 19200 is configred at 8MHz oscillator fequency
**********************************************************************/
#pragma config OSC = IRCIO7, PWRT = OFF, WDT = OFF, MCLRE = ON, DEBUG = ON, LVP = OFF //..............Fuse stup

#define USE_OR_MASKS
#include <p18cxxx.h>
#include "usart.h"

unsigned char Rxdata[11];
unsigned char Txdata[] = " Test Connection ";

void main(void)
{
    unsigned char config=0,spbrg=0,baudconfig=0,i=0;
    
    Nop();
 OSCCONbits.IRCF0=1;
  OSCCONbits.IRCF1=1;
  OSCCONbits.IRCF2=1;
  OSCTUNEbits.PLLEN =1;
  ADCON1=0x0A; //set up ADC
  CMCON=0x07;
    CloseUSART();  //turn off usart if was previously on

//-----configure USART -----
    config = USART_TX_INT_OFF | USART_RX_INT_OFF | USART_ASYNCH_MODE | USART_EIGHT_BIT | USART_CONT_RX | USART_BRGH_HIGH;
//-----SPBRG needs to be changed depending upon oscillator frequency-------
    spbrg = 207;                    //At 8Mhz of oscillator frequency & baud rate of 38400.

    OpenUSART(config, spbrg);        //API configures USART for desired parameters

    baudconfig =  BAUD_16_BIT_RATE | BAUD_AUTO_OFF;
    baudUSART (baudconfig);
while(1)
{
Nop();
//------USART Transmission ----
baudUSART (baudconfig);
    while(BusyUSART()); 
    TXREG=PORTD;
    WriteUSART( PORTD );  
    Nop();          //Check if Usart is busy or not
    putsUSART((char *)Txdata );                //transmit the string
    while(BusyUSART());
    TXREG=PORTD;
Nop();
Nop();
 WriteUSART( PORTD );
//---USART Reception ---
 /*   getsUSART((char *)Rxdata,24);             //Recieve data upto 24 bytes

    while(BusyUSART());             //Check if Usart is busy or not
    putsUSART((char *)Rxdata);              //echo back the data recieved back to host

   // CloseUSART();
   */
//   while(1);                        //end of program

}
}

how do i get it to print port values etc over the serial??
many thank yous its one the last parts for my bot ive never done serial before sorry
 
First you need to recall that you will read some value between 0x00 and 0xFF from the port. You must convert this number to ASCII prior to sending it to the USART.

baudUSART (baudconfig); does not need to be executed each time through the loop. Get rid of the 2nd copy inside the while(1) loop.

Code:
    TXREG=PORTD;
    WriteUSART( PORTD );

I think if you look you can see what is wrong with the above code. You really need to know what each line of code is doing rather then copy it mindlessly from examples.

Clean this code up then post it again if you are still having problems.
 
the code is lazt code i tried TXREG=PORTD; first and didnt get anything so the tried WriteUSART(PORTD); without deleteing the other but what has realy confused me is the example in the docs folder of c18 they have the following example
Code:
#include <p18C452.h>
#define USE_OR_MASKS
#include <usart.h>
 
void main(void)
{
  // configure USART
  OpenUSART( USART_TX_INT_OFF  |
             USART_RX_INT_OFF  |
             USART_ASYNCH_MODE |
             USART_EIGHT_BIT   |
             USART_CONT_RX     |
             USART_BRGH_HIGH,25 );
 
  while(1)
  {
    while( ! PORTAbits.RA0 );  //wait for RA0 high
 
    WriteUSART( PORTD );       //write value of PORTD
 
    if(PORTD == 0x80)          // check for termination
      break;                   //   value
  }
 
  CloseUSART();
}

i havnt tried it but there isnt any convertion so i thought WriteUSART( PORTD ); was the correct syntax:confused::confused::confused: dad is thinking of buying hi tech pro as he needs to use 12f pics for something so i might try and learn hi tech they have realy good docs! i like the C manual in the folder its easy to use
 
i am not going to change! i was waffling :D actualy i was kind of having a moan about C18 docs. the code i posted was from an example i am going to start again and try and write my own with using only some of the supplied lib code some the microchip libs are good some are not so good
 
ok thank you i am more stuck on the actual syntax with the brackets around the variable and that kind of thing but i assume it will be similar to Hitech C and there manual is good for that the first code i posted dosnt work at all for displaying the port it dosnt show anything so i need to dig deeper on that as i thought i would of least got rubish
 
LG.... I know it takes more memory... Look into streams, it saves a hell of a lot of heart ache...

sprintf(); "print to a stream"..

Take the above example...

Code:
   char buffer[20];
   sprintf(buffer,"%d", PORTD);
   putsUSART(buffer);

Output will be the decimal representation on port D.... in text...
I always use a stream buffer..... for LCD and for Serial comms.
 
Ian is right, use sprintf, although I always thought it means string print formatted, not stream?

HiTech is free in lite mode, can you not use the version that comes bundled with MPLAB to "try before you buy"?

3v0 is right also, the problem is with the syntax not the compiler.

Yes the C18 docs suck! I find the standard C library docs help though and combine that with PIC specifics to get the best of both worlds.

Regards

Wilksey
 
Ian is right, use sprintf, although I always thought it means string print formatted, not stream?
So did I!!The only reason I think it's stream is because this is what its called in C++.... iosteam library.. I have a little hand reference from Borland and the definition was stream.

However... there is also swprinf() which is defined as wide string!!!!
 
thanks everyone thats realy helpful, now all i got to do is turn it all into a useful debug tool for my robot and other stuff, ive been using lcd and leds but on my robot i need more information so i thought would be good to get serial working :D i am sure i will be back soon for more questions :D
 
The standard streams are stdout stdin and stderr. sprintf is string print because we print to a string. It has been that way since billy was a calf.

But in C18 you need to look at the notion of H_USER as none of the std streams are directly supported. However it one uses something like TXREG = PORTD or putUSART it bypasses the notion of the standard streams.

I hope that helps.

3v0
 
where can i find out more mr 3v0? i have looked in the docs folder but cant see anything that gives a good explanation just a quick by the way it does ..... i like examples of use maybe hitech has a better description
 
where can i find out more mr 3v0? i have looked in the docs folder but cant see anything that gives a good explanation just a quick by the way it does ..... i like examples of use maybe hitech has a better description

This jargon we're harping on about is standard ansi C... Most good C books cover these standard library functions...
 
first you have to decide on which or the 3 or so methods you want to use.

Is this about learning or just getting it working?

If you have not seen them I suggest that you look at the C18 compiler library documethttps://ww1.microchip.com/downloads/en/devicedoc/51297c.pdf Look at example
2.3.2 Example Use of the Capture Routines
It shows a more realistic example then 2.10.2 which talks about the USART.

Stop using the Hi-Tech docs with the C18 compiler.
 
Last edited:
i have it working at a basic level so its about learning and seeing what i can do with it, i know i shouldnt use the wrong manual but its better laid out it gives a list of library function and there is even one to read device id but mainly i like it because the ingormation is in one place not in loads different pdfs btw thanks for the pdf i havnt seen that one its got alot better information in it the other thing i am doing is getting things to work with the libs then i am rewriting it using the datasheet to diretly change the registers but i need the libs first so i know it works :D like the open usart i like to do all that setting up myself with registers but i have to make it work first the other way
 
i will get a c book i also want to get to learn C# :D
 
i will post the code as i go i am still altering it via the datasheet at the moment
 
Status
Not open for further replies.

Latest threads

Back
Top