Daily Coding Miscellaneous

Using a function in Embedded C.

In this example, we will make animations by turning off and on Leds with some delays. We could simply do it like this. We could simply do it like this. PORTB=0B10101010; _delay_ms(1000); PORTB=0B01010101; _delay_ms(1000); . . . PORTB=0B01000001; _delay_ms(1000); But instead of writing like this, we would use the function Led_pin( ). It will reduce the typing a bit but in the next blog, we will learn to make it very small by learning a concept called Bit Shifting. Let\’s go for an example. #define F_CPU 16000000UL //defining the clock speed of the processor #include <avr/io.h>  // library for using registers [PORTX,PORTD,PINX] #include <util/delay.h> // library for using delay void Led_pin(uint8_t byte) // Making a function with argument \”byte\” having data type unsigned int 8. { PORTB = byte;   // Whenever the function is called it will store the argument byte in PORTB with delay. _delay_ms(100); } int main(void) { DDRB = (0b00111110); while(1) { Led_pin(0b00101010); Led_pin(0b00010100); Led_pin(0b00101110); Led_pin(0b00010001); Led_pin(0b00101010); Led_pin(0b00011100); Led_pin(0b00101011); Led_pin(0b00010100); PORTB=0b00000000; _delay_ms(1000); } return(0); } In the above code we made a function  Led_pin( ). void Led_pin(uint8_t byte) { PORTB = byte;   _delay_ms(100); } Here Led_pin takes a single byte of data type unsigned int [uint8_t]. Whenever our code calls this function the arguments inside it directly go to the PORTB and blink for 100 microseconds asset in delay. OUTPUT If you are new and feeling confused about other libraries and functions then please refer to this blog.

Daily Coding Embedded Basics Miscellaneous

Introduction on embedded c

Embedded C is an extremely popular programming language when we talk about electronic devices. If you wanna go for robotics it would be a good start for you. In our day-to-day life, we use mobile, laptops, and fridges every electronic device we use is made up of using embedded C. Now without wasting any time let\’s dive a bit deep into it by looking into a very basic example Blinking LED using Embedded C. Blinking Led is a hello world for embedded C which means this is the first basic code that takes us into the world of embedded C. #define F_CPU 16000000UL#include <avr/io.h>#include <util/delay.h>int main(void){DDRB|= (0B00100000);while (1){PORTB=0B00100000;_delay_ms(1000);PORTB=0B11011111;_delay_ms(1000);}return(0);} Let’s understand our code from top to bottom. #define F_CPU 16000000UL Let’s break it into 3 parts #define F_CPU Here we are defining the clock speed of the processor. 16000000 Here we are setting the clock speed at 16Mhz as our atmega328p’s default value is 1Mhz hence 16Mhz will make it 16 times slower. UL Here we are doing nothing but declaring the data type of the clock speed. We used unsigned long because clock speed cant is negative. #include <avr/io.h> Whenever we want to use libraries of some function from some other source or even we wanna use our own code we use #include.  #include <avr/io.h>  includes a header file that contains code for using pins, ports, etc. for the Avr microcontroller. #include <util/delay.h> This library is used to put delays in our code. int main(void) The main function is when the AVR starts executing code. While(1) While loops execute the code inside it until the condition inside the parentheses remains true. We all know in C 1 refers to true and 0 refers to false. Here the code inside the while loop will run again and again because 1 can never be false. return(0); If we don\’t write return(0) at the end of the code we will get an error for sure. This happens because our operating system needs confirmation that the code we ran is running properly. This is more of a line of confirmation. You must be wondering what these words DDRB or PORTB are? Well, these are called hardware registers which are extremely important to understand so let\’s get to know what they are and what they do? Hardware Registers In our Atmega series of Avr, we have mainly 3 hardware registers. DDRx PORTx PINx Here x is referred to the bank. We have three of’em B, C, D.Every bank contains 8 pins. Depending on which bank’s pin you are using we chose the bank. DDRx[Data-Direction registers] DDRx configures the pins as output or input. As we are using an 8-bit microcontroller. The default value of the DDR is 0 which means whenever we give power it is not giving any output. If we want to set it for the output we need to set the bit we want to use for sending the output as 1. Output = 1Input    = 0DDRB = 0b00000000       DDRB = 0b00100000 Here 0b is telling us that we are writing the number in binary. We all know 0 refers to the ‘off’ and 1 refers to the ‘on’. Hence the above example is telling us at first all pins were off later on we set pin 5 as 1 for giving output. If you are familiar with Arduino Ide then you can relate DDR with pinMode.      PORTx[Port x data registers] After setting the DDRx bits to the 1(output), The port registers the voltage as HIGH or LOW. When we say the voltage is Low it means the voltage is 0V and if we say voltage is HIGH we consider the voltage as 5V.  HIGH = 1LOW    = 0PORTB = 0b00000000  PORTB = 0b00100000 If we take the example of our code we have seen above we are setting pb5 as output. In the picture you can see above PB5 is connected with pin 13 of Arduino. Now you can see we are turning we Led on which is connected with pin 13 which is connected with PB5 on Atmega 328p. If you are familiar with Arduino Ide then you can relate PORTx with digitalWrite. PINx[port c input data pin address] The pin register addresses are used when we want to read the digital voltage values for each pin we set as input in DDRX. You can relate this pin with digitalRead from Arduino IDE. I believe now it\’s clear what we did in the  above example. In the next blog, we will see how we can write a function and use it for calling. So that we can reduce our writing.

Daily Coding Miscellaneous

Static Variables in C

Static is a keyword in the c/cpp language. It is a storage class specifier that can be applied to any data type. A static keyword can be used with both variables and functions. The static keyword tells the compiler to make the variable or function limited to scope but allows it to persist throughout the life of a program. The static keyword limits the scope of variables or functions to the current source file only. So static keyword helps in hiding the data to one source file only.  Static words help in achieving encapsulation in c language. In this blog, we are going to cover the use of static keywords with variables. Static variables are stored in BSS or data segment of the C program Memory Layout. The BSS segment contains the uninitialized data. The DATA segment keeps the initialized data. The static variable is only initialized once, if it is not initialized then it is automatically initialized to 0. void demo(int value) { static int count  = 0; count = value; cout<<count; } count = value: is not initialization, it is an assignment. Static can be assigned as many times as we wish. static int count = 0: is initialization & that happens only once for static variables. We can have static global variables or static local variables. Static local variables Static variables are able to retain their value b/w function calls. Static keywords, when used with variables inside the function, are then called static local variables.  Static global variables The static keywords when used with variables outside the functions,  are then called static global variables. Static global variables help in achieving enumeration, that is they make the scope of the variable limited to the present file only. References: The static keyword in C (c-programming-simple-steps.com)

Stay Updated With Us

Error: Contact form not found.