View previous topic :: View next topic |
Author |
Message |
Somor
Joined: 18 Dec 2014 Posts: 12
|
How to transfer 8bit integer to a single pin bit by bit... |
Posted: Wed Apr 08, 2015 4:28 am |
|
|
Well, i have a question that i can't figure out... I have an 8bit integer for example:
int8 a = 10;
i want that integer to transfer to a PIN_B7 using output function bit by bit
So if my integer a is 1010'b i want to use cycle to output 0,1,0,1 to PIN_B7 for example.
I just can't figure how to do it, any suggestions? |
|
|
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
|
Posted: Wed Apr 08, 2015 4:45 am |
|
|
Code: | void serial_data_on_pin(int8 a)
{
int i;
for(i = 0; i < 8; i++)
{
output_bit(PIN_B7, (a & 1));
a >>= 1;
}
} |
_________________ A person who never made a mistake never tried anything new. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Wed Apr 08, 2015 5:12 am |
|
|
you want to 'convert byte to binary', so try googling that...
also you'll need a delay_ms(100) after you output the bit information onto the I/O pin other wise you'll never see it !
hth
jay |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Wed Apr 08, 2015 7:16 am |
|
|
Quote: |
transfer to a PIN_B7
|
you may not realize it yet - but this implies a function of PIN_STATE versus TIME.......
and hence MORE than just the state of the pin you toggle changing.
the high/low condition of the pin must be RELATIVE to some time function - either
ABSOLUTE or relative to ANOTHER pin that is providing the CLOCK reference for the states you wish to output.
without a timebase - (absolute or relative- pick one) - the state changes
will convey nothing.
Ask yourself how a receiving device will handle a run of all zeros or all ones coming from your pin as you conceive of it?
Unless you create an NRZ type of code ( self clocking ) -your idea will never work.
here is a link to a page with a typical diagram
http://www.ni.com/tutorial/6221/en/ |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Wed Apr 08, 2015 7:24 am |
|
|
And (of course), using relative timings from a 'time reference' when you first toggle the pin, is the basis of asynchronous serial comms (which is what #use RS232 generates for you automatically), while using timings from a second pin reference (a 'clock' pin), is what #use spi can also generate for you.... |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed Apr 08, 2015 7:41 am |
|
|
What's wrong with RS232?
Mike |
|
|
Somor
Joined: 18 Dec 2014 Posts: 12
|
|
Posted: Wed Apr 08, 2015 9:22 am |
|
|
The idea is if I'm using shift register, like 74hc4049 using D input of shift register for input 0 or 1 and clock input for the new bit... i want to collect whole byte before I enable the Strobe, so i can output the whole number..
Something like:
I have
Code: | int8 a = 10 // (which is 1010'b)
for(i = 0; i < 8; i++)
{
output_bit(PIN_B7, (a & 1));
a >>= 1;
output_high(PIN_B0, 1) //clock bit of shift register
delay_us(30);
output_LOW(PIN_B0,0)
}
output(PIN_B1,1); // strobe of shift register enabled
delay_ms(10);
output(PIN_B1,0); // strobe disabled
|
Thank you rikotech8! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Wed Apr 08, 2015 10:52 am |
|
|
OK.
For a shift register, you are sending the data bit, and a shift clock.
The compiler can do this totally for use, with #use SPI!...
SPI, is effectively a shift register interface. Normally more complex than you need, supporting shifting the data both in and out at the same time, and with the ability to use hardware to do it really fast, but the CCS library happily supports doing this in one direction only, using any pins.
Code: |
#use spi(DO=PIN_B7, CLK=PIN_B0, BAUD=1MHz, LOAD=PIN_B1, STREAM=HC4049)
//Then send the byte with
int8 a=10;
spi_xfer(HC4049,a,8); //clock out 8 bits
|
This will strobe B1 after the byte is sent. (This is what the 'load' pin does).
This will send the data MSB first, if you want LSB first, then specify this in the #use SPI (look at the manual). |
|
|
Somor
Joined: 18 Dec 2014 Posts: 12
|
|
Posted: Wed Apr 08, 2015 1:41 pm |
|
|
Thank you very much! |
|
|
|