bu arada arkadaşlar elimde assembler ile yazılmış PID ve PWM mevcut olan kod var ama C gerekiyor çeviirebilicek arkadaşlar içinde buraya koyuyorum.
;********************************************************
;
; DC MOTOR HIZ KONTROLÜ
; (PI KONTROLÖRLÜ)
; Divice : PIC16F877
; Author : SERVER DOGAN ÜMIT ÇELIK
;********************************************************
list p=pic16f877
include p16f877.inc
__CONFIG _HS_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF
;**************** Program Start ***********************
led equ h'20'
org 0 ;Reset vektör
goto BASLA
org 4 ;Kesme vektörü
goto KESME
;**************** Hazirlik islemi *********************
BASLA
;*** Port Hazirlama
bsf STATUS,RP0 ; Bank1
movlw b'00000001' ;AN0 giris mod
movwf TRISA ; TRISA giris
clrf TRISD
clrf TRISB
clrf TRISC ; C Portu Çikis
bcf STATUS,RP0 ; Bank0
;*** A/D cevirim hazirligi
movlw b'10000001' ;ADCS=10 CHS=AN0 ADON=ON
movwf ADCON0 ; ADCON0 yükle
bsf STATUS,RP0 ;Bank1
movlw b'00001110' ;ADFM=0 PCFG=1110
movwf ADCON1 ; ADCON1 kur
bcf STATUS,RP0 ;Bank0
;*** PWM hazirligi
clrf TMR2 ; TMR2 yi sil
movlw b'11111111'
clrf CCPR1L ; CCPR1L sil
bsf STATUS,RP0 ;CBank1
movlw d'255' ;Period=1638.4usec(610Hz)
movwf PR2 ; PR2 yükle
bcf STATUS,RP0 ; Bank0
movlw b'00000110' ;Pst=1:1 TMR2=ON Pre=1:16
movwf T2CON ; T2CON kur
movlw b'00001100' ;CCP1XY=0 CCP1M=1100(PWM)
movwf CCP1CON ;CCP1CON kur
;*** Compare modu Hazirlama
clrf TMR1H ;TMR1H sil
clrf TMR1L ; TMR1L sil
movlw h'09' ;H'9C4'=2500
movwf CCPR2H ;CCPR2H yükle
movlw h'c4' ;2500*0.4usec = 1msec
movwf CCPR2L ;CCPR2L yükle
movlw b'00000001' ;Pre=1:1 TMR1=Int TMR1=ON
movwf T1CON ; T1CON yükle
movlw b'00001011' ;CCP2M=1011(Compare)
movwf CCP2CON ;CCP2CON yükle
;*** Interruption control
bsf STATUS,RP0 ; Bank1
movlw b'00000001' ;CCP2IE=aktif
movwf PIE2 ; PIE2 yükle
bcf STATUS,RP0 ; Bank0
movlw b'11010000' ;GIE=ON PEIE=ON INTE=ON
movwf INTCON ; INTCON yükle
wait
goto $ ;Kesme Bekleme
;*************** Kesme Islemi *****************
KESME
clrf PIR2 ;Kesme bayragini sifirla
button_check
btfss INTCON,INTE ;Dış kesme set mi?
goto button_check ;hayir tekrarla
bcf PORTC,CCP1 ;PORTC CCP1 bacagini sifirla
goto int_end ;sona git
ad_check
btfsc ADCON0,GO ;A/D tamam mi ?
goto ad_check ;hayir tekrarla
movf ADRESH,W ;dijital bilgiyi yükle
movwf PORTB ;portb at
comf PORTB,W ;portb yi tersle w ye at
movwf CCPR1L ; w yi ccpr1l ye at
comf PORTB,F ;portb tersle
comf CCPR1L,W ;ccpr1l tersle
movwf led ;w yi led'e at
movlw b'00010000' ;litarel degeri w'ye at
subwf led,w ;LED - litarel
btfsc STATUS,C ;düsükmü ?
goto led1 ;hayir
movlw b'01000101' ;Led kayitcisini yükle
goto int_end ;int_end git
led1 movlw b'00100000' ;litarel degeri w'ye at
subwf led,w ;LED - data
btfsc STATUS,C ;düsükmü ?
goto led2 ;hayir
movlw b'01000000' ;Led kayitcisini yükle
goto int_end ;int_end git
led2 movlw b'01000000' ;litarel degeri w'ye at
subwf led,w ;LED - data
btfsc STATUS,C ;düsükmü ?
goto led3 ;hayir
movlw b'00110101' ;Led kayitcisini yükle
goto int_end ;int_end git
led3 movlw b'01100000' ;litarel degeri w'ye at
subwf led,w ;LED - data
btfsc STATUS,C ;düsükmü ?
goto led4 ;hayir
movlw b'00110000' ;Led kayitcisini yükle
goto int_end ;int_end git
led4 movlw b'10000000' ;litarel degeri w'ye at
subwf led,w ;LED - data
btfsc STATUS,C ;düsükmü ?
goto led5 ;hayir
movlw b'00100101' ;Led kayitcisini yükle
goto int_end ;int_end git
led5 movlw b'10100000' ;litarel degeri w'ye at
subwf led,w ;LED - data
btfsc STATUS,C ;düsükmü ?
goto led6 ;hayir
movlw b'00100000' ;Led kayitcisini yükle
goto int_end ;int_end git
led6 movlw b'11000000' ;litarel degeri w'ye at
subwf led,w ;LED - data
btfsc STATUS,C ;düsükmü ?
goto led7 ;hayir
movlw b'00010101' ;Led kayitcisini yükle
goto int_end ;int_end git
led7 movlw b'11100000' ;litarel degeri w'ye at
subwf led,w ;LED - data
btfsc STATUS,C ;düsükmü ?
goto led8 ;hayir
movlw b'00010000' ;Led kayitcisini yükle
goto int_end ;int_end git
led8 movlw b'00000000' ;Led kayitcisini yükle
;************ cikis **************
int_end
movwf PORTD ;Set PROTB
retfie
;********************************************************
; END
;********************************************************
end