CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

SFR bit setting

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
aroonkriss



Joined: 03 Dec 2012
Posts: 19
Location: kerala

View user's profile Send private message

SFR bit setting
PostPosted: Sun Dec 30, 2012 9:49 pm     Reply with quote

Hi all,

I need to set individual bits in SFRs. I tried the following code but it didn't work.
Code:

#byte U1MODE=getenv("SFR:U1MODE")
#byte U2MODE=getenv("SFR:U2MODE")

#use rs232(baud=9600,parity=N,xmit=PIN_C13,rcv=PIN_C14,bits=8,stream=UART1)
#use rs232(baud=2400,parity=N,xmit=PIN_F5,rcv=PIN_F4,bits=8,stream=UART2)


bit_set(U1MODE,10);  // to enable the alternate pins for uart1

bit_clear(U2MODE,15); // to disable the uart

I checked the uart2 part, it was communicating even though I disabled the enable bit.

I need to use the alternate pins of uart1. So I need to set the 'altio' bit of u1mode register.

PLEASE HELP ME. ANY HELP WOULD BE APPRECIATED.
_________________
I dream world peace
aroonkriss



Joined: 03 Dec 2012
Posts: 19
Location: kerala

View user's profile Send private message

SFR bit setting
PostPosted: Sun Dec 30, 2012 11:13 pm     Reply with quote

I used the following code and it worked.
Code:

#word U1MODE=0x20C
#word U2MODE=0x216

#bit  ALTIO = U1MODE.10   
#bit  U2EN = U2MODE.15
#bit  U1EN = U1MODE.15

But I wish to know what was my mistake with the earlier code. Thanks in advance.
_________________
I dream world peace
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Mon Dec 31, 2012 2:11 am     Reply with quote

bit_set(U1MODE,10); // to enable the alternate pins for uart1

Allows you to set a bit in a variable, but is _limited to the size of the variable_.

#byte U1MODE=getenv("SFR:U1MODE")

maps a _byte_ variable at the address - 8 bits

#word U1MODE=getenv("SFR:U1MODE")

maps a _word_ variable at the address - 16 bits

You can't access 'bit 10', in a byte sized variable.

If you code:
Code:

#byte U1MODE=getenv("SFR:U1MODE")
#byte U2MODE=getenv("SFR:U2MODE")

bit_clear(U2MODE,15); // to disable the uart

Then look at the assembler:
Code:

....................    bit_clear(U2MODE,15);  // to enable the alternate pins for uart1
.................... 

No instruction is generated, since you are trying to access a bit beyond the end of the variable.
However code as:
Code:

#word U1MODE=getenv("SFR:U1MODE")
#word U2MODE=getenv("SFR:U2MODE")

bit_clear(U2MODE,15); // to disable the uart

The assembler is then:
Code:

....................    bit_clear(U2MODE,15); // to disable the uart
0130:  BCLR.B  217.7

Which is accessing the seventh bit in the byte after 'U2MODE'.

As a more 'elegant' route, why not use the compiler's knowledge of bits:

Code:

#bit UART_ALTIO=getenv("BIT:ALTIO")
#bit UART2_ENABLE=getenv("BIT:U2MODE.UARTEN")
 
   UART_ALTIO=TRUE;
   UART2_ENABLE=FALSE;

Or use the compiler's ability to handle this for you:
Code:

#use rs232(baud=9600,parity=N,UART1A,bits=8,stream=UART1)
//select UART1 on alternate pins (1A)

#use rs232(baud=2400,parity=N,xmit=PIN_F5,rcv=PIN_F4,bits=8,stream=UART2)

    setup_uart(FALSE,UART2); //turn off UART2


Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group