Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC16F1936 variable duty cycle with lookup table and potentiometer

Status
Not open for further replies.

jean12

Advanced Member level 2
Joined
Aug 27, 2013
Messages
529
Helped
5
Reputation
12
Reaction score
6
Trophy points
18
Activity points
5,497
Hello,there could anyone help me to create a PWM signal with variable duty cycle using potentiometer and using a lookup table from internal EEPROM?

I am able to adjust the duty cyle using a potentiometer on RA0 but still have a problem on reading from EEPROM and pass those values to the PWM module,please help.!!

I am using ASM for better precision!!

Thank you!!
 

it is said that give delay between successive read and write from EEProm have u considered that.!

Also let us know if u are able to read value from eeprom..(not passing to PWM)
 

Hi,

show us your code.

and a description what you want to do. step by step.

Klaus
 

see my codes here,I am using assembly I read EEPROM values and would like to use them for having an approxiamtely sine wave at the output when a low pass filter is added,
Code:
	LIST		P = P16F1936
	INCLUDE		"P16F1936.INC"
	__CONFIG H'8007',H'3FAA'
	__CONFIG H'8008',H'3FBC'


	UDATA_SHR	
W_TEMP		RES	1	;LOCATION FOR SAVING 'WREG' DURING INTERRUPT
STATUS_TEMP	RES	1	;LOCATION FOR SAVING 'STATUS' DURING INTERRUPT
FLAGS		RES	1	;FLAGS REGISTERS USED TO INDICATE DIFFERENT STATUS
tmp1        RES 1
tmp2        RES 1


EEPROM CODE 0X0000
			DE D'123'
			DE D'127'
			DE D'129'
			DE D'133'
			DE D'135'
			DE D'139'
			DE D'141'
			DE D'145'
			DE D'146'
			DE D'150'
			DE D'152'
			DE D'156'
			DE D'158'
			DE D'162'
			DE D'164'
			DE D'167'
			DE D'169'
			DE D'173'
			DE D'174'
			DE D'177'
			DE D'179'
			DE D'182'
			DE D'184'
			DE D'187'
			DE D'189'
			DE D'192'
			DE D'194'
			DE D'197'
			DE D'198'
			DE D'201'
			DE D'203'
			DE D'205'
			DE D'207'
			DE D'209'
			DE D'210'
			DE D'213'
			DE D'214'
			DE D'216'
			DE D'218'
			DE D'219'
			DE D'221'
			DE D'222'
			DE D'223'
			DE D'225'
			DE D'226'
			DE D'227'
			DE D'228'
			DE D'229'
			DE D'230'
			DE D'231'
			DE D'231'
			DE D'233'
			DE D'233'
			DE D'234'
			DE D'234'
			DE D'235'
			DE D'235'
			DE D'235'
			DE D'236'
			DE D'236'
			DE D'236'
			DE D'236'
			DE D'236'
			DE D'235'
			DE D'235'
			DE D'235'
			DE D'234'
			DE D'233'
			DE D'233'
			DE D'232'
			DE D'231'
			DE D'231'
			DE D'230'
			DE D'229'
			DE D'228'
			DE D'227'
			DE D'226'
			DE D'224'
			DE D'223'
			DE D'222'
			DE D'221'
			DE D'218'
			DE D'218'
			DE D'215'
			DE D'214'
			DE D'212'
			DE D'210'
			DE D'208'
			DE D'207'
			DE D'204'
			DE D'203'
			DE D'200'
			DE D'198'
			DE D'195'
			DE D'194'
			DE D'191'
			DE D'189'
			DE D'186'
			DE D'184'
			DE D'181'
			DE D'179'
			DE D'176'
			DE D'174'
			DE D'171'
			DE D'169'
			DE D'165'
			DE D'164'
			DE D'159'
			DE D'158'
			DE D'154'
			DE D'152'
			DE D'148'
			DE D'146'
			DE D'142'
			DE D'141'
			DE D'137'
			DE D'135'
			DE D'131'
			DE D'129'
			DE D'125'
			DE D'123'
			DE D'119'
			DE D'117'
			DE D'113'
			DE D'111'
			DE D'107'
			DE D'105'
			DE D'101'
			DE D'100'
			DE D'96'
			DE D'94'
			DE D'90'
			DE D'88'
			DE D'84'
			DE D'82'
			DE D'79'
			DE D'77'
			DE D'73'
			DE D'72'
			DE D'69'
			DE D'67'
			DE D'64'
			DE D'62'
			DE D'59'
			DE D'57'
			DE D'54'
			DE D'52'
			DE D'49'
			DE D'48'
			DE D'45'
			DE D'43'
			DE D'41'
			DE D'39'
			DE D'37'
			DE D'36'
			DE D'33'
			DE D'32'
			DE D'30'
			DE D'28'
			DE D'27'
			DE D'25'
			DE D'24'
			DE D'23'
			DE D'21'
			DE D'20'
			DE D'19'
			DE D'18'
			DE D'17'
			DE D'16'
			DE D'15'
			DE D'15'
			DE D'13'
			DE D'13'
			DE D'12'
			DE D'12'
			DE D'11'
			DE D'11'
			DE D'11'
			DE D'10'
			DE D'10'
			DE D'10'
			DE D'10'
			DE D'10'
			DE D'11'
			DE D'11'
			DE D'11'
			DE D'12'
			DE D'13'
			DE D'13'
			DE D'14'
			DE D'15'
			DE D'15'
			DE D'16'
			DE D'17'
			DE D'18'
			DE D'19'
			DE D'20'
			DE D'22'
			DE D'23'
			DE D'24'
			DE D'25'
			DE D'28'
			DE D'28'
			DE D'31'
			DE D'32'
			DE D'34'
			DE D'36'
			DE D'38'
			DE D'39'
			DE D'42'
			DE D'43'
			DE D'46'
			DE D'48'
			DE D'51'
			DE D'52'
			DE D'55'
			DE D'57'
			DE D'60'
			DE D'62'
			DE D'65'
			DE D'67'
			DE D'70'
			DE D'72'
			DE D'75'
			DE D'77'
			DE D'81'
			DE D'82'
			DE D'87'
			DE D'88'
			DE D'92'
			DE D'94'
			DE D'98'
			DE D'100'
			DE D'104'
			DE D'105'
			DE D'109'
			DE D'111'
			DE D'115'
			DE D'117'
			DE D'121'
			DE D'123'

;POWER ON RESET VECTOR LOCATION
;**************************************************************************************
STARTUP	CODE	0X0100			;RESET VECTOR ADDRESS 
	GOTO	START				
	CODE	0X04			;INTERRUPT VECTOR LOCATION
	GOTO	readadc	     	;GOTO INTERRUPT SERVICE ROUTINE

;**************************************************************************************
;PROG0	CODE 

;**************************************************************************************
;INITIALIZATION OF THE PORTS AND TIMERS
START

		BANKSEL	WDTCON
 		CLRF	WDTCON
		MOVLW 0x0F
		BANKSEL TRISA
		MOVWF TRISA
		BANKSEL TRISB
		BANKSEL T2CON
		CLRF T2CON		
		MOVLW	0X0C
		BANKSEL CCP1CON	
		MOVWF CCP1CON
        BANKSEL	PR2
		MOVLW	d'74'		;LOAD PR2 VALUE TO PR2 REGISTER  For 10KHZ generated signal
		MOVWF	PR2			
		MOVLW 0XFF
		BANKSEL APFCON
		MOVWF APFCON
		MOVLW 0x10
		BANKSEL PWM1CON
		MOVWF PWM1CON
		MOVLW 0X0F
		BANKSEL CCP1AS
		MOVWF CCP1AS
		MOVLW 0X1F
		BANKSEL PSTR1CON
		MOVWF PSTR1CON	
		MOVLW 0x00
		BANKSEL CCPTMRS0
		MOVWF CCPTMRS0
		BANKSEL TRISC
		BCF TRISC,2
		BANKSEL T2CON
		MOVLW 0x05
		MOVWF T2CON			
		CLRF CCPR1L	
;///////////////////////////////////////////////////////////////////////////////////////
;						PROGRAMME PRINCIPAL
;///////////////////////////////////////////////////////////////////////////////////////


readEEPROM                       ;To be checked for DATA_EEADR
		BANKSEL EEADRL
		MOVWF EEADRL
		BANKSEL EECON1
		BCF EECON1,CFGS
		BCF EECON1,EEPGD
		BSF EECON1,RD
		MOVLW D'91'
		MOVWF tmp1
readok	MOVF EEDATL,W 
		DECFSZ tmp1,f
		GOTO raedok
		RETURN  
readadc  
		BSF PIE1,ADIE
	    BSF INTCON,GIE
		BSF INTCON,PEIE
		BTFSS PIE1,ADIE
		return
		BTFSS PIR1,ADIF
		RETURN		
		CALL bosco
		BCF PIR1,ADIF
		return
bosco
		BANKSEL ANSELA
		MOVLW 0X01
		MOVWF ANSELA
		MOVLW 0x01
		BANKSEL ADCON0
		MOVWF ADCON0
		MOVLW 0x20
		BANKSEL ADCON1
		MOVWF ADCON1		
		BANKSEL ADCON0
		BSF ADCON0,GO
		BSF ADCON0,CHS0
		call delay1
		BANKSEL ADRESH	
		MOVF ADRESH,W
		BANKSEL CCPR1L
		MOVWF CCPR1L
		return
		
readvoltage
		MOVLW 0X20
		BANKSEL ADCON1
		MOVWF ADCON1
		MOVLW 0X02
		BANKSEL ADCON0
		MOVWF ADCON0		
		BCF ADCON0,CHS0
		BSF ADCON1,CHS1
		MOVF ADRESH,W
		MOVWF voltage
		XORLW 0XFF
		BTFSC STATUS,Z
		RETURN	
		
delay1
	MOVLW d'200'
	MOVWF tmp2
delay2
	DECFSZ tmp2,f
	GOTO delay2
	RETURN
	END
before adding EEPROM values and read from it,I am able to adjust the duty cycle with a potentiometer on RAO of PIC16F1936.

My requirement is to improve using a lookup tale so that I can have a sine wave at the output with a low pass filter added.

Please help.

- - - Updated - - -

Hello Arya Kumar how may I proceed for that?I may implement that using a potentiometer.!!

Thx
 

Why do you store it in eeprom ? Can you store it in ROM and read it when needed ? If you are not modifying the table then store it in ROM and access it. I can show you an example C Code for that if needed.
 

PIC Programmer is right if you are not changing values then better store in ROM.
Sorry i am tired and i dont like to see assembly code but on a glimpse i cannot see delay between successive reads.
So please check again if there is delay of 200ms between successive reads..

I was talking about if u can debug and see if u are able to receive eeprom data correctly either using debugger or something like lcd or Uart !!
Because i feel problem might be over there
 

Hi,

Please describe clearly what you want to do.

In post#1 there is nothing about sine.
But in post#4 you talk about sine.

We need to guess..
Do you want sine wave generation using pwm?
But what about the pot at the ADC? Is it involved somehow? Frequency setup? Amplitude setup?

If you want to generate sine with pwm, you need to specify:
* sine frequency (range)
* amplitude (range)
* pwm frequency
* how you want to achieve this. There are many discussions with different solutions here in the forum.

Klaus
 

Yes,the ADC helps in setting the frequency and/or amplitude I want to be able to adjust the frequency and this has a relation with the amplitude,with the look up table it helps in having a sine wave at the output when a filter is added.The signal obtianed is used for controlling a MOSFET.

--The sine frequency varies from 1HZ-----35HZ
--amplitude of the voltage,we do have a network of 220VAC/50HZ
--PWM frequency is 10KHZ
--Quartz is of 20MHZ
--The looup table has 180 values only for the positive half cycle so I will need also to reverse that at 180 degrees in order to have a complete cycle.


I am using PIC16F1936.

Thank you!!
 

I use mikroC PRO Compiler and mikroE says that between successive eeprom read/write we have to use 20 ms delay.
 

Hi,

Do a search on "spwm" for general information
Or "sine wave generation pic"

Either here in the forum or google.

Still unclear with the ADC and pot: do you have two pots, one for frequency, one for amplitude?
Or a single one where you want to adjust both frequency and amplitude at once?
or anything else...

Please give complete and unambiguous informations.

Klaus
 

No,the pot has to adjust the frequency and then the amplitude has to change in function of the frequency set with the potentiometer,
this is accomplished by reading analog voltage ratio from the feedback,the output voltage is brought back at the input via a comparator and this set the output voltage amplitude after checking the frequency set;as you know the voltage across the pot is 0V-----5V,so this is used for setting the frequency of the system and then the output voltage is of 0V----220VAC;

At Ov of the potentiometer,the frequency is 0HZ and the output is 0VAC/0Hz
at 1V of the potentiomter,the frequency is 10Hz(as example) and the output voltage is for example 60VAC/10HZ
At 2.5V of the potentiometer,the frequecy is 17.5Hz(as example) and the output voltage is for example 110VAC/17.5Hz
At 5V of the potentiometer,the frequency is 35HZ (as example )and the output voltage is 220VAC/35hz,

REMEMBER:the frequency is set by the potentiomter but this frequency is referred to for setting the amplitude of the output voltage.

Please help,the PWM signal for controlling the MOSFET has to consider the Look up table values stored in the EEPROM,so those values has to be read and then used also for adjusting the voltage thefore when a low pass filter is placed at the output the sine wave is obtained.


A feedback of voltage designed using a voltage comparator by LM339 operational amplifier.

Consider my codes here below,the Look up table is addressed in EEPROM and I changed the values to 120 values of the lookup table.
PHP:
	LIST P=P16F1936
	#include <P16F1936.INC>
	__CONFIG H'8007',H'3FAA'
	__CONFIG H'8008',H'3FBC'





;	UDATA_SHR	
	UDATA 0X20
W_TEMP		RES	1	;LOCATION FOR SAVING 'WREG' DURING INTERRUPT
STATUS_TEMP	RES	1	;LOCATION FOR SAVING 'STATUS' DURING INTERRUPT
FLAGS		RES	1	;FLAGS REGISTERS USED TO INDICATE DIFFERENT STATUS
tmp1        RES 1
tmp2        RES 1


CNT15 EQU h'25'


	ORG 0XF000
			DE D'123'
			DE D'127'
			DE D'129'
			DE D'133'
			DE D'135'
			DE D'139'
			DE D'141'
			DE D'145'
			DE D'146'
			DE D'150'
			DE D'152'
			DE D'156'
			DE D'158'
			DE D'162'
			DE D'164'
			DE D'167'
			DE D'169'
			DE D'173'
			DE D'174'
			DE D'177'
			DE D'179'
			DE D'182'
			DE D'184'
			DE D'187'
			DE D'189'
			DE D'192'
			DE D'194'
			DE D'197'
			DE D'198'
			DE D'201'
			DE D'203'
			DE D'205'
			DE D'207'
			DE D'209'
			DE D'210'
			DE D'213'
			DE D'214'
			DE D'216'
			DE D'218'
			DE D'219'
			DE D'221'
			DE D'222'
			DE D'223'
			DE D'225'
			DE D'226'
			DE D'227'
			DE D'228'
			DE D'229'
			DE D'230'
			DE D'231'
			DE D'231'
			DE D'233'
			DE D'233'
			DE D'234'
			DE D'234'
			DE D'235'
			DE D'235'
			DE D'235'
			DE D'236'
			DE D'236'
			DE D'236'
			DE D'236'
			DE D'236'
			DE D'235'
			DE D'235'
			DE D'235'
			DE D'234'
			DE D'233'
			DE D'233'
			DE D'232'
			DE D'231'
			DE D'231'
			DE D'230'
			DE D'229'
			DE D'228'
			DE D'227'
			DE D'226'
			DE D'224'
			DE D'223'
			DE D'222'
			DE D'221'
			DE D'218'
			DE D'218'
			DE D'215'
			DE D'214'
			DE D'212'
			DE D'210'
			DE D'208'
			DE D'207'
			DE D'204'
			DE D'203'
			DE D'200'
			DE D'198'
			DE D'195'
			DE D'194'
			DE D'191'
			DE D'189'
			DE D'186'
			DE D'184'
			DE D'181'
			DE D'179'
			DE D'176'
			DE D'174'
			DE D'171'
			DE D'169'
			DE D'165'
			DE D'164'
			DE D'159'
			DE D'158'
			DE D'154'
			DE D'152'
			DE D'148'
			DE D'146'
			DE D'142'
			DE D'141'
			DE D'137'
			DE D'135'
			DE D'131'
			DE D'129'
			DE D'125'
			DE D'123'
			
;POWER ON RESET VECTOR LOCATION
;**************************************************************************************
;STARTUP	CODE	0X0100			;RESET VECTOR ADDRESS 
	ORG 0X100
	GOTO	START				
	;CODE	0X04			;INTERRUPT VECTOR LOCATION
	ORG 0X0004
	GOTO	readadc	     	;GOTO INTERRUPT SERVICE ROUTINE

;**************************************************************************************
;PROG0	CODE 

;**************************************************************************************
;INITIALIZATION OF THE PORTS AND TIMERS
START

		BANKSEL	WDTCON
 		CLRF	WDTCON
		MOVLW 0x0F
		BANKSEL TRISA
		MOVWF TRISA
		BANKSEL TRISB
		BANKSEL T2CON
		CLRF T2CON		
		MOVLW	0X0C
		BANKSEL CCP1CON	
		MOVWF CCP1CON
        BANKSEL	PR2
		MOVLW	d'74'		;LOAD PR2 VALUE TO PR2 REGISTER  For 10KHZ generated signal
		MOVWF	PR2			
		MOVLW 0XFF
		BANKSEL APFCON
		MOVWF APFCON
		MOVLW 0x10
		BANKSEL PWM1CON
		MOVWF PWM1CON
		MOVLW 0X0F
		BANKSEL CCP1AS
		MOVWF CCP1AS
		MOVLW 0X1F
		BANKSEL PSTR1CON
		MOVWF PSTR1CON	
		MOVLW 0x00
		BANKSEL CCPTMRS0
		MOVWF CCPTMRS0
		BANKSEL TRISC
		BCF TRISC,2
		BANKSEL T2CON
		MOVLW 0x05
		MOVWF T2CON			
		CLRF CCPR1L	
;///////////////////////////////////////////////////////////////////////////////////////
;						PROGRAMME PRINCIPAL
;///////////////////////////////////////////////////////////////////////////////////////

  
readEEPROM	
		MOVLW d'121'
		MOVWF CNT15
		BANKSEL EEADRL
		MOVWF EEADRL
		BANKSEL EECON1
		BCF EECON1,CFGS
		BCF EECON1,EEPGD
		BSF EECON1,RD
		DECFSZ CNT15,f
		GOTO readEEPROM
		RETURN 
      
readok	MOVF EEDATL,W 
		DECFSZ tmp1,f
		GOTO readok
		RETURN 
 
readadc  
		BSF PIE1,ADIE
	 	BSF INTCON,GIE
		BSF INTCON,PEIE
		BTFSS PIE1,ADIE
		return
		BTFSS PIR1,ADIF
		RETURN		
		CALL bosco
		BCF PIR1,ADIF
		return
bosco
		BANKSEL ANSELA
		MOVLW 0X01
		MOVWF ANSELA
		MOVLW 0x01
		BANKSEL ADCON0
		MOVWF ADCON0
		MOVLW 0x20
		BANKSEL ADCON1
		MOVWF ADCON1		
		BANKSEL ADCON0
		BSF ADCON0,GO
		BSF ADCON0,CHS0
		call delay1
		BANKSEL ADRESH	
	    MOVF ADRESH,W
		BANKSEL CCPR1L
		MOVWF CCPR1L
		return
		
readvoltage
		MOVLW 0X20
		BANKSEL ADCON1
		MOVWF ADCON1
		MOVLW 0X02
		BANKSEL ADCON0
		MOVWF ADCON0		
		BCF ADCON0,CHS0
		BSF ADCON1,CHS1
		MOVF ADRESH,W
		;MOVWF voltage
		;XORLW 0XFF
		;BTFSC STATUS,Z
		RETURN	
		
delay1
;	MOVLW d'200'
;	MOVWF tmp2
delay2
;	DECFSZ tmp2,f
;	GOTO delay2
;	RETURN
	END




Please help!!
 
Last edited:

What is the purpose of writing in assembly language ? To minimize the code size ? I can write in C for you and the code size will be almost the same size as you get when written in assmebly language. I will store the table in ROM and access it.
 

I only looked quickly but the 'readEEPROM' routine looks wrong. Isn't it looping 121 times before returning nothing useful?

Brian.
 

What is the purpose of writing in assembly language ? To minimize the code size ? I can write in C for you and the code size will be almost the same size as you get when written in assmebly language. I will store the table in ROM and access it.

This is a big project,this is a startup that is why I want high precision and then I use MPASM,the target is to do as explained above and then use three CCP Modules shifted so that I can control a three phase system.

Please help!!
 

You mean C code generated .hex file will be more in size compared to assemebly code generated .hex file ? If you explain what all there will be in your project then I will try to write a C code which fits in your PIC and also executes faster and works as needed. I will use in-line assembly coding with C code.
 

Hi,

This is a big project,this is a startup that is why I want high precision and then I use MPASM,the target is to do as explained above and then use three CCP Modules shifted so that I can control a three phase system.

--> "high precision" has nothing to do with ASM or C.

***

The "howto" of generating three phase shifted sine wave generation is disscussed on ther thread before.

***
A feedback of voltage designed using a voltage comparator by LM339 operational amplifier.
I wonder how you measure the amplitude using a comparator..


Klaus
 

Hi,



--> "high precision" has nothing to do with ASM or C.

***

The "howto" of generating three phase shifted sine wave generation is disscussed on ther thread before.

***

I wonder how you measure the amplitude using a comparator..


Klaus


Yes,
it has been discussed but here I am asking for other issues,reading LUT table from EEPROM and use it for generating SPWM,the other thread has not be really fruitfull because no outcome gained I got only on how a given number of digits is applied on the phase but implementing the corresponding loop has not been clarified.

I hope using a voltage diveder which senses the analog value of AC can help in reading the amplitude,likewise the method used for implementing an AC voltmeter.!!

Please help.

- - - Updated - - -

Dear pic.programmer;can you help me to handle in c language??

Please help
 

Looking further now I have a little more time, the code will not run anyway. The code flow is wrong, the interrupts are not set up and if they actually ran they have no proper exit.
I will use in-line assembly coding with C code.
That is absolutely unnecessary and only serves to complicate things. Write it in assembly OR in 'C' but mixing the two has no benefit in this application and make the code harder to follow and debug.

Jean12, as you have started in assembler, it would be wasteful to discard it and start again in a different language. I suggest you write shorter sections of code to do different things then merge them together. For example, write an ISR that reads the ADC value. The code is very simple but please save yourself a lot of effort by copying the template provided by Microchip as your starting code then add your own lines to it. The template already shows how to use interrupts properly. If you are using Windows, look in "\Program Files\Microchip\MPASM Suite\Template\Object\16F1936TMPO.ASM" for the file and COPY it to your own project, do not edit the original in case you need it again in the future.

Brian.
 

If you can help in any other language it could be helpful,the important for me is not the language but running codes!!

Please help.

Thank you!!
 

Hi,

Yes,
it has been discussed but here I am asking for other issues,reading LUT table from EEPROM and use it for generating SPWM,the other thread has not be really fruitfull because no outcome gained I got only on how a given number of digits is applied on the phase but implementing the corresponding loop has not been clarified.

There are posts that telling you not to use EEPROM to store the table. It´s not clear why you want EEPROM. EEPROM is problematic, because of read timing.

Klaus
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top