The post How to Setup and Program Arduino Board appeared first on BINARYUPDATES.
]]>Arduino board is a simple and easy to use electronic prototyping platform. The cool thing about Arduino hardware is that it’s been provided with free Arduino IDE, code libraries and plenty of example projects. Also, we would not have to care much about driver installation. It will be managed by IDE itself while installing Arduino IDE. When we look at Arduino kit it looks very simple with ATmega328 as a central processing unit and few components on board. To follow this complete tutorial you only need Arduino board, USB Type A to B Cable and Laptop/PC. As shown in the picture below.
There are different Arduino derivatives available in market. These all looks physically similar except serial communication chip. The original Uno uses MEGA16U2 chip for serial communication whereas FREEDUINO uses FT232RL chip to communicate over USB. As a beginner, I suggest you to do not worry because despite of differences they still follow same setup and programming procedure.
Features of Arduino Uno:
Microcontroller: ATmega328
Crystal Frequency: 16 MHz (external crystal)
Flash Memory: 32 KB from which 0.5 KB used by bootloader (ATmega328)
RAM Memory: 2 KB (ATmega328)
EEPROM Memory: 1 KB (ATmega328)
Operating Voltage: 5V DC
Input Voltage: 7-12V (Powered by either USB or DC Adapter)
Analog Inputs: 6
Digital I/O: 16 (out of them 6 PWM Pins)
If in case, you’re not able to understand these features then feel free to ignore them for a while. Arduino is very easy to get you start and believe me you need not to understand all specifications just to blink LED. Let’s jump right into the programming part of Arduino.
This video will guide you through step-by-step process of installation, configuration. There are few settings need to confirm such as selecting right COM Port and Arduino Uno board before we upload the sketch.
In the end of this post you will find troubleshoot guide. If in case you face any problem while establishing communication between Arduino and PC/Laptop.
This code belongs to built-in example project, Open Arduino IDE –> File –> Examples –> Basics –> Blink. We already have shown in video.
/* Blinky Example This example turns on an LED ON and OFF for one second, simultaneously. Almost all Arduino board have an on board LED as show in previous image. On the UNO, this LED is attached to digital pin 13. LED_BUILTIN takes care of use the correct LED. */ // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin LED_BUILTIN as an output. pinMode(LED_BUILTIN, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (digital pin 13) delay(1000); // wait for a 1 Sec. digitalWrite(LED_BUILTIN, LOW); // turn the LED off (digital pin 13) delay(1000); // wait for a 1 Sec. }
We already explained each and every line of code in the video. This program will blink onboard LED with the delay of 1 second. I recommend you to play with delay() function. You can pass a different value in delay function and observe the effect. Let’s have a look at all the pins and its functions on Arduino. Together there are around 30-pins including Power Pins. Also, there are few of connectors.
I think we don’t need to list functions of each pin on Arduino. They are pretty self explanatory as shown in picture above.
In my opinion, there are two things which make Arduino very special among other boards. One is its very simple programming language some people also refers it as Arduino Sketch and the second one, is its built-in Bootloader. Now question is What is this Arduino Bootloader? The answer is, it’s a piece of code or software program that sits onto dedicated portion of memory. Whenever you Power On the board, bootloader is the code that runs first and then user program. Almost all microcontrollers have some form of bootloader but Arduino Bootloader is very special because it makes programming of Arduino like a piece of cake without using any additional Programming Adapter. This means Arduino uses serial USB interface to transfer user program/sketch from PC to Arduino board. This not only saves money but also make programming easy for newbies. Normally this bootloader code takes around 2 KB of flash memory. As Arduino is open source hardware, you’ll get entire source code on official site of Arduino. Usually, every Arduino board comes with pre-loaded bootloader onto ATmega328. As a beginner you need not to do anything with bootloader. But once you are familiar with Arduino System, then you can explore and play around bootloader code.
There may be possibility to face some common errors. These errors most often experienced with Arduino derivates or duplicate boards. We have listed some error messages and their solutions. This will help you troubleshoot while programming Arduino.
avrdude: stk500_getsync(); not in sync : resp=0x00
There are three possible mistake causes to run into this type of error:
java.lang.NullPointerException at processing.app.serial.SetDTR
Ser_Send(): write error: sorry no info avail.
If you get this error message, that means you have not selected proper serial port. I recommend you to go ahead and make sure COM Port setting.
avrdude: Expected signature for ATMEGA is…..
This error message appears when you have wrong board selected into the Tools menu. So make sure board selection menu in Arduino IDE.
I recommend you to stay away from cheap Arduino derivates. It’s always better to spend few more bucks and save time. I have tried few of these cheap products and had long frustrating hours to find my way around. In this process very often I have failed to make things work. The same energy invested in programming will give you better payoff. Anyways choice is yours.
This is it for now. I hope now you know How to Setup and Program Arduino board or any other similar board. If you feel Pin functions needed then I will consider adding them into the post. In future posts, we will learn interfacing of various sensors and peripheral chips to build some cool projects around Arduino. Thanks for reading and don’t forget to leave a comment.
The post How to Setup and Program Arduino Board appeared first on BINARYUPDATES.
]]>The post Bitwise Operations in Embedded Programming appeared first on BINARYUPDATES.
]]>Now let’s concentrate only on bitwise operations. We’ll learn how these bitwise operations allow’s us for Setting, Inverting, Toggling, Clearing, Extracting and Inserting bits in embedded programming. Here is a table which summarizes operations with 2-operands.
A | B | A | B | A & B | A ^ B |
0 | 0 | 0 | 0 | 0 |
1 | 0 | 1 | 0 | 1 |
0 | 1 | 1 | 0 | 1 |
1 | 1 | 1 | 1 | 0 |
Let’s take variable A and B. We’ll perform bitwise operation on these two variables. This example will help you understand their operation. Let’s first declare these two variables:
uint8_t A = 0x3B //00111011; uint8_t B = 0x96 //10010110; |
AND compares each bit and returns 1 if the two bits are 1 (TRUE) otherwise 0 (FALSE). So: C = A & B;
00111011………………..(A=0x3B) | |
& | 10010110………………..(B=0x96) |
00010010 |
The value of C will be 0x12 or in binary 00010010. That is where there is a 1 in each bit for both values.
OR (| in C)
OR will return a 1 if there is a 1 in either value at that bit. So: C = A | B;
00111011………………..(A=0x3B) | |
| | 10010110………………..(B=0x96) |
10111111 |
The value of C will be 0xBF or in binary 10111111
XOR will return a 1 if there is a 1 at that bit for either value but not both. For example, if bit position 5 for both values has a 1 then XOr returns a 0. But if only one has a 1 and the other has a 0 XOR returns a 1. So: C = A^B;
00111011………………..(A=0x3B) | |
^ | 10010110………………..(B=0x96) |
10101101 |
The value of C will be 0xAD or in binary 10101101.
This returns the compliment of a value. This mean where there was a 0 there will now be a 1 and vice versa. So: C = ~(A);
! | 00111011………………..(A=0x3B) |
11000100………………..(C=0xC4) |
The value of C will be 0xC4 or in binary 11000100
And then there are two shift operators – Left shift and Right shift. These operators shift the bits by the corresponding value, in other word’s move the bits. The sign << for left shift and >> for right shift. Here is an example:
C = A << 2; // left shift A by 2 |
The value of C becomes 0xEC or in binary 11101100 after shifting 2-bits to the left.
D = B >> 4; // right shift B by 4 |
The value of D becomes 0x03 or in binary 00001001 after shifting 4-bits to right.
Let’s say we have variable called bits and we have asked to set the bit-7. This can be achieved by writing this single line of code.
bits = bits | (1 << 7) ; /* sets bit 7 */This would usually be written more succinctly as:
bits |= (1 << 7) ; /* sets bit 7 */Inverting (toggling) is accomplished with bitwise-XOR. In following, example we’ll toggle bit-6.
bits ^= (1 << 6) ; /* toggle bit 6 */
Form a mask with 1 in the bit position of interest, in this case bit-6. Then bitwise AND the mask with the operand. The result is non-zero if and only if the bit of interest was 1:
if ((bits & 64) != 0) /* check to see if bit 6 is set */Same as:
if (bits & 0x64) /* check to see if bit 6 is set */Same as:
if (bits & (1 << 6)) /* check to see if bit 6 is set */
Let’s say if we want to clear bit-7. This can be accomplished using bitwise-AND operator
Mask must be as wide as the operand! if bits is a 32-bit data type, the assignment must be 32-bit:
bits &= ~(1L << 7) ; /* clears bit 7 */
Let’s say we have given a 32-bit number and we asked to extract bits from it. Assume that 32-bit number in hex is 0xD7448EAB. In binary= 1101 0111 0100 0100 1000 1110 1010 1011. Now we have asked to extract 16-bits from bit number 10 through 25.
To extract the bits first we have to use bitwise operator in combination with a bit mask to extract bits 10 through 25. The masking value will be 0x3FFFC00. Now we have two ways we can achieve result.
Method-I
unsigned int number = 0xD7448EAB; unsigned int value = (number & 0x3FFFC00) >> 10;
Method-II
unsigned int number = 0xD7448EAB; unsigned int value = (number >> 10) & 0xFFFF;
Now if we follow Method-I and after equating. We’ve got 0x3448C00 (In binary 11010001001000110000000000). When we shift 10 bits to right, we’ll get 0xD123 i.e. in binary 1101000100100011.
Monitoring specific bit in register is very important. In Embedded programming, very often we need to read status of flag bit in hardware register. These flag bit controls or indicate hardware feature. Also they are every useful while reading, writing data to and from microchip. So we continuously monitor these bits in register to carry out desired function by microchip. Let’s say if we want to monitor 4^{th} bit for any change, we’ll write function as below:
while( bits & (1<<4) ) // monitor for 4th bit changing from 0 to 1 { ....... ....... }
The 4^{th} bit of register bits is ‘1’ the result of (bits & (1<<4)) will always be zero. When 4^{th} bit is ‘1’ then (bits & (1<<4)) will be equal to (1<<4) which is greater than 0 and so this evaluates as TRUE condition and code inside while loop get executed.
Now let’s say if we want to monitor 4^{th} bit state from 1 to 0. In this case, we only need to negate the condition inside of while statement.
while( ~(bits & (1<<4)) ) // monitor for 4th bit changing from 1 to 0 { ....... ....... }
Bitwise operators save memory and it is fast. This all leads to improve performance. I hope now you know that why bitwise operations in embedded programming used such a widely. If you have any questions or suggestions then feel free to leave a comment.
The post Bitwise Operations in Embedded Programming appeared first on BINARYUPDATES.
]]>The post Signed and Unsigned Numbers in Computer Programming appeared first on BINARYUPDATES.
]]>In Number System we’ve assumed, we have as many bits as needed to represent numbers. But in computers, we have a fix number of bits to represent value. These bit sizes are typically 8-bit, 16-bit, 32-bit, 64-bit. These sizes are usually multiple of 8, because system memories are organized on an 8-bit byte basis. When a specific number of bits being used to represent a number. This number determines the range of possible values that can be represented. For example, there are 256 possible combinations of 8-bits, therefore an 8-bit number can represent 256 distinct numeric values and the range is typically considered to be 0-255 (we have provided table in later part of this tutorial). So we can’t represent numbers larger than 255 using 8-bit number. Similarly, 16 bits allows a range of 0-65535.
Until now we have only considered positive values for binary numbers. When a fixed binary number is used to hold positive values, it is considered as unsigned. In this case, the range of positive values that can be represented is from 0 – 2^{n}-1, where n is the number of bits being used. It is also possible to represent signed (negative as well as positive) numbers in binary. In this case, some part of the total range of values is used to represent positive values, and the remaining of the range is used to represent negative values.
There are several ways we can represent signed numbers in binary, but the most common representation used is called two’s complement method. The term two’s complement is somewhat ambiguous, in that it is used in two different ways. First, as a representation, two’s complement is a way of interpreting and assigning meaning to a bit pattern contained in a fixed precision binary quantity. Second, the term two’s complement is also used to refer to an operation that can be performed on the bits of a binary quantity. As an operation, the two’s complement of a number is formed by inverting all of the bits and adding 1 to it. In a binary number being interpreted using the two’s complement representation, the high order bit of the number indicates the sign. If the sign bit is 0, the number is positive, and if the sign bit is 1, the number is negative. For positive numbers, the rest of the bits hold the true magnitude of the number. For negative numbers, the lower order bits hold the magnitude of the number. It is important to note that two’s complement representation can only be applied to fixed precision quantities, that is, quantities where there are a set number of bits.
The 2’s complement method of representation is used because it reduces the complexity of the hardware in the ALU (arithmetic-logic unit) of a computer’s CPU. Using a 2’s complement method, all of the arithmetic operations can be performed by the same hardware whether the numbers are considered to be unsigned or signed. The bit operations performed are identical; the difference comes from the interpretation of the bits. The interpretation of the value will be different depending on whether the value is considered to be unsigned or signed.
The table below shows counting sequence for an 8-bit binary number using 2’s complement:
In above table, we’re counting up from 0. When 127 reached, the next binary pattern in the sequence corresponds to -128. The values from the largest positive number to the largest negative number, but that the sequence is as expected after that. Say for example, adding 1 to –128 gives –127. When the count has progressed to FFh (or the largest unsigned magnitude possible) the count wraps around to 0. Say for example adding 1 to –1 gives 0. This is it for Signed and Unsigned Numbers in computer programming. If you have any question, then feel free to leave a comment.
The post Signed and Unsigned Numbers in Computer Programming appeared first on BINARYUPDATES.
]]>The post Number System in Embedded Programming appeared first on BINARYUPDATES.
]]>Decimal numbering system uses digits from 0 to 9 i.e. (0, 1, 2, 3, 4, 5, 6, 7, 8, and 9). The base of decimal number system is 10 because we use 10 digits to represent decimal number. When we write decimal numbers, we use a positional notation system. Each digit is multiplied by an appropriate power of 10 depending on its position in the number. Let’s take example: 5319
5319 | = 5 x 10^{3} + 3 x 10^{2} + 1 x 10^{1} + 9 x 10^{0} |
= 5 x 1000 + 3 x 100 + 1 x 10 + 9 x 1 | |
= 5000 + 300 + 10 + 9 |
For whole number, the rightmost digit is 1^{st} position (10^{0} = 1). The numeral in that position indicates how many ones are present in the number. The next position to the left is ten’s, then hundred’s, thousand’s, and goes on. Each digit position has a weight that is ten times the weight of the position to its right.
In the decimal number system, there are ten possible values that can appear in each digit position, and so there are ten numerals required to represent the quantity in each digit position. The decimal numerals are the familiar zero through nine (0, 1, 2, 3, 4, 5, 6, 7, 8 and 9).
Binary Numbers uses only 0 and 1 digit. The base of binary number system is 2. The binary number system is also a positional notation numbering system. Each digit position in a binary number represents a power of two. So, when we write a binary number, each binary digit is multiplied by an appropriate power of 2 based on the position in the number. Example: Find decimal equivalent of binary number 10101
10101 | = 1 x 2^{4} + 0 x 2^{3} + 1 x 2^{2} + 0 x 2^{1} + 1 x 2^{0} |
= 1 x 16 + 0 x 8 + 1 x 4 + 0 x 2 + 1 x 1 | |
= 16 + 4 + 1 |
The decimal equivalent of 10101 is 21. Each digit in binary number carries specific multiplication factor. By default the bit on extreme right which is also called as LSB bit or 0^{th} bit and that on the extreme left is MSB or last bit. SO technically in computing first bit will be 0^{th} index and hence the right will have an order of 2^{0}, next bit will have an order of 2^{1} and 2^{2}, 2^{3} and so on. And when we start conversion we have to consider from LSB i.e. from RIGHT we multiply each bit with increasing power of 2 so Bit 0 will be multiplied with 2^{0}, Bit 1 will be multiplied with 2^{1} and so on.
Hexadecimal Number System uses 16 digits from 0 to 9 and A to F. The alphabets A to F represent decimal numbers from 10 to 15. The base of Hexadecimal uNmber system is 16. There are sixteen numerals required. Here are the hexadecimal numerals: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F.
The reason for the common use of hexadecimal numbers is the relationship between the numbers 2 and 16. Sixteen is a power of 2 (16 = 2^{4}). Because of this relationship, four digits in a binary number can be represented with a single hexadecimal digit. This makes conversion between binary and hexadecimal numbers very easy, and hexadecimal can be used to write large binary numbers with much fewer digits. When working with large digital systems, such as computers, it is common to find binary numbers with 8, 16 and even 32 digits. Writing a 16 or 32 bit binary number would be quite tedious and error prone. By using hexadecimal, the numbers can be written with fewer digits and much less likelihood of error. This makes programmer life easy while writing programs.
Example: Find Hexadecimal equivalent of 10110, where 10110 is binary number.
To convert any binary number into its equivalent hexadecimal number, we first need to group the bits each group will have 4-bits. In our example we have 5 binary digits, so we will group them into 0001–0110. After grouping bits directly replace group with its equivalent hexadecimal number. Make sure we always start grouping from RIGHT i.e. LSB bit to LEFT i.e. MSB bit. Hence the hex equivalent of 10110 is 16 or (0x16).
Equivalent Numbers in Decimal, Binary, Hexadecimal Notations,
Decimal | Binary | Hexadecimal |
0 | 00000000 | 00 |
1 | 00000001 | 01 |
2 | 00000010 | 02 |
3 | 00000011 | 03 |
4 | 00000100 | 04 |
5 | 00000101 | 05 |
6 | 00000110 | 06 |
7 | 00000111 | 07 |
8 | 00001000 | 08 |
9 | 00001001 | 09 |
10 | 00001010 | 0A |
11 | 00001011 | 0B |
12 | 00001100 | 0C |
13 | 00001101 | 0D |
14 | 00001110 | 0E |
15 | 00001111 | 0F |
16 | 00010000 | 10 |
17 | 00010001 | 11 |
31 | 00011111 | 1F |
32 | 00100000 | 20 |
63 | 00111111 | 3F |
64 | 01000000 | 40 |
65 | 01000001 | 41 |
127 | 01111111 | 7F |
128 | 10000000 | 80 |
129 | 10000001 | 81 |
255 | 11111111 | FF |
256 | 0000000100000000 | 0100 |
32767 | 0111111111111111 | 7FFF |
32768 | 1000000000000000 | 8000 |
65535 | 1111111111111111 | FFFF |
BCD is Binary Coded Decimal. In BCD system, numbers are represented in decimal form. However, each decimal digit is encoded using a 4-bit binary number. Here is an example. The decimal number 568 would be represented in BCD as follows:
568 | = 0101 0110 1000 |
= 5 6 8 |
Conversion of numbers between decimal and BCD is quite simple. To convert from decimal to BCD, simply write down the 4-bit binary pattern for each decimal digit. To convert from BCD to decimal, divide the number into groups of 4 bits and write down the corresponding decimal digit for each 4-bit group.
There are a couple of variations on the BCD representation, namely packed and unpacked. An unpacked BCD number has only a single decimal digit stored in each data byte. In this case, the decimal digit will be in the low four bits and the upper 4-bits of the byte will be 0. In the packed BCD representation, two decimal digits are placed in each byte. Generally, the high order bits of the data byte contain the more significant decimal digit.
BCD numbers is not as common as binary number system because it is not space efficient. In packed BCD format, only 10 of the 16 possible bit patterns in each 4-bit unit are used. In unpacked BCD, only 10 of the 256 possible bit patterns in each byte are used. A 16-bit quantity can represent the range 0-65535 in binary, 0-9999 in packed BCD and only 0-99 in unpacked BCD.
This is it for Number system in Embedded Programming. Now we hope you’ve understood the concept of Binary, BCD and Hexadecimal Number Systems with its inter-conversion. Please do write us if you have any suggestion/comments or come across any error on this page.
The post Number System in Embedded Programming appeared first on BINARYUPDATES.
]]>The post Wolfson Audio Card for Raspberry Pi appeared first on BINARYUPDATES.
]]>Wolfson audio card is highly integrated low power audio system based on WM5102 coded which targets mobile devices such as smart phones, tablets, MP3 players and so on… making it the perfect fit for the ultra portable Raspberry Pi. Wolfson audio is based in Edinburg. Thankfully this wolfson audio card is available at reasonable cost of Rs. 2464 in India from element14.
The post Wolfson Audio Card for Raspberry Pi appeared first on BINARYUPDATES.
]]>