Hardware PWM doesn't work because CCP1CON<5:4> registers has different name. Tried to rename pwm file and convert, but then had another problem.
Have tried to set the registers manually, and probably close to getting the job done. A bit unsure how you initialize, clear, and set CCP1. Just checking with a DVM for expected frequency at the moment.
Kent
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
From the datasheet, it looks like Microchip have just renamed a few bits. Adding these lines to pwm.h or your program should make it work:
#define CCP1X DC1B1
#define CCP1Y DC1B0
To enable PWM, the subroutine PWMOn is called. This copies the value out of CCPCONCache and into CCP1CON. The PWMOff sub clears CCP1CON and thus disables PWM. InitPWM is called automatically at the start of the program if you use PWMOn or PWMOff.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
_LVP_OFF equ 0x3FFF
'BCF STATUS,RPO 'Bank 0
BANK0
clrf PortA
movlw 0x07
movwf CMCON0 ;Comparator Off
clrf ANSEL 'A/D inputs to digital
clrf VRCON 'Vref Off
#define Motor PortC.5
dir PortC.5 out
INITPWM
PWMON
Main:
Set Motor On
wait 5 s
Set Motor Off
wait 5 s
Goto Main
Any suggestions would be appreciated.
Thanks,
Kent
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
O.K. here's the code less artifacts, it compiles fine, but no signal.
'A program to move Tamiya motor on RC5
'Chip model
#chip 16f684,8
#config _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _CP_OFF & _CPD_OFF & _BOD_OFF & _MCLRE_OFF & _WDT_OFF
clrf PortA
movlw 0x07
movwf CMCON0 ;Comparator Off
clrf ANSEL 'A/D inputs to digital
clrf VRCON 'Vref Off
Main:
PWMOn
wait 5 s
PWMOFF
wait 5 s
Goto Main
Still looking for some sort of frequency on RC.5. The motor will be switched thru an Irf510 mosfet. A 1N4001 diode is across the Fet, and a seperate 3V motor supply is filtered with a .1uf, and 220uf caps. This setup works fine when just setting/clearing a Port pin.
Thanks for your help.
Kent
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi Kent, if You have one terminal of the motor connected to the drain of mosfet and the other terminal of motor connected to the positive of power supply, You must put the diode across the motor (katode to positive power supply, anode to drain) not across the mosfet, in order to avoid voltage spike across the mosfet.
Mosfets have just an intrisecal diode across drain and source.
An other thing, you should use a logic level mosfet, if you see the "typical output charateristics" in the data sheets, you can see that a vgs=5V the current that mosfet can switch on and off as a switch is about 1A.
Regards
Stefano
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hey Stefanodel, I am switching the motor on the drain side of Mosfet. Now the diode is properly positioned, Thanks for catching that.
Regarding the logic level device, I can see that the irl510 can go up to about 6A with Vgs = 5V and Vds = 3V. A big difference!
Will surely use the logic level models in any final designs, or use some gate drivers like the microchip tc4426(7). As it is, the irf510 works O.K. for now under no load.
Interested in knowing if you have tried the GCBASIC PWM , or have setup the hardware PWM?
Kent
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi Kent, TC427 or TC4427 (improved version) can be used as level translator and driver for power-mosfet. If you desire to have a fast switch of mosfet you need an high peak of current in the gate. The parameter Qg [nC]give you an idea of the current that must delivered to the gate.
TC4426 is an inverting gate, so if you want to use it, you must negate pwm.
Next step in driving motor could be a full bridge topology, you can change the sense of rotation of motor and brake the motor.
I think to begin to try GCBASIC pwm this night.
Regards
Stefano
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I am not familiar with the compiler but you need to set up the PR2 register with the PWM value. when this matches. PWM will be on until a match between PR2 and timr2 then off. If PR2 is zero then no output.
Use an LED with a series resistor of 1k on the pwm o/p pin to see the effect without the motor.
You need to put a pull down resistor (10 k+) on the output if you drive the FET direct.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Have added the 10k resistor, good idea to keep pin from floating, thanks. Led has been in place.
The GCBASIC compiler does the PWM calculation for PR2 thru a script and include file. This is a little awkward, so the moderator has promised to improve this in the next revsion.
Kent
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm working on PWM right at the moment! I've decided to leave all of the old routines as they are, and do two things:
(1) Add a new sub, probably called HPWM, which sets all of the values on the PIC. This is proving difficult at the moment - once it is done, the next version of GCBASIC will be ready. It will be possible to change frequency and duty cycle on the fly. Also, both CCP ports of the 877 and similar chips should become accessible. I'm trying to take into account the concerns on this thread as much as I can while writing the code.
(2) Implement software PWM. I've already written this routine. It is a little more tricky to set up to produce a specific frequency, as some calculations must be done manually. However, it does not require anything in particular on the PIC and so should be more reliable.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
There was one post that I saw where someone had calculated the frequency bounderies associated with their respective significant bits. Maybe somewhere along the line, you have to set the limitations on the frequency (<=38-40khz) or some such number. Also, maybe contain resolution/steps to unsigned 256 or signed 127 or whatever makes sense.
It seems that there are too many variables to make a one size fits all sub? Good luck.
Regards,
Kent
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Looking at those sites, it appears that I have been making a mistake all along with the duty cycle. It is not calculated using CCP1RL/255, but rather using CCP1RL/PR2.
I'll upload a fixed version of 0.9.2.0 in a few hours, and make the new pwm.h available from the front page. I am sorry for causing confusion here!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The fix has been applied, and seems to work very nicely. The bug involved both the new HPWM routine and the older InitPWM/PWMOn routines, and would explain a lot of the difficulties with PWM so far.
The new HPWM routine is quite an improvement over the older hardware PWM routines. When setting the duty cycle, only CCPR1L is set - this avoids the DC1B1, DC1B0 vs. CCP1Y, CCP1X issue. The loss of the 2 LSBs should have very minimal impact on the accuracy of the duty cycle. HPWM also gives access to the second CCP port where available as promised above, and allows settings to be changed on the fly.
The frequency range allowed for PWM depends on the speed of the PIC - the help file has approximate limits. I have prepared a spreadsheet that can calculate whether or not a particular speed chip can handle a given frequency. It is now online at http://gcbasic.sourceforge.net/PWMCalc.xls
Software PWM is also implemented now, using the PWMOut sub. It is difficult to adjust for a specific frequency, but it works well for controlling the brightness of LEDs and assumedly other items.
Hopefully this will be the end of PWM difficulties for a while! Please let me know how the new HPWM works on the 16F684!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The older PWM routine seems very solid now. Good Work! A little glitch when running the 16f684 on internal osc:
8 mhz actual freq. = freq./2
actual Duty = Duty/4
4 mhz actual freq. = freq. Good!
actual Duty = Duty/4
Spent some time trying to run the HPWM but have not got the routine down. It comes down to my ignorance of calling the sub, and setting the port. My very feeble attempt:
......
......
#define Led PortC.5
dir PortC.5 out
#define PWMChannel 1
Main:
PWMFreq = 4
PWMDuty = 50
Sub HPWM (PWMChannnel, HPWMFreq, HPWMDuty,) [#NR]
Set Led On ????
goto Main
Look forward, to trying the serial LCD.
Regards,
Kent
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The 2 bit LCD routines are not intended for serial LCD display.
Use them if you want to drive standard LCD display using only two I/O pins plus some additional hardware.
For a circuit schematics example see
Calling the HPWM sub is the same as calling any other sub - all that is needed is the name of the sub, and then the list of parameters. In your program, this line will work:
HPWM 1, 4, 127
The duty cycle is different in HPWM to the old routines. The old routines use a percentage, while HPWM uses a value between 0 and 255. 50% corresponds to 127.
Also, there is no need to set the LED on - if HPWM is used, it disables the SET command on the CCP pin anyway. This is a hardware limitation of the PIC.
The new HPWM works fine on the 16f684, although the compiler is still looking for the two LSB's, its a minor issure. The duty cycle loses its full resolution somewhere below 40khz for 4 or 8mhz internal osc. The unfortunate side effect is that the max duty cycle drops to 50%. Further reductions in frequency bring successive reductions in duty cycle down to only about 1-2% at 4khz.
The net effect of the HPWM is that you would not want to control a motor this way. Currently the effective duty cycle range is very narrow and at the high end of the range. This is because the frequency is too high for low quality dc motors, apparently the "on" time is not enough?
Tried to verify by going back to the PWMOn method, but somehow I have broke the routine, which worked fine yesterday. Go figure.
Hardware PWM doesn't work because CCP1CON<5:4> registers has different name. Tried to rename pwm file and convert, but then had another problem.
Have tried to set the registers manually, and probably close to getting the job done. A bit unsure how you initialize, clear, and set CCP1. Just checking with a DVM for expected frequency at the moment.
Kent
From the datasheet, it looks like Microchip have just renamed a few bits. Adding these lines to pwm.h or your program should make it work:
#define CCP1X DC1B1
#define CCP1Y DC1B0
To enable PWM, the subroutine PWMOn is called. This copies the value out of CCPCONCache and into CCP1CON. The PWMOff sub clears CCP1CON and thus disables PWM. InitPWM is called automatically at the start of the program if you use PWMOn or PWMOff.
No compile problems now, but still not getting PWM. Here's my code:
'A program to move Tamiya motor on RC5
'Chip model
#include <pwm.h>
#chip 16f684,8
#config _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _CP_OFF & _CPD_OFF & _BOD_OFF & _MCLRE_OFF & _WDT_OFF
_LVP_OFF equ 0x3FFF
'BCF STATUS,RPO 'Bank 0
BANK0
clrf PortA
movlw 0x07
movwf CMCON0 ;Comparator Off
clrf ANSEL 'A/D inputs to digital
clrf VRCON 'Vref Off
#define Motor PortC.5
dir PortC.5 out
INITPWM
PWMON
Main:
Set Motor On
wait 5 s
Set Motor Off
wait 5 s
Goto Main
Any suggestions would be appreciated.
Thanks,
Kent
These lines here are not needed anymore - the LVP bug is fixed and GCBASIC automatically handles banks:
_LVP_OFF equ 0x3FFF
'BCF STATUS,RPO 'Bank 0
BANK0
INITPWM is also not needed - GCBASIC will call it automatically if you have PWMOn and/or PWMOff in the program.
To switch the PWM on and off, use PWMOn and PWMOff. Setting PORTC.5 will have no effect as normal I/O is disabled when PWM is on.
Don't worry about including pwm.h - GCBASIC will automatically include it and everything else listed in lowlevel.dat.
I'm not sure as to why there is still no PWM - I'll have a closer look at the 16F684 datasheet tonight and get back to you.
One more thing - do you have some kind of transistor between the PIC and the motor? The PIC cannot drive a motor directly.
O.K. here's the code less artifacts, it compiles fine, but no signal.
'A program to move Tamiya motor on RC5
'Chip model
#chip 16f684,8
#config _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _CP_OFF & _CPD_OFF & _BOD_OFF & _MCLRE_OFF & _WDT_OFF
clrf PortA
movlw 0x07
movwf CMCON0 ;Comparator Off
clrf ANSEL 'A/D inputs to digital
clrf VRCON 'Vref Off
Main:
PWMOn
wait 5 s
PWMOFF
wait 5 s
Goto Main
Still looking for some sort of frequency on RC.5. The motor will be switched thru an Irf510 mosfet. A 1N4001 diode is across the Fet, and a seperate 3V motor supply is filtered with a .1uf, and 220uf caps. This setup works fine when just setting/clearing a Port pin.
Thanks for your help.
Kent
Hi Kent, if You have one terminal of the motor connected to the drain of mosfet and the other terminal of motor connected to the positive of power supply, You must put the diode across the motor (katode to positive power supply, anode to drain) not across the mosfet, in order to avoid voltage spike across the mosfet.
Mosfets have just an intrisecal diode across drain and source.
An other thing, you should use a logic level mosfet, if you see the "typical output charateristics" in the data sheets, you can see that a vgs=5V the current that mosfet can switch on and off as a switch is about 1A.
Regards
Stefano
Hey Stefanodel, I am switching the motor on the drain side of Mosfet. Now the diode is properly positioned, Thanks for catching that.
Regarding the logic level device, I can see that the irl510 can go up to about 6A with Vgs = 5V and Vds = 3V. A big difference!
Will surely use the logic level models in any final designs, or use some gate drivers like the microchip tc4426(7). As it is, the irf510 works O.K. for now under no load.
Interested in knowing if you have tried the GCBASIC PWM , or have setup the hardware PWM?
Kent
Hi Kent, TC427 or TC4427 (improved version) can be used as level translator and driver for power-mosfet. If you desire to have a fast switch of mosfet you need an high peak of current in the gate. The parameter Qg [nC]give you an idea of the current that must delivered to the gate.
TC4426 is an inverting gate, so if you want to use it, you must negate pwm.
Next step in driving motor could be a full bridge topology, you can change the sense of rotation of motor and brake the motor.
I think to begin to try GCBASIC pwm this night.
Regards
Stefano
I am not familiar with the compiler but you need to set up the PR2 register with the PWM value. when this matches. PWM will be on until a match between PR2 and timr2 then off. If PR2 is zero then no output.
Use an LED with a series resistor of 1k on the pwm o/p pin to see the effect without the motor.
You need to put a pull down resistor (10 k+) on the output if you drive the FET direct.
Have added the 10k resistor, good idea to keep pin from floating, thanks. Led has been in place.
The GCBASIC compiler does the PWM calculation for PR2 thru a script and include file. This is a little awkward, so the moderator has promised to improve this in the next revsion.
Kent
I'm working on PWM right at the moment! I've decided to leave all of the old routines as they are, and do two things:
(1) Add a new sub, probably called HPWM, which sets all of the values on the PIC. This is proving difficult at the moment - once it is done, the next version of GCBASIC will be ready. It will be possible to change frequency and duty cycle on the fly. Also, both CCP ports of the 877 and similar chips should become accessible. I'm trying to take into account the concerns on this thread as much as I can while writing the code.
(2) Implement software PWM. I've already written this routine. It is a little more tricky to set up to produce a specific frequency, as some calculations must be done manually. However, it does not require anything in particular on the PIC and so should be more reliable.
That sounds great! I seem to have gotten bogged down on PWM and lost focus. A couple of links that I came across that may help or just add to the confusion.
http://forum.microchip.com/tm.aspx?m=188794
http://www.winpicprog.co.uk/pic_tutorial8.htm
There was one post that I saw where someone had calculated the frequency bounderies associated with their respective significant bits. Maybe somewhere along the line, you have to set the limitations on the frequency (<=38-40khz) or some such number. Also, maybe contain resolution/steps to unsigned 256 or signed 127 or whatever makes sense.
It seems that there are too many variables to make a one size fits all sub? Good luck.
Regards,
Kent
Looking at those sites, it appears that I have been making a mistake all along with the duty cycle. It is not calculated using CCP1RL/255, but rather using CCP1RL/PR2.
I'll upload a fixed version of 0.9.2.0 in a few hours, and make the new pwm.h available from the front page. I am sorry for causing confusion here!
The fix has been applied, and seems to work very nicely. The bug involved both the new HPWM routine and the older InitPWM/PWMOn routines, and would explain a lot of the difficulties with PWM so far.
The new HPWM routine is quite an improvement over the older hardware PWM routines. When setting the duty cycle, only CCPR1L is set - this avoids the DC1B1, DC1B0 vs. CCP1Y, CCP1X issue. The loss of the 2 LSBs should have very minimal impact on the accuracy of the duty cycle. HPWM also gives access to the second CCP port where available as promised above, and allows settings to be changed on the fly.
The frequency range allowed for PWM depends on the speed of the PIC - the help file has approximate limits. I have prepared a spreadsheet that can calculate whether or not a particular speed chip can handle a given frequency. It is now online at http://gcbasic.sourceforge.net/PWMCalc.xls
Software PWM is also implemented now, using the PWMOut sub. It is difficult to adjust for a specific frequency, but it works well for controlling the brightness of LEDs and assumedly other items.
Hopefully this will be the end of PWM difficulties for a while! Please let me know how the new HPWM works on the 16F684!
The older PWM routine seems very solid now. Good Work! A little glitch when running the 16f684 on internal osc:
8 mhz actual freq. = freq./2
actual Duty = Duty/4
4 mhz actual freq. = freq. Good!
actual Duty = Duty/4
Spent some time trying to run the HPWM but have not got the routine down. It comes down to my ignorance of calling the sub, and setting the port. My very feeble attempt:
......
......
#define Led PortC.5
dir PortC.5 out
#define PWMChannel 1
Main:
PWMFreq = 4
PWMDuty = 50
Sub HPWM (PWMChannnel, HPWMFreq, HPWMDuty,) [#NR]
Set Led On ????
goto Main
Look forward, to trying the serial LCD.
Regards,
Kent
The 2 bit LCD routines are not intended for serial LCD display.
Use them if you want to drive standard LCD display using only two I/O pins plus some additional hardware.
For a circuit schematics example see
http://www.rentron.com/Myke1.htm
Regards
Stefano
Calling the HPWM sub is the same as calling any other sub - all that is needed is the name of the sub, and then the list of parameters. In your program, this line will work:
HPWM 1, 4, 127
The duty cycle is different in HPWM to the old routines. The old routines use a percentage, while HPWM uses a value between 0 and 255. 50% corresponds to 127.
Also, there is no need to set the LED on - if HPWM is used, it disables the SET command on the CCP pin anyway. This is a hardware limitation of the PIC.
There is a complete description and example of the HPWM command at http://gcbasic.sourceforge.net/help/hpwm.htm
To operate a serial LCD, you'll need to use the RS232 routines. I might add proper support for serial LCDs in the next version.
The new HPWM works fine on the 16f684, although the compiler is still looking for the two LSB's, its a minor issure. The duty cycle loses its full resolution somewhere below 40khz for 4 or 8mhz internal osc. The unfortunate side effect is that the max duty cycle drops to 50%. Further reductions in frequency bring successive reductions in duty cycle down to only about 1-2% at 4khz.
The net effect of the HPWM is that you would not want to control a motor this way. Currently the effective duty cycle range is very narrow and at the high end of the range. This is because the frequency is too high for low quality dc motors, apparently the "on" time is not enough?
Tried to verify by going back to the PWMOn method, but somehow I have broke the routine, which worked fine yesterday. Go figure.
I really like working with the serial lcd's, mostly because there are only three pins to worry about when experimenting. Some cheap kits:
http://www.phanderson.com/lcd106/lcd107.html
or
http://www.wulfden.org/k107/index.shtml
Regards,
Kent