Tuesday, July 2, 2019

STM32F103XX UART COMMUNICATION

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
HAL_UART_Transmit(&huart1,"hello\n",10,100);
HAL_Delay(1000);
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */

  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/


Sunday, June 30, 2019

sample calculator using simple gui tkinter in python


""" ***********************************************
programming Language :Python
Tool(IDE) used :Anaconda -> Spider
 date:03-07-2019
 created by:ANIL DURGAM

*****************************************************"""
# Python program to create a simple GUI

# calculator using Tkinter

# import everything from tkinter module

from tkinter import *


# globally declare the expression variable

expression = ""


# Function to update expressiom

# in the text entry box

def press(num):

 # point out the global expression variable

 global expression

 # concatenation of string

 expression = expression + str(num)
 # update the expression by using set method

 equation.set(expression)
# Function to evaluate the final expression

def equalpress():

 # Try and except statement is used

 # for handling the errors like zero

 # division error etc.

 # Put that code inside the try block

 # which may generate the error

 try:

  global expression

  # eval function evaluate the expression

  # and str function convert the result

  # into string

  total = str(eval(expression))



  equation.set(total)



  # initialze the expression variable

  # by empty string

  expression = ""

 # if error is generate then handle

 # by the except block

 except:

  equation.set(" error ")

  expression = ""

# Function to clear the contents

# of text entry box

def clear():

 global expression

 expression = ""

 equation.set("")

# Driver code

if __name__ == "__main__":

 # create a GUI window

 gui = Tk()
 # set the background colour of GUI window

 gui.configure(background="light green")
 # set the title of GUI window

 gui.title("Simple Calculator")
 # set the configuration of GUI window

 #gui.geometry("265x125")
 gui.geometry("360x180")

 # StringVar() is the variable class

 # we create an instance of this class

 equation = StringVar()
 # create the text entry box for

 # showing the expression .

 expression_field = Entry(gui, textvariable=equation)
 # grid method is used for placing

 # the widgets at respective positions

 # in table like structure .

 expression_field.grid(columnspan=4, ipadx=80)

 equation.set('enter your expression')

 # create a Buttons and place at a particular

 # location inside the root window .

 # when user press the button, the command or

 # function affiliated to that button is executed .

 button1 = Button(gui, text=' 1 ', fg='black', bg='white',

     command=lambda: press(1), height=1, width=7)

 button1.grid(row=2, column=0)



 button2 = Button(gui, text=' 2 ', fg='black', bg='white',

     command=lambda: press(2), height=1, width=7)

 button2.grid(row=2, column=1)



 button3 = Button(gui, text=' 3 ', fg='black', bg='white',

     command=lambda: press(3), height=1, width=7)

 button3.grid(row=2, column=2)



 button4 = Button(gui, text=' 4 ', fg='black', bg='white',

     command=lambda: press(4), height=1, width=7)

 button4.grid(row=3, column=0)



 button5 = Button(gui, text=' 5 ', fg='black', bg='white',

     command=lambda: press(5), height=1, width=7)

 button5.grid(row=3, column=1)



 button6 = Button(gui, text=' 6 ', fg='black', bg='white',

     command=lambda: press(6), height=1, width=7)

 button6.grid(row=3, column=2)



 button7 = Button(gui, text=' 7 ', fg='black', bg='white',

     command=lambda: press(7), height=1, width=7)

 button7.grid(row=4, column=0)



 button8 = Button(gui, text=' 8 ', fg='black', bg='white',

     command=lambda: press(8), height=1, width=7)

 button8.grid(row=4, column=1)



 button9 = Button(gui, text=' 9 ', fg='black', bg='white',

     command=lambda: press(9), height=1, width=7)

 button9.grid(row=4, column=2)



 button0 = Button(gui, text=' 0 ', fg='black', bg='white',

     command=lambda: press(0), height=1, width=7)

 button0.grid(row=5, column=0)



 plus = Button(gui, text=' + ', fg='black', bg='white',

    command=lambda: press("+"), height=1, width=7)

 plus.grid(row=2, column=3)



 minus = Button(gui, text=' - ', fg='black', bg='white',

    command=lambda: press("-"), height=1, width=7)

 minus.grid(row=3, column=3)



 multiply = Button(gui, text=' * ', fg='black', bg='white',

     command=lambda: press("*"), height=1, width=7)

 multiply.grid(row=4, column=3)



 divide = Button(gui, text=' / ', fg='black', bg='white',

     command=lambda: press("/"), height=1, width=7)

 divide.grid(row=5, column=3)



 equal = Button(gui, text=' = ', fg='black', bg='white',

    command=equalpress, height=1, width=7)

 equal.grid(row=5, column=2)



 clear = Button(gui, text='Clear', fg='black', bg='white',

    command=clear, height=1, width=7)

 clear.grid(row=5, column='1')



 # start the GUI

 gui.mainloop()



output:




Saturday, June 29, 2019

python interview questions part 1


Question 1:
x = 6
print(6)
print("6")
what is the out put for the above code??

Question 2:

value1 = eval(input('Please enter a number: '))
value2 = eval(input('Please enter another number: '))
sum = value1 + value2
print(value1, '+', value2, '=', sum)

what is output for the above code??


Question 3:

print(10/3, 3/10, 10//3, 3//10)

out put of the above print??





input function in python


""" ***********************************************
programming Language :Python
Tool(IDE) used :Anaconda -> Spider
 date:06-06-2019
 created by:ANIL DURGAM

*****************************************************"""
print('Please enter an integer value:')
x = input()
print('Please enter another integer value:')
y = input()
num1 = int(x)
num2 = int(y)
print(num1, '+', num2, '=', num1 + num2)

output:
Please enter an integer value:

25
Please enter another integer value:

52
25 + 52 = 77

typed function use in python

print('Please enter some text:')
x = input()
print('Text entered:', x)
print('Type:', type(x))

output:
Please enter some text:

anil
Text entered: anil
Type: <class 'str'>



type is used for find the catagory of the given input

Importing functions from a module in python

Importing functions from a module
Three ways to import functions:
1.
the simplest way: import everything from a module
advantage: simple usage e.g. of math functions
disadvantage: risk of naming conflicts when a variable has the same name as a module function
from numpy import *
print sin(pi/4)
# With this import method the following would give an error:
#sin = 5 # naming conflict!
#print sin(pi/4)
2.
import module under an alias name that is short enough to enhance code clarity
advantage: it is clear to see which function belongs to which module
import numpy as np
print np.sin(np.pi/4)
3.
import only the functions that are needed
advantage: simple usage e.g. of math functions
naming conflict possible, but less probable than with 1.
disadvantage: you must keep track of all the used functions and adapt the import statement if a
new function is used
from numpy import linspace, sin, exp, pi
print sin(pi/4)

using matplot lib


""" ***********************************************
programming Language :Python
Tool(IDE) used :Anaconda -> Spider
 date:06-06-2019
 created by:ANIL DURGAM

*****************************************************"""
from numpy import linspace, sin, exp, pi
import matplotlib.pyplot as mp
# calculate 500 values for x and y without a for loop
x = linspace(0, 10*pi, 500)
y = sin(x) * exp(-x/10)
# make diagram
mp.plot(x,y)
mp.show()


output:


numpy usage in python


""" ***********************************************
programming Language :Python
Tool(IDE) used :Anaconda -> Spider
 date:06-06-2019
 created by:ANIL DURGAM

*****************************************************"""
import numpy as np
# calculate 100 values for x and y without a for loop
x = np.linspace(0, 2* np.pi, 100)
y = np.sin(x)
print(x)
print(y)

output:

[0.         0.06346652 0.12693304 0.19039955 0.25386607 0.31733259
 0.38079911 0.44426563 0.50773215 0.57119866 0.63466518 0.6981317
 0.76159822 0.82506474 0.88853126 0.95199777 1.01546429 1.07893081
 1.14239733 1.20586385 1.26933037 1.33279688 1.3962634  1.45972992
 1.52319644 1.58666296 1.65012947 1.71359599 1.77706251 1.84052903
 1.90399555 1.96746207 2.03092858 2.0943951  2.15786162 2.22132814
 2.28479466 2.34826118 2.41172769 2.47519421 2.53866073 2.60212725
 2.66559377 2.72906028 2.7925268  2.85599332 2.91945984 2.98292636
 3.04639288 3.10985939 3.17332591 3.23679243 3.30025895 3.36372547
 3.42719199 3.4906585  3.55412502 3.61759154 3.68105806 3.74452458
 3.8079911  3.87145761 3.93492413 3.99839065 4.06185717 4.12532369
 4.1887902  4.25225672 4.31572324 4.37918976 4.44265628 4.5061228
 4.56958931 4.63305583 4.69652235 4.75998887 4.82345539 4.88692191
 4.95038842 5.01385494 5.07732146 5.14078798 5.2042545  5.26772102
 5.33118753 5.39465405 5.45812057 5.52158709 5.58505361 5.64852012
 5.71198664 5.77545316 5.83891968 5.9023862  5.96585272 6.02931923
 6.09278575 6.15625227 6.21971879 6.28318531]
[ 0.00000000e+00  6.34239197e-02  1.26592454e-01  1.89251244e-01
  2.51147987e-01  3.12033446e-01  3.71662456e-01  4.29794912e-01
  4.86196736e-01  5.40640817e-01  5.92907929e-01  6.42787610e-01
  6.90079011e-01  7.34591709e-01  7.76146464e-01  8.14575952e-01
  8.49725430e-01  8.81453363e-01  9.09631995e-01  9.34147860e-01
  9.54902241e-01  9.71811568e-01  9.84807753e-01  9.93838464e-01
  9.98867339e-01  9.99874128e-01  9.96854776e-01  9.89821442e-01
  9.78802446e-01  9.63842159e-01  9.45000819e-01  9.22354294e-01
  8.95993774e-01  8.66025404e-01  8.32569855e-01  7.95761841e-01
  7.55749574e-01  7.12694171e-01  6.66769001e-01  6.18158986e-01
  5.67059864e-01  5.13677392e-01  4.58226522e-01  4.00930535e-01
  3.42020143e-01  2.81732557e-01  2.20310533e-01  1.58001396e-01
  9.50560433e-02  3.17279335e-02 -3.17279335e-02 -9.50560433e-02
 -1.58001396e-01 -2.20310533e-01 -2.81732557e-01 -3.42020143e-01
 -4.00930535e-01 -4.58226522e-01 -5.13677392e-01 -5.67059864e-01
 -6.18158986e-01 -6.66769001e-01 -7.12694171e-01 -7.55749574e-01
 -7.95761841e-01 -8.32569855e-01 -8.66025404e-01 -8.95993774e-01
 -9.22354294e-01 -9.45000819e-01 -9.63842159e-01 -9.78802446e-01
 -9.89821442e-01 -9.96854776e-01 -9.99874128e-01 -9.98867339e-01
 -9.93838464e-01 -9.84807753e-01 -9.71811568e-01 -9.54902241e-01
 -9.34147860e-01 -9.09631995e-01 -8.81453363e-01 -8.49725430e-01
 -8.14575952e-01 -7.76146464e-01 -7.34591709e-01 -6.90079011e-01
 -6.42787610e-01 -5.92907929e-01 -5.40640817e-01 -4.86196736e-01
 -4.29794912e-01 -3.71662456e-01 -3.12033446e-01 -2.51147987e-01
 -1.89251244e-01 -1.26592454e-01 -6.34239197e-02 -2.44929360e-16]

functions2 in puthon


""" ***********************************************
programming Language :Python
Tool(IDE) used :Anaconda -> Spider
 date:06-06-2019
 created by:ANIL DURGAM

*****************************************************"""
# function definition
def greeting():
    print("HELLO")
# main program using defined functions
greeting()

output:
HELLO

functions in python

""" ***********************************************
programming Language :Python
Tool(IDE) used :Anaconda -> Spider
 date:06-06-2019
 created by:ANIL DURGAM

*****************************************************"""
# function definitions
# calculate area of a rectangle
def area(b, h):
    A = b * h
    return A

#calulates perimeter of a rectangle
def perimeter(b, h):

    P = 2 * (b+h)
    return P

# main program using defined functions
width = 5
height = 3
print ("Area = ", area(width, height))
print ("Perimeter = ", perimeter(width, height))


output:
Area =  15
Perimeter =  16




example 2:


""" ***********************************************
programming Language :Python
Tool(IDE) used :Anaconda -> Spider
 date:06-06-2019
 created by:ANIL DURGAM
*****************************************************"""
# function definitions
# calculate area of a rectangle
#calulates perimeter of a rectangle
def area_perimeter(b, h):
    A = b * h
    P = 2 * (b+h)
    return A,P

# main program using defined functions
width = 5
height = 3
ar,pr=area_perimeter(width,height)
print("area=",ar)
print("perimeter=",pr)

output:
area= 15
perimeter= 16

iterating with index using python

""" Dispay resistor colour code values"""
colours = [ "black", "brown", "red", "orange", "yellow",
"green", "blue", "violet", "grey","white" ]
cv = list (enumerate (colours))
for c in cv:
    print(c[0], "\t", c[1])


output:
0        black
1        brown
2        red
3        orange
4        yellow
5        green
6        blue
7        violet
8        grey
9        white

python class topic video