Showing posts with label counting. Show all posts
Showing posts with label counting. Show all posts

Thursday, 18 May 2017

Counting the number of times pressed of Switch C program in MPLAB X

Counting the number of times pressed of Switch C program in MPLAB X


In this program we are going to count the number of times a switch pressed,and displaying it on a LCD .. and each time the switch pressed a LED will lighted up.




First create a new project and add a c main file like in the  steps for  Blinking a LED using MPLAB X , PIC16F877A

Components 


1. PIC16F877A micro controller
2. animated LED  
3. A toggle switch
4.16x2 LCD display




Steps in the program

1. define the ports for easy usage 

Defines the control pins of LCD to the 0th,1st and 2nd pin of PORT D
    #define RS RD0
    #define RW RD1
   #define EN RD2
Defines the data port of the  LCD to PORT B
   #define DATA PORTC
Defines switch in the 0th pin of PORT C
   #define S RC0
Defines LED on the 3rd pin of PORT D
   #define L RD3
2. Function declarations .. here we declare 3 functions  and all the 3 functions is for LCD

void LCD_CMD(unsigned int value); //This function is pass the LCD commands
void LCD_DAT(unsigned int value); // this is for pass the DATA to the LCD which is to displayed 
void LCD_INIT(); //this to initialize the LCD
void delay(); // this is to delay the LCD commands 

for more refer the    Interfacing LCD with PIC16F877A in MPLAB X

3. next is main function .. in main function we make 
  •  PORT C as input where we have connected the switch
  •  PORT D as output where we have connected the LED and control pins of LCD
  • PORT B as outout where we have connected DATA pins of LCD
    TRISC  =0x0F;
    TRISD  =0x00;
    TRISB = 0x00;

4. The function LCD_INIT(); is called for initializing the LCD
5. Declared a variable called count, and initialized it to zero
   int count;
   count=0;
6.  we put the LED off 
7. now we are going for a infinite loop by 
    while(1)
    { 
     }
8. In the loop we are checking switch is ON or OFF condition
 if the switch is ON ; LED is on and count is incremented by 1 and display the value of count in LCD 

if the Switch is OFF it wait for the condition when the Switch is ON 

schematic diagram 



full Program
 ###################################################################################
    /*
 * File:   switchcount.c
 * Author: Ebin Ephrem
 
 */

#include<htc.h>
__CONFIG(0x193A);

C
#define RS RD0
#define RW RD1
#define EN RD2
#define DATA PORTB
#define S RC0
#define L RD3

void LCD_CMD(unsigned int value);
void LCD_DAT(unsigned int value);
void LCD_INIT();
void delay();



void main(void)
{
    TRISC  =0x0F;
    TRISD  =0x00;
    TRISB = 0x00;
LCD_INIT();
int count;
count=0;
L=0;

  while(1)
  {
if( S == 1)
       {
    while(S==1);
            L= ~L;
                     count = count+1;
              LCD_CMD(0x01);
        delay ();
    LCD_DAT(0 + (count % 10));
}
       }
 }

void LCD_INIT()
 {
    LCD_CMD(0x01);
    delay ();
    LCD_CMD(0x38);
    delay ();
    LCD_CMD(0x0F);
    delay ();
    LCD_CMD(0x06);
    delay ();
    LCD_CMD(0x0c);
    delay ();

    LCD_CMD(0x80);
    delay ();
}
void LCD_CMD(unsigned int  value)
{
    DATA =  value;
    RS=0;
    RW=0;
    EN=1;
    delay ();
    EN=0;
    delay ();
 }

void LCD_DAT(unsigned int value)
{
    DATA= value ;

    RS=1;
    RW=0;
    EN=1;
    delay ();
    EN=0;
    delay();

}

void delay(void)
{
    int counter = 0;
    for (counter = 0; counter<1000; counter++) {
        ;
    }
}





{ Read More }


Friday, 14 April 2017

Counting lines in shell variables

Counting lines in shell variables


Very often one stores the output of a command in a variable, sometimes the output is a multiline string and one wants to count the number of lines (all of the below is in Bourne shell).

Example:
A=`who` NUM_USERS=`echo "$A"|wc -l` echo "Number of users: $NUM_USERS" 
Unfortunately this approach is not correct.

If there are users on the system it works fine and the correct count is reported.
But if there are no users then the code above still reports a user count of 1.

Why?
Because echo of an empty variable still adds a newline to the output which is counted by wc. Look at this example (assuming that you dont have a variable called avTyh).
echo "$avTyh" |wc -l 1 

Replacing echo by printf does not help much.
printf "$A"|wc -l
works correctly for empty variables but it counts wrongly for a multiline string since it omits the final newline.

Rather than using an if ... else ... construct there is a more elegant solution. Look at this:
printf "$A${A:+ }" |wc -l 
The parameter substitution ${A:+ } achieves the following:
If $A is set and is non-null then substitute a newline; otherwise substitute nothing.
So if $A is empty then "$A${A:+ }" is something empty and nothing which counts to zero.
If $A is non-empty then "$A${A:+ }" is something plus a newline.

Note: in the end I think the strange thing is that in an assignment like A=`who` the string is missing a final newline which leads to the issue after all, I should check why this is the case, maybe in another post.
{ Read More }