In: Electrical Engineering
Write a program to display (on 7-segment) continuous up-counting numbers from 00 to 99 (in decimal system) when switch SW2 is pressed and released, and down-counting when switch SW3 is pressed and released.
PLEASE CODE IN C.
PROVIDE ALGORITHM ALSO.
ALGORITHM--
For common cathode 7-seg led Hex code corresponding to digits
{0,1,2,3,4,5,6,7,8,9}
will be{0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82, 0xF8,0x80,0x90}
Step 1: Start
Step 2: Declare variables SW2,SW3,LED1,LED2,i,j
Step 3: Initialize hex code array LED_CODE[]
Step 4: Define a delay function delay() to generate 200ms delay
Step 5: Read SW1 & SW2 from user.
Step 6: If SW2=1
i=0;
Repeat the steps until i<10
j=0;
6.1 Repeat the steps until j<10
LED2 = LED_CODE[j]
call delay() function
j=j+1
6.2 LED1 = LED_CODE[j]
6.3 i=i+1
Step 7: If SW3=1
i=9
Repeat the steps until i>=0
J=9;
7.1 Repeat the steps until j>=0
LED2 = LED_CODE[j]
call delay() function
j=j-1
7.2 LED1 = LED_CODE[j]
7.3 i=i-1
Step 8: Stop
C CODE--(For 8051 microcontroller)
#include <reg51.h>
#define LED1 P2 //PORT2.0 is connected to segment '1'
#define LED2 P1 //PORT1.0 is connected to segment '2'
void msdelay() // Function for creating delay of 200 milliseconds.
{
unsigned i;
for(i=0;i<25000;i++);
}
void main()
{
unsigned char i,j;
unsigned int sw2,sw3;
unsigned char seg_code[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82, 0xF8,0x80,0x90};
if(sw2==1)
{
for(i=0;i<10;i++)
{
LED1=seg_code[i];
for(j=0;j<10;j++)
{
LED2=seg_code[j];
msdelay();
}
}
}
if(sw3==1)
{
for(i=9;i>=0;i--)
{
LED1=seg_code[i];
for(j=9;j>=0;j--)
{
LED2=seg_code[j];
msdelay();
}
}
}
}