Posts

SPI (Serial Peripheral Interface)

Image
  What is SPI ? SPI (Serial Peripheral Interface) is a synchronous , full-duplex communication protocol used to transfer data between a master (e.g., microcontroller) and one or more slaves (e.g., sensors, memory chips). SPI uses 4 main lines : Line Full Name                 Direction Purpose MOSI      Master Out Slave In          Master → Slave          Master sends data to slave MISO      Master In Slave Out         Slave  → Master           Slave sends data to master SCLK      Serial Clock         Master → Slave          Clock signal generated by master SS      Slave Select         Master → Slave          Selects which slave to talk to SPI Communication Cha...

UART (Universal Asynchronous Receiver/Transmitter) :

Image
   What is UART? UART (Universal Asynchronous Receiver/Transmitter) is a hardware communication protocol used for serial communication between two devices. It is simple, widely used, and requires only two wires : TX (Transmit) RX (Receive) UART Communication Characteristics : Asynchronous : No shared clock between devices. Baud Rate : Both devices must agree on the speed (e.g., 9600, 115200 bits/sec). Framing : Each byte is wrapped with a start bit and stop bit . Optional Parity : For error checking. UART Data Frame Format : Start Bit    Data Bits (5-8)    Parity Bit (Optional)   Stop Bit(s) 0 e.g. 01100001            e.g. 0      1 void UART_init(unsigned int baud) {     unsigned int ubrr = F_CPU / 16 / baud - 1;     UBRR0H = (ubrr >> 8);     UBRR0L = ubrr;     UCSR0B = (1 << RXEN0) | (1 << TXEN0); // Enable RX and TX   ...

Print even and odd using two thread in synchronized way

 Print even and odd using two thread in synchronized way : Example :  ODD Thread will print  1                    EVEN Thread will print  2                  ----------------------------------------------                   ODD Thread will print till pre-define range                   EVEN Thread will print till pre-define range Solutions : #include <iostream> #include <thread> #include <mutex> #include <condition_variable> #define MAXSIZE 1000 using namespace std; mutex mu; condition_variable con; int mycount = 1; void printodd() {        cout << "i am in ODD thread" << endl;                 while (mycount < MAXSIZE) {         unique_lock<mutex> mylock(mu); ...

Implementation of Queue using Array

 _________________________Main.cpp_____________________________________ #include"OwnQueue.h" int main() {     std::cout << "MY.. QUEUE!\n" << endl;     OwnQueue queue;     queue.dequeue();     queue.enqueue(10);     queue.enqueue(20);     queue.enqueue(0);     queue.enqueue(40);     cout << queue.peek() << endl;     queue.enqueue(50);     queue.enqueue(70);     cout << queue.isempty() << endl;     cout << queue.isfull() << endl;     queue.dequeue();     queue.dequeue();     queue.dequeue();     queue.dequeue();     cout << queue.peek() << endl;     queue.dequeue();     queue.dequeue();     cout << queue.isempty() << endl;     cout << queue.isfull() << endl;      queue.printqueue...

Print Left View of Binary tree

// Print Left view of Binary tree #include <iostream> #include <vector> using namespace std; class tree {        public:      int data ;   tree * left, *right;      tree(int data)   {       this->data = data;       left = right = nullptr;   } }; void printleftview(tree *node, int level) {     static int maxlevel = 0;          if(node == nullptr)     {         return;     }          if(maxlevel < level)     {       maxlevel = level;       cout << node->data << endl;     }          printleftview(node->left, level+1);     printleftview(node->right, level+1);      } int main() {        cout<<"left view of binary tree" << endl;...

optimized bubble sort

#include <iostream> #include <vector> using namespace std; int main() {     vector<int> vec = {10,20,30};          //booble sort     bool  check = false;          for(int i = 0; i<= vec.size(); i++)     {                   for (int j= i+1; j<vec.size(); j++)         {                          if(vec[j] < vec[i])             {                 int temp = vec[i];                 vec[i] = vec[j];                 vec[j] = temp;                 check = true;             }                   ...

Creating Dynamic Array using C++

 #include <iostream> using namespace std; template <typename T> class Array { private: T* base; int size; public : Array(T arr[], int length) { base = new T[sizeof(arr)]; size = length; for (int i = 0; i < length; i++) base[i] = arr[i]; } void print(); }; template <typename T> void Array<T>::print() { for (int i = 0; i < size; i++) cout << " " << *(base + i) << endl; } int main() { int arr1[] = {10, 20, 30}; Array<int> obj(arr1, 3); obj.print(); char arr2[] = { 'v', 'a', 'r','u','n' }; Array<char> obj1(arr2, (sizeof(arr2) / sizeof(arr2[0]))); obj1.print(); return 0; }