Running ADC Controlled PWM from Flowcode Examples

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Post Reply
medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Running ADC Controlled PWM from Flowcode Examples

Post by medelec35 »

Sorry if questions are basic, I have not been using flowcode Pro for long.

Change Chip to p12f683
wanted PWM frequency at 5KHz
Set: clock speed to 4MHz.
Clock source to clk/1
period register
Compile to hex

load complied hex into a simulator
Within simulator ran scope on port a,2
When simulator running, I was watching Microcontroller View and scope.
At first port a,1 was showing A for analogue input, so set for 2.5V
then just after ADC sampling port a,2 changed to Digital input, then back to analogue with default of 0V.

Ran simulator disassembler which displayed:

Code: Select all

;-----------------------------------
;PIC DISASSEMBLER LISTING
;Label  Instruction
;-----------------------------------
	GOTO L1
	ADDLW 0xFF
	ADDLW 0xFF
	ADDLW 0xFF
	MOVWF 0x07F
	SWAPF STATUS,W
	BCF STATUS,RP0
	MOVWF 0x020
	SWAPF PCLATH,W
	MOVWF 0x021
	SWAPF FSR,W
	MOVWF 0x022
	BCF PCLATH,3
	BCF PCLATH,4
	GOTO L2
	ADDLW 0xFF
L9:	DECF 0x025,W
	BTFSS STATUS,Z
	RETURN
	MOVF 0x026,W
	MOVWF CCPR1L
	RETURN
L6:	MOVLW 0xC7
	BSF STATUS,RP0
	MOVWF T2CON
	MOVLW 0x04
	BCF STATUS,RP0
	MOVWF T2CON
	DECF 0x025,W
	BTFSS STATUS,Z
	RETURN
	MOVLW 0x0C
	MOVWF CCP1CON
	BSF STATUS,RP0
	BCF GPIO,2
	RETURN
L7:	BSF STATUS,RP0
	MOVF GPIO,W
	BCF STATUS,RP0
	MOVWF 0x025
	MOVLW 0x02
	BSF STATUS,RP0
	IORWF GPIO,W
	MOVWF GPIO
	MOVLW 0x22
	MOVWF ADCON0
	MOVLW 0x05
	BCF STATUS,RP0
	MOVWF ADCON0
	CLRF 0x026
L4:	MOVLW 0x28
	SUBWF 0x026,W
	BTFSC STATUS,C
	GOTO L3
	INCF 0x026,F
	GOTO L4
L3:	MOVLW 0x02
	IORWF ADCON0,W
	MOVWF ADCON0
L5:	BTFSC ADCON0,GO_DONE
	GOTO L5	
	[b]MOVF 0x025,W
	BSF STATUS,RP0
	MOVWF GPIO
	MOVLW 0x20
	MOVWF ADCON0
	BCF STATUS,RP0
	CLRF ADCON0	[/b]	
                RETURN
L8:	BCF STATUS,RP0
	MOVF ADRESH,W
	MOVWF 0x025
	RETURN
L11:	MOVLW 0x07
	BCF STATUS,RP0
	MOVWF CMCON0
	MOVLW 0xC0
	BSF STATUS,RP0
	MOVWF TMR0
	MOVLW 0x01
	BCF STATUS,RP0
	MOVWF 0x025
	CALL L6
L10:	CALL L7
	CALL L8
	MOVF 0x025,W
	MOVWF 0x023
	MOVLW 0x01
	MOVWF 0x025
	MOVF 0x023,W
	MOVWF 0x026
	CALL L9
	MOVF 0x023,W
	SUBLW 0xFF
	MOVWF 0x024
	MOVLW 0x02
	MOVWF 0x025
	MOVF 0x024,W
	MOVWF 0x026
	CALL L9
	GOTO L10
L1:	BCF PCLATH,3
	BCF PCLATH,4
	GOTO L11
L2:	BCF STATUS,RP0
	SWAPF 0x022,W
	MOVWF FSR
	SWAPF 0x021,W
	MOVWF PCLATH
	SWAPF 0x020,W
	MOVWF STATUS
	SWAPF 0x07F,F
	SWAPF 0x07F,W
	RETFIE	
	END
After Removing

Code: Select all

MOVF 0x025,W
BSF STATUS,RP0
MOVWF GPIO
MOVLW 0x20
MOVWF ADCON0
BCF STATUS,RP0
CLRF ADCON0	
RETURN
Then re-assembled, simulator works same as flowcode simulator.

My questions are:
1) why was code I removed,in place to start with?
I'm unsure why ADC i/p was required to change to digital i/p since I have already got a pic programmed and running with pwm variable form 0 to 100% without ADC i/p changing to digital i/p

2) How do I change flowchart or settings so PWM% = ADC%?
Currently 50% ADC = 64% PWM

3)If target chip is changed to p12F615 why can only one PMW component be use when data sheet shows 1 or 2 output channels?
is it 1 component per ccp?

User avatar
Steve
Matrix Staff
Posts: 3433
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times

Re: Running ADC Controlled PWM from Flowcode Examples

Post by Steve »

Hopefully someone else will be able to answer the other questions, but I can answer the first. Those lines in bold represent the following lines of C code:

Code: Select all

//restore old tris value, and reset adc registers
trisio = old_tris;
ansel = 0x20;
adcon0 = 0x00;
Basically, after an a/d conversion has finished, the ports are returned to digital i/o lines and the adc module is turned off.

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: Running ADC Controlled PWM from Flowcode Examples

Post by medelec35 »

Thank you for answering 1st question. Is there an advantage to switching ADC off then straight-back on, rather then then just keep re-sampling ADC without changing to digital state, since latter works well in practice?
Last edited by medelec35 on Sat Jan 24, 2009 11:48 pm, edited 1 time in total.
Martin

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: Running ADC Controlled PWM from Flowcode Examples

Post by medelec35 »

Update.
I Have worked out how to make PWM Duty% = ADC%
Since percentage adjustment ( or ratio) is required.
Using Formula: ADC% / Duty% = Req% adjustment

ADC%/Duty% = x / 100 = Req% adjustment
Transposing for x
x = ADC% * 100 / Duty%

Therefore if Pot is turned so ADC% = 50
and Duty cycle = 64%
using above formula
x=50 * 100 / 64 = 78.125
rounding up to 79 to obtain 100% Duty if ADC% = 100, A calculation box is placed between ADC(0) x=ReadA... and PWM(0) SetDutuyC...
with formula x = x * 79 /100
Last edited by medelec35 on Sat Jan 24, 2009 11:47 pm, edited 2 times in total.
Martin

User avatar
Steve
Matrix Staff
Posts: 3433
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times

Re: Running ADC Controlled PWM from Flowcode Examples

Post by Steve »

The reason that it is turned back to a digital i/o line is so that the Flowcode program does not need to check the state of the ADC unit each time it performs a digital i/o operation.

Post Reply