I cant wait to try this out.. i fixed the hardware of the programmer , now i am rewriting some the software..motion said:I have used the PIC on a motor driver application in which the PWM duty cycle is a function of the ADC output and here are few tips I can give.
In your application as in mine, the A/D result is used to calculate the PWM duty. On the PWM module, the duty cycle can only change at timer2 overflow. In your case, it's at a 5Khz rate. This is regardless of how many times you load CCPR1L. Therefore, the 5Khz rate should dictate the rate you make A/D conversions and not the other way around (i.e., the A/D conversion rate dictating how fast you update CCPR1L).
A 5Khz A/D conversion rate is well within the limits of the PIC ADC. Selecting 8Tosc for Tad, the A/D conversion time is 12Tad or about 20usec. Since at 5Khz there is 200usec between A/D conversion intervals, there is 180usec of sampling time allowed.
All these is easily done if A/D conversion is synchronized with timer2 overflow. That is, you don't need to check the GO/DONE bit but instead poll the TMR2IF bit.
1. Poll the TMR2IF bit until it is set.
2. Clear TMR2IF bit.
3. Read the result from the previous A/D conversion and save result.
4. Use the A/D result to set the PWM duty.
5. Start a new conversion. This step can be done ahead of step 4.
6. Repeat step 1. While waiting for TMR2IF to be set in 200usec, the A/D conversion is completed after 20usec and the rest of the time is spent sampling.
BTW, you can also use interrupts here, using TMR2IF to initiate interrupt.
;******************************************************************
org 0x0000
RESET clrf STATUS ; |B0
clrf PORTA ; clear Port A data latches |B0
clrf PORTB ; clear Port B data latches |B0
movlw h'07' ; |B0
movwf CMCON ; turn comparator off |B0
bsf STATUS,RP0 ; select bank 1 |B1
clrf TRISA ; port A all outputs |B1
clrf TRISB ; port B all outputs |B1
bcf STATUS,RP0 ; select bank 0 |B0
;
; setup PWM (looks like CCPR1L uses Tosc while PR2 uses Tcyc)
;
clrf T2CON ; TMR2 prescale:1 |B0
movlw d'100' ; same as (100*4)>>2 |B0
movwf CCPR1L ; 100 usecs, 50% duty cycle? |B0
bsf STATUS,RP0 ; select bank 1 |B1
movlw d'200'-1 ; 200 1.0 usec 'ticks' |B1
movwf PR2 ; Period=200 usecs, Freq=5.0 KHz |B1
bcf STATUS,RP0 ; select bank 0 |B0
movlw b'00001100' ; |B0
movwf CCP1CON ; put CCP module in PWM mode |B0
bsf T2CON,TMR2ON ; turn on TMR2 |B0
;
; now test to see if the LED on RB3 is glowing at 50% brightness
;
LOOP goto LOOP ; loop indefinately |B0
;
;***************************************************************
__CONFIG _CONFIG1, _CCP1_RB0&_LVP_OFF&_PWRTE_ON&_WDT_OFF&_HS_OSC
__CONFIG _CONFIG2, _IESO_OFF & _FCMEN_OFF
results of my PWM LED test..
i adjusted the PWM to keep the current at 2mA...
Elapsed time /-/ Voltage on 2.2 F capacitance..
20 min 3.56 V 2mA
30 min 2.97 V 2mA
40 min 2.30 V 2mA
45 min 2.05 V 2mA
47 min 1.94 V 2mA
48 min 1.88 V 2mA
48 min 30 sec 1.83 V 1.8 mA
Could this be why i am getting intermittant blips in the PWM signal??..because i still havnt tried this yet..motion said:.
All these is easily done if A/D conversion is synchronized with timer2 overflow. That is, you don't need to check the GO/DONE bit but instead poll the TMR2IF bit.
1. Poll the TMR2IF bit until it is set.
2. Clear TMR2IF bit.
3. Read the result from the previous A/D conversion and save result.
4. Use the A/D result to set the PWM duty.
5. Start a new conversion. This step can be done ahead of step 4.
6. Repeat step 1. While waiting for TMR2IF to be set in 200usec, the A/D conversion is completed after 20usec and the rest of the time is spent sampling.
BTW, you can also use interrupts here, using TMR2IF to initiate interrupt.
Could this be why i am getting intermittant blips in the PWM signal??..
i tell ya this Varible PWM is the greatest thing since sliced bread
LIST P=16F84
; include "P16F628A.inc"
TEMP EQU 0x0c
ADON EQU 0X00
ADIF EQU 0X06
GO_DONE EQU 0X02
TMR2ON EQU 0X02
RP0 EQU 0x05
RP1 EQU 0x06
W EQU H'0000'
F EQU H'0001'
;----- Register Files------------------------------------------------------
INDF EQU H'0000'
TMR0 EQU H'0001'
PCL EQU H'0002'
STATUS EQU H'0003'
FSR EQU H'0004'
PORTA EQU H'0005'
PORTB EQU H'0006'
PCLATH EQU H'000A'
INTCON EQU H'000B'
PIR1 EQU H'000C'
PIR2 EQU H'000D'
TMR1L EQU H'000E'
TMR1H EQU H'000F'
T1CON EQU H'0010'
TMR2 EQU H'0011'
T2CON EQU H'0012'
CCPR1L EQU H'0015'
CCPR1H EQU H'0016'
CCP1CON EQU H'0017'
RCSTA EQU H'0018'
TXREG EQU H'0019'
RCREG EQU H'001A'
ADRESH EQU H'001E'
ADCON0 EQU H'001F'
OPTION_REG EQU H'0081'
TRISA EQU H'0085'
TRISB EQU H'0086'
PIE1 EQU H'008C'
PCON EQU H'008E'
PR2 EQU H'0092'
TXSTA EQU H'0098'
SPBRG EQU H'0099'
EEDATA EQU H'009A'
ANSEL EQU H'009B'
CMCON EQU H'009C'
EECON2 EQU H'009D'
ADRESL EQU H'009E'
ADCON1 EQU H'009F'
org 0x00
;this is where the program starts
clrf PORTA
clrf PORTB
CLRF STATUS
movlw 0x07
movwf CMCON
bsf STATUS ,RP0 ;bank 1
CLRF TRISA
CLRF TRISB
BCF STATUS ,RP0
CLRF T2CON
MOVLW D'000'
MOVWF CCPR1L
BSF STATUS,RP0
MOVLW d'255'-1
MOVWF PR2
BCF STATUS,RP0
MOVLW b'00001100'
MOVWF CCP1CON
BSF T2CON,TMR2ON
BSF STATUS,RP0
MOVLW b'00000001'
MOVWF TRISA
MOVLW b'00000001'
MOVWF ANSEL
MOVLW b'01000000'
MOVWF ADCON1
BCF STATUS,RP0
MOVLW b'01000000'
MOVWF ADCON0
LOOP CALL ADC
MOVWF CCPR1L
GOTO LOOP
ADC BSF ADCON0,ADON
BCF PIR1,ADIF
MOVLW d'13'
MOVWF TEMP
ACQ DECFSZ TEMP,F
GOTO ACQ
BSF ADCON0,GO_DONE
ADX BTFSC ADCON0,GO_DONE
GOTO ADX
MOVF ADRESH,W
BCF ADCON0,ADON
RETURN
end
LIST P=16F88
; include "P16F628A.inc"
TEMP EQU 0x0c
ADON EQU 0X00
ADIF EQU 0X06
GO_DONE EQU 0X02
TMR2ON EQU 0X02
TMR2IF EQU 0X01
RP0 EQU 0x05
RP1 EQU 0x06
W EQU H'0000'
F EQU H'0001'
;----- Register Files------------------------------------------------------
INDF EQU H'0000'
TMR0 EQU H'0001'
PCL EQU H'0002'
STATUS EQU H'0003'
FSR EQU H'0004'
PORTA EQU H'0005'
PORTB EQU H'0006'
PCLATH EQU H'000A'
INTCON EQU H'000B'
PIR1 EQU H'000C'
PIR2 EQU H'000D'
TMR1L EQU H'000E'
TMR1H EQU H'000F'
T1CON EQU H'0010'
TMR2 EQU H'0011'
T2CON EQU H'0012'
CCPR1L EQU H'0015'
CCPR1H EQU H'0016'
CCP1CON EQU H'0017'
RCSTA EQU H'0018'
TXREG EQU H'0019'
RCREG EQU H'001A'
ADRESH EQU H'001E'
ADCON0 EQU H'001F'
OPTION_REG EQU H'0081'
TRISA EQU H'0085'
TRISB EQU H'0086'
PIE1 EQU H'008C'
PCON EQU H'008E'
PR2 EQU H'0092'
TXSTA EQU H'0098'
SPBRG EQU H'0099'
EEDATA EQU H'009A'
ANSEL EQU H'009B'
CMCON EQU H'009C'
EECON2 EQU H'009D'
ADRESL EQU H'009E'
ADCON1 EQU H'009F'
org 0x00
;this is where the program starts
clrf PORTA
clrf PORTB
CLRF STATUS
BSF STATUS ,RP0 ;bank 1. It should be ahead of CMCON
ifdef __16F628A
movlw 0x07
movwf CMCON
endif
CLRF TRISA
CLRF TRISB
BCF STATUS ,RP0
CLRF T2CON
MOVLW D'000'
MOVWF CCPR1L
BSF STATUS,RP0
MOVLW d'255'-1
MOVWF PR2
BCF STATUS,RP0
MOVLW b'00001100'
MOVWF CCP1CON
BSF T2CON,TMR2ON
BSF STATUS,RP0
MOVLW b'00000001'
MOVWF TRISA
MOVLW b'00000001'
MOVWF ANSEL
MOVLW b'01000000'
MOVWF ADCON1
BCF STATUS,RP0
MOVLW b'01000000'
MOVWF ADCON0
BSF ADCON0,ADON
CLRF PIR1
LOOP CALL ADC
MOVWF CCPR1L
GOTO LOOP
ADC BTFSS PIR1,TMR2IF
GOTO ADC
BCF PIR1,TMR2IF
MOVF ADRESH,W
BSF ADCON0,GO_DONE
RETURN
END
BSF STATUS,RP0
MOVLW b'00000001'
MOVWF TRISA
MOVWF ANSEL
BSF STATUS,RP0
MOVLW b'00000001'
MOVWF TRISA
MOVLW b'00000001'
MOVWF ANSEL
btw the blips i was getting were about one every second
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?