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 support@ccsinfo.com

Read Port for BCD value

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







Read Port for BCD value
PostPosted: Thu Aug 07, 2003 3:22 pm     Reply with quote

I have connected a 10 position BCD rotary switch to B4-B7 and need to be able to place the value into a variable. When I programmed in PIC Basic Pro this was pretty straight forward:

BCD_In = PORTB & \%11110000 ' Read BCD switch on ports B4-B7.

I haven't been able to find anything simular in PIC-C.
Thanks in advance..
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516792
R.J.Hamlett
Guest







Re: Read Port for BCD value
PostPosted: Fri Aug 08, 2003 8:38 am     Reply with quote

:=I have connected a 10 position BCD rotary switch to B4-B7 and need to be able to place the value into a variable. When I programmed in PIC Basic Pro this was pretty straight forward:
:=
:=BCD_In = PORTB & \%11110000 ' Read BCD switch on ports B4-B7.
:=
:=I haven't been able to find anything simular in PIC-C.
:=Thanks in advance..
Lots of different possible answers, according to whether you want to keep the result in the high four bits (as you shown in your basic example), or move it to the bottom four bits. Also depending on the 'style' you want to use:

1)

BCD_In=INPUT_B() & 0B1111000;

This is the exact equivalent of your basic line, in CCS C.

2)

#byte PORTB=0x6
//Substitute the address for portB on your chip here

BCD_In=PORTB & 0B11110000;

Exact equivalent again, but with the different 'style' of I/O.

3)

BCD_In=INPUT_B()>>4;

This returns the top four bits of port B, now rotated into the low four bits of the result (so result is now 0-9 from the BCD switch, with no further conversion needed).

This could also be coded in the alternative style allready given.

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516820
R.J.Hamlett
Guest







Re: Read Port for BCD value
PostPosted: Fri Aug 08, 2003 9:03 am     Reply with quote

:=I have connected a 10 position BCD rotary switch to B4-B7 and need to be able to place the value into a variable. When I programmed in PIC Basic Pro this was pretty straight forward:
:=
:=BCD_In = PORTB & \%11110000 ' Read BCD switch on ports B4-B7.
:=
:=I haven't been able to find anything simular in PIC-C.
:=Thanks in advance..
Lots of different possible answers, according to whether you want to keep the result in the high four bits (as you shown in your basic example), or move it to the bottom four bits. Also depending on the 'style' you want to use:

1)

BCD_In=INPUT_B() & 0B1111000;

This is the exact equivalent of your basic line, in CCS C.

2)

#byte PORTB=0x6
//Substitute the address for portB on your chip here

BCD_In=PORTB & 0B11110000;

Exact equivalent again, but with the different 'style' of I/O.

3)

BCD_In=INPUT_B()>>4;

This returns the top four bits of port B, now rotated into the low four bits of the result (so result is now 0-9 from the BCD switch, with no further conversion needed).

This could also be coded in the alternative style allready given.

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516822
whmeade10
Guest







Re: Read Port for BCD value
PostPosted: Fri Aug 08, 2003 9:08 am     Reply with quote

Thanks for your response. It is exactly what I was looking for. I was told in another response to take a look and a FAQ How does one map a variable to an I/O port and I couldn't relate any of your examples to it or anywhere else in the manual. Is there a better reference for PIC-C that shows better examples?



:=:=I have connected a 10 position BCD rotary switch to B4-B7 and need to be able to place the value into a variable. When I programmed in PIC Basic Pro this was pretty straight forward:
:=:=
:=:=BCD_In = PORTB & \%11110000 ' Read BCD switch on ports B4-B7.
:=:=
:=:=I haven't been able to find anything simular in PIC-C.
:=:=Thanks in advance..
:=Lots of different possible answers, according to whether you want to keep the result in the high four bits (as you shown in your basic example), or move it to the bottom four bits. Also depending on the 'style' you want to use:
:=
:=1)
:=
:=BCD_In=INPUT_B() & 0B1111000;
:=
:=This is the exact equivalent of your basic line, in CCS C.
:=
:=2)
:=
:=#byte PORTB=0x6
:=//Substitute the address for portB on your chip here
:=
:=BCD_In=PORTB & 0B11110000;
:=
:=Exact equivalent again, but with the different 'style' of I/O.
:=
:=3)
:=
:=BCD_In=INPUT_B()>>4;
:=
:=This returns the top four bits of port B, now rotated into the low four bits of the result (so result is now 0-9 from the BCD switch, with no further conversion needed).
:=
:=This could also be coded in the alternative style allready given.
:=
:=Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516823
R.J.Hamlett
Guest







Re: Read Port for BCD value
PostPosted: Fri Aug 08, 2003 10:13 am     Reply with quote

:=Thanks for your response. It is exactly what I was looking for. I was told in another response to take a look and a FAQ How does one map a variable to an I/O port and I couldn't relate any of your examples to it or anywhere else in the manual. Is there a better reference for PIC-C that shows better examples?
:=
The 'mapping to an I/O port' solution, is the second one I give, where I put the #byte defintion. This command allows you to 'map' a symbolic name to a particular address in memory. So in the case of port B (which is at memory address '6' for the 12, and 16 family chips, but at address 0xF81, on the 18x family chips), the line:
#byte PORTB=0x6

now allows you to use the name 'PORTB', to refer to this register in the processor. The compiler avoids you having to do this, by offering the functions 'input_b', and 'output_b', so you don't have to bother to look up the register address. The 'mapping' solution though, can produce neater looking code, and also allows you to access whole registers, or bits when needed (the command #bit, allows you to assign a symbolic name to a particular bit in a register).
These functions, are described quite clearly in the manual, both under the #bit, and #byte entries, and under the 'how do I map a variable to an I/O port' section. If you scroll down the 'contents' in the manual (on line version), the end of the entries, have a whole series of 'how do I' entries. In the paper manual, these form an appendix at the rear.

Best Wishes

(hopefully the server will post this without 'hiccuping'...

:=:=:=I have connected a 10 position BCD rotary switch to B4-B7 and need to be able to place the value into a variable. When I programmed in PIC Basic Pro this was pretty straight forward:
:=:=:=
:=:=:=BCD_In = PORTB & \%11110000 ' Read BCD switch on ports B4-B7.
:=:=:=
:=:=:=I haven't been able to find anything simular in PIC-C.
:=:=:=Thanks in advance..
:=:=Lots of different possible answers, according to whether you want to keep the result in the high four bits (as you shown in your basic example), or move it to the bottom four bits. Also depending on the 'style' you want to use:
:=:=
:=:=1)
:=:=
:=:=BCD_In=INPUT_B() & 0B1111000;
:=:=
:=:=This is the exact equivalent of your basic line, in CCS C.
:=:=
:=:=2)
:=:=
:=:=#byte PORTB=0x6
:=:=//Substitute the address for portB on your chip here
:=:=
:=:=BCD_In=PORTB & 0B11110000;
:=:=
:=:=Exact equivalent again, but with the different 'style' of I/O.
:=:=
:=:=3)
:=:=
:=:=BCD_In=INPUT_B()>>4;
:=:=
:=:=This returns the top four bits of port B, now rotated into the low four bits of the result (so result is now 0-9 from the BCD switch, with no further conversion needed).
:=:=
:=:=This could also be coded in the alternative style allready given.
:=:=
:=:=Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516824
whmeade10
Guest







Re: Read Port for BCD value
PostPosted: Fri Aug 08, 2003 10:38 am     Reply with quote

I will have to take a look at the online manual. I have been going by the printed version dated August 2002.

:=:=Thanks for your response. It is exactly what I was looking for. I was told in another response to take a look and a FAQ How does one map a variable to an I/O port and I couldn't relate any of your examples to it or anywhere else in the manual. Is there a better reference for PIC-C that shows better examples?
:=:=
:=The 'mapping to an I/O port' solution, is the second one I give, where I put the #byte defintion. This command allows you to 'map' a symbolic name to a particular address in memory. So in the case of port B (which is at memory address '6' for the 12, and 16 family chips, but at address 0xF81, on the 18x family chips), the line:
:=#byte PORTB=0x6
:=
:=now allows you to use the name 'PORTB', to refer to this register in the processor. The compiler avoids you having to do this, by offering the functions 'input_b', and 'output_b', so you don't have to bother to look up the register address. The 'mapping' solution though, can produce neater looking code, and also allows you to access whole registers, or bits when needed (the command #bit, allows you to assign a symbolic name to a particular bit in a register).
:=These functions, are described quite clearly in the manual, both under the #bit, and #byte entries, and under the 'how do I map a variable to an I/O port' section. If you scroll down the 'contents' in the manual (on line version), the end of the entries, have a whole series of 'how do I' entries. In the paper manual, these form an appendix at the rear.
:=
:=Best Wishes
:=
:=(hopefully the server will post this without 'hiccuping'...
:=
:=:=:=:=I have connected a 10 position BCD rotary switch to B4-B7 and need to be able to place the value into a variable. When I programmed in PIC Basic Pro this was pretty straight forward:
:=:=:=:=
:=:=:=:=BCD_In = PORTB & \%11110000 ' Read BCD switch on ports B4-B7.
:=:=:=:=
:=:=:=:=I haven't been able to find anything simular in PIC-C.
:=:=:=:=Thanks in advance..
:=:=:=Lots of different possible answers, according to whether you want to keep the result in the high four bits (as you shown in your basic example), or move it to the bottom four bits. Also depending on the 'style' you want to use:
:=:=:=
:=:=:=1)
:=:=:=
:=:=:=BCD_In=INPUT_B() & 0B1111000;
:=:=:=
:=:=:=This is the exact equivalent of your basic line, in CCS C.
:=:=:=
:=:=:=2)
:=:=:=
:=:=:=#byte PORTB=0x6
:=:=:=//Substitute the address for portB on your chip here
:=:=:=
:=:=:=BCD_In=PORTB & 0B11110000;
:=:=:=
:=:=:=Exact equivalent again, but with the different 'style' of I/O.
:=:=:=
:=:=:=3)
:=:=:=
:=:=:=BCD_In=INPUT_B()>>4;
:=:=:=
:=:=:=This returns the top four bits of port B, now rotated into the low four bits of the result (so result is now 0-9 from the BCD switch, with no further conversion needed).
:=:=:=
:=:=:=This could also be coded in the alternative style allready given.
:=:=:=
:=:=:=Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516825
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