Continue to Site

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.

  • 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.

TSTA,RCSTA and SPBRG

Status
Not open for further replies.

antoni1

New Member
HI
Does anyone know how to initialise TSTA , RCSTA and SPBRG in C language for 16F877?
I use 4Mhz external crystal oscilator
i thing that for
TSTA= 11100100
RCSTA=10010000
but im' not sure
thanks
 
In HiTech picclite you can address each individual option bit, for example BRGH=1; . Which compiler are you using?

antoni1 said:
TSTA= 11100100
RCSTA=10010000
I see no reason why the above would not work though :roll: .

Check page 98 of the 16f877 datasheet for the value of SPBRG. It depends on the baud rate that you want the USART to operate on (e.g SPBRG=12 Baud Rate = 19.2k). I think that @ 4MHz and high data rate this is the relevant table. Check to be sure though!

  • BRGH=1, Fosc=4MHz
    -----------------
    Baud - SPBRG
    1200 - 207.3
    2400 - 103.2
    4800 - 51.1
    9600 - 25.0
    19200 - 12.0
    38400 - 5.5
    57600 - 3.3
    115200 - 1.2
 
You can't have bit string constants in C. You have to convert the binary data to hex.

TSTA = 0xE4;
RCSTA = 0x90;

It's easy to do binary to hex conversion each 4 bits translate to one hex character. The windows calculator will do it for you too if you put it in scientific mode.

I don't use PICS so I can't say whether the bits are set correctly.
 
bmcculla said:
You can't have bit string constants in C. You have to convert the binary data to hex.

:oops: Oooppss, sorry to have misled!

I have used bit register initialisation in this way:
Code:
TSTA=0b11100100;
RCSTA=0b10010000;
 
BAUD RATE

HI spirosd
I'm using MPLAB with CC5X compiler
How can i find which Baud rate should i use? There's a formula
Baud Rate = FOSC/(16(X+1)) (I ll use BRGH = 1 (High Speed))
I know the fosc=4Mhz but what about X=????
X = value in SPBRG (0 to 255) how i decide about X=???

Also for TSTA i set the bit 6=1 (1 = Selects 9-bit transmission) because bits TRISC<7:6> have to be set in order to configure pins RC6/TX/CK

but for RCSTA i set the bit 6=0 (0 = Selects 8-bit reception)
but i don't know if this will work--> using 9 bit transmission for TSTA and 8 bit recieve for RCSTA
Shall i set both TSTA and RCSTA to 9 bit transmission?


thanks
 
Re: BAUD RATE

antoni1 said:
HI spirosd
I'm using MPLAB with CC5X compiler
How can i find which Baud rate should i use? There's a formula
Baud Rate = FOSC/(16(X+1)) (I ll use BRGH = 1 (High Speed))
I know the fosc=4Mhz but what about X=????
X = value in SPBRG (0 to 255) how i decide about X=???

You don't need to calculate anything, tables in the datasheet give you all the information you generally need - although the formulas are there if you happen to be using a really obscure clock frequency.

Also for TSTA i set the bit 6=1 (1 = Selects 9-bit transmission) because bits TRISC<7:6> have to be set in order to configure pins RC6/TX/CK

but for RCSTA i set the bit 6=0 (0 = Selects 8-bit reception)
but i don't know if this will work--> using 9 bit transmission for TSTA and 8 bit recieve for RCSTA
Shall i set both TSTA and RCSTA to 9 bit transmission?


thanks

I don't see what you are on about with TRISC?, it's totally seperate to the serial registers, you can set the USART to be 8 or 9 bit as you wish, regardless of TRISC.

This is the serial setup routine from my tutorials, it sets 8 bit mode at 9600 baud, with a 20MHz clock.

Code:
SER_INIT
            	BSF     STATUS, RP0           ;select bank 1
     		MOVLW   d'129'                ;9600 baud @ 20 Mhz Fosc +0.16 err
     		MOVWF   SPBRG
     		MOVLW   b'00100100'           ;brgh = 1
     		MOVWF   TXSTA                 ;enable Async Transmission, set brgh
            	BCF     STATUS, RP0           ;select bank 0
     		MOVLW   b'10010000'
     		MOVWF   RCSTA                 ;enable Async Reception
            	RETURN
 
Hi antony1,

It might be easier if we back up and try to determine what your goal here is with the USART

1) do you need sync or async comms
2) what is the size of data that you need to send
3) what are your time constraints
4) is this pic to pic / pic to computer

yada yada ... you get the idea.

As Nigel said, just read the table, once you have selected the relevant baud rate that is required; that will give you SPBRG.

I am not familiar with the CC5X compiler, does it have a USART sample that can be used as a basis?

For what it's worth, (as an example) I am including the initialisation that I am using in one of my projects at the moment (16f877, 19.2k Baud)
Code:
        SPBRG=12;
        BRGH=1;         // high data rate for sending
        SYNC=0;         //asynchronous
        SPEN=1;         //enable serial port pins
        CREN=1;         //enable reception
        TXIE=0;         //disable tx interrupts
        RCIE=1;         //enable rx interrupts
        TX9=0;          //8-bit transmission
        RX9=0;          //8-bit reception
        TXEN=0;         //reset transmitter
        TXEN=1;         //enable the transmitter

You will need to cross reference the relevant bits with the datasheet to construct your initialisation bit string.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top