Want help to write program in C language for 8051 Micro Controller
Discussion in "Project Help" started by jigneshdoshi Dec 11, 2015.
Fri Dec 11 2015, 12:42 pm
Hello all,
Greetings for the day...!!!
still today i write program in assembly language for 8051 Micro Controller but Now i want to write program in C language and i have basic knowledge of C language.
my problem is if i want to do addition, multiplication, hex to decimal conversion, etc.. for more then 8 bits then how can i write.
Logic is same as assembly language or is there any other formula that i will use and my work is easy?
Greetings for the day...!!!
still today i write program in assembly language for 8051 Micro Controller but Now i want to write program in C language and i have basic knowledge of C language.
my problem is if i want to do addition, multiplication, hex to decimal conversion, etc.. for more then 8 bits then how can i write.
Logic is same as assembly language or is there any other formula that i will use and my work is easy?
Sun Dec 13 2015, 04:12 am
In C, you work with variables, which can be 8 bit ,16 bit ,or more.
Multiplying two 16 bit values is as easy as
I'm not sure what you mean by "hex to decimal conversion".
You can use printf() to print numbers but it uses a lot of code.
Multiplying two 16 bit values is as easy as
// unsigned integers are usually 16 bit for 8 bit micros unsigned int value1 = 1000; unsigned int value2 = 60; unsigned int result=0; result=value1 * value2;
I'm not sure what you mean by "hex to decimal conversion".
You can use printf() to print numbers but it uses a lot of code.
Fri Dec 18 2015, 06:31 am
Just use a 32 bit variable size.
Check with your compiler reference.
For Keil it is a unsigned long int or a signed long int
depending on what you are doing.
Unsigned long handle bigger numbers but signed long
handles negative values.
Of course the result may not fit into a 32 bit variable, so you would
need to make result 64bit , but not all compilers have 64 bit variables.
Check with your compiler reference.
For Keil it is a unsigned long int or a signed long int
depending on what you are doing.
Unsigned long handle bigger numbers but signed long
handles negative values.
unsigned long int value1 = 200000; unsigned long int value2 = 100000; unsigned long int result=0; result=value1 + value2;
Of course the result may not fit into a 32 bit variable, so you would
need to make result 64bit , but not all compilers have 64 bit variables.
[ Edited Fri Dec 18 2015, 06:42 am ]
Fri Dec 18 2015, 10:30 am
Thanks for your reply...
and then if i want to display it on 20x4 Alphanumeric LCD then how can i do?
and then if i want to display it on 20x4 Alphanumeric LCD then how can i do?
Mon Dec 21 2015, 08:04 am
Thanks for your reply...
and then if i want to display it on 20x4 Alphanumeric LCD then how can i do?jigneshdoshi
Take a look at our LCD tutorial, it gives examples in c and asm.
Then write a routine to print to the LCD.
You can write your own code to turn a number into an
ASCII string or use sprintf(), if your compiler has it.
Not all do.
sprintf() creates an ASCII string in a buffer, you just copy it
to the LCD display.
Sat Feb 20 2016, 02:35 pm
What is the meaning of using 2 in below code?
is it compulsory to write down this in interrupt routine?
void timer0 (void) interrupt 1 using 2 {
if (++interruptcnt == 4000) { /* count to 4000 */
second++; /* second counter */
interruptcnt = 0; /* clear int counter */
}
is it compulsory to write down this in interrupt routine?
void timer0 (void) interrupt 1 using 2 {
if (++interruptcnt == 4000) { /* count to 4000 */
second++; /* second counter */
interruptcnt = 0; /* clear int counter */
}
Mon Feb 22 2016, 04:51 am
What is the meaning of using 2 in below code?
void timer0 (void) interrupt 1 using 2 {
if (++interruptcnt == 4000) { /* count to 4000 */
second++; /* second counter */
interruptcnt = 0; /* clear int counter */
}jigneshdoshi
I believe the 8051 has two sets of registers
R0,R1,R2 etc
"using 2" tells the compiler to use set 2 for this interrupt
instead of the default, set 1.
I suppose in some cases it will save a couple of microseconds.
Normally I doubt it matters.
Try with "using 1" or nothing, let us know if it makes
a difference.
Tue Feb 23 2016, 05:29 pm
ok thanks,
Now i want to take data from interrupt subroutine to main function or other subroutine then how can i take?
Now i want to take data from interrupt subroutine to main function or other subroutine then how can i take?
Wed Feb 24 2016, 03:59 pm
You can use global variables (or buffers etc.) which gets filled in interrupt routine and a flag which indicates that new data has come. You can check state of flag and use the new data.
//example char x; volatile char new_data = 0; void isr_code() { x = 123; new_data = 1; } // in main function void main() { //... other piece of code if (new_data) { new_data = 0; //... use the new updated x } }
jigneshdoshi like this.
Powered by e107 Forum System