ORG 00H MOV DPTR,#SSDisplay // moves the address of LUT to DPTR MOV P1,#00000000B // sets P1 as output port MOV P0,#00000000B // sets P0 as output port CLR P3.0 // sets P3.0 as output for sending trigger SETB P3.1 // sets P3.1 as input for receiving echo MOV TMOD,#00100000B // sets timer1 as mode 2 auto reload timer MAIN: MOV TL1,#210D // loads the initial value to start counting from MOV TH1,#210D // loads the reload value MOV A,#00000000B // clears accumulator SETB P3.0 // starts the trigger pulse ACALL DELAY1 // gives 10uS width for the trigger pulse CLR P3.0 // ends the trigger pulse HERE: JNB P3.1,HERE // loops here until echo is received BACK: SETB TR1 // starts the timer1 HERE1: JNB TF1,HERE1 // loops here until timer overflows (ie;48 count) CLR TR1 // stops the timer CLR TF1 // clears timer flag 1 INC A // increments A for every timer1 overflow JB P3.1,BACK // jumps to BACK if echo is still available MOV R4,A // saves the value of A to R4 ACALL DLOOP // calls the display loop SJMP MAIN // jumps to MAIN loop DELAY1: MOV R6,#2D // 10uS delay LOOP1: DJNZ R6,LOOP1 RET DLOOP: MOV R5,#50D // loads R5 with 100D BACK1: MOV A,R4 // loads the value in R4 to A MOV B,#100D // loads B with 100D DIV AB // isolates the first digit SETB P1.0 // activates LED display unit D1 ACALL DISPLAY // calls DISPLAY subroutine MOV P0,A // moves digit drive pattern for 1st digit to P0 ACALL DELAY // 1mS delay ACALL DELAY MOV A,B // moves the remainder of 1st division to A MOV B,#10D // loads B with 10D DIV AB // isolates the second digit CLR P1.0 // deactivates LED display unit D1 SETB P1.1 // activates LED display unit D2 ACALL DISPLAY MOV P0,A // moves digit drive pattern for 2nd digit to P0 ACALL DELAY ACALL DELAY MOV A,B // moves the remainder of 2nd division to A CLR P1.1 // deactivates LED display unit D2 SETB P1.2 // activates LED display unit D3 ACALL DISPLAY MOV P0,A // moves the digit drive pattern for 3rd digit to P0 ACALL DELAY ACALL DELAY CLR P1.2 // deactivates LED display unit D3 DJNZ R5,BACK1 // repeats the display loop 100 times RET DELAY: MOV R7,#250D // 1mS delay LABEL2: DJNZ R7,LABEL2 RET DISPLAY: MOVC A,@A+DPTR // gets the digit drive pattern for the content in A RET SSDisplay: DB 0C0H // Hex code to display DISPLAY 0 DB 0F9H // DISPLAY 1 DB 0A2H // DISPLAY 2
Write, Run & Share C Language code online using OneCompiler's C online compiler for free. It's one of the robust, feature-rich online compilers for C language, running the latest C version which is C18. Getting started with the OneCompiler's C editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'C' and start coding!
OneCompiler's C online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample C program which takes name as input and print your name with hello.
#include <stdio.h>
int main()
{
char name[50];
printf("Enter name:");
scanf("%s", name);
printf("Hello %s \n" , name );
return 0;
}
C language is one of the most popular general-purpose programming language developed by Dennis Ritchie at Bell laboratories for UNIX operating system. The initial release of C Language was in the year 1972. Most of the desktop operating systems are written in C Language.
When ever you want to perform a set of operations based on a condition if-else
is used.
if(conditional-expression) {
// code
} else {
// code
}
You can also use if-else for nested Ifs and if-else-if ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to if-else-if ladder.
switch(conditional-expression) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
// code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
// code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.
data-type array-name[size];
data-type array-name[size][size];
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
Two types of functions are present in C
Library functions are the in-built functions which are declared in header files like printf(),scanf(),puts(),gets() etc.,
User defined functions are the ones which are written by the programmer based on the requirement.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
//code
}