• AVR Freaks

Hot!PIC 18F4550 with multiple UART

Page: 123 > Showing page 1 of 3
Author
othmanetalmoute
New Member
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2020/03/23 06:57:11
  • Location: 0
  • Status: offline
2020/03/24 00:54:14 (permalink)
0

PIC 18F4550 with multiple UART

hello im stuck with a probleme in PIC18F4550, i need to communicate with multiple devices (3), so im trying to make 2 soft_UART, with 1 hard_UART to make it 3, but this aint work for me, can any one help me plz, working on mplab x ide v5.3 and coding with C, thank you
#1

40 Replies Related Threads

    ric
    Super Member
    • Total Posts : 27750
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC 18F4550 with multiple UART 2020/03/24 15:04:40 (permalink)
    +2 (2)
    Are some of the UARTS transmit only, or do they all have to receive as well?
    How are you implementing them?
    All your own code, or some peripheral library?
    Do you have to stick with the ancient PIC18F4550?
     

    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
    katela
    Super Member
    • Total Posts : 1816
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: offline
    Re: PIC 18F4550 with multiple UART 2020/03/24 15:24:23 (permalink)
    +3 (3)
    As above,
    If your main requirement is UART (3 devices), do you have to use that ancient PIC with only 1-UART? Do you need the USB communication?
    If you select a PIC with more hardware UART peripherals, it will make your life easier, for example the PIC18F45Q43 has 5-UART.
    There are some MCUs you can choose from. Use the MAPS to select one that meet your requirements. 

    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
    #3
    othmanetalmoute
    New Member
    • Total Posts : 21
    • Reward points : 0
    • Joined: 2020/03/23 06:57:11
    • Location: 0
    • Status: offline
    Re: PIC 18F4550 with multiple UART 2020/03/25 00:52:51 (permalink)
    0
    well, yes i have to stick with this pic, cz it s a school project and they got only this pic, and the pic need to read from 2 devices, and read/transmit to 1 device, i got stucked with soft_uart solution aint work for me, so i mooved to use a mux 2bits, can you gimme an advice please ?
    #4
    oliverb
    Super Member
    • Total Posts : 285
    • Reward points : 0
    • Joined: 2009/02/16 13:12:38
    • Location: 0
    • Status: offline
    Re: PIC 18F4550 with multiple UART 2020/03/25 01:27:58 (permalink)
    +1 (1)
    OK first question is do you actually need to implement USB or do you just happen to have to use that PIC?
     
    As far as I'm aware the USB interface is quite demanding, including generating interrupts, and if you use it you kind of need to code round it.
     
    Do you have a choice of what baud rate to use.
     
    Do you have to be able to receive at any time or just in response to a command?
     
    Do you understand what the "software uart" code is doing or are you just trying to link it in as a magic box?
     
    Generally transmitting is easy, your program just needs to perform the right bit-flips at the right time, so you could hard code sending a specific letter that way. A little more work and you have a procedure that can be supplied a byte and it will send it.
     
    Recieving is harder as you have to detect the start bit and synchronise to it.
     
    #5
    ric
    Super Member
    • Total Posts : 27750
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC 18F4550 with multiple UART 2020/03/25 02:16:51 (permalink)
    +2 (2)
    othmanetalmoute
    well, yes i have to stick with this pic, cz it s a school project and they got only this pic, and the pic need to read from 2 devices, and read/transmit to 1 device, i got stucked with soft_uart solution aint work for me, so i mooved to use a mux 2bits, can you gimme an advice please ?

    You still haven't made clear if this is all happening at the same time, or if only one interface is active at a time.
    A mux will certainly only let you talk to one at a time.
    We still don't know WHAT software uart code you are trying to use.
    Well written software uart code most certainly CAN work if used correctly.
     

    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!
    #6
    othmanetalmoute
    New Member
    • Total Posts : 21
    • Reward points : 0
    • Joined: 2020/03/23 06:57:11
    • Location: 0
    • Status: offline
    Re: PIC 18F4550 with multiple UART 2020/03/25 03:27:14 (permalink)
    0
    well the pic need to communicate to one at time, will explaine u the project, is to make an embedded system to locate and secure cars, so i need a fingerprint at first so if it s the owner nothing will happend, else the pic will read the data from GPS and send it to GSM, then the GSM will send it to the owner, owner can send a msg too, dont need the USB i guess, only at first to programme it, yes i guess my software uart wasnt correct cz i made changes on the simple uart(hard one) changed the ports but didnt work 
    #7
    oliverb
    Super Member
    • Total Posts : 285
    • Reward points : 0
    • Joined: 2009/02/16 13:12:38
    • Location: 0
    • Status: offline
    Re: PIC 18F4550 with multiple UART 2020/03/25 04:02:26 (permalink)
    +1 (1)
    OK well I'm not sure about the fingerprint reader, hopefully that will have some kind of flow control but as it kicks off the process you might get away with an infinite wait loop at that point.
     
    The GPS should only react to a query so you should be able to send the query then wait for the position data.
     
    #8
    othmanetalmoute
    New Member
    • Total Posts : 21
    • Reward points : 0
    • Joined: 2020/03/23 06:57:11
    • Location: 0
    • Status: offline
    Re: PIC 18F4550 with multiple UART 2020/03/25 04:47:22 (permalink)
    0
    well can i get any help to use mux 2 bit for tx and mux 2 bit for rx with pic 18f4550
    #9
    oliverb
    Super Member
    • Total Posts : 285
    • Reward points : 0
    • Joined: 2009/02/16 13:12:38
    • Location: 0
    • Status: offline
    Re: PIC 18F4550 with multiple UART 2020/03/25 05:22:41 (permalink)
    +1 (1)
    Well if you go with mux it is pretty easy. I'd suggest using an analogue multiplexer, that way you can use the same chip for in and for out. There will be two control pins to select circuits. On the transmit side you might need four pull-up resistors, maybe 10-100K.
     
     74HC4051
     
    It has two 4:1 muxes which can handle input or output. If you use digital multiplexers you'll need a different one for input and for output because a digital multiplexer and a demultiplexer are different parts.
     
     
    post edited by oliverb - 2020/03/25 06:14:54
    #10
    othmanetalmoute
    New Member
    • Total Posts : 21
    • Reward points : 0
    • Joined: 2020/03/23 06:57:11
    • Location: 0
    • Status: offline
    Re: PIC 18F4550 with multiple UART 2020/03/25 06:19:28 (permalink)
    0
    oliverb
    Well if you go with mux it is pretty easy. I'd suggest using an analogue multiplexer, that way you can use the same chip for in and for out. There will be two control pins to select circuits. On the transmit side you might need four pull-up resistors, maybe 10-100K.
     
     74HC4051
     
    It has two 4:1 muxes which can handle input or output. If you use digital multiplexers you'll need a different one for input and for output because a digital multiplexer and a demultiplexer are different parts.
     
     




    well i added this mux with my pic with a decalartion of (ANSELA= 0b00001111, and TRISA=0) and in my main function i added (PORTAbits.AN0=0)&&(PORTAbits.AN1=0) do(my function) but it aint work can u help me please.
    #11
    pcbbc
    Super Member
    • Total Posts : 1703
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: PIC 18F4550 with multiple UART 2020/03/25 07:26:43 (permalink)
    +1 (1)
    othmanetalmoute
    Well I added this mux with my pic with a decalartion of (ANSELA= 0b00001111, and TRISA=0) and in my main function I added (PORTAbits.AN0=0)&&(PORTAbits.AN1=0) do(my function) but it doesn't work. Can you help me please?

    We'll need a lot more to go on than that.  It's like you think we can see your screen and your schematic from here...
     
    But that should probably be...
    (PORTAbits.AN0==0)&&(PORTAbits.AN1==0)

    Assuming you are testing the bits?
    Although TRISA=0 immplies they are OUTPUTS.
     
    1. Post actual code, not descriptions.  Ideally ALL of your code.
    2. "It aint work" is not an adequate problem description:
    a) What did you try?
    b) What did you expect to see?
    c) What did you actually see?
    d) How did you measure this?
    #12
    othmanetalmoute
    New Member
    • Total Posts : 21
    • Reward points : 0
    • Joined: 2020/03/23 06:57:11
    • Location: 0
    • Status: offline
    Re: PIC 18F4550 with multiple UART 2020/03/25 09:45:16 (permalink)
    0
    pcbbc
    othmanetalmoute
    Well I added this mux with my pic with a decalartion of (ANSELA= 0b00001111, and TRISA=0) and in my main function I added (PORTAbits.AN0=0)&&(PORTAbits.AN1=0) do(my function) but it doesn't work. Can you help me please?

    We'll need a lot more to go on than that.  It's like you think we can see your screen and your schematic from here...
     
    But that should probably be...
    (PORTAbits.AN0==0)&&(PORTAbits.AN1==0)

    Assuming you are testing the bits?
    Although TRISA=0 immplies they are OUTPUTS.
     
    1. Post actual code, not descriptions.  Ideally ALL of your code.
    2. "It aint work" is not an adequate problem description:
    a) What did you try?
    b) What did you expect to see?
    c) What did you actually see?
    d) How did you measure this?



    #include<pic18f4550.h>
    #include<string.h>
    #include<stdio.h>
    #include<stdlib.h>
    #include "Configuration_Header_File.h"
    #include "LCD_20x4_H_file.h"
    #include "USART_Header_File.h"

    float convert_to_degrees(float);
    #define GGA_Buffer_Size 80
    #define GGA_Pointers_Size 20
    #define TRISA 0
    ANSELA = 0xb00001111;
    char GGA_Buffer[GGA_Buffer_Size]; /* to store GGA string */


    void main(void) {
    unsigned long int Time;
    float Latitude,Longitude,Altitude;
    char GPS_Buffer[15];

    OSCCON = 0x72; /* use internal osc. of 8MHz Freq. */
    LCD_Init();
    INTCONbits.GIE=1; /* enable Global Interrupt */
    INTCONbits.PEIE=1; /* enable Peripheral Interrupt */
    PIE1bits.RCIE=1; /* enable Receive Interrupt */
    USART_Init(9600);
    ...
    ....
    void __interrupt() Serial_ISR()
    {
    while((PORTAbits.AN0=0)&&PORTAbits.AN1=0)){
    if(RCIF){
    GIE = 0; /* Disable global interrupt */
    unsigned char received_char = RCREG;
    if(RCSTAbits.OERR){ /* check if any overrun occur due to continuous reception */
    CREN = 0;
    NOP();
    CREN=1;
    }

    if(received_char =='$'){ /* check for '$' */
    GGA_Index = 0;
    IsItGGAString = 0;
    CommaCounter = 0;
    }
    else if(IsItGGAString == 1){ /* if true save GGA info. into buffer */
    if(received_char == ',' ) GGA_Pointers[CommaCounter++] = GGA_Index; /* store instances of ',' in buffer */
    GGA_Buffer[GGA_Index++] = received_char;
    }
    else if(GGA_CODE[0] == 'G' && GGA_CODE[1] == 'G' && GGA_CODE[2] == 'A'){ /* check for GGA string */
    IsItGGAString = 1;
    GGA_CODE[0] = 0; GGA_CODE[1] = 0; GGA_CODE[2] = 0;
    }
    else{
    GGA_CODE[0] = GGA_CODE[1]; GGA_CODE[1] = GGA_CODE[2]; GGA_CODE[2] = received_char;
    }
    }
    }
    well, here is a part of my code i removed the other function cz they got no probleme, well the programme has to read data from gps and show it on lcd, it works without mux but when adding the mux nothing apears on the lcd 
    #13
    ric
    Super Member
    • Total Posts : 27750
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC 18F4550 with multiple UART 2020/03/25 12:30:17 (permalink)
    +1 (1)
    What the hell is this code doing inside your interrupt service routine?
    void __interrupt() Serial_ISR()
    {
    while((PORTAbits.AN0=0)&&PORTAbits.AN1=0)){
     
    Also, NEVER twiddle with the GIE bit inside an ISR!
     
    Did you write this code from scratch, or take it from another project?
     

    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
    othmanetalmoute
    New Member
    • Total Posts : 21
    • Reward points : 0
    • Joined: 2020/03/23 06:57:11
    • Location: 0
    • Status: offline
    Re: PIC 18F4550 with multiple UART 2020/03/25 13:08:47 (permalink)
    0
    ric
    What the hell is this code doing inside your interrupt service routine?
    void __interrupt() Serial_ISR()
    {
    while((PORTAbits.AN0=0)&&PORTAbits.AN1=0)){
     
    Also, NEVER twiddle with the GIE bit inside an ISR!
     
    Did you write this code from scratch, or take it from another project?
     


    well, shall i write it outside the ISR ? on this ISR the pic need to communicate with the PIC
    #15
    mlp
    boots too small
    • Total Posts : 937
    • Reward points : 0
    • Joined: 2012/09/10 15:12:07
    • Location: previously Microchip XC8 team
    • Status: offline
    Re: PIC 18F4550 with multiple UART 2020/03/25 13:12:05 (permalink)
    0
    othmanetalmoute
     
    #include<pic18f4550.h>

    Replace this with
    #include <xc.h>

     
    #include<string.h>
    #include<stdio.h>
    #include<stdlib.h>

    Only keep these if you are actually using things declared or defined within them.
     
    ...
    ....

    This is not valid C code.
     
    well, here is a part of my code i removed the other function cz they got no probleme

    Since you can not find the problem in your code and have come here for advice, by definition you do not know what parts of your code are relevant. Post a complete compilable sample.

    Mark (this opinion available for hire)
    #16
    othmanetalmoute
    New Member
    • Total Posts : 21
    • Reward points : 0
    • Joined: 2020/03/23 06:57:11
    • Location: 0
    • Status: offline
    Re: PIC 18F4550 with multiple UART 2020/03/25 13:16:33 (permalink)
    0
     
    ...
    ....

    This is not valid C code.
     


     
    i removed the the code just to past here cz it s too large and replaced them by ...., my probleme is to read from the GPS using a mux 74HC4051
    #17
    1and0
    Access is Denied
    • Total Posts : 10928
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: PIC 18F4550 with multiple UART 2020/03/25 13:21:59 (permalink)
    0
    othmanetalmoute

     
    #define TRISA 0
    ANSELA = 0xb00001111;
     
     
    while((PORTAbits.AN0=0)&&PORTAbits.AN1=0)){
     
    GIE = 0; /* Disable global interrupt */


    A quick scan thru your code reveals the above lines that are problems. ;)
     
     
    #18
    othmanetalmoute
    New Member
    • Total Posts : 21
    • Reward points : 0
    • Joined: 2020/03/23 06:57:11
    • Location: 0
    • Status: offline
    Re: PIC 18F4550 with multiple UART 2020/03/25 13:44:27 (permalink)
    0

    #include<pic18f4550.h>
    #include<string.h>
    #include<stdio.h>
    #include<stdlib.h>
    #include "Configuration_Header_File.h"
    #include "LCD_20x4_H_file.h"
    #include "USART_Header_File.h"
    unsigned long int get_gpstime();
    float get_latitude(unsigned char);
    float get_longitude(unsigned char);
    float get_altitude(unsigned char);
    void convert_time_to_UTC(unsigned long int);
    float convert_to_degrees(float);
    #define GGA_Buffer_Size 80
    #define GGA_Pointers_Size 20
    #define TRISA 0
    ANSELA = 0b00001111;
    char GGA_Buffer[GGA_Buffer_Size]; /* to store GGA string */
    char GGA_CODE[3];
    unsigned char N_S, E_W; /* for getting direction polarity */
    unsigned char GGA_Pointers[GGA_Pointers_Size]; /* to store instances of ',' */
    char CommaCounter;
    char Data_Buffer[15];
    volatile unsigned int GGA_Index;
    volatile unsigned char IsItGGAString = 0;
    void main(void) {
    unsigned long int Time;
    float Latitude,Longitude,Altitude;
    char GPS_Buffer[15];

    OSCCON = 0x72; /* use internal osc. of 8MHz Freq. */
    LCD_Init();
    INTCONbits.GIE=1; /* enable Global Interrupt */
    INTCONbits.PEIE=1; /* enable Peripheral Interrupt */
    PIE1bits.RCIE=1; /* enable Receive Interrupt */
    USART_Init(9600);

    while(1){
    memset(GPS_Buffer,0,15);
    LCD_String_xy(1,0,"UTC Time: ");
    Time = get_gpstime(); /* Extract Time */
    convert_time_to_UTC(Time); /* convert time to UTC */
    LCD_String(Data_Buffer);
    LCD_String(" ");

    LCD_String_xy(2,0,"Lat: ");
    Latitude = get_latitude(GGA_Pointers[0]); /* Extract Latitude */
    Latitude = convert_to_degrees(Latitude); /* convert raw latitude in degree decimal*/
    sprintf(GPS_Buffer,"%.05f",Latitude); /* convert float value to string */
    LCD_String(GPS_Buffer); /* display latitude in degree */
    memset(GPS_Buffer,0,15);

    LCD_String_xy(3,0,"Long: ");
    Longitude = get_longitude(GGA_Pointers[2]);/* Extract Latitude */
    Longitude = convert_to_degrees(Longitude);/* convert raw longitude in degree decimal*/
    sprintf(GPS_Buffer,"%.05f",Longitude); /* convert float value to string */
    LCD_String(GPS_Buffer); /* display latitude in degree */
    memset(GPS_Buffer,0,15);

    LCD_String_xy(4,0,"Alt: ");
    Altitude = get_altitude(GGA_Pointers[7]); /* Extract Latitude */
    sprintf(GPS_Buffer,"%.2f",Altitude); /* convert float value to string */
    LCD_String(GPS_Buffer); /* display latitude in degree */
    }
    }
    unsigned long int get_gpstime(){
    unsigned char index;
    unsigned char Time_Buffer[15];
    unsigned long int _Time;

    /* parse Time in GGA string stored in buffer */
    for(index = 0;GGA_Buffer[index]!=','; index++){
    Time_Buffer[index] = GGA_Buffer[index];
    }
    _Time= atol(Time_Buffer); /* convert string of Time to integer */
    return _Time; /* return integer raw value of Time */
    }
    float get_latitude(unsigned char lat_pointer){
    unsigned char lat_index = lat_pointer+1; /* index pointing to the latitude */
    unsigned char index = 0;
    char Lat_Buffer[15];
    float _latitude;
    /* parse Latitude in GGA string stored in buffer */
    for(;GGA_Buffer[lat_index]!=',';lat_index++){
    Lat_Buffer[index]= GGA_Buffer[lat_index];
    index++;
    }
    lat_index++;
    N_S = GGA_Buffer[lat_index];
    _latitude = atof(Lat_Buffer); /* convert string of latitude to float */
    return _latitude; /* return float raw value of Latitude */
    }
    float get_longitude(unsigned char long_pointer){
    unsigned char long_index;
    unsigned char index = long_pointer+1; /* index pointing to the longitude */
    char Long_Buffer[15];
    float _longitude;
    long_index=0;

    /* parse Longitude in GGA string stored in buffer */
    for( ; GGA_Buffer[index]!=','; index++){
    Long_Buffer[long_index]= GGA_Buffer[index];
    long_index++;
    }
    long_index++;
    E_W = GGA_Buffer[long_index];
    _longitude = atof(Long_Buffer); /* convert string of longitude to float */
    return _longitude; /* return float raw value of Longitude */
    }
    float get_altitude(unsigned char alt_pointer){
    unsigned char alt_index;
    unsigned char index = alt_pointer+1; /* index pointing to the altitude */
    char Alt_Buffer[12];
    float _Altitude;
    alt_index=0;

    /* parse Altitude in GGA string stored in buffer */
    for( ; GGA_Buffer[index]!=','; index++){
    Alt_Buffer[alt_index]= GGA_Buffer[index];
    alt_index++;
    }
    _Altitude = atof(Alt_Buffer); /* convert string of altitude to float */
    return _Altitude; /* return float raw value of Altitude */
    }
    void convert_time_to_UTC(unsigned long int UTC_Time)
    {
    unsigned int hour, min, sec;

    hour = (UTC_Time / 10000); /* extract hour from integer */
    min = (UTC_Time % 10000) / 100; /* extract minute from integer */
    sec = (UTC_Time % 10000) % 100; /* extract second from integer*/
    sprintf(Data_Buffer, "%d:%d:%d", hour,min,sec); /* store UTC time in buffer */

    }
    float convert_to_degrees(float NMEA_lat_long){


    float minutes, dec_deg, decimal;

    int degrees;

    float position;
    degrees = (int)(NMEA_lat_long/100.00);
    minutes = NMEA_lat_long - degrees*100.00;
    dec_deg = minutes / 60.00;
    decimal = degrees + dec_deg;
    if (N_S == 'S' || E_W == 'W') { // return negative
    decimal *= -1;
    }
    /* convert raw latitude/longitude into degree format */
    return decimal;
    }
    while((PORTAbits.AN0=0)&&PORTAbits.AN1=0)){
    GIE = 0; /* Disable global interrupt */
    void __interrupt() Serial_ISR()
    {

    if(RCIF){

    unsigned char received_char = RCREG;
    if(RCSTAbits.OERR){ /* check if any overrun occur due to continuous reception */
    CREN = 0;
    NOP();
    CREN=1;
    }

    if(received_char =='$'){ /* check for '$' */
    GGA_Index = 0;
    IsItGGAString = 0;
    CommaCounter = 0;
    }
    else if(IsItGGAString == 1){ /* if true save GGA info. into buffer */
    if(received_char == ',' ) GGA_Pointers[CommaCounter++] = GGA_Index; /* store instances of ',' in buffer */
    GGA_Buffer[GGA_Index++] = received_char;
    }
    else if(GGA_CODE[0] == 'G' && GGA_CODE[1] == 'G' && GGA_CODE[2] == 'A'){ /* check for GGA string */
    IsItGGAString = 1;
    GGA_CODE[0] = 0; GGA_CODE[1] = 0; GGA_CODE[2] = 0;
    }
    else{
    GGA_CODE[0] = GGA_CODE[1]; GGA_CODE[1] = GGA_CODE[2]; GGA_CODE[2] = received_char;
    }

    }
    }
    }
    well here is my full code, and still got the probléme with the while

    #19
    ric
    Super Member
    • Total Posts : 27750
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC 18F4550 with multiple UART 2020/03/25 14:38:18 (permalink)
    +1 (1)
    othmanetalmoute

    ...
    well here is my full code, and still got the probléme with the while

    Because that is rubbish code.
    You didn't bother putting any comments in saying what you expect each line to do.
    Much of it is pointless, so we can't guess what you really intended.
     

    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!
    #20
    Page: 123 > Showing page 1 of 3
    Jump to:
    © 2020 APG vNext Commercial Version 4.5