On Mon, 30 May 2011 21:51:46 +0100 (BST) "IV3YNB op. Matteo" wrote: > I have to control a DDS chip and I need to set his frequency: I have > to split a 32 bits word into 4 bytes to manage the frequency into the > DDS chip and to save in the memory of the PIC (I will use a 16F877 or > similar).=20 The PIC only deals with a single byte at a time. A 32-bit word, as you've already discovered, is exactly four bytes, since each byte is eight bits. Generally, one of the four bytes is referred to as the MSB (Most Significant Byte), that being the one whose value changes the value of the 32-bit word the most (a change of 1 in the MSB is worth 2^24 in the 32-bit word), while the one at the other end is referred to as the LSB (Least Significant Byte), and has the smallest effect on the value of the 32-bit word (a change of 1 in the LSB is worth 1 in the 32-bit word). If the numbers don't make sense, write out a 32-bit word in binary, split it into four groups of eight bits, and see what happens to the value of the word when you change the lowest bit in each of the groups. There are now two basic questions you have to answer. The first question is how does the frequency word get from the outside world into the PIC? Does it come over a serial port? An SPI port? Some kind of bit-banged interface (serial or parallel)? Most of these interfaces handle only a single byte at a time (for example, the PIC's serial port hands you one byte at a time in RCREG). To send a 32-bit word over such an interface, you have to decide in which order to send the bytes (MSB first or LSB first). Obviously, the sender and receiver need to agree on this ordering. Since you're building the receiver, you need to know what the sender will send. Alternatively, if the interface is fully parallel (using 32 pins), you will need to know which pin corresponds to which bit, and this will give you your ordering. The second question is how does the frequency word get from the PIC into the DDS chip? Exactly the same considerations apply, but in reverse: the DDS chip was built to expect the bytes to arrive in some order, and you need to send them in that order. If you're writing your PIC code in assembly, there's nothing further to think about, because it's not physically possible for a PIC16F877 to think about anything larger than a single byte in assembly: any time you work on larger quantities, you write out the code to do so a byte at a time by hand. So you get to choose which byte is stored where. If you're writing in C, you have the option of using larger data types, like uint32_t. The problem then becomes how to pack the individual bytes (coming in from the outside world) into a uint32_t, and how to unpack bytes from the uint32_t to send to the DDS chip. To do this, you want to look into the C bit shifting operators, << and >>. If you don't want to do any math on the frequency in the PIC (you only want to store it as it comes in and send it to the DDS chip unchanged), it may be easier to use an array of 4 uint8_ts and not bother expressing it as a uint32_t at all. Chris --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .