Posts

Showing posts from June, 2014

msp430 timer programming interrupt based

Image
Basically msp4302xxx family has two 16-bit timers. Timer A and Timer B. We will see today about Timer A. Timer A can work in four modes based on the values of MCx in TACTL register If MCx = 00 then it is in stop mode. It means the timer is stopped. If MCx = 01 then it is in Up mode. It means the timer counts up to the value present in the TACCR0 register and goes to zero and repeats the cycle If MCx = 10 then it is in Continuous mode,in which the timer counts up to the value  0xffff and jumps to zero and repeats the same. If MCx = 11 then it is in Up/Down mode where the timer counts up to the value present in the TACCR0 and starts decrementing to zero and again starts from zero to TACCR0 The timer can be sourced from either ACLK,TACLK,SMCLK and INCLK with a divisor factor upt0 8 based on the values of TASSELx and IDx in the TACTL register We will see how to program the timer A present in msp430. For programming we have to use the Timer A registers. 1) TAR: It i

#pragma warn example in C

For disabling warnings (non-critical Messages ) we can use #pragma warn directive Syntax: #pragma warn +xxx #pragma warn -xxx #pragma warn .xxx + -----> means on - -----> means off . ------> means toggle xxx is  a 3 character warning code. For example rvl means ignore all the warnings where there is function which has to  return value but is not returning. Code: #include<stdio.h> #pragma warn rvl int add(int a,int b); int main() { add(3,5); } int add(int a,int b){ printf("The result is %d",a+b); } This will not be supported by gcc  compiler as it does not support all the #pragma directives . First if we compile this program with gcc ,the compiler will not show any warnings. To see the warnings we have to pass -Wall arguments to the gcc compiler. 

Pragmas in C language

Pragma directive is a method specified by the C standard for providing additional information to the C compiler.Basically it can be used to turn on or turn off certain features.Pragmas are operating system specific and are different for every compiler. Examples of some of the pragmas  are #pragma startup,#pragma exit Example of #pragma startup: Syntax:  #pragma startup <function name> [priority] By using #pragma startup directive we can call the functions on startup before main function execution #include<stdio.h> void print1() { printf("Before Main 1\n"); } void print2() {     printf("Before Main 2\n"); } #pragma startup print1 0 #pragma startup print2   1 int main() {                                                   printf("In Main\n");     return 0; } This will not work when compiled on gcc as it does not recognize this pragma directive.If we want any function to be called before main function by us

C Program for shutting down the Operating System

#include<stdio.h> #include<stdlib.h> int main() { //for linux based systems system("sudo init 0"); //system executes a  shell command. //for windows        //system("shutdown -s"); return 0; }

C-Program to check whether a number is power of 2 or not

#include <stdio.h> #include <string.h> main() {   int number;   printf("Enter the number\n");   scanf("%d",&number);   if(!(number&(number-1)))       printf("%d is a power of 2\n",number);   else       printf("%d is not a power of 2\n",number); }

C-Program to count the number of ones in a Number

We will see the different ways in which a number of ones in a number can be calculated. For example in the number 25(11001) it has 3 ones present in it. We will get that value programmatically. There are two methods for getting the result: Method1: #include <stdio.h> #include <string.h> main() {   int number,count=0;      printf("Enter the number\n");   scanf("%d",&number);      while(number>0){       count++;       number=number&(number-1); //this will clear the last set bit          }   printf("The count is %d",count); } Method 2: #include <stdio.h> #include <string.h> main() {   int number,count=0;      printf("Enter the number\n");   scanf("%d",&number);      while(number>0){ //checking the last bit whether it is 1 and shifting by 1       if(number&1)         count++;       number=number>>1;   }   printf("The