Monday, June 4, 2018

STM8 MCU LED Blinking Sample Code

STM8 MCU LED Blinking Sample Code

STM8S103K3 LED Blinking Sample Code:

Example 1:


#include "stm8s.h"

#define LED_GPIO_PORT (GPIOC) //Port configuration may be vary. Please check.
#define LED_GPIO_PINS (GPIO_PIN_3 | GPIO_PIN_2 | GPIO_PIN_1 | GPIO_PIN_0)

void delay(uint16_t myCount);

void main(void)
{

GPIO_Init(LED_GPIO_PORT, (GPIO_Pin_TypeDef)LED_GPIO_PINS, GPIO_MODE_OUT_PP_LOW_FAST);
while(1)
{
GPIO_WriteReverse(LED_GPIO_PORT, (GPIO_Pin_TypeDef)LED_GPIO_PINS);
delay(0xFFFF);
}
}

void delay(uint16_t myCount)
{
while(myCount != 0)
{
myCount--;
}
}

#ifdef USE_FULL_ASSERT

void assert_failed(uint8_t* file, uint32_t line)
{
while(1)
{}
}
#endif


Example 2:


#include "stm8s.h"
#include "STM8S003K3.h"
void myDelay(void);
void myDelay()
{
   int i, j;
   for(i=0; i<1000; i++)
   {
       for(j=0; j<100; j++);    
   }  
}   
int main()
{
    // Configure pins (Port/Pin number may be vary)
    PC_DDR = 0x17;
    PC_CR1 = 0x17;
// Loop
while(1)
{
           PC_ODR ^= 0x17;
           myDelay();
}
}

Thursday, September 8, 2016

How to find factorial of a given number in C Language

C Program for factorial without recursion:

#include<stdio.h>
//Main funtion
void main()
{
int num, factorial;
num = 5; factorial = 1;

while(num >= 2)
{
if(num <= 1)
{ break; }
else
{ factorial = factorial * num; }
--num;
}
printf("factorrial of given number is = %d", factorial);
}

C Program for factorial with recursion:

#include<stdio.h>

//Funtion that calculate factorial using recursion
int factorial(int n)
{
return(n<=1 ? 1 : n*factorial(n-1));
}

//Main funtion that calls factorial function
void main()
{
int x = 5;
printf("factorrial of given number is = %d", factorial(x));
}

Saturday, July 16, 2016

How to write a low level file copy program in C Language

Low level file copy program in C Language

Low Level File Copy Program:


#include "fcntl.h"
#include "types.h"
#include "stat.h"
main(int argc, char *argv[])
{
char buffer[512];
int in, out, bytes;
in = open(argv[1], O_RDONLY|O_BINARY);
if (in == -1)
{
puts("Cannot open file");
exit();
}
out = open(argv[2], O_CREATE|O_WRONLY|O_BINARY|S_IWRITE);
if(out == -1)
{
puts("Cannot open file");
exit();
}
while(1)
{
bytes = read(in, out, 512);
if (bytes > 0)
{
write(out, buffer, bytes);
}
else
{
break;
}
}
close(in);
close(out);
}

Monday, May 23, 2016

How to find GCD of two numbers in C Language

Find GCD of two numbers in C Language

C Program for GCD of Two Numbers:


#include<stdio.h>
void main()
{
int x, y;
x = 12; y = 18;

while(x != y)
{
if(x > y)
x = x - y;
else
y = y - x;
}

printf("GCD of x and y is = %d", x);
}

How to find LCM of two numbers in C Language

Find LCM of two numbers in C Language

C Program for LCM of Two Numbers:


#include<stdio.h>
void main()
{
int x, y, LCM;
x = 4; y = 9; LCM =1;

for(;;)
{
if(LCM % x != 0 || LCM % y != 0)
++LCM;
else
break;
}

printf("LCM of x and y is = %d", LCM);
}

How to swap two numbers in C Language

Swapping of two numbers in C Language

Using Two Variable:

Program 1:

#include<stdio.h>
#define SWAP(a,b) (a)^=(b)^=(a)^=(b)
void main()
{
int a,b;
a=12; b=17;
SWAP(a,b);
printf("a=%i,b=%i",a,b);
}

Program 2:

#include<stdio.h>
void main()
{
int a,b;
a=12; b=17;
a=a+b;
b=a-b;
a=a-b;
printf("a=%i,b=%i",a,b);
}

Using Third Variable:

#include<stdio.h>
void main()
{
int a,b,temp;
a=12; b=17; temp=0;
temp=a;
a=b;
b=temp;
printf("a=%i,b=%i",a,b);
}

Sunday, May 15, 2016

How to print "Hello World" without using semicolon in main?

How to print "Hello World" without using semicolon in main

There are number of ways you can do it.

First Way:

#include<stdio.h>
void main()
{
if (printf("Hello World")) {}
}

Second Way:

#include<stdio.h>
void main()
{
while(!printf("Hello World")) {}
}

Third Way:

#include<stdio.h>
void main()
{
switch(printf("Hello World")) {}
}

Fourth Way:

#include<stdio.h>
#define PRINT printf("Hello World")
void main()
{
if(PRINT) {}
}