PDA

View Full Version : What am I doing wrong ?



malc-c
- 8th June 2006, 22:48
I have the need to pulsate an LED rather than just turn it on or off. I've modified the code I used for my model train detection project that used a 16F628A and have a working example on my bread board. Although its not rocket science I used the PWM command and a for next loop to increase the on time of the LED and thus its brighness. However given that this is just my second project with PBP I'm well chuffed that it worked with very few attempts.

However, I really want to use a 12F675 so I copied and pasted the code, substituting PORTB.4 for GPIO.0 and compiled the code once the chip was selected in Microcode IDE. However it fails to work with the 12F675. Ive included the setting to make ports digital (I think) but it still fails, and I would really like some guidance here.

This is the code for the 12F675



GPIO.0 = 0
CMCON = 7 ' PortA Digital inputs
VRCON = 0 ' Voltage reference disabled
OPTION_REG.7 = 0


@RC_OSC_NOCLKOUT
@WDT_ON
@PWRT_ON
@MCLR_OFF
@BOD_ON
@LVP_OFF
@CPD_OFF
@PROTECT_OFF

led var GPIO.0

i var byte

Main:
For i = 0 to 255
Pwm GPIO.0,i,1
next i
if i = 255 then goto down

down:
for i = 255 to 1 step -1
pwm GPIO.0,i,1
next i
If i = 0 then goto main


Is the software command PWM supported on all chips ? I thought it simply pulses the pin for a duration and cycle.

Hope to hear from you guys soon

Cheers

Malcolm

HenrikOlsson
- 9th June 2006, 06:54
Hi Malcolm,
The 12F675 has comparators, which you correctly turned off with CMCON and VRCON but it also has an A/D and the the pin needs to be set to digital (I think).

Try setting the ANSEL register and set the port to output with the TRISIO register.



TRISIO.0 = 0 'Set GPIO.0 to output.
ANSEL.0 = 0 'Set GPIO.0 to digital.


I think that is your problem.

/Henrik Olsson.

malc-c
- 9th June 2006, 08:08
Thanks for the reply.

I've added those two lines to the code and I still get nothing out from the PIC.

The top section of the code is:


TRISIO.0 = 0 'Set GPIO.0 to output.
ANSEL.0 = 0 'Set GPIO.0 to digital
GPIO.0 = 0
CMCON = 7 ' PortA Digital inputs
VRCON = 0 ' Voltage reference disabled
OPTION_REG.7 = 0


I've double checked the hardware and the PIC is receiving 5v, but all pins other than GP5 remain low (0.1v, with GP5 +2.6v) - could it be something to do with the internal OSC setting ?? - I want to use the built in 4 Mhz OSC

Malcolm

sayzer
- 9th June 2006, 08:25
Try these settings instead of yours especially for "RC_OSC_NOCLKOUT" ;



@ DEVICE PIC12F675, INTRC_OSC, WDT_OFF, MCLR_OFF,PROTECT_ON

paul borgmeier
- 9th June 2006, 08:28
change this

if i = 255 then goto down

to this

if i = 0 then goto down

and all should be well .....

or better yet delete the line (it adds no value) and replace the last line of the program shown with this

goto main

(instead of the if-then, which adds no value)

Paul Borgmeier
Salt Lake City, Utah
USA

Acetronics2
- 9th June 2006, 08:59
Hi, Malc

Don't you think a simple triangle slow wave generator based upon a 555 or a twin AOP could perfectly do the job ???

Note a simple bulb lamp could also do all the job itsef ... with a rectangular wave input.

mmmmh, back to the original idea ... you'll have to seriously slow down the PWM change rate ...

May be intended for some stop light for road/rail crossing, I presume ???

Alain

PS : and why not use a lookup table, just to reproduce exactly the truth ( exponential rise and fall of enlightment ) ???
just a " particular shape " wave generator ...in fact.

malc-c
- 9th June 2006, 11:55
Try these settings instead of yours especially for "RC_OSC_NOCLKOUT" ;



@ DEVICE PIC12F675, INTRC_OSC, WDT_OFF, MCLR_OFF,PROTECT_ON


Thanks for the suggestion, but I now get a warning when I try to compile (using MPSAM) - it states "Found lable [DEVICE]" and "Illegal Opcode [12F675]"

malc-c
- 9th June 2006, 12:00
Hi, Malc

Don't you think a simple triangle slow wave generator based upon a 555 or a twin AOP could perfectly do the job ???

Note a simple bulb lamp could also do all the job itsef ... with a rectangular wave input.


Quite possibly, however that wouldn't improve my understanding of PBP ;)


May be intended for some stop light for road/rail crossing, I presume ???

Errr.. actually you don't want to know... Oh well all right..

A friend wants to model the Tardis from the BBC series Dr Who... and it has a slow pulsing light on top..... me and my big mouth offered to help, and like I said it works fine on a 16F628a, but I just need to port it to a 12F675

Malc

Acetronics2
- 9th June 2006, 12:47
Hi, Malc

That's a bit more complicated ... as Dr Who is completely unknown here !!!

May be some more info about that light timing and appearance ( flash, slow rise and fall, Pulsing light ....) could help us to help you ...

Alain

sayzer
- 9th June 2006, 12:56
Thanks for the suggestion, but I now get a warning when I try to compile (using MPSAM) - it states "Found lable [DEVICE]" and "Illegal Opcode [12F675]"


In your code, put a space between '@' and "DEVICE"


--------------

malc-c
- 9th June 2006, 13:36
In your code, put a space between '@' and "DEVICE"


--------------

Yup, have done (I copied and pasted the line in your post) and still fails to compile.

Malc

malc-c
- 9th June 2006, 13:40
Hi, Malc

May be some more info about that light timing and appearance ( flash, slow rise and fall, Pulsing light ....) could help us to help you ...

Alain

I was going to attach a short avi (DVIX codec) of the result I have with the 16F628a chip, which is exactly what my friend wants..., but even zipped its slightly more than the max 200K limit we're allowed to upload. If I can't get the 12F675 working I'll just mail him the '628 chip and save all the headache !

mister_e
- 9th June 2006, 13:41
if you use MPASM, DEVICE is not the right command, the the FAQ thread about the config fuse.


@ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON
' Internal Oscillator
' Enable watch dog timer
' Enable power up timer
' Disable MCLR pin
' Enable brown out detect


Or you may set them manually in your device programmer software.

HTH

malc-c
- 9th June 2006, 16:05
Steve,

Looks like I might have to set them manually or use the 16F628 as I still get an error when I copy and past the line of code from your mail.

This time I get:

Error 118: overwriting previous address contents 2007

What the hell that means I have no idea ?

I think I need to go back to basics and get a LED flashing on a PIN and then impliment my code for the PWM

Thanks guys for your continuing help, its this support that makes PBP my preferred programming language ;)

Malc

mister_e
- 9th June 2006, 16:57
so it seems you didn't read the FAQ ;)
pay attention to post #5 of the link bellow
http://www.picbasic.co.uk/forum/showthread.php?t=543

Acetronics2
- 9th June 2006, 17:03
Hi, Steve

somewhat tired in the end of that week ...

Alain

mister_e
- 9th June 2006, 17:07
Alain, try it and uncomment the default config fuses.. what do you get?

Maudit francais :D

Steve who laught a big time right now!

malc-c
- 9th June 2006, 17:43
so it seems you didn't read the FAQ ;)
pay attention to post #5 of the link bellow
http://www.picbasic.co.uk/forum/showthread.php?t=543

Thanks for the link, and whist I did look at the FAQ's.. I must admit that I didn't scroll down much further than the original post - Sorry !

However the good news is that the LED is now pulsing away on GPIO 0 :)

However I really want to learn from this and I'm a tad confused (easy to do). Whats the difference between having the line in the program code and the INC file. ?

I found that if I removed the line

@ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON
from the programing code and left the INC file intact the program compiled but the LED did nothing. - Reming out the line in the INC file and adding it to the code file, resulted in the code compiling without errors and the LED is pulsing away fine !

Should (or is that could) I use the INC file and have some statement in the programming code to call this file (something like include 12F675.inc)

Also, will I have to modify every INC file for every PIC that I use so i don't get this problem again ?

Cheers

Malc

paul borgmeier
- 9th June 2006, 18:50
Although technically correct, after some sleep and a few cups of coffee I can see that my suggestions would not have helped you. (#5 in this thread)

good luck,

Paul

mister_e
- 9th June 2006, 22:50
Yeah it's pain dude, you'll have to modify all .INC for all PIC you have in stock. The MPAM 'errorlevel -' stuff can't work on that... it would be just too much convenient.

I already think of doing an automatic application to comment out every .inc file... maybe i'll do it one day... maybe ;)

But i think that even if you get this Error message, your config fuse will be set as per your code. It will just be an error message like the one for Page Boundary, or Find directive in column x, or... ,.. wich can be ignored. It's just unplesant to see it in your face :)

malc-c
- 9th June 2006, 23:12
I guess its all part of the learning curve ! As I stick to just two or three PICs I can't really moan about having to rem out the config line... I had a look at the 16F628a inc file and that too was remed out.. just don't remember doing it (like a lot of things these days... must be getting old !)

Thanks for all the pointers and help

Malc