• AVR Freaks

Helpful ReplyHot!USART Serial Communication

Author
jellyfishers
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2020/10/12 18:47:04
  • Location: 0
  • Status: offline
2020/10/18 20:03:20 (permalink)
0

USART Serial Communication

Hello everyone,
 
I have edited my post so that it is easier for viewing and reading. I thought it was too messy.
 
Just to explain my problem again, I am trying to connect a digital temperature sensor to a PIC18f4550 board and display my results on my LCD 16x2 screen through serial communication. But so far I have not managed to do it and I have no idea why and where it went wrong in my code. Hope you guys can guide me on this issue as I am fairly new on microchip. :)
 
 

 
unsigned char temp, reading, serial_data;
char temp_msg [] = "Temperature is: ";
char temp_reading [20];



void main(void) {
  
    int i;
    lcd_init();
    
    SPBRG = 19;
    
    TRISCbits.TRISC6 = 0;
    TRISCbits.TRISC7 = 1; // Make RC7 an input pin
    
    RCSTA = 0x90;
    TXSTA = 0x20;
    
    lcd_write_cmd(0x80);
    for (i=0; i<16; i++)
        {
            temp = temp_msg[i];
            lcd_write_data(temp);
        }
    

    for (i=0; i<10; i++) {
         if ( PIR1bits.RCIF ==1) //if byte is received
        {
            temp_reading[i] = RCREG;
            
    }
         
         lcd_write_cmd(0xC0);
    for (i=0; i<10; i++) {
        lcd_write_data (temp_reading [i]);
        
    }
    }
 }
 

post edited by jellyfishers - 2020/10/20 20:08:36
#1
ric
Super Member
  • Total Posts : 28951
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: UART serial communication with a digital sensor 2020/10/19 12:42:13 (permalink)
0
Please post your code again with "code" tags around it
i.e.
[code]
 C source line
 C source line
 C source line
[/code]
Your code is corrupted because "i" between "[]" is taken as a "start italics" command by the forum.
(Don't just cut and paste my code above. I deliberately mangled the opening tag so you could see it.)
 

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
Aussie Susan
Super Member
  • Total Posts : 3788
  • Reward points : 0
  • Joined: 2008/08/18 22:20:40
  • Location: Melbourne, Australia
  • Status: offline
Re: UART serial communication with a digital sensor 2020/10/19 18:35:21 (permalink) ☄ Helpfulby jellyfishers 2020/10/20 23:08:37
0
Also separate your problems: firstly are you getting expected characters on the LCD screen at all?
Secondly is the UART working?
For the first problem, you should be able to write a short program that initialises the LCD and then writes some text to it.
For the second problem, you can use the debugger to see if a character is being received and it is what you expect.
Also with these older MCUs you really do need to follow the instructions in the data sheet as to how to initialise the UART.
It is generally better to set the individual bit fields rather than the register as a whole for the UART.
Susan
#3
katela
Super Member
  • Total Posts : 1907
  • Reward points : 0
  • Joined: 2013/06/11 05:25:18
  • Location: South Africa
  • Status: offline
Re: - 2020/10/20 08:37:53 (permalink)
+1 (1)
jellyfishers
- Content removed from original author -


This is not how forum works, whether you found the solution to your problem or not, you just can't remove your question.
This is complete disregard to those people who have already used their time to try and assist you and also for all the forum users

Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
#4
jellyfishers
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2020/10/12 18:47:04
  • Location: 0
  • Status: offline
Re: - 2020/10/20 20:07:30 (permalink)
0
Hi katela
 
Sorry for this misunderstanding caused! I changed the post so that I could find out on how I can make my post neater and had no intentions on trying to remove my post like that. Was also thinking if I should repost on another forum topic 
(PIC microcontrollers). I really appreciate the help here and thank you on helping me improve on my ethics.
 
Regards
jellyfishers
#5
jellyfishers
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2020/10/12 18:47:04
  • Location: 0
  • Status: offline
Re: UART serial communication with a digital sensor 2020/10/20 20:10:20 (permalink)
0
i tried it, is it better now?
#6
jellyfishers
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2020/10/12 18:47:04
  • Location: 0
  • Status: offline
Re: UART serial communication with a digital sensor 2020/10/20 20:22:14 (permalink)
0
hi susan,  thank you so much for ur reply!
 
so far, I managed to display strings/messages on my LCD from the pic alone. It is only after my sensor that is connected to my pic micro-controller board, it is unable to send any ASCII characters onto my LCD screen like it should.
 
as my school helped me download my mplab ide for me there was no debugging tool provided for the students, is there anywhere i can get the debugger then? also, whats the difference of setting individual bits than setting the whole register for it?
 
jellyfishers
#7
ric
Super Member
  • Total Posts : 28951
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: UART serial communication with a digital sensor 2020/10/20 20:33:00 (permalink) ☄ Helpfulby jellyfishers 2020/10/20 23:08:28
+1 (1)
How is your LCD connected to your PIC?
You should be able to remove the sensort reading code, get the LCD working, then add the new code back bit by bit until the LCD fails to see which bit causes the problem.
 
Note, in this code, you are letting your main() function exit.
You should never do that in an embedded environment, as there is no operating system to return to.
If you want your code to run once, then stop, you should add a "do nothing" loop forever to the end of your code.
i.e.
    while (1)    //loop forever
        ;

 

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!
#8
davea
Super Member
  • Total Posts : 504
  • Reward points : 0
  • Joined: 2016/01/28 13:12:13
  • Location: Tampa Bay FL USA
  • Status: online
Re: UART serial communication with a digital sensor 2020/10/20 22:36:21 (permalink) ☄ Helpfulby jellyfishers 2020/10/20 23:08:22
+1 (1)
    for (i=0; i<10; i++) {
         if ( PIR1bits.RCIF ==1) //if byte is received
        {
            temp_reading[i] = RCREG;
            PIR1bits.RCIF = 0; // clear flag
            while{!PIR1bits.RCIF}; // wait for next chr
      }

not the best way but shows your error
#9
ric
Super Member
  • Total Posts : 28951
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: UART serial communication with a digital sensor 2020/10/20 22:59:52 (permalink)
+1 (1)
RCIF is a read only flag, so it's pointless trying to clear it.

I would suggest that code should be

    for (i=0; i<10; i++) {
            while{!PIR1bits.RCIF}; // wait for next chr
            temp_reading[i] = RCREG;
      }


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!
#10
jellyfishers
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2020/10/12 18:47:04
  • Location: 0
  • Status: offline
Re: UART serial communication with a digital sensor 2020/10/20 23:07:34 (permalink)
0
May I ask why you suggested while{!PIR1bits.RCIF}; ?
#11
davea
Super Member
  • Total Posts : 504
  • Reward points : 0
  • Joined: 2016/01/28 13:12:13
  • Location: Tampa Bay FL USA
  • Status: online
Re: UART serial communication with a digital sensor 2020/10/20 23:22:09 (permalink)
0
@ric forgot reading buffer clears flag 
if you don't wait for the next chr, temp_reading buffer will contain 10 of the same value
 
 
#12
davea
Super Member
  • Total Posts : 504
  • Reward points : 0
  • Joined: 2016/01/28 13:12:13
  • Location: Tampa Bay FL USA
  • Status: online
Re: USART Serial Communication 2020/10/20 23:59:57 (permalink)
+1 (1)
 

  while(1){
    for (i=0; i<10; i++) {
         if ( PIR1bits.RCIF ==1) {   //if byte is received
            temp_reading[i] = RCREG;
            while{!PIR1bits.RCIF};
            }
    }         // <<<<<<<<<<<<<<<<
         lcd_write_cmd(0xC0);
    for (i=0; i<10; i++) {
        lcd_write_data (temp_reading [i]);
    }
  }
}
 

brackets are all wrong




#13
ric
Super Member
  • Total Posts : 28951
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: UART serial communication with a digital sensor 2020/10/21 00:12:36 (permalink)
0
jellyfishers
May I ask why you suggested while{!PIR1bits.RCIF}; ?

As davea suggested, you must wait for each character to be received. Your if() code does not do that correctly.
Remember your code is looping MUCH faster than the characters are being received.

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!
#14
oliverb
Super Member
  • Total Posts : 342
  • Reward points : 0
  • Joined: 2009/02/16 13:12:38
  • Location: 0
  • Status: offline
Re: USART Serial Communication 2020/10/21 00:19:19 (permalink)
0
Is your board something you've created for a purpose, or is it a development board. If it is a dev board can you share some details just in case it turns out the temperature data is never even reaching the PIC in the first place.
 
Also do you have the ability to test PIC to PC serial connection? If absolutely nothing works I'd try a test program that just echoes everything it recieves. If that doesn't work I'd unplug the PIC and wire link the serial pins on the board just to prove the test jig works. Back when I used assembly language I wasted a lot of time on serial communication because of some subtle mistakes in configuration.
 
I have a distant recollection that the pic18F4550 has some functions on unusual pins to compensate for all the pins the USB port takes up. In particular I seem to recall the SPI port shared a pin with the USART.
#15
davea
Super Member
  • Total Posts : 504
  • Reward points : 0
  • Joined: 2016/01/28 13:12:13
  • Location: Tampa Bay FL USA
  • Status: online
Re: USART Serial Communication 2020/10/21 11:25:23 (permalink)
0
  while(1){
    // you need to get the digital temperature sensor to start sending data
    // the data may NOT be ASCII if so you must convert it...
      i =  0;
      x = 2000; //  depends on baud rate and OSC frequency
  do{ 
      if ( PIR1bits.RCIF ==1) { //if byte is received
          temp_reading[i++] = RCREG;
          x = 2000; // reset timer
          } --x;
     } while { i < 10 || x != 0); // 
     // if x = 0; 10 bytes NOT recived, timed out
 
    for (i=0; i<10; i++) {
         if ( PIR1bits.RCIF ==1) { //if byte is received
            temp_reading[i] = RCREG; 
            while{!PIR1bits.RCIF}; // wait for next byte
            }
    } // <<<<<<<<<<<<<<<<
 
         lcd_write_cmd(0xC0);
    for (i=0; i<10; i++) {
        lcd_write_data (temp_reading [i]);
    }
  }
}

and don't use INT i (16bits) when the max value will fit into char
and maybe the temperature sensor part # as well
I must say the code just looks "wacked"
davea
post edited by davea - 2020/10/21 12:59:47
#16
Jump to:
© 2020 APG vNext Commercial Version 4.5