Suggestions to tighten novice 8051 code

I recently got involved with the 8051 and a devlopment board and have been writing some simple programs to get used to the code and hardware. The last one that I wrote was a practice problem in the manual that came with the board. The task was to use an on-board potentiometer to increase/decrease the speed of an LED array, as well as make the 2 PWMs complement each other.

My code is at the end. It works, but I'd like to know what the veterans might have done to tighten it up and make it better? Right now it takes a second or so to update, and I'd like it to be more realtime. Any suggestions/tips/comments would be appreciated.

Thanks.

----- start code -----

mov pwmp, #20 ; set frequency of pwm mov p1, #1 ; turn LED 0 on at the start START: acall delay ; call the delay loop straight away mov a, p1 ; move the current LED status into the ; accumulator rl a ; rotate the LED position one place to ; the left.

mov p1, a ; light the new LED position sjmp start ; restart the program

DELAY: mov adcon, #08h ; turn on the a/d converter adc: mov b, adcon ; put the a/d status in the b register jnb b.4, adc ; check to see when the adc has been ; read

mov pwm0, adch ; move the adc value into the pwm0 to control ; duty cycle

mov a, adch ; move the adc value into the ; accumulator

cpl a ; complement the accumulator mov pwm1, a ; move the complemented adc value into ; the pwm1 register

mov r0, #10 ; set 10 in the register for delay

loop: mov th0, adch ; set the high time of the timer to ; the adc value

mov tl0, adch ; set the low time of the timer to the ; adc value

setb tr0 ; start the timer time: jnb tf0, time ; wait for the timer to expire clr tr0 ; clear the timer register clr tf0 ; clear the timer flag djnz r0, loop ; loop 10 times for visible rate ret ; return to the main program

Reply to
Jacob Burnetski
Loading thread data ...

Why sit around in that count loop, doing almost nothing? Look into interrupts: start a timer that kicks an interrupt - that way, you can have the micro doing other work while the timer runs down.

Reply to
Robert Baer

I have some MCS48 and MCS51 code you can study it enables to build a 2 set point process controller it is very compact MCS48(subset of MCS51) the circuits also are on same site. it will not work out of the box.

formatting link
more on this site
formatting link

Reply to
Anantha Narayan

That depends on what better means to you.

I assume that the program gives about the right delay for various settings, since you can increase or decrease the gain by changing the fixed loop count of 10. What do you mean by "more realtime", then? If you want a quicker response to input change, then you can put the A/D reading in the loop, updating the PWMs and loop timer each time. That will give you a quicker response.

You copied the A/D reading into both the timer upper and lower byte. There is nothing bad about that, but you get nearly the same thing by copying into the high byte only. Instead of timing 256*ADC, you are timing 257*ADC.

Other than those two items, your code looks reasonable.

Thad

Reply to
Thad Smith

I have some sample (quick-and-dirty) timer/int code here (Assembler)

formatting link
which resolves to 1ms idle (sleep) times. The rest of the timer code just keeps a "clock" and was part of a realtime clock routine I had in a terminal program, but it does show how to get different scales of delay times, with software, and spends most of its time in idle mode-when not doing anything. (The 9V battery in the plac lasts about 1-1.5 years, depending on how often it's used. :

Reply to
gary drummond

Sorry about that, I should have just pointed to the page. If you don't have .asm or .src set up as types for an editor, you will need to

download it to look at it in notepad or wordpad.

I'll change the types to .txt my next update of the web pages.

The page is:

formatting link

Gary

PS-I'll be on another ISP starting Sat. I'll still have this group, but my email will be changed to gbronline.com (1.4GB of spam and virus messages is too much each month...)

Reply to
gary drummond

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.