PDA

View Full Version : PWM on PIC16F88



peterdeco1
- 6th January 2006, 11:32
Hello Everyone. I am migrating from a 16F819 to a 16F88 for more codespace. I've used the HPWM command on the 16F818 without any problems. I've adjusted the top of the code to try making the 16F88 have the PWM output on portb.3 but it always comes out on portb.0 regardless of my DEFINE statement. Does anyone know what I'm doing wrong? Thank you.

OSCCON = $60 'set int osc to 4mhz
ANSEL = 0 'all inputs digital
TRISA = %11111111
TRISB = 0
PORTB = 0
@ DEVICE MCLR_OFF, INTRC_OSC, WDT_ON, LVP_OFF, BOD_OFF, PWRT_ON, PROTECT_ON
DEFINE CCP1_BIT 3 ' TRYING TO GET PWM ON RB3. IT DOES WORK BUT 'ALWAYS COMES OUT OF RB0
Pause 500

START:
CCP1CON = 0 'TURN OFF PWM
NAP 4
IF PORTA.1 = 0 Then blah blah '

GoTo START

Melanie
- 6th January 2006, 12:00
If you followed the instructions in this thread...

http://www.picbasic.co.uk/forum/showthread.php?t=543

You would have discovered you needed CCPMX_OFF somewhere in your @ DEVICE statement.

If using MPASM you would have needed _CCP1_RB3 in your config definitions.

The clue is in the fact that this is a CONFIGURATION WORD (CONFIG FUSE) setting (see PICs Datasheet) therefore you need to make those changes in your CONFIG Definitions which are burnt into the chip at Program Time.

peterdeco1
- 7th January 2006, 11:48
Thanks Melanie but it still doesn't work. My programmer gives me the option to change CCP1 ports on the F818 but not the F88. I spent hours changing the code but the HPWM command always comes out on RB0.

@ DEVICE MCLR_OFF, INTRC_OSC, WDT_ON, LVP_OFF, BOD_OFF, PWRT_ON, PROTECT_ON, CCPMX_OFF
DEFINE CCP1_BIT 3 ' ***IT'S STILL ON RB0

It compiles without errors but RB0 still outputs the PWM. HELP!! The PC Board pattern will allow the use of RB3 perfectly. If I HAVE to use RB0, I have to cut traces & solder jumper wires. Thank you.

Melanie
- 7th January 2006, 15:39
If your programmer does not give you full control of your Configuration Word for the 16F88, you must get back to the vendor that produced your programmer software for an update, or buy a new programmer, or call a friend who has the facility on theirs, or buy a few crates of beer (most problems dissappear after the first crate)...

peterdeco1
- 8th January 2006, 11:15
I guess I have to settle for PWM on RB0. No matter what I do, I can't enable RB3. Now I have 2 new things on my "to do" list. 1 - update my programmer, 2 - buy a case of Budweiser.

mister_e
- 8th January 2006, 17:44
well the Beer option may help or not... What about if you use the MPASM assembler instead of PM?
Look at the 16F88.INC file..


;================================================= =========================
;
; Configuration Bits
;
;================================================= =========================

_CONFIG1 EQU H'2007'
_CONFIG2 EQU H'2008'

;Configuration Byte 1 Options
_CP_ALL EQU H'1FFF'
_CP_OFF EQU H'3FFF'
_CCP1_RB0 EQU H'3FFF'
_CCP1_RB3 EQU H'2FFF'
_DEBUG_OFF EQU H'3FFF'
_DEBUG_ON EQU H'37FF'
_WRT_PROTECT_OFF EQU H'3FFF' ;No program memory write protection
_WRT_PROTECT_256 EQU H'3DFF' ;First 256 program memory protected
_WRT_PROTECT_2048 EQU H'3BFF' ;First 2048 program memory protected
_WRT_PROTECT_ALL EQU H'39FF' ;All of program memory protected
_CPD_ON EQU H'3EFF'
_CPD_OFF EQU H'3FFF'
_LVP_ON EQU H'3FFF'
_LVP_OFF EQU H'3F7F'
_BODEN_ON EQU H'3FFF'
_BODEN_OFF EQU H'3FBF'
_MCLR_ON EQU H'3FFF'
_MCLR_OFF EQU H'3FDF'
_PWRTE_OFF EQU H'3FFF'
_PWRTE_ON EQU H'3FF7'
_WDT_ON EQU H'3FFF'
_WDT_OFF EQU H'3FFB'
_EXTRC_CLKOUT EQU H'3FFF'
_EXTRC_IO EQU H'3FFE'
_INTRC_CLKOUT EQU H'3FFD'
_INTRC_IO EQU H'3FFC'
_EXTCLK EQU H'3FEF'
_HS_OSC EQU H'3FEE'
_XT_OSC EQU H'3FED'
_LP_OSC EQU H'3FEC'

;Configuration Byte 2 Options
_IESO_ON EQU H'3FFF'
_IESO_OFF EQU H'3FFD'
_FCMEN_ON EQU H'3FFF'
_FCMEN_OFF EQU H'3FFE'

so you must use the following line... written AS IS


@ __CONFIG _CONFIG1, _CCP1_RB3 & _DEBUG_OFF & _LVP_OFF & _BODEN_ON & _MCLR_OFF & _PWRTE_ON & _WDT_ON & _INTRC_IO

NOW, what about something like


@ __CONFIG _CONFIG1, _CCP1_RB3 & _DEBUG_OFF & _LVP_OFF & _BODEN_ON & _MCLR_OFF & _PWRTE_ON & _WDT_ON & _INTRC_IO
OSCCON=$60 ' use internal 4MHZ osc
PAUSE 100 ' start-up delay
TRISB=0
DEFINE CCP1_REG PORTB
DEFINE CCP1_BIT 3
HPWM 1,1000,128
HERE: GOTO HERE

Maybe you'll have the same results... maybe not. Keep us inform.

mister_e
- 8th January 2006, 19:06
DARGH! Maybe not, just read the datasheet and the PM >INC file CCPMX should set ON.


CCPMX_ON equ 2FFF0000h ; X0 XXXX XXXX XXXX
CCPMX_OFF equ 2FFF1000h ; X1 XXXX XXXX XXXX

in datasheet... you'll find
<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=709&stc=1&d=1136747056">

NOW, what about


@ DEVICE MCLR_OFF, INTRC_OSC, WDT_ON, LVP_OFF, BOD_OFF, PWRT_ON, PROTECT_ON, CCPMX_ON
OSCCON=$60 ' use internal 4MHZ osc
PAUSE 100 ' start-up delay
TRISB=0
DEFINE CCP1_REG PORTB
DEFINE CCP1_BIT 3
HPWM 1,1000,128
HERE: GOTO HERE

peterdeco1
- 9th January 2006, 10:23
Thanks Steve but I think Melanie is right - time for a lot of beer. Followed your code exactly and the PWM still comes out on RB0. Do you have any other ideas I can try when I get sober?

mister_e
- 9th January 2006, 14:54
i'll check that later tonight with my own stuff.

mister_e
- 9th January 2006, 21:57
well i just tried that one...


@ DEVICE MCLR_ON, INTRC_OSC, WDT_ON, LVP_OFF, BOD_OFF, PWRT_ON, PROTECT_OFF, CCPMX_ON
OSCCON=$60 ' use internal 4MHZ osc
PAUSE 100 ' start-up delay
TRISB=0
DEFINE CCP1_REG PORTB
DEFINE CCP1_BIT 3
HPWM 1,128,1000
HERE: GOTO HERE

no big difference on the previous,just correcting the way i wrote HPWM, enable the MCLR and remove CodeProtect... it's working on RB3 as it's suppose to.

BTW i'd attach the .HEX file to dump on your PIC. Let's assume that it could be a compiler problem... maybe.

let us know.

PS just rename .txt to .hex

peterdeco1
- 10th January 2006, 08:54
Hi Steve. I copied your code exactly and it still outputs only on RB0. I suspect my programmer and/or software is the problem. There is an option "CCP1 on RB3" that works on the 16F818 but the option is "grayed out" when I program the 16F88. I guess it will not allow the use of RB3. I think it's time to get the latest hardware/software from Microchip. Thank you for your help.

peterdeco1
- 20th January 2006, 10:34
That was it. My software was forcing CCP1 onto RB0, while MPLAB software shows "CCP1 ON RB3". Thanks Everybody.

Dick Ivers
- 21st January 2006, 12:17
peterdeco1,

What programmer software and hardware are you using that causes the problem? We would all like to know. We want to avoid running in to the same difficulties.

ntege
- 12th November 2007, 09:25
hi guys can any one help with assembly code for dimming an led using the pic16f88. ihave read the datasheet but i dont seem to underatand it i have come with code but it doesnt work can anybody tell me whats wrong with my code.


list p=16f88
INCLUDE "p16f88.inc"


_;****Set up the Constants****

STATUS equ 03h
TRISA equ 85h ;Address of the STATUS register
TRISB equ 86h
PORTA equ 05h ;Address of the tristate register for port A
PORTB equ 06h ;Address of Port A
COUNT1 equ 20h ;First counter for our delay loops
COUNT2 equ 21h ;Second counter for our delay loops
ANSEL equ 9Bh
OSCCON equ 8Fh
CCP1CON equ 17h
T2CON equ 12h
TMR2 equ 11h
CCPR1L equ 15h
PR2 equ 92h
PIR1 equ 0Ch
PIE1 equ 8Ch


movlw B'00000000' ; 31.25khz
bsf STATUS, 5
movwf OSCCON
nop
nop
nop
bcf STATUS, 5
movlw b'00111100'
movwf CCP1CON

bsf 03h,5 ;Go to Bank 1
movlw 00h ;Put 00000 into W
movwf TRISA ;Move 00000 onto TRISA – all pins set to output

movlw 00h ;Set the Port B pins
movwf TRISB ;to output. ;Put 00000 into W

movlw 00h
movwf ANSEL ;Move 00000 onto TRISA – all pins set to output

movlw b'11111111'
movwf PR2

bcf 03h,5 ;Come back to Bank 0
movlw b'00001111'
movwf TMR2



Start
movlw b'00001111'
movwf CCPR1L ; set duty cycle
Loop1
decfsz CCPR1L ;dimming led by varying the duty cycle
goto Loop1
goto Start
end

mister_e
- 12th November 2007, 22:08
I'll bet at least on the configuration fuses settings... AND... is there any specific reason why you want to run your PIC @ 32KHz?

i didn't did the calc, but i feel your current setting doesn't work anyways...

try the following


list p=16f88
INCLUDE "p16f88.inc"

; Program Configuration Register 1
__CONFIG _CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_OFF & _PWRTE_OFF & _WDT_OFF & _INTRC_IO

; Program Configuration Register 2
__CONFIG _CONFIG2, _IESO_OFF & _FCMEN_OFF

errorlevel -302 ; disable ' ensure .. bank pouet pouet' warning message

BANKSEL OSCCON
MOVLW B'01100000'
MOVWF OSCCON ; 4 MHz INT CLOCK MODE
BTFSS OSCCON,IOFS ; wait 'till int osc is stable
GOTO $-1

BANKSEL PORTB
CLRF PORTB ; Clear all
CLRF PORTA ; outputs

BANKSEL TRISA
CLRF TRISA ; all pins set to output
CLRF TRISB ;

BANKSEL ANSEL
CLRF ANSEL ; Disable all ADCs

;
; PWM Setup
; ---------
BANKSEL PR2
movlw .249 ; ~250Hz PWM FREQUENCY
movwf PR2

BANKSEL T2CON
movlw b'00000111' ; prescaler 1:16, Timer2=on
movwf T2CON

BANKSEL CCP1CON
movlw b'00001100' ; pwm mode
movwf CCP1CON

Start
BANKSEL CCPR1L
movlw .250
movwf CCPR1L ; set duty cycle to ~max, 100%

Loop1 ; ((10+bananas) *250) uSec delay loop
; to provide slower fading effect
goto $+1
goto $+1
goto $+1
goto $+1
goto $+1
DECFSZ W,F
GOTO Loop1

decfsz CCPR1L,F ; dimming led by varying the duty cycle
goto Loop1
goto Start
end

Sorry, ASM is really not my cup of tea :( So i'll not be surprised if some comes with a better solution, or with some constructive comments on the above (apart that i should care a little bit more about the bank switching... i feel there's a few useless BANKSEL here and there... that's what happen when you're lazy :D )

<hr>
Please, next time, try to not double, triple post the same question on various forum section.

ntege
- 13th November 2007, 11:31
hey thanks alot ,it works. i have just tested it.can you please tell me what $+1 means so that i can change the code to control a motor.if you can send me sample assembly code on how to use the analogue to digital converter on the pic16f88. code that can display a binary values on leds of a varying voltage signal. your help would be highly appreciated

mister_e
- 13th November 2007, 17:37
$+1 mean 'from here to the next instruction' this avoid to use labels. The GOTO $+1 just jump to the next instruction. a single goto use 2 instruction cycle.

$-1 jump to the previous instruction.

You can also change the 1 to whatever else amount of line... but this could make your code harder to read.
<hr>
i'll look at that later today for the other code.

Hey! you're such a lucky guy to have ASM support on a PICBASIC forum.

mister_e
- 14th November 2007, 01:55
play with this one



; ---------------------------------------------------------------
;
; Simple program which display ADC results of AN0
; to 8 LED connected to PORTB<7:0>
;
; Enjoy!
; Steve AKA Mister_e
;
; ---------------------------------------------------------------

list p=16f88, W=-302
; bank switching

INCLUDE "p16f88.inc"

; Program Configuration Register 1
CFG1 = _INTRC_IO & _MCLR_OFF & _BODEN_ON & _PWRTE_ON & _WDT_OFF & _LVP_OFF
CFG2 = _DEBUG_OFF & _CP_OFF & _CCP1_RB0 & _WRT_PROTECT_OFF & _CPD_OFF
__CONFIG _CONFIG1, CFG1 & CFG2

; Program Configuration Register 2
__CONFIG _CONFIG2, _IESO_OFF & _FCMEN_OFF

ORG 0

BANKSEL OSCCON
MOVLW B'01100000'
MOVWF OSCCON ; 4 MHz INT CLOCK MODE
BTFSS OSCCON,IOFS ; wait 'till int osc is stable
GOTO $-1

BANKSEL TRISA
MOVLW .1
MOVWF TRISA ; porta.0 as input, other to output
CLRF TRISB ; all pins set to output

MOVLW .7
MOVWF CMCON ; disable analog comparator

;
; ADC setup
; ---------
BANKSEL ANSEL
MOVLW .1
MOVWF ANSEL ; Disable all ADCs but AN0

CLRF ADCON1 ; Left justified results
; ADCS2 : disabled
; Vref : Vdd, Vss

BANKSEL ADCON0
MOVLW B'01000001'
MOVWF ADCON0 ; ADCS : Fosc/8
; select AN0
; ADON = 1

;
; Hardware initialisation
; -----------------------
CLRF PORTB ; Clear all
CLRF PORTA ; outputs

;
; Program Start
; -------------
Start
;
; ADC Conversion
; --------------
MOVLW .12 ; ~12 uSec acquisition loop
DECFSZ W,F ;
GOTO $-1 ;

BSF ADCON0, GO_DONE ; Start conversion
BTFSC ADCON0, GO_DONE ; conversion finished?
GOTO $-1 ; NO, wait again

;
; Display result
; --------------
MOVF ADRESH,W ; Move ADC result to Wreg
MOVWF PORTB ; display it on PORTB
GOTO Start
END

ntege
- 14th November 2007, 14:11
hi ya am surely one lucky guy.i know i might me asking for alot but can u please send the schematic for the adc code. by the way am called gonza, am from uganda in eastafrica.its like am one of the very few people in my part of the world dealing with microcontrollers.its really cool stuff i must say...... thanks alot for the support i appreciate it.

mister_e
- 14th November 2007, 15:15
There's no real difficulties here, LED cathode goes to GND, anode goes to a resistor (200-300 ohm) lead, and the other resistor lead goes to the PIC PORTB pins. repeat it 8 times.

pot ( <10K ) wiper goes to the AN0 pin (PORTA.0), one pot side on gnd, the other to Vdd.

Not much!

ntege
- 14th November 2007, 18:33
okay mate am going to try that.

ntege
- 15th November 2007, 10:50
hey the code works very well. thank you. pliease help me with sample code for reading and writing to the eeprom.plays with leds. thanks

mister_e
- 15th November 2007, 15:45
Don't push your luck too much :D you should have some code example in the datasheet?

Bruce
- 15th November 2007, 18:03
And when you find a spare moment;

I need code in binary please & full schematics in Protel format please to measure the speed of sub-atomic dark matter particles in a vacume, as they pass through a 1/2 eaten snickers bar, at -900 degrees kelvin, on the dark side of the moon, 10+E1200 meters from the event horizon of a black hole, at full moon, in the 3rd cycle. Can you help?

No hurry. I have 12 minutes before my ship departs....;o}

mister_e
- 16th November 2007, 07:30
that's easy, just enter the tunnel with your ship and read it, you should have plenty of time to write it on a little piece of paper.
http://knowledge.insead.edu/contents/images/binarycode.JPG

Sorry... i don't have Protel on my VIC-20 as my dog chew the audio tape of that software :(

Ioannis
- 16th November 2007, 07:59
I could help but my rubber keys on my ZX-Spectrum got stuck on the fingers of my doughter will she was chewing a gum....

Oh, and the tapes were gone with Steve's...

Ioannis

mister_e
- 16th November 2007, 13:50
it sucks, my Ti-99/4A doesn't work either :mad:

26 years, it's so young to die :(

Ioannis
- 17th November 2007, 12:09
26 years, it's so young to die :(

Yeah! Still just a kid!

Well, I managed to recover my ZX Spectrum! Its alive and with whole 48Kbytes of memory! Incredible what programs I wrote the old times with Zeus assembler!

Ioannis

Bruce
- 17th November 2007, 14:56
I have an old Radio Shack TRS-80 in my garage. And I thought I was old..;o}

Melanie
- 17th November 2007, 15:50
It's not what you've got - it's what you do with it... *smiles*

I've read that your average 99 cent Digital Wristwatch has more processing power than the Apollo Lunar Lander... still can't figure the right button combination on mine to get off-world...

Bruce
- 17th November 2007, 16:32
I think only the $1.00 watches have the beam me up option..;o}

earltyso
- 17th November 2007, 18:37
OK so I was a bit board this morning and decided to read this thread... HA HA HA
You guys and gals.... Mister E, Bruce, and Melanie are FRIKIN hillarious!!!!!!!!
ha ha
You made my day, I must now rejoin the normal world and get back to my family.
see ya on the dark side of the forum

mister_e
- 17th November 2007, 20:13
I have an old Radio Shack TRS-80 in my garage. And I thought I was old..;o}

Bruce, you don't need to think... YOU ARE OLD :D

Ioannis
- 19th November 2007, 07:21
Hmm, I think we have on this forum olders than Bruce.

Lucky you Bruce, you got the Beam watch!

Ioannis

RussMartin
- 19th November 2007, 08:21
As I was thinning down the stuff in my storage unit, I ran across a few goodies:

An original Compaq "portable"--still working!

Two Toshiba 1100+ laptops--also working!

And a Timex/Sinclair.

I don't know how old Bruce is, but I was already a schoolkid when Sputnik went up.

RussMartin
- 19th November 2007, 08:24
I've read that your average 99 cent Digital Wristwatch has more processing power than the Apollo Lunar Lander... still can't figure the right button combination on mine to get off-world...

Did you know that a lot of the early Soviet space equipment used vacuum tubes (valves)?

Ioannis
- 19th November 2007, 08:31
Did you know that a lot of the early Soviet space equipment used vacuum tubes (valves)?

Ain't that freaky???

Ioannis