James Wages kiramek.com> writes: >=20 > Joe & Sergey, >=20 > Thank you for your replies. >=20 > I downloaded the SIN wave generator code from the following link: >=20 > http://www.g4jnt.com/PIC_DDS.zip >=20 James,=20 I have developed a square wave generator with a scan function a while ago (http://beaglerobotics.com/catalog.pl?pID=3D58). The source code is in CCS= C, but should be readable. The relevant pieces of code are below. Basically, function set_f0() calculates settings for timer2 (PIC16F876A) and configure= s it. When scanning the frequency, increment log(F) (variable log_f) at some rate and call set_F0(). I believe I use log(F) to make scanning more smoot= h. =20 #int_TIMER1 TIMER1_isr() { output_toggle(PIN_C1); set_timer1(60536); } void set_f0() { float ftemp; unsigned long ltemp; ftemp =3D ((float)log_f) /100; ltemp =3D (unsigned long) pow(10, ftemp); pr2_mult =3D 5000 / ltemp; if (pr2_mult < PR2_LOW_DIV1) //set lower limit for pwm period { pr2_mult =3D PR2_LOW_DIV1; }; if (pr2_mult PR2_HIGH_DIV16) //high limit for pwm period { pr2_mult =3D PR2_HIGH_DIV16; }; pr2 =3D make8(pr2_mult / 16, 0); setup_timer_2 (T2_DIV_BY_16, pr2, 1); set_pwm1_duty (pr2/2); return; } ///// in the main event loop: if(scan) { delay_ms(scan_delay); switch (dir) { case 1: //Increase frequency log_f +=3D incr; if (log_f >=3D log_f_MAX) { dir =3D 2; log_f =3D log_f_MAX; }; break; case 2: //Decrease frequency log_f -=3D incr; if (log_f <=3D log_f_MIN) { dir =3D1; log_f =3D log_f_MIN; }; break; }; set_f0(); set_led_intensity(); }; I can send the full source code if interested, please contact me off-list. The code was written in 2007, and I have not used CCS C since about 2009, the code might be a bit archaic. =20 Best, Sergey=20 --=20 http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .