Thread: PIC16F876 's USART

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

    PIC16F876 's USART

    i am doing project use a PIC16F876 to control motor.
    Compiler is Mplap.

    this program should print a > prompt character when first started and whenever it is ready to accept a new command.

    after running this program,
    the following things are occur:
    >/nŮ>/nŮ>/nŮ>/nŮ>çzrů˝šůů˝šŮů˝še>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>Ď
    zr
    ů˝šůů˝šŮů˝še>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>Ďzrů˝šůů˝šŮů˝še>/nŮ>/

    >/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>Ďzrů˝šůů˝šŮů˝še>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/

    >/nŮ>/nŮ>/nŮ>/nŮ>Ďzrů˝šůů˝šŮů˝še>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>Ď
    zr
    ů˝šůů˝šŮů˝še>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>Ďzrů˝šůů˝šŮů˝še>/nŮ>/

    >/nŮ>/nŮ>/nŮ>/nŮ>/nُjˇm>~jˇjˇjˇjˇo>~jˇjˇjˇjˇů˝šŮů˝še>/nŮ>/nŮ>/nŮ>/nŮ>
    /n
    Ů>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>Ďzrů˝šůů˝šŮů˝še>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>
    /n
    Ů>/nŮ>Ďzrů˝šůů˝šŮů˝še>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>Ďzrů˝šůů˝šŮů
    ˝š
    e>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>Ďzrů˝šůů˝šŮů˝še>/nŮ>/nŮ>/nŮ>/nŮ>
    /n
    Ů>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>Ďzrů˝šůů˝šŮů˝še>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>
    /n
    Ů>/nŮ>Ďzrů˝šůů˝šŮů˝še>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>Ďzrů˝šůů˝šŮů
    ˝š
    e>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>Ďzrů˝šůů˝šŮů˝še>/nŮ>/nŮ>/nŮ>/nŮ>
    /n
    Ů>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>Ďzrů˝šůů˝šŮů˝še>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>
    /n
    Ů>/nŮ>Ďzrů˝šůů˝šŮů˝še>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>Ďzrů˝šůů˝šŮů
    ˝š
    e>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>Ďzrů˝šůů˝šŮů˝še>/nŮ>/nŮ>/nŮ>/nŮ>
    /n
    Ů>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>Ďzrů˝˙>/n>/n~>/nv>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/n
    Ů>
    /nŮ>/nŮ>/nٞzrů˝šůů˝šŮů˝še>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nٞzrů˝šůů
    ˝š
    Ůů˝še>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nٞzrů˝šůů˝šŮů˝še>/nŮ>/nŮ>/nŮ>/

    >/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nٞzrů˝šůů˝šŮů˝še>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/nŮ>/n
    Ů>
    /nŮ>/nŮ>>>>>Ÿ>€>ƒ>î>>p>v>ƒ>¸>ť>p>Ć>w>†>ă>ˇ>6>|>ž>ž>ß>s> ö>ď>ž>űžňň2ňúóóór>>p>v>
    ƒ>
    ¸>ť>p>Ć>w>†>ă>ˇ>6>|>ž>ž>ß>s>ö>ď>ž>űžňň2ňúóóór>>p>v >ƒ>¸>ť>p>Ć>w>†>ă>ˇ>6>|>ž>ž>ß
    >s
    >ö>ď>ž>űžňň2ňúóóór>>p>v>



    Why? thanks!

    #include <pic.h>
    #include <stdio.h>
    #include "pwm.h"

    #define Fosc (4000000L)

    void sendByte(char c);
    unsigned char adcChannel = 1;
    const char string[] = ">";

    void initUSART()
    {

    BRGH = 0; // high speed data rate "Fosc/(16(X+1))
    SPBRG = 207;
    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
    }

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

    void sendByte(char c){
    /* Wait until TXIF == 0 */
    while (0==TXIF);

    /* Now set TXREG */
    TXREG = c;
    }

    unsigned char getByte()
    {
    if (OERR)
    {
    CREN = 0;
    CREN = 1;
    }
    if (RCIF)
    return RCREG;
    else return 0x00;
    }
    void pwm_init500()
    {
    PR2 = 0b01111100; //PERIOD OF 500HZ. FROM ONLINE FREQUENCY CALCULATOR
    CCPR1L= 0b00001100; //duty cycle = 40%
    //CCPR1L= 0x80;
    TRISC2 = 0;
    CCP1CON = 0b00000011;
    T2CKPS1 = 1; //SETTING PRESCALER OF 1:16
    TMR2ON = 1; //TIMER 2 ON BIT.
    }
    /*motor forward running*/
    void forward()
    {
    RA0=0;
    RA1=1;
    RA2 = 1;
    }

    /*main function*/
    main(){

    unsigned char temp;
    unsigned char f;

    ADFM = 1;
    TRISC = 0;

    TRISA = 0;

    initUSART();
    pwm_init500();
    sendBytes(string);

    if(RCIF)
    {
    temp = getByte();
    sendByte(temp);
    if(temp = f) forward();

    }
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I don't know if this is related to your bug, but:

    >if(temp = f) forward();
    Should be:
    if(temp == f) forward();

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >if (OERR)
    >{
    >CREN = 0;
    >CREN = 1;
    >}

    What's the purpose of this?

    Function main should return a value.

  4. #4
    tombear2
    Guest
    I updated my program.

    Usart can make microcontroller to communicate with host computer.

    this program was compiled well. compile is mplap.

    but nothing display on Terminal.


    #include <pic.h>
    #include <stdio.h>
    #include "pwm.h"

    #define Fosc (4000000L)
    // Function declaration

    void adc_read(unsigned char channel);
    void initUSART();
    void sendBytes(const char *tx);
    char *getInput();
    unsigned char getByte();
    void pwm_init500();
    void forward();
    void reverse();
    void stop();
    void sendByte(char c);
    void main(){

    unsigned char temp;
    char *str1;

    ADFM = 1;
    TRISC = 0;

    TRISA = 0;
    initUSART();
    pwm_init500();
    // sendBytes(string);

    while(1)
    {
    str1 = getInput();
    sendBytes(str1);
    }

    /* temp = getByte();
    while(temp
    temp = getByte();
    if(RCIF){
    sendByte(getByte);
    if(temp == 'f') forward();
    */
    }

    void adc_read(unsigned char channel)
    {
    ADCON0 = (channel <<3) + 0xC1;
    ADGO = 1;
    while(ADGO)
    continue;
    }

    void initUSART()
    {

    BRGH = 0; // high speed data rate "Fosc/(16(X+1))
    SPBRG = 207;
    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
    }

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

    void sendByte(char c){
    /* Wait until TXIF == 0 */
    while (0==TXIF);//TXIF indicates the status TXREG -wait for TXREF to be empty

    /* Now set TXREG */
    TXREG = c;
    }

    unsigned char getByte()
    {
    if (OERR) //Overrun Error bit(can be cleared by clearing bit CREN)
    {
    CREN = 0;
    CREN = 1;
    }
    if (RCIF)
    return RCREG;
    else return 0x00;
    }

    // try to get character from keyboard
    char *getInput()
    {
    char * buff;
    char temp;
    int i;
    temp = getByte();
    while (temp != 13) // "13" is enter key
    {
    *(buff+i) = temp;
    i++;
    temp = getByte();
    }
    *(buff+i) = '\0';
    return buff;
    }
    void pwm_init500()
    {
    PR2 = 0b01111100; //PERIOD OF 500HZ. FROM ONLINE FREQUENCY CALCULATOR
    CCPR1L= 0b00001100; //duty cycle = 40%
    //CCPR1L= 0x80;
    TRISC2 = 0;
    CCP1CON = 0b00000011;
    T2CKPS1 = 1; //SETTING PRESCALER OF 1:16
    TMR2ON = 1; //TIMER 2 ON BIT.

    }

    void forward()
    {
    RA0=0;
    RA1=1;
    RA2 = 1;
    }
    void reverse()
    {
    RA0=1;
    RA1=0;
    }
    void stop()
    {
    RA0=RA1;
    }

    main(){

    unsigned char temp;
    char *str1;


    ADFM = 1;
    TRISC = 0;

    TRISA = 0;

    initUSART();
    pwm_init500();
    // sendBytes(string);

    while(1)
    {
    str1 = getInput();
    sendBytes(str1);
    }

    /* temp = getByte();
    while(temp
    temp = getByte();
    if(RCIF){
    sendByte(getByte);
    if(temp == 'f') forward();
    */
    }

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Please use code tags

Popular pages Recent additions subscribe to a feed