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