James, James Williams wrote: > The duty cycle is not the issue. The issue is trying to change > the period or frequency of the PWM once it has been set up. I don't understand what goes wrong with you, but I have no problem with changing PWM frequency on a 18F242. I don't write in Asm, but in C (for CC8E compiler). I've added below the source of a stripped down program with ADC input and PWM output. The code is very simple and the essential part is practically one-to-one translatable to Asm. The PWM frequency is dropped by a factor 4 by changing the prescaler setting when the analog input becomes higher than approx 90% of +Vref (maybe not of any practical value, but it shows how it works). Hope this helps you! (If you want I'll send you the hex-file to check if your chip is OK). Regards, Rob. --------------------------------------------------------------------------------------- // PWM + ADC experiment on PIC18F242 (20MHz) - Rob Hamerling 2003 #include <18c242.h> #pragma config[00] = 0b0000.0000 // #pragma config[01] = 0b0010.0010 // HS oscillator, PLL disabled #pragma config[02] = 0b0000.0001 // BOR disabled, pwrt enabled #pragma config[03] = 0b0000.0000 // WDT disabled #pragma config[04] = 0b0000.0000 // #pragma config[05] = 0b0000.0001 // #pragma config[06] = 0b1000.0001 // no bg debug, no LVP, STVREN #pragma config[07] = 0b0000.0000 // #pragma config[08] = 0b0000.1111 // #pragma config[09] = 0b1100.0000 // #pragma config[10] = 0b0000.1111 // #pragma config[11] = 0b1110.0000 // #pragma config[12] = 0b0000.1111 // #pragma config[13] = 0b0100.0000 // #define ADCGO 0b0000.0100 // start of ADC #define delay() W = 12; \ X: W = W + 255; \ skipIfZero(W); \ goto X; void main(void) { PORTA = 0x00000000; // PortA all off TRISA = 0b00001011; // PortA (3 pins An. input) PORTB = 0; // PortB all off TRISB = 0b00000000; // PortB all output PORTC = 0b00000000; // PortC all off TRISC = 0b00000000; // PortC all output T2CKPS0 = 1; // ) prepare for 1:4 prescaling T2CKPS1 = 1; // ) start with 1:16 prescaling TMR2ON = 1; // activate Timer2 PR2 = 255; // long period ('low' PWM freq) CCP1CON = 0b0000.1100; // select PWM mode ADCON0 = 0b10.001.001; // ADC: FRC, AN1, enable A/D ADCON1 = 0b0.0.00.0100; // ADC: Left justified, Fosc/32,, 3/0 for (;;) { // forever delay(); // approx 12(?) mus delay ADCON0 |= ADCGO; // start ADC while (ADCON0 & ADCGO) // poll for cleared state ; if (ADRESH > 225) // higher than approx 90% T2CKPS1 = 0; // set 1:4 prescaling else // lower T2CKPS1 = 1; // set 1:16 prescaling CCPR1L = ~ADRESH; // update duty cycle } } ---------------------------------------------------------- -- Rob Hamerling, Vianen, NL phone +31-347-322822 homepage: http://www.robh.nl/ -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body