• AVR Freaks

Hot!serial monitor and pic

Author
surfer
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2021/03/02 06:06:06
  • Location: 0
  • Status: offline
2021/03/03 06:40:13 (permalink)
0

serial monitor and pic

hi all,
now i'm learning adc module on 16f690,
i have uart code, and can send text on pc software serial monitor Putty,
UART_Print("Hello world!\r\n"); 
 
but if I send some int value, or adc result without quotes " ..... "
UART_Print("Hello world!\r\n", result);  // UART print
UART_Print(12345);  // UART print
 
result = (ADRESH<<8)+ADRESL; //combine the 10 bits of the conversion
 
i receive only strange data,


if i understand, need convert this value,
or try installing mcc and generate uart code for printf function
or add some void putch(unsigned char ch) function
what best way for this
 
/********************** UART functions **************************/
void UART_Init(const uint32_t baud_rate)
{
  int16_t n = ( _XTAL_FREQ / (16 * baud_rate) ) - 1;
  
  if (n < 0)
    n = 0;
 
  if (n > 255) // low speed
  {
    n = ( _XTAL_FREQ / (64 * baud_rate) ) - 1;
    if (n > 255)
      n = 255;
    SPBRG = n;
    TXSTA = 0x20; // transmit enabled, low speed mode
  }
 
  else // high speed
  {
    SPBRG = n;
    TXSTA = 0x24; // transmit enabled, high speed mode
  }
 
  RCSTA = 0x90; // serial port enabled, continues receive enabled
}
 
__bit UART_Data_Ready()
{
  return RCIF; // return RCIF bit (register PIR1, bit 5)
}
 
uint8_t UART_GetC()
{
  while (RCIF == 0) ; // wait for data receive
  if (OERR) // if there is overrun error
  { // clear overrun error bit
    CREN = 0;
    CREN = 1;
  }
  return RCREG; // read from EUSART receive data register
}
 
void UART_PutC(const char data)
{
  while (TRMT == 0); // wait for transmit shift register to be empty
  TXREG = data; // update EUSART transmit data register
}
 
void UART_Print(const char *data)
{
  uint8_t i = 0;
  while (data[i] != '\0')
    UART_PutC (data[i++]);
}
/********************** end UART functions **************************/

void main(void)
{
  OSCCON = 0x70; // set internal oscillator to 8MHz
  UART_Init(9600); // initialize UART module with 9600 baud
  __delay_ms(1000); // wait 1 seconds
 
  UART_Print("Hello world!\r\n"); // UART print
 }

 
/////////////////////// // Main Program Loop /////
while(1)
{
__delay_us(10); //Wait the acquisition time (about 10us).
ADCON0bits.GO = 1; //start the conversion
while(ADCON0bits.GO==1){}; //wait for the conversion to end
result = (ADRESH<<8)+ADRESL;
}

#1

9 Replies Related Threads

    ric
    Super Member
    • Total Posts : 30244
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: serial monitor and pic 2021/03/03 15:47:10 (permalink)
    +3 (3)
    Your UART_Print() can only output a string, you can't pass an integer to it.
    You need to convert the integer to a string. The sprintf() function is an easy way to do this.
    It works very similarly to the common printf() function, put places the result into a string variable (i.e. a character array)
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    surfer
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2021/03/02 06:06:06
    • Location: 0
    • Status: offline
    Re: serial monitor and pic 2021/03/04 15:03:20 (permalink)
    0
    thnx,
    after many days of torment,
    thought my head would explode :))
    I did it.
     

     
    then I have question,
    I read the forum and saw that
    some people divide 10bits into two packets, upper bytes, lower bytes, sending and receive in reverse,
     
    this is due to the additional memory load when using the printF functions and floating point library,
    or it is more related to the conversion of data types for communication of different devices (for example, the output of a barcode printer and a Wi-Fi terminal for control, etc.)
    thnx
    post edited by surfer - 2021/03/04 15:23:20
    #3
    ric
    Super Member
    • Total Posts : 30244
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: serial monitor and pic 2021/03/04 15:08:23 (permalink)
    0
    I'm not sure if you are asking a question, or just reporting what you did.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    Aussie Susan
    Super Member
    • Total Posts : 3859
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: serial monitor and pic 2021/03/04 18:57:48 (permalink)
    +1 (1)
    What gets sent/received by a UART is generally 8 bits (plus the overhead its). However those 8 bits are whatever you want them to be.
    What often gets sent via a UART are ASCII characters but that is only the interpretation that is placed on the 8 bits by the programs that send and receive them. As far as the UART is concerned it is just sending/receiving an 8 bit binary value.
    That means you can send a binary value if you want to (rather than converting it to an ASCII string and sending each character) but only 8 bits at a time.
    What you described is sending a 10 bit binary value over a UART that handles 8 bits at a time. What you do is to separate off the top 2 bits, and then send the 2-bit and 8-bit values in whichever order you decide, and at the far end put them back into a 1 bit value.
    Of course this can be extended up as far as you like - if you want to send a 32-bit floating point value then you can separate the 32 bits into 4 8 bit values, send those and reconstruct the value at the far end.
    Ultimately you get to realise that the computer has absolutely no understanding of the values that it holds in memory or manipulates in the CPU or any peripheral module. You, as the programmer, layer on that level of interpretation.
    Susan
    #5
    oliverb
    Super Member
    • Total Posts : 441
    • Reward points : 0
    • Joined: 2009/02/16 13:12:38
    • Location: 0
    • Status: offline
    Re: serial monitor and pic 2021/03/05 02:46:34 (permalink)
    0
    Bear in mind that OP is using a terminal, Putty, so they are pretty much limited to ASCII communication.
     
    Communicating raw binary data is less demanding on the PIC but you'll generally have to code your own "client" program to see the data on a PC.
    #6
    KTrenholm
    Super Member
    • Total Posts : 865
    • Reward points : 0
    • Joined: 2012/08/08 14:04:23
    • Location: Connecticut, USA
    • Status: offline
    Re: serial monitor and pic 2021/03/05 07:27:22 (permalink)
    +1 (1)
    No need for a custom client program,  you can view raw bytes no problem with the right terminal program.  Here's a few that are capable of showing the raw hex data:
     
    YAT (Yet Another Terminal):https://sourceforge.net/projects/y-a-terminal/
    AccessPort: http://www.sudt.com/en/ap/
    RealTerm: https://sourceforge.net/projects/realterm/
     
    AccessPort is probably my preferred one of the above, but if anyone's got one they prefer, post it.  Always on the lookout for a nice serial terminal program.
    post edited by KTrenholm - 2021/03/05 07:32:49
    #7
    1and0
    Access is Denied
    • Total Posts : 12269
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: serial monitor and pic 2021/03/05 07:36:45 (permalink)
    +1 (1)
    KTrenholm
    No need for a custom client program,  you can view raw bytes no problem with the right terminal program.  Here's a few that are capable of showing the raw hex data:
     
    YAT (Yet Another Terminal):https://sourceforge.net/projects/y-a-terminal/
    AccessPort: http://www.sudt.com/en/ap/
    RealTerm: https://sourceforge.net/projects/realterm/
     
    AccessPort is probably my preferred one of the above, but if anyone's got one they prefer, post it.  Always on the lookout for a nice serial terminal program.

    I used [link=mailto:Br@y]Br@y[/link] Terminal: https://sites.google.com/site/terminalbpp/
     
    #8
    KTrenholm
    Super Member
    • Total Posts : 865
    • Reward points : 0
    • Joined: 2012/08/08 14:04:23
    • Location: Connecticut, USA
    • Status: offline
    Re: serial monitor and pic 2021/03/05 07:40:45 (permalink)
    0
    I think I'll give that one a shot.  I like the sound of that macro feature...
    #9
    paulfjujo
    paulfjujo
    • Total Posts : 119
    • Reward points : 0
    • Joined: 2011/03/08 05:33:46
    • Location: France 01700
    • Status: offline
    Re: serial monitor and pic 2021/03/06 03:36:29 (permalink)
    0
    hello,
     
    I used before VBRAY Terminal  with WinXP,  very usefull !  with Macros and script langage like pascal
    but with Win10 and virtual com port (  Prolific USB-UART cable , or Bluetooth )  , behavior is not good , not stable ,
    many crash occured .
    so ,now, i am using YAT , wich is OK with Win10 , very stable ,
    even with 3 simultaneous open virtual terminal  at 19200 bds.
    I use it to trace and debug my PIC applications .. and Bluetooth or RF transmitter
    (nota : Up to 3 UARTs on 18F46K22 could be possible )
     
    #10
    Jump to:
    © 2021 APG vNext Commercial Version 4.5