PDA

View Full Version : 12F683 - how to set it to use external 4 MHz clock?



jimbostlawrence
- 25th September 2009, 12:46
Hi there,

I'm new to all this PIC business but loving it! I'm using a 12F683 to output a HPWM on pin 5 no problem. I'm also trying to bit bang data to it on the serial but on pins 6/7, which generally works but does get some garbage through. I've opted to fit a 4 MHz crystal oscillator to pins 2/3. My problem is non-familiarity with PBP! I've been looking at how to set the PIC to use the external oscillator instead of the internal however PBP just gives an error. I've been reading for hours and seem to be getting myself stuck in a loop!
The error I'm getting at the moment for:

@ DEVICE XT_OSC
DEFINE OSC 4

is:

Warning[207] e:\.............\test.asm 47 : Found label after column 1. (DEVICE)
Error[122] e:\...............\test.asm 47 : Illegal opcod (XT_OSC)

I know it is going to be something stupid that I've not done but just getting lost in all the jargon.

Cheers

Jimbo

mehmetOzdemir
- 25th September 2009, 13:13
There must be no spaces before @ statement.

"@" must be at the begining of row.

jimbostlawrence
- 25th September 2009, 13:33
Hi there,

Many thanks for your prompt reply. I now accepts the @DEVICE bit but says XT_OSC is an illegal opcode (also tried _XT_OSC).

Any ideas?

Cheers

Jimbo

mehmetOzdemir
- 25th September 2009, 13:47
- xt_osc is an illegal opcode


i think you use mpasm.

USE PM.

DONT SELLECT "USE MPASM BOX" AT THE COMPILE AND PROGRAM OPTIONS.

jimbostlawrence
- 25th September 2009, 13:51
It progressess! :)

It's actually picbasic pro compiler 2.50 I'm using. I unticked 'use mpasm' box as you suggested.

Now it says "Error TEST.ASM 47 : [235] opcode expected instead of _xt_osc"

???!

mehmetOzdemir
- 25th September 2009, 13:55
Remove "_"

use
@ device xt_osc

jimbostlawrence
- 25th September 2009, 13:56
ARGHHHHHHHHHHHHHH

...and I thought that I'd tried every conceivable combination of @ / device / + or - _ !!!

Many thanks for that.

Best regards

Mark

mackrackit
- 25th September 2009, 14:01
For PM


@ device pic12F683, xt_osc, wdt_on, mclr_on, protect_off

FOR MPASM


@ __config _XT_OSC & _WDT_ON & _MCLRE_ON & _CP_OFF

jimbostlawrence
- 25th September 2009, 14:08
Hi there,

Run into more problems now lol. With 'use mpasm' unticked, I can no longer use mplab ide to build/upload the file to the pic. It gives a stream of errors.

However, if I tick the 'use mpasm' again, I get errors in picbasic pro:

Warning[207] e:\.......\sertest.asm 93 : Found label after column 1. (device)
Error[122] e:\.......\sertest.asm 93 : Illegal opcode (pic12F683)

for code:

#####################################
@ device pic12F683, xt_osc, wdt_on, mclr_on, protect_off
DEFINE OSC 4
Include "modedefs.bas"
OSCCON = %00001000
ANSEL=0
CMCON0=%00000111
pw var byte
led var gpio.4
OSCCON=%01110000
oldpwm var byte
#########################################

etc...

I think I basically don't understand how these applications work together creating multiple files that are all linked together... I come from the old school - one program and run! ;)

jimbostlawrence
- 25th September 2009, 14:11
oooh further to my last I think I misunderstood what you said Dave... so I switched to the:

@ __config _XT_OSC & _WDT_ON & _MCLRE_ON & _CP_OFF

and now the error I get in the compiler is Error[118] e:\....\sertest.asm 93 : Overwriting previous address contents (2007)

?!

Is this perhaps related to my program and not this config aspect?

mehmetOzdemir
- 25th September 2009, 14:30
OPEN YOUR PBP FOLDER AND FIND 12F683.INC

YOU WILL SEE



NOLIST
ifdef PM_USED
LIST
include 'M12F683.INC' ; PM header
device pic12F683, intrc_osc_noclkout, wdt_on, mclr_on, protect_off
XALL
NOLIST
else
LIST
LIST p = 12F683, r = dec, w = -302
INCLUDE "P12F683.INC" ; MPASM Header
__config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_ON &_CP_OFF
NOLIST
endif
LIST


CHANGE THE RED PART AFTER ELSE TO THIS ;



NOLIST
ifdef PM_USED
LIST
include 'M12F683.INC' ; PM header
device pic12F683, intrc_osc_noclkout, wdt_on, mclr_on, protect_off
XALL
NOLIST
else
LIST
LIST p = 12F683, r = dec, w = -302
INCLUDE "P12F683.INC" ; MPASM Header
__config _XT_OSC & _WDT_ON & _MCLRE_ON & _CP_OFF
NOLIST
endif
LIST


AND YOU DONT NEED TO WRITE "@ DEVICE" STATEMENT AT THE BEGINNING OF YOUR CODE ANYMORE.

mackrackit
- 25th September 2009, 14:36
In my opinion forget about using PM, always use MPASM. Later PM will nor work when you get into different things.

Now I forgot to tell you when you set the fuses in code space you will have to comment out some lines in the chips *.inc file that is in the PBP directory.
http://www.picbasic.co.uk/forum/showthread.php?t=543

Or you can modify the *.inc file.

My inc for this chip looks like this.


;************************************************* ***************
;* 12F683.INC *
;* *
;* By : Leonard Zerman, Jeff Schmoyer *
;* Notice : Copyright (c) 2005 microEngineering Labs, Inc. *
;* All Rights Reserved *
;* Date : 08/31/05 *
;* Version : 2.46a *
;* Notes : *
;************************************************* ***************
NOLIST
ifdef PM_USED
LIST
include 'M12F683.INC' ; PM header
; device pic12F683, intrc_osc_noclkout, wdt_on, mclr_on, protect_off
XALL
NOLIST
else
LIST
LIST p = 12F683, r = dec, w = -302
INCLUDE "P12F683.INC" ; MPASM Header
; __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_ON & _CP_OFF
NOLIST
endif
LIST

jimbostlawrence
- 25th September 2009, 14:43
Excellent!!! I was just reading another persons similar problem and was trying to track it down. Many thanks for that. It does indeed now compile and MPLAB IDE lets me upload the code to the PIC.

Out of interest, is there any way that I can check if the thing has actually switched to the external oscillator [short of removing the oscillator!] by looking at the assembler file?

Many thanks all for help with this - has been bugging me for hours! :)

mackrackit
- 25th September 2009, 14:49
COOL!!!
You are using MPLAB!!!!
Tool bar - configure - configuration bits.
That shows the current sttings.

jimbostlawrence
- 25th September 2009, 14:53
Haha, you make it sound soooooooooo easy :P

'XT' :) Cheers

I was hoping that this would help with my serial comm's issue but it hasn't lol. Maybe try another PIC, one with a serial interface built in perhaps?!

I'll persist with this PIC for now and try to do some error-checking handshaking.

I can say that I've definitely learned something today ;)

Best regards to all

J.

mackrackit
- 25th September 2009, 15:02
Now that the configs seem to be worked out, post your code and tell what is happening or not happening and we will see what can be done about it.

jimbostlawrence
- 28th September 2009, 13:31
Hi there,

I think I was having a Friday moment. It all seems to be working very well now that it's using the external osc'. I've even bumped up the baud rate to 9600 and getting a good response. Just putting together a simple algorithm to take input over the serial but and set the pulse frequency/duty cycle for HPWM via Hyperterminal.

Many thanks all for all your help. When I get the code done I'll post it on here as a working version. Won't be long.

Cheers

J.

jimbostlawrence
- 28th September 2009, 15:49
Hi again,

Ok, not the most elegant piece of code, but it works. Basically this allows you to instruct the 12F683 to output a regular pulse at a desired frequency or duty cycle on pin 5. You can communicate with the chip via a serial com port [I'm using a USB to RS232 converter].

Personally, I establish a connection with hyperterminal that comes with XP.
Press 'f' to set frequency [press enter after you type a value in Hz]
Press 'f' to set the dutycycle (0 - 100%) [+enter] - the program will auto scale it 0 - 255.

Or alternatively, type 'f?' or 'd?' to determine the current set dutycycle of frequency.

Note: There is little in the way of error checking on the input. I just wanted to get this back to you guys to show what I'd done and that I was grateful for your help last Friday. Now that I have reliable 2-way com's on the serial bus, I'll set it up with a nice little application in MatLab.

Best regards, J.

P.S. One thing I couldn't sort out was clearing the screen at the start in hyperterminal from the serial port. People say to use Esc/[2J or something but I can't get it to work!!!


Include "modedefs.bas"
ANSEL=0
CMCON0 = %00000111
define osc 4

Serial_out var gpio.0
Serial_in var gpio.1
command var byte
input_char var byte
pulse_width var word
pulse_width_percent var byte
pulse_width_percent_new var byte
pulse_frequency var word
pulse_frequency_new var word
variable_output var word

pulse_width = 128
pulse_width_percent = 50
pulse_frequency = 20000

pause 100
serout Serial_out,T9600,[13]
pause 100
serout Serial_out,T9600,[13," "]
pause 100
SEROUT Serial_out,T9600,[13,"This is a test",13,10]
SEROUT Serial_out,T9600,[13,"f for frequency",13,10]
SEROUT Serial_out,T9600,[13,"d for dutecycle (0-100)",13,10]
SEROUT Serial_out,T9600,[13,"Press ? after f/d to find current value",13,10]

mainloop:

Serin Serial_in,T9600,command

If (command = 100) then
SEROUT Serial_out,T9600,["Enter duty cycle : "]
input_char = 0
pulse_width_percent_new = 0
while input_char<10
pulse_width_percent_new = (pulse_width_percent_new * 10) + input_char
gosub getchar
wend
if input_char <> 15 then
pulse_width = pulse_width_percent_new * 256 / 100
hpwm 1,pulse_width,pulse_frequency
SEROUT Serial_out,T9600,[10,13]
else
SEROUT Serial_out,T9600,[#pulse_width_percent,10,13]
endif
pulse_width_percent = pulse_width_percent_new
endif

If (command = 102) then
SEROUT Serial_out,T9600,["Enter frequency cycle : "]
pulse_frequency_new = 0
input_char = 0
while input_char<10
pulse_frequency_new = (pulse_frequency_new * 10) + input_char
gosub getchar
wend
if input_char <> 15 then
pulse_frequency = pulse_frequency_new
hpwm 1,pulse_width,pulse_frequency_new
SEROUT Serial_out,T9600,[10,13]
else
SEROUT Serial_out,T9600,[#pulse_frequency,10,13]
endif
endif

Goto mainloop

getchar:
serin Serial_in,T9600,input_char
input_char = input_char - 48
if input_char < 10 then
serout Serial_out,T9600,[48+input_char]
endif
return

mackrackit
- 28th September 2009, 20:37
COOL! Glad you got it working!

I do use hyperterminal much so I do not know for sure the command but here is something from way back. Looks like it says almost the same as you are trying???
http://list.picbasic.com/forum/messages/5058/5809.html?1054384602

jimbostlawrence
- 29th September 2009, 09:16
Hi,

Thanks for your email. I saw that webpage before but couldn't get it to work. However, the problem was just familiarity with picbasic/this whole setup! I simply looked up the ascii codes and send them as you would expect, and it 'does' work :)

serout Serial_out,T9600,[27,91,50,74]

I'm just putting some error checking into the routine, to try and make it at least a little fool proof. It just feels so good now that I have comm's with the PIC. Feel like I've got over a full hurdle. Reliable comm's back and forth is so important to allow one to develop this stuff!

I'll not add any further questions to this thread as I'm going off topic with my next issue. I'll see how it goes and then if desperation calls, I'll call you! I want to try to have 3 pic's all reading off the same RS232 RX line[and TXing on the same line]. Each PIC will be programmed separately with a unique identifier and will only address the TX line if it received data relevant to itself. But, I don't want to cover the board with 4 MHz xtals so I'm going to try to run 3 of them off the one xtal! Not sure how possible that is, but if it works, then great :P

Anyway,

Thanks to you and all for your help in getting me started on this PIC stuff. Feel like I've just been given my first car and have the freedom to go anywhere! ;)

J.