Showing posts with label C Programming Language. Show all posts
Showing posts with label C Programming Language. Show all posts

Thursday, October 15, 2020

MM32F103C8T6 with EC20 modules test code

 Using simple serial/UART communication using we can connect with MM32 mcu with EC20 gsm modules.


please find the code below

download

Thursday, September 24, 2020

STM32F407VET6 RTC example code

 /* USER CODE BEGIN Header */

/**

  ******************************************************************************

  * @file           : main.c

  * @brief          : Main program body

  ******************************************************************************

  * @attention

  *

  * <h2><center>&copy; Copyright (c) 2020 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"


/* Private includes ----------------------------------------------------------*/

/* USER CODE BEGIN Includes */


/* USER CODE END Includes */


/* Private typedef -----------------------------------------------------------*/

/* USER CODE BEGIN PTD */

RTC_TimeTypeDef sTime = {0};

RTC_DateTypeDef sDate = {0};

RTC_AlarmTypeDef sAlarm = {0};


/* 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 ---------------------------------------------------------*/

RTC_HandleTypeDef hrtc;


/* USER CODE BEGIN PV */


/* USER CODE END PV */


/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_RTC_Init(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_RTC_Init();

  /* USER CODE BEGIN 2 */

sTime.Hours = 11;

  sTime.Minutes = 17;

  sTime.Seconds = 10;

HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN);

sDate.WeekDay = RTC_WEEKDAY_FRIDAY;

  sDate.Month = RTC_MONTH_SEPTEMBER;

  sDate.Date = 25;

  sDate.Year = 2020;

HAL_RTC_SetDate(&hrtc,&sDate,  RTC_FORMAT_BIN);

HAL_Delay(200);

  /* USER CODE END 2 */


  /* Infinite loop */

  /* USER CODE BEGIN WHILE */

  while (1)

  {

    /* USER CODE END WHILE */

HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);

HAL_RTC_GetDate(&hrtc,&sDate,  RTC_FORMAT_BIN);

HAL_Delay(200);

    /* 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};

  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};


  /** Configure the main internal regulator output voltage 

  */

  __HAL_RCC_PWR_CLK_ENABLE();

  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the CPU, AHB and APB busses clocks 

  */

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;

  RCC_OscInitStruct.HSIState = RCC_HSI_ON;

  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;

  RCC_OscInitStruct.LSIState = RCC_LSI_ON;

  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();

  }

  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;

  PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;

  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)

  {

    Error_Handler();

  }

}


/**

  * @brief RTC Initialization Function

  * @param None

  * @retval None

  */

static void MX_RTC_Init(void)

{


  /* USER CODE BEGIN RTC_Init 0 */


  /* USER CODE END RTC_Init 0 */


  RTC_TimeTypeDef sTime = {0};

  RTC_DateTypeDef sDate = {0};

  RTC_AlarmTypeDef sAlarm = {0};


  /* USER CODE BEGIN RTC_Init 1 */


  /* USER CODE END RTC_Init 1 */

  /** Initialize RTC Only 

  */

  hrtc.Instance = RTC;

  hrtc.Init.HourFormat = RTC_HOURFORMAT_24;

  hrtc.Init.AsynchPrediv = 127;

  hrtc.Init.SynchPrediv = 255;

  hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;

  hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;

  hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;

  if (HAL_RTC_Init(&hrtc) != HAL_OK)

  {

    Error_Handler();

  }


  /* USER CODE BEGIN Check_RTC_BKUP */

    

  /* USER CODE END Check_RTC_BKUP */


  /** Initialize RTC and set the Time and Date 

  */

  sTime.Hours = 10;

  sTime.Minutes = 35;

  sTime.Seconds = 10;

  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;

  sTime.StoreOperation = RTC_STOREOPERATION_RESET;

  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)

  {

    Error_Handler();

  }

  sDate.WeekDay = RTC_WEEKDAY_FRIDAY;

  sDate.Month = RTC_MONTH_SEPTEMBER;

  sDate.Date = 25;

  sDate.Year = 2020;


  if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)

  {

    Error_Handler();

  }

  /** Enable the Alarm A 

  */

  sAlarm.AlarmTime.Hours = 0;

  sAlarm.AlarmTime.Minutes = 0;

  sAlarm.AlarmTime.Seconds = 0;

  sAlarm.AlarmTime.SubSeconds = 0;

  sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;

  sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;

  sAlarm.AlarmMask = RTC_ALARMMASK_NONE;

  sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;

  sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;

  sAlarm.AlarmDateWeekDay = 1;

  sAlarm.Alarm = RTC_ALARM_A;

  if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK)

  {

    Error_Handler();

  }

  /* USER CODE BEGIN RTC_Init 2 */


  /* USER CODE END RTC_Init 2 */


}


/* 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****/



https://drive.google.com/file/d/1s1pKxCdkc1oZJDfm-WhoNq1UeWmM2W1r/view?usp=sharing

to download file click above link


Wednesday, September 23, 2020

MM32 microcontroller with GSM and TCP/IP

 https://drive.google.com/file/d/14yXySxDJGcEgbD1S2hihYivG_d6Abnru/view?usp=sharing

please find the code in the above link

Tuesday, September 15, 2020

c program

 #include <stdio.h>

/* print Fahrenheit-Celsius table

for fahr = 0, 20, ..., 300 */


int main(void)

{

float fahr, celsius;

int lower, upper, step;


lower = 0;

upper = 300;

step = 20;


/* lower limit of temperature table *1

1* upper limit *1

1* step size */

fahr = lower;

while (fahr <= upper)

  {

celsius = 5.0/9.0 * (fahr-32.0) ;

printf("%3.0f \t %6.1f\n", fahr, celsius);

/*

   %d print as decimal integer

   %6d print as decimal integer, at least 6 characters wide print as floating point

   %6f print as floating point, at least 6 characters wide

   %.2f print as floating point, 2 characters after decimal point

                   %6.2f print as floating point, at least 6 wide and 2 after decimal point

*/

fahr = fahr + step;

}

}

Friday, September 11, 2020

MM32 MCU with relay ON/OFF code

 #include "sys.h"

#include "delay.h"

#include "uart.h"

#include "led.h"


int main(void)

{

    delay_init();    

    

    Relay_Init();


    while(1)             

    {

//relay connected on PB13

        Relay = !Relay;

        delay_ms(1000);

    }

}


led.h

#ifndef __LED_H

#define __LED_H

#include "sys.h"

//////////////////////////////////////////////////////////////////////////////////

//¿ª·¢°å

//LEDÇý¶¯´úÂë

//////////////////////////////////////////////////////////////////////////////////

//#define LED4 PAout(15) // PA15

//#define LED3 PBout(3) // PB3

//#define LED2 PBout(4) // PB4

#define Relay PBout(13) // PB13-relay is connected at that pin


void Relay_Init(void);//³õʼ»¯



#endif


led.c

void Relay_Init(void)

{


    GPIO_InitTypeDef  GPIO_InitStructure;



    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); //¿ªÆôGPIOA,GPIOBʱÖÓ

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);//¿ªÆô¸´ÓÃʱÖÓ£¬Õâ¸öÒ»¶¨Òª¿ª

    GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);//ʧÄÜJTAG£¬Ê¹ÄÜSW


  /*  GPIO_InitStructure.GPIO_Pin  =  GPIO_Pin_15;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

    GPIO_Init(GPIOA, &GPIO_InitStructure);

*/

    GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_13 ;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

    GPIO_Init(GPIOB, &GPIO_InitStructure);


    Relay = 1;


}



Tuesday, May 5, 2020

STM32F407VET6 bare metel led blink code

#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"

void Delay()
{
  
for(int i=0;i<500000;i++);
}
int main()
{
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
GPIOA -> MODER |= GPIO_MODER_MODER6_0;
// GPIOA -> MODER |= GPIO_MODER_MODER8_0;
while(1)
{
GPIOA -> ODR |= 0x00000040 ;//GPIO_ODR_6;
Delay();
GPIOA -> ODR &= ~0x00000040;
Delay();
}

}

Friday, May 1, 2020

stm32f407vet6 led toggle code working

/* USER CODE END WHILE */
//HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
//HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_RESET);
// HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,0);

HAL_Delay(1000);
//HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_SET);
//HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,1);
HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_4);
HAL_Delay(1000);
HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_6);
HAL_Delay(1000);
HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_7);

Thursday, April 23, 2020

reverse the given array in c language

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num, *arr, i;
    scanf("%d", &num);
    arr = (int*) malloc(num * sizeof(int));
    for(i = 0; i < num; i++) {
        scanf("%d", arr + i);
    }


    /* Write the logic to reverse the array. */


    for(i = num-1; i>-1; i--)
        printf("%d ", *(arr + i));
    return 0;
}

output:
  • 6
  • 16 13 7 2 1 12 
  • 
    
  • 
    
  • 12 1 2 7 13 16

how to print the range between two numbers in text format

int main() 
{
    int a, b;
    char *str[] = {"one", "two", "three", "four", "five", "six", "seven",  "eight",  "nine", "even", "odd"};
    scanf("%d\n%d", &a, &b);
    // Complete the code.
         
  for (int i=a; i<=b; i++) {
    if (i <= 9) 
         printf ("%s\n", str[i-1]);
    else 
        printf ("%s\n", str[9+(i%2)]);

  }
  return 0;
}

output:
input:
8
11
output:
eight
nine
even
odd

Wednesday, April 15, 2020

print a character and a string and sentence

#include <stdio.h>
#include <stdlib.h>


int main()
{
char a,a1[50],a2[100];
scanf("%c%*c",&a);
scanf("%s%*c",&a1);
scanf("%[^\n]",&a2);
printf("%c\n%s\n%s",a,a1,a2);
return 0;
}

output:
h
hello
how are you

h
hello
how are you

Monday, February 17, 2020

How to Find the Average of the marks of a student in C language

#include <stdio.h>
#include <stdlib.h>

  main()
  {  int m1,m2,m3,m4,m5,sum;
        float avg;
        printf("Enter Subject 1 Marks : ");
        scanf("%d",&m1);
        printf("Enter Subject 2 Marks : ");
        scanf("%d",&m2);
        printf("Enter Subject 3 Marks : ");
        scanf("%d",&m3);
        printf("Enter Subject 4 Marks : ");
        scanf("%d",&m4);
        printf("Enter Subject 5 Marks : ");
        scanf("%d",&m5);
        sum = m1+m2+m3+m4+m5;
        avg = sum/5;
        printf("THE AVERAGE IS  %f",avg);
       }

output:
Enter Subject 1 Marks : 95
Enter Subject 2 Marks : 98
Enter Subject 3 Marks : 93
Enter Subject 4 Marks : 96
Enter Subject 5 Marks : 92
THE AVERAGE IS  94.000000

Sunday, February 16, 2020

Write a program to find area and circumference of a circle In C language

#include <stdio.h>
#include <stdlib.h>

void main()
{ float rad,area,circum;
printf("\n enter radius:    ");
scanf("%f",&rad);
printf("\n radius = %f",rad);
area = 22.0/7.0 * rad * rad;
circum = 2.0 * 22.0/7.0 * rad;
printf("\n area of a circle = %f", area);
printf("\n circumference of a circle = %f", circum);
getch();
}

output:

 enter radius:    10

 radius = 10.000000
 area of a circle = 314.285706
 circumference of a circle = 62.857143

how to program for reading the ASCII code of a character and vice versa in C language

#include <stdio.h>
#include <stdlib.h>


void main()
{
    char x;
 int n;

printf("\n enter a character");
scanf("%c",&x);
printf ("\n given character is  %c", x);
printf ("\n ASCII code is %d\n " , x);
printf("\n enter a ASCII code");
scanf("%d",&n);
printf ("\n ASCII code is  %d", n);
printf ("\n equivalent character is %c", n);
getch();
}

output:
 enter a charactera

 given character is  a
 ASCII code is 97

 enter a ASCII code65

 ASCII code is  65
 equivalent character is A

how to Program for reading integers, characters and strings from the keyboard and displaying them in C language

#include <stdio.h>
#include <stdlib.h>

void main()
{int i;
char c , a[20];
printf("\n enter a character");
scanf("%c",&c);
printf("\n enter an integer");
scanf("%d",&i);
printf("\n enter a string");
scanf("%s",a);
printf("\n given character = %c",c);
printf("\n given integer = %d",i);
printf("\n given string = %s",a);
getch();
}

output:
 enter a charactera

 enter an integer123

 enter a stringanildurgam

 given character = a
 given integer = 123
 given string = anildurgam

How to Addition, Subtraction, Multiplication, Division and Modulus operation on two integers in C language

#include <stdio.h>
#include <stdlib.h>

void main()
{ int a,b;
printf("\n enter two values: ");
scanf("%d %d",&a,&b);
printf("\n a + b = %d", a+b);
printf("\n a - b = %d", a-b);
printf("\n a * b = %d", a*b);
printf("\n a / b = %d", a/b);
printf("\n a modulus b = %d", a%b);
getch();
}

output:

 enter two values: 56
64

 a + b = 120
 a - b = -8
 a * b = 3584
 a / b = 0
 a modulus b = 56

Thursday, January 23, 2020

How to Perform Addition, Subtraction, Multiplication, Division and Modulus operation on two integers.


#include<stdio.h>
void main()
{ int a,b;
printf("\n enter two values: ");
scanf("%d %d",&a,&b);
printf("\n a + b = %d", a+b);
printf("\n a - b = %d", a-b);
printf("\n a * b = %d", a*b);
printf("\n a / b = %d", a/b);
printf("\n a modulus b = %d", a%b);
getch();
}

input:

 enter two values: 13  5

output:

 a + b = 18           
 a - b = 8
 a * b = 65
 a / b = 2
 a modulus b = 3

Monday, December 9, 2019

Perform Addition, Subtraction, Multiplication, Division and Modulus operation on two integers.


#include<stdio.h>
void main()
{ int a,b;
printf("\n enter two values: ");
scanf("%d %d",&a,&b);
printf("\n a + b = %d", a+b);
printf("\n a - b = %d", a-b);
printf("\n a * b = %d", a*b);
printf("\n a / b = %d", a/b);
printf("\n a modulus b = %d", a%b);
getch();
}




input:

 enter two values: 13  5

output:

 a + b = 18           
 a - b = 8
 a * b = 65
 a / b = 2
 a modulus b = 3

Tuesday, October 29, 2019

"sizeof" function in c language

#include <stdio.h>
#include <stdlib.h>

int main()
{
   int a =0;
   char c="b";
   float d=1.10;
   long e=12345678;
   double f=1234567812345;
   long double g=123456789;
   printf("int==%d\nchar==%d\nfloat====%d",sizeof(a),sizeof(c),sizeof(d));
   printf("\nlong==%d",sizeof(e));
   printf("\ndouble==%d",sizeof(f));
   printf("\nlong double==%d",sizeof(g));
    return 0;
}

output:

int==4
char==1
float====4
long==4
double==8

long double==12

note :i am using code block ide ...so my values are like this ....if your using any other compiler then values will be changed as per the compiler

Wednesday, August 28, 2019

NUMBER SERIES BASIC TESTING

/**************************************************
** written by :ANIL DURGAM
** NUMBER SERIES TESTING VER 1.0
**DATE:29-08-2019
**************************************************/



#include <stdio.h>
#include <stdlib.h>

int main()
{
    int first_num,sec_num,third_num,fourth_num,next_number;
    int first_diff,second_diff,third_diff,fourth_diff;
    printf("please enter the FOUR number after every number hit ENTER\n");
    scanf("%d%d%d%d",&first_num,&sec_num,&third_num,&fourth_num);
    printf("first number=%d,\nsecond number=%d,\nthird number=%d,\nfourth number=%d\n",first_num,sec_num,third_num,fourth_num);
    first_diff=sec_num-first_num;
    second_diff=third_num-sec_num;
    third_diff=fourth_num-third_num;
    if(first_diff==second_diff)
    {
        if(third_diff==second_diff)
        {
            next_number=first_diff+fourth_num;
            printf("next number is ==%d\n",next_number);
        }
        else
        {
            printf("third and fourth number is not matching\n");
        }
    }
    else{

        printf("number not matching\n");
    }

    return 0;
}


OUTPUT:
please enter the FOUR number after every number hit ENTER
12
24
36
48
first number=12,
second number=24,
third number=36,
fourth number=48


next number is ==60

format type in c language

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a=789,b=1234;
    printf("a=%3d, b=%4d", a, b);
    return 0;
}

output:
a=789,b=1234

input is
a=9,b=4;
a=  9, b=   4


python class topic video