Sorry I was not online earlier. Which PIC are you using?
Ioannis
Sorry I was not online earlier. Which PIC are you using?
Ioannis
I am using a 18F2550.
I seem to have it working at the moment however it is not very smooth.
When the on/off button is pressed I need to sweep from 0% duty cycle up to the set limit in 1 second. I can see this happening by writing the incremental counter results to EEPROM and reading them later, however, the duty cycle does not increase for every +1 increase of the duty cycle byte. I assume this is a resolution issue due to running at 48MHz?
I really need to smooth that out and give a very nice gradual increase in light over time.
Also, what is a good way to have the program wait for a pin to go high?
Currently I am using:
waitforon:
If PORTB.7 = 1 then 'on/off button pulled high = on
gosub rampup 'increments final_duty and outputs value to CCP1
endif
PORTC.0 = 1 'flash heartbeat LED
pause 100
PORTC.0 = 0
pause 100
If final_duty = limit then 'if returned to waitforon from rampup, then final_duty is at set limit, so move on to main program
goto main
endif
goto waitforon
Sometimes this will not move to rampup: or move on to main: but the heartbeat LED will always flash correctly.
This is the rampup subroutine.
rampup:
For final_duty = 1 to limit
delay = 1000 / limit
pause delay
CCP1CON = %00111100
PR2 = 44
high_duty=(final_duty>>2) 'high 6 bits in CCPR1L
low_duty=(final_duty<<6) 'low two bits for CCP1CON
low_duty=(low_duty>>2) 'shift back to CCP1CON<5:4>
low_duty.3=1 'PWM configuration bit
low_duty.2=1 'PWM configuration bit
CCPR1L=high_duty
CCP1CON = $0C
T2CON = 4
Next final_duty
return
Alright, last question of the day, I promise!
This is the main loop code. It works fine holding the current limit by pulse skipping. The current limit is set by comparing the ADC reading to a value stored in fb, 205 in this case, or about 175mA for this application.
The problem comes when the down button is pressed. Instead of lowering fb by 1 it goes to 100% duty cycle, and my current goes as high as the current limit is set on my bench power supply and I have to yank the power.
I thought by lowering, or raising the value in fb, I would be able to control the current limit? What is going on?
Code:main: If PORTB.6 = 1 then fb = fb - 1 If fb => 205 then fb = fb endif endif adcin 1, temp If temp > fb then final_duty = final_duty - 1 IF final_duty = 0 then final_duty = final_duty endif endif If temp < fb Then final_duty = final_duty + 1 If final_duty = 44 then final_duty = final_duty endif endif CCP1CON = %00111100 PR2 = 44 high_duty=(final_duty>>2) 'high 6 bits in CCPR1L low_duty=(final_duty<<6) 'low two bits for CCP1CON low_duty=(low_duty>>2) 'shift back to CCP1CON<5:4> low_duty.3=1 'PWM configuration bit low_duty.2=1 'PWM configuration bit CCPR1L=high_duty CCP1CON = $0C T2CON = 4 goto main
I don't see where you write to the EEPROM. If you do, maybe the timing for EEPROM storing (~10 msec) is creating the problem.
Other than that it looks OK the ramp up.
Ioannis
I used it once for logging and removed it. I did compensate for the write period. I actually slowed it down a lot so I could see what was happening at individual increments.
I tried i = fb and incrementing the i byte and not the fb byte and it works. I get a smooth up and down output now. It will current limit at each brightness setting.
So, there is the last problem to resolve:
When ramping up I am using duty cycle to increase brightness from zero to full on regardless of where the current limit is set at. The problems are that when I save the new current limit during the main loop, I get flashing as the output goes off during the write command and the pause 10 after the write command. Therefore, I can either write to EEPROM and ramp up to the newly set current limit, or have no flashing and ramp up to full output, go to the main loop and drop straight back to the new current limit. Both options dont look very good.
I am not sure how to write to EEPROM and stop it from flashing.
I think you may need some logic in there to check for button release. As it looks now, it will run the fb = fb - 1 VERY fast, and every time through the loop when you press the button. Chances are you will not release the button before lots of main loop runs.
-Bert
The glass is not half full or half empty, Its twice as big as needed for the job!
http://foamcasualty.com/ - Warbird R/C scratch building with foam!
Bookmarks