The pic 12F683 is a really interesting, useful and cheap picmicro of only 8 pins. It can be considered, in fact, an enhanced and updated version of the 'old' 12F675 because it belongs all characteristics of the 12F675 but runs upto 20mhz (using external crystals) or 8mhz (internal oscillator), 2K flash memory, 4 adc modules, 1 pwm etc.
Using mikrobasic: program adc_pwm start: |
The program is elementary. We have to:
The analog input (10bit) will report a value betweeen 0 and 1023. We'll report such a value to set the duty cicle (divided by 4 because: 0-255 = 0-100% duty cicle). That's all. |
But how, for instance, we can vary the frequency (and not the duty cicle) of the PWM signal? unfortunately it is not directly possible because we cannot report the value of the adc_read routine as parameter of the pwm1_init() routine.
The solution consist in the replacement of all the pwm instructions and using the delay function. For example:
program adc_pwm
dim adc as word
main:
CMCON0 = 7 'comparators off
ANSEL = %00001000 'AN3 enabled
TRISIO.3 = 1 'set input
TRISIO.2 = 0
start:
while(1)
gpio.2 = 0
delay_us (4) (it is a small delay necessary to be recognized by a stepper motor driver, for instance)
gpio.2 = 1
for t=1 to adc next t (a delay proportional to the adc value read by the adc_read function)
wend
end.
Such a solution will generates (according to the speed of the crystal) a simulated pwm signal on GP2.
Using Mikroc:
void main() {
unsigned int temp;
GPIO = 0;
cmcon0 = 7 ; //disable comparators
cmcon1 = 0;
osccon = 0b01110001; //set int osc to 8MHz
ansel = 0b00001000; //An3 enabled
TRISIO = 0b00001000;
do {
temp = ADC_Read(ANS3); //read
GPIO.F2 = 1;
delay_cyc (temp); //proportional delay
GPIO.F2 = 0;
} while(1);
}