Thread: embeded system

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    17

    Angry embeded system

    Hello, every one,

    I am doing an embeded system recently. I used Microchip PIC16F876 as microcontroller. Compiler is MPLAB. I used two RS-232s. one is the actual debugger and it connects to the host PC via RS-232. another is provided to comunicate to the host computer.

    Now, the program can be installed into microcontroller-pic16f876.
    However when I test microcontroller's Universal Serial Asynchronous Receive&Transmit (USART)module. I failed.

    The programming is following:
    #include <pic.h>
    #include <stdio.h>

    #define Fosc (4000000L)


    void initUSART(void);
    void sendBytes(const char *);
    void sendByte(char c);
    char getByte();


    const char TXstring[] = "\r\n\n\nCommunication port open.Please type a letter:";
    char TXrb0[]= "\r\n\nButton pressed";
    char buffer[]=" ";


    main(){
    TRISB = 1// for button RB0;
    initUSART();
    ADFM = 1;
    sendBytes(TXstring);
    while(1)
    {

    while(RB0) // loops until button is pressed
    {
    if(RCIF) sendByte(getByte());
    }
    sprintf(buffer,"Received:%x\r\n", (int)((ADRESL>2) + (ADRESH<<6)));
    sendBytes(buffer);
    }

    // for(;{
    // while(RB0)
    // {
    // ;
    // }

    // sprintf(TXrb0,"%d\n\r", (ADRESL + ADRESH*256) / 4);
    // sendBytes(TXrb0);
    //}
    }

    void initUSART()
    {
    BRGH = 1; // high speed data rate "Fosc/(16(X+1))
    // if(1) SPBRG = 51;
    // SPBRG = 25;
    SYNC = 0; //asnchronous serial port
    SPEN = 1; //enable serial port pins
    CREN = 1; //enable reception
    RCIE = 0; //disable rx interrupts
    TXIE = 0; //disable tx interrupts
    TX9 = 0; //8-bit transmission
    RX9 = 0; //8-bit reception
    TXEN =1; //enable transmission
    CREN = 1; // enable reception
    }

    void sendBytes(const char *tx)
    {
    while( *tx != 0 )
    {
    sendByte(*tx++);
    }
    }

    void sendByte(char c)
    {
    while (0 == TXIF)
    {
    TXREG = c;
    }
    }

    char getByte()
    {
    if(OERR)
    {
    CREN = 0;
    CREN =1;
    if (RCIF) return RCREG; else return 0x00;

    }
    }

    this program is built sucessfully. and runs well. unforunately, the Terminal displays nothing.

    There is anybody who can find any bugs.

    thanks!

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    First, use code tags, they make your program more readable.

    Second, what exactly is the problem?

    Third, I've put some NOTE's into the code.


    Code:
    #include <pic.h> 
    #include <stdio.h> 
    
    #define Fosc (4000000L) 
    
    void initUSART (void);
    void sendBytes (const char *);
    void sendByte (char c);
    char getByte ();
    
    const char TXstring[] = "\r\n\n\nCommunication port open.Please type a letter:"; 
    char TXrb0[]= "\r\n\nButton pressed"; 
    char buffer[]=" "; 
    
    int main ()
    { 
        TRISB = 1       // for button RB0; 
        initUSART(); 
        ADFM = 1; 
        sendBytes(TXstring); 
    
        while (1) 
        { 
            while(RB0) // loops until button is pressed 
            { 
                if (RCIF) 
                    sendByte(getByte()); 
            } 
            sprintf(buffer,"Received:%x\r\n", (int)((ADRESL>2) + (ADRESH<<6))); 
            sendBytes(buffer); 
        }    
    
        /* NOTE: function main must return a value */    
        return 0;
    } 
    
    void initUSART() 
    { 
        BRGH = 1;   // high speed data rate "Fosc/(16(X+1)) 
        SYNC = 0;   //asnchronous serial port 
        SPEN = 1;   //enable serial port pins 
        CREN = 1;   //enable reception 
        RCIE = 0;   //disable rx interrupts 
        TXIE = 0;   //disable tx interrupts 
        TX9 = 0;    //8-bit transmission 
        RX9 = 0;    //8-bit reception 
        TXEN =1;    //enable transmission 
        CREN = 1;   // enable reception 
    } 
    
    void sendBytes (const char *tx) 
    { 
        while (*tx != 0) 
        { 
            sendByte (*tx++); 
        } 
    } 
    
    void sendByte(char c) 
    {
        /* NOTE: if TXIF is a variable, then it is usual
           to write while (TXIF == 0) */ 
        while (0 == TXIF)
        { 
            TXREG = c; 
        } 
    } 
    
    char getByte() 
    { 
        if (OERR) 
        { 
            CREN = 0; 
            CREN = 1; 
            
            if (RCIF) 
                return RCREG; 
            else 
                return 0x00; 
        }
    }

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    17
    Thanks Shiro,

    I am not sure whether u used Matlap that is a compiler.

    I wrote this program ,'cause i want to test whether the microcontroller can response with the host computer.

    the problem is, when i compiled this program. it is ok. however, when i run this problem, nothing display.

    Do u have any idea?

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    I don't know that compiler.

    But the problem could be here:

    Code:
    void sendByte(char c) 
    {
        while (TXIF == 0)
        { 
            TXREG = c; 
        } 
    }
    If TXIF is not equal to 0 when entering the loop, the loop will imediately stop, so TXREG will never get the current value of c. I guess you need to wait until TXIF is equal to 0, so you could maybe do something like this:

    Code:
    void sendByte(char c) 
    {
        /* Wait until TXIF == 0 */
        while (TXIF != 0);
    
        /* Now set TXREG */    
        TXREG = c;
    }
    I don't know which processes can change TXIF, but in that case it may be possible that between the two statements TXIF is altered by a different process.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    17

    thanks

    yes, thanks, that is ok now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File System Implementation
    By dodgeviper in forum C Programming
    Replies: 9
    Last Post: 11-16-2007, 01:04 PM
  2. Using system icons
    By @nthony in forum Windows Programming
    Replies: 1
    Last Post: 01-13-2007, 07:56 PM
  3. Linux database system needed
    By BobS0327 in forum Tech Board
    Replies: 7
    Last Post: 06-11-2006, 03:56 PM
  4. measuring system resources used by a function
    By Aran in forum C Programming
    Replies: 1
    Last Post: 03-13-2006, 05:35 PM
  5. BIOS system and memory allocation problem
    By beely in forum Tech Board
    Replies: 9
    Last Post: 11-25-2003, 07:12 AM