Click to See Complete Forum and Search --> : How do I combine an ASM program with a C program


hkboy313
October 24th, 2004, 12:08 AM
i am a noob ...im taking an embedded class and i have a lab that wants me to generate a square wave signal...well i got the time period which is 200 microsec. and 50% duty cycle is 100 microsec. crystal freq is 22.183 with that i got FF47 for the value (I think) that will go into the timer register.

with that given...and that i calculated...i programmed something at least...

#include <8051io.h>
#include <8051bit.h> /* Bit set/clear macros */
#include <8051reg.h>

void main()
{


serinit(9600);

asm
{
MOV TMOD,#01
HERE: MOV TL0,#047H
MOV TH0,#0FFH
CPL P1.5
ACALL DELAY
SJMP HERE

DELAY:
SETB TR0
AGAIN: JNB TF0, AGAIN

CLR TR0
CLR TF0
RET
}

}

i know its kinda stupid to do that but...i saw an example on the program i used...micro-ide ...u can find the 8051 program at www.bipom.com butu need the hardware stuff to download program. i cannot compile this bcause i think of the asm language inside that prg.

well my question is if im on the right track or total lost.... or in the asm above do i have to have the input in binary with "#%"? this is really confusing..its not even in our book! "The 8051 Microcontroller and embedded system" by muhammad ali mazidi and anice mazidi...

sorry for long explanations...just had a couple concerns i wanted to get helped on.
thanks so much in advance...

bigBA
October 26th, 2004, 03:34 AM
#include <8051io.h>
#include <8051bit.h> /* Bit set/clear macros */
#include <8051reg.h>

void main()
{


serinit(9600);

asm
{
MOV TMOD,#01
HERE: MOV TL0,#047H
MOV TH0,#0FFH
CPL P1.5
ACALL DELAY
SJMP HERE

DELAY:
SETB TR0
AGAIN: JNB TF0, AGAIN

CLR TR0
CLR TF0
RET
}

}


if it is what you want, your code is ok. - but you really should do it in another way :)

if you can work in c, then why don't you use it?
a MOV TMOD, #01 is the same as writing TMOD = 1; in c!
you can do that, because your are including some header files, and there the registers and port bits and so on are defined.

so one other thing: how you generate the signal on P1.5 is, yeah... its ok. (but is P1.5 a output-only pin? if no - please set it explicitly to output before, otherwise it will maybe set initially to input, and you won't see any signal on the outside)

generation of the signal... you could do it better with a interrupt service routine (ISR):

in main: set up the timer and let it run, then you have to code an infinite loop like this: while(1);
because otherwise the controller will continue trying to execute the opcodes in the memory, and that can, yeah it will, lead to undefined behavior.

back to that isr.
don't know how to code it for the 8051, but for the C167 i would do it (somehow, can't well remember) this way:


void timer0_isr() interrupt = 0xXX
{
TL0 = 0x47;
TH0 = 0xFF;
P1.5 = !P1.5;
}


and you have to set the according interrupt service register for that timer (i had to do it with the c167... so i don't know if it is the same with the 8051, but i think so)

and now, every time the timer overflows, it will generate an interrupt. this will lead the controller to call the ISR. in it you reload the timer and toggle the output pin... and you have your signal.

hope i could help you a little bit!

hkboy313
October 28th, 2004, 12:53 PM
hey, thanks so much on the help, i finally got the program and it was similar to it too! kinda weird but ya

bigBA
October 29th, 2004, 03:12 AM
you're welcome! :wave: