Here is my code in Mikrobasic:
program MyPWM_numca ' * Description ' PWM library was unable to drive the SG5010 digital servo, so i am writing ' my own program. It will read Analog from PORTA.0 and it will create pulse ' of apropriate width every 20ms in PORTC.2 ' ' * Test configuration ' MCU: P16F887 ' Dev.Board: 44pin demo board ' Oscillator: internal 4Mhz dim temp as word sub procedure WAIT(dim temp10 as word) dim i as word i = 0 for i = 0 to temp10 delay_us(1) next i end sub main: ANSEL = 000001 ' Configure AN0 pin as analog TRISA = 000001 ' PORTA is input TRISC = 000000 ' PORTC is output for pulses TRISD = 000000 ' PORTD is output for temp while true temp = ADC_READ(0) PORTD = temp PORTC = 000100 delay_us(400) WAIT(temp div %1000) PORTC = 000000 WAIT(1000-temp) delay_us(18600) wend end.
Here are the files of this project.
Ελληνικό post: http://www.hlektronika.gr/forum/showthread.php?p=328745#post328745