1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

xc8 option register 16F505

Discussion in 'Microcontrollers' started by be80be, Aug 20, 2017.

  1. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,792
    Likes:
    134
    Location:
    morristown,tn
    Im trying to get my head around on how to set the option register I tried this don't work
    Code (text):

        T0CS = 0;        // Select TMR0 in Timer Mode  
        PSA  = 1;        // Prescaler assigned to WDT
     
    I read that you need to do a << all at once any help would be nice Thanks.
     
  2. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,792
    Likes:
    134
    Location:
    morristown,tn
    Well I'm trying to get a sw uart working on this chip 16f505
    I get all this
    Code (text):

    main.c:72: warning: (752) conversion to shorter data type
    main.c:72: warning: (751) arithmetic overflow in constant expression
    main.c:82: warning: (752) conversion to shorter data type
    main.c:86: warning: (752) conversion to shorter data type
    main.c:88: warning: (752) conversion to shorter data type
    main.c:98: warning: (752) conversion to shorter data type
    main.c:98: warning: (751) arithmetic overflow in constant expression
    main.c:104: warning: (752) conversion to shorter data type
    main.c:105: warning: (752) conversion to shorter data type
    main.c:108: warning: (752) conversion to shorter data type
    main.c:112: warning: (752) conversion to shorter data type
    main.c:104: warning: (759) expression generates no code
     
    Here the main c if anyone wants to help out it a remake of romanblack code using xc8
    and C.A.Roper work.
    Code (text):

    //____________________________________________________
    //
    //    Soft UART Test for PIC16F505
    //    Based on http://www.romanblack.com/bitbangserial.htm
    //
    //    C.A.Roper - 2013/02/23
    //____________________________________________________
    // CONFIG
    #pragma config OSC = ExtRC_CLKOUTEN// Oscillator Selection bits (External RC oscillator/CLKOUT function on RB4/OSC2/CLKOUT pin)
    #pragma config WDT = OFF         // Watchdog Timer Enable bit (WDT enabled)
    #pragma config CP = OFF         // Code Protection bit (Code protection off)
    #pragma config MCLRE = ON       // RB3/MCLR Pin Function Select bit (RB3/MCLR pin function is MCLR)

    // #pragma config statements should precede project file includes.
    #include <xc.h>
    #include <stdint.h>
    //#include <pic.h>
    //

    // Use project enums instead of #define for ON and OFF.
    // Prototypes of functions used
    //
    void send_serial_byte(unsigned char);
    unsigned char receive_serial_byte(void);
    //
    //    Global Defines
    //
    #define _XTAL_FREQ         4000000            // System Clock rate
    #define BAUDRATE        9600            // Desierd BAUD Rate (tested) Rates 9600 and 19200
    #define SER_BAUD         ((_XTAL_FREQ / 4) / BAUDRATE - 3)
    #define SER_BIT            0                // Signal MODE - 1 = Normal 0 = Inverted (Use Inverted for direct 232)
    //
    // I/O Pins
    //
    #define TxPin             PORTCbits.RC0    // Output Tx
    #define RxPin             PORTCbits.RC1    // Input  Rx
    //
    //    MAIN
    //
    void main(void)
    {
    //
    //    Setup - Run once
    //
        //    Default I/O Setup
        //
        OSCCAL= 0b01111110;
        PORTC        = 0;                // Turn all Outputs OFF
        TRISC        = 0b001010;            // INPUTS: GP1 = RxPin, GP3 = Button
        OPTION = 0b11110110;
    //
    //    Main Loop - Run Forever
    //  
        while(1)
        {  
            if(RxPin == !SER_BIT)        // wait here for serial byte start bit
            {
                send_serial_byte(receive_serial_byte());    // Echo back      
            }  
        }
    }

    //
    //    User Functions
    //
    void send_serial_byte(unsigned char data)
    {
        unsigned char i;
        i = 8;                                // 8 data bits to send
     
        TxPin = !SER_BIT;                    // make start bit
        TMR0  = (256 - SER_BAUD);            // load TMR1 value for first baud;
        while(TMR0 & (1 << 7));                // wait for baud
     
        while(i)                            // send 8 serial bits, LSB first
        {
            if(data & 1<<0)TxPin= SER_BIT;    // send data bit
            else           TxPin=!SER_BIT;
         
            data = (data >> 1);                // rotate right to get next bit
            i--;
            TMR0 -= SER_BAUD;                // load corrected baud value
            while(TMR0 & 1<<7);                // wait for baud
        }  
        TxPin =  SER_BIT;                    // make stop bit
        TMR0 -= SER_BAUD;                    // wait a couple of baud for safety
        while(TMR0 & 1<<7);
        TMR0 -= SER_BAUD;
        while(TMR0 & 1<<7);
    }

    unsigned char receive_serial_byte(void)
    {
        unsigned char RxChr;                // holds the serial byte that was received
        unsigned char i;                    // Bit Index
        i = 8;                                // 8 data bits to receive
     
        TMR0 =(256 - (SER_BAUD - 19));        // load TMR1 value to offset ~center of RxBit
        while(TMR0 & 1<<7);                    // wait for baud
        while(i)                            // receive 8 serial bits, LSB first
        {
            RxChr = (RxChr>>1);              // rotate right to store each bit
            if(  RxPin == 1)                // save data bit
                RxChr = RxChr | SER_BIT<<7;
            else RxChr = RxChr |!SER_BIT<<7;  
         
            i--;                            // Next Bit
            TMR0 -= SER_BAUD;                // load corrected baud value
            while(TMR0 & 1<<7);                // wait for baud
        }
     
        TMR0 -= SER_BAUD;                    // wait for stop bit, ensure serial port is free
        while(TMR0 & 1<<7);
        return RxChr;
    }
     
     
  3. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,008
    Likes:
    316
    Location:
    Brisbane Australia
    The errors are only warnings so your code should compile. The first warning is probably on the line TMR0 =(256 - (SER_BAUD - 19)); and is because 256 is too big to be a char so it's an int and it overflows. Try changing it to TMR0 =(0 - (SER_BAUD - 19));

    Mike.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,792
    Likes:
    134
    Location:
    morristown,tn

    Yes when I changed this OSCCAL= 0b01111110; from just trying to set the bit's as first post it complies. Got to sleepy to too test it.
     

Share This Page