Anatoly, You don't need any interrupt to read the encoder and set the pwm value. Just do the following trick: set TMR0 for overflow at x mS (where x is the perfect match for your encoder speed, try x=10 or so). Read the enc_a and enc_b inputs only when TMR0_IF owerflows. Then reset the TMR0_IF. Increment (enc_a) or decrement (enc_b) the pwm dutycicle register.
Use the pwm_hardware library from Jallib pack. Take a look how to use it from the samples. There are a few examples for 12F683. PWM output of the CCP is GP2. First make your code to work for PWM only using the ADC and a potmeter. Then replace ADC with your encoder routine. Sorry but I'm not able to write any code for you, I forgot everything...:) If doesn't work, try also to post your questions at yahoo jallist, there are a lot of guys there which are eating code. Vasile On Mon, Apr 11, 2016 at 7:27 PM, Anatoly Titov <[email protected]> wrote: > Hello again! And thanks for reply! > Can you help me with new code using interrupts for the same task, it does > not work and I don't mind why.I even switched off the PWM because it uses > interrupts too (I think so). With no result =( > Code is small: > -- target PICmicro > include 12f683 > pragma target CLOCK 8_000_000 > pragma target OSC INTOSC_NOCLKOUT > pragma target WDT disabled > pragma target MCLR internal -- reset on pin A3 > pragma target WDT DISABLED -- watchdog > pragma target BROWNOUT DISABLED -- brownout reset > pragma target FCMEN DISABLED -- clock monitoring > pragma target IESO DISABLED -- int/ext osc. switch > -- > OSCCON_SCS = 0 -- select primary > oscillator > OSCCON_IRCF = 0b111 -- 8 MHz > enable_digital_io() -- make all pins digital > I/O > -- setting pins > > alias enc_b is pin_A5 > pin_A5_direction = input > > alias enc_a is pin_A4 > pin_A4_direction = input > > alias led is pin_A2 > pin_A2_direction = output > led = off > > OPTION_REG_NGPPU = 0 --\ > WPU_WPU4 = 1 -- подтяжка > WPU_WPU5 = 1 --/ > IOC_IOC5 = 1 --разрешаем прерывание > OPTION_REG_INTEDG = 0 -- прерывание по переходу в низкий уровень > INTCON_GIE = on -- глобальное разрешение прерываний > INTCON_GPIF = off -- снимаем флаг прерывания по GPIO > -- > > --setting variables > var byte pwm_cnt, pwm = 10 > -- > > procedure interrupt is -- обработчик прерывания по нажатию кнопки > pragma interrupt > if INTCON_GPIF then -- снимаем флаг прерывания по GPIO > INTCON_GPIF = off > INTCON_GPIE = off -- запрещаем прерывания по GPIO > > if enc_a then > if pwm!=254 then pwm = pwm + 1 end if > else > if pwm!=0 then pwm = pwm - 1 end if > end if > > INTCON_GPIE = on -- разрешаем прерывания по GPIO > end if > end procedure > > forever loop > pwm_cnt = pwm_cnt + 1 --когда дотикает до 255,само станет нулем > if pwm_cnt < pwm then led = on else led = off end if > end loop > > > > > понедельник, 11 апреля 2016 г., 7:14:26 UTC+3 пользователь vasile написал: >> >> Congratulations Anatoly! >> >> I'm using ISR in a way I've learn with old jal and never failed. Never do >> it with libraries even they works ok. >> >> On Sun, Apr 10, 2016 at 10:55 PM, Anatoly Titov <[email protected]> >> wrote: >> >>> Closed. I changed clock to 8Mhz and decreased check rate to 1 ms. All >>> works fine. >>> >>> воскресенье, 10 апреля 2016 г., 12:03:04 UTC+3 пользователь Anatoly >>> Titov написал: >>>> >>>> Hello! I made a code adapted from Arduino sketch. Sketch works, my code >>>> nope =(((( Please tell, how to get working code without interrupts. >>>> http://www.hobbytronics.co.uk/arduino-tutorial6-rotary-encoder working >>>> sketch >>>> my code: >>>> >>>> -- target PICmicro >>>> include 12f683 >>>> pragma target CLOCK 4_000_000 >>>> pragma target OSC INTOSC_NOCLKOUT >>>> pragma target WDT disabled >>>> pragma target MCLR internal -- reset on pin A3 >>>> pragma target WDT DISABLED -- watchdog >>>> pragma target BROWNOUT DISABLED -- brownout reset >>>> pragma target FCMEN DISABLED -- clock monitoring >>>> pragma target IESO DISABLED -- int/ext osc. switch >>>> -- >>>> enable_digital_io() -- make all pins >>>> digital I/O >>>> --OPTION_REG_NGPPU = 0 -- pull-ups ON >>>> OSCCON_SCS = 0 -- select primary >>>> oscillator >>>> OSCCON_IRCF = 0b110 -- 4 MHz >>>> -- >>>> -- setting pins >>>> >>>> >>>> alias enc_b is pin_A5 >>>> pin_A5_direction = input >>>> >>>> >>>> alias enc_a is pin_A4 >>>> pin_A4_direction = input >>>> >>>> >>>> -- configure PWM >>>> pin_ccp1_direction = output >>>> include pwm_hardware >>>> pwm_max_resolution(1) >>>> pwm1_on() >>>> >>>> >>>> -- timer setup >>>> const TIMER0_ISR_RATE = 1000 -- 1 kHz isr rate >>>> const DELAY_SLOTS = 1 -- support slots >>>> include timer0_isr_interval >>>> timer0_isr_init() -- init timer0 isr >>>> >>>> >>>> --setting variables >>>> var byte pwm >>>> var bit old_a >>>> -- >>>> pwm = 0 >>>> pwm1_set_dutycycle_percent(pwm) >>>> >>>> >>>> forever loop >>>> >>>> >>>> if check_delay(0) then >>>> set_delay(0, 5) >>>> >>>> >>>> if old_a & !enc_a then >>>> if enc_b then >>>> if pwm!=254 then pwm = pwm + 1 end if >>>> else >>>> if pwm!=0 then pwm = pwm - 1 end if >>>> end if >>>> pwm1_set_dutycycle_percent(pwm) >>>> end if >>>> >>>> >>>> old_a = enc_a >>>> end if >>>> >>>> >>>> end loop >>>> >>>> -- >>> You received this message because you are subscribed to the Google >>> Groups "jallib" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to [email protected]. >>> To post to this group, send email to [email protected]. >>> Visit this group at https://groups.google.com/group/jallib. >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> -- > You received this message because you are subscribed to the Google Groups > "jallib" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at https://groups.google.com/group/jallib. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "jallib" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/jallib. For more options, visit https://groups.google.com/d/optout.
