PDA

View Full Version : 18F4523 beginner needing help to blink an LED...



thesparky
- 16th February 2011, 23:00
Hi,

I have just started using PICBASIC Pro and have had a lot of problems trying to work out what I am doing. I started using a 16F690 and after what seemed like weeks finally managed to get it to blink an LED. I then could work out what I wanted to do after this, which was to read in a temperature, display it on an LCD and then switch on a fan at certain temperatures

I am now working on a different project with a different device and have hit a wall again. Ideally I would like to repeat what I have done with the 16F690, just so I can learn about this new device. I want to use a 18F4523, but I cannot even get an LED to blink yet.

I have printed off and read the data sheet, but I cannot see what to do to get the device working. I have also tried the search function on the forums, but this device does not seem widely used.

I don't mind changing devices either if this will help. I need around 30 inputs, be able to read a RTC (1307)? in the future, and have at least two 12 bit ADC's.

Can anyone help me with the code needed to get this device (PIC18F4523) to blink an led, using the internal oscillator (4Mhz?). I do not care which pin the led is on. I know it will be a few lines that set certain registers but I have tried hundreds of different ways and keep failing.

Once I have this working I'm sure I'll have a lot more problems.... :o

Thanks.

mackrackit
- 17th February 2011, 03:47
Post your code and configs so we can see what you have done.

thesparky
- 17th February 2011, 10:42
Hi,

Thanks for taking the time to look. I tried to reply earlier, but I guess something went wrong, but if another post pops up just ignore that one....

I have not got any code yet as I have been trying lots of different things to get this to work. I have been trying for a week now to no avail. I just can't get anything to happen.

I had a lot of problems with a PIC16F690 before, until I set ANSEL and ANSELH. I guess it is something like this again, but I cannot find what, and have almost given up.

I thought I'd try this forum to see if anyone else had used thie 18F4523, and if so what they did to set it up.

As for code, I guess I would do something like this:

LH:
PortB.3 = 1 '(just an example, any port would do!!!)
Pause 1000
PortB.3 = 0
Pause 1000
Goto LH
End

But I know I have to set up the outputs, Osc etc first and this is where I am getting stuck. I have tried so many different variations but I am not getting anywhere.

I guess I should really keep looking through the 380 pages of the data sheet, and eventually I may come accross what I'm after, but I am new to this and was hopeing not to have to spend another few weeks before I can get anything to happen....

As for the harware, I have just got the VDD connected to +5v and VSS connected to GND (both sets of VDD and VSS). I am using 10Uf, and 100nf decoupling capacitors. For the outpt I am just looking at various pins using a volt meter. I am programming it using a Pickit II programmer, and I am not sure what pinout I am using for this as I have not got the circuit with me at the moment.

So in short I have not got the experience to go through the data sheet to find the info that I need, Please help!. Once I have something that I know should work I can use this to see if my hardware setup is fine, ie not missing any pullups etc (I think I tried MCLR pullup once).

Thanks.

cncmachineguy
- 17th February 2011, 12:48
ADCON1 configures analog or digital operation on this device.

As a note, I was really confused at first when I went to get the datasheet. It was only 54 pages! Well it seems they have a big note at the beginning explaining this is just the differences between this and another chip. Further they want me to use that datasheet except for the parts listed in the 54 page sheet. Anyway, the A/D stuff is in the "short sheet" So I suspect you are reading the other one as stated by you it is 380 pages.

Brian
- 17th February 2011, 14:34
Sorry you found that already. I deleted post.

Brian
- 17th February 2011, 15:05
This should work on an 18F2320. I have tons of code working on this chip but I don't have a way to program or test it here at work.

I did have a copy of an old file I was playing with and I edited it and posted it here.

Also, you need to use MPASM to compile your code with this chip. I am not sure about the 18F4523 but I would think it would too. You can select that option in MicroCode studio under options.

As you figured out before the datasheet is the best way to understand how to setup a chip. Setting or configuring the chip you are using can be difficult when you first start but after you get the hang of it and use the data sheet it gets easier.



'************************************************* ***************
'* Name : 18f2320 *
'* Author : [set under view...options] *
'* Notice : Copyright (c) 2005 [set under view...options] *
'* : All Rights Reserved *
'* Date : 2/14/2005 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************

DEFINE OSC 4

OSCTUNE = 0 ' at calibrated frequency so not offset
OSCCON = %01100000 'OSC at 4Mhz

@ __CONFIG _CONFIG1H, _INTIO2_OSC_1H & _IESO_OFF_1H ' use internal osc
@ __CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L
@ __CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
@ __CONFIG _CONFIG3H, _MCLRE_OFF_3H & _PBAD_DIG_3H & _CCP2MX_OFF_3H
@ __CONFIG _CONFIG4L, _LVP_OFF_4L


ADCON0.0 = 0
ADCON1 = %11001111

CMCON = 7 'comparators off
CVRCON = 0
LVDCON = 0

'set pins for output
trisb = 0
trisc = 0
trisa = 0

LED var portb.7 ' could be any pin you want here all configured for outputs

'varible declarations

'constants



'set variable and misc.



goto endsub


endsub:


main:

high led
pause 200
low led
pause 200

end

thesparky
- 17th February 2011, 21:06
Thanks for all the replies.

I have just checked my microcode studio and it is currently using MPASM, thanks for the pointer though, if this was set wrong I would have never found it...

I'll have a play with the code posted tomorrow, and post how I get on.

Cheers,
AD.

thesparky
- 18th February 2011, 15:14
Still cannot get anything to work with this PIC18f4523.

My code is (at the moment...):

DEFINE OSC 8
OSCCON = %01110000
ADCON0 = %00000000
ADCON1 = %00001111
CMCON = %00000111
TRISE=0
TRISD=0
PORTE.0 = 1
PORTD.2 = 1

I have tried various different things all day but I still cannot get any output.

I cannot try the code Brian supplied as it was for a different PIC (well, I did try it but it did not compile with the @__... lines in place, once I'd rem'd them out it programmed fine but still did not work). I can't use a 18F2320 as this has not got enough pins for the final project I want to use this pic for, and ideally I would like at least 1 12 bit adc, preferably 2.

mackrackit
- 18th February 2011, 15:19
did not compile with the @__... lines in place,

What exactly happened?

To set the configs in code space
http://www.picbasic.co.uk/forum/showthread.php?t=543


Your are using MPASM as the assembler?.

mark_s
- 18th February 2011, 15:26
Still cannot get anything to work with this PIC18f4523.

My code is (at the moment...):

DEFINE OSC 8
OSCCON = %01110000
ADCON0 = %00000000
ADCON1 = %00001111
CMCON = %00000111
TRISE=0
TRISD=0
PORTE.0 = 1
PORTD.2 = 1

I have tried various different things all day but I still cannot get any output.

I cannot try the code Brian supplied as it was for a different PIC (well, I did try it but it did not compile with the @__... lines in place, once I'd rem'd them out it programmed fine but still did not work). I can't use a 18F2320 as this has not got enough pins for the final project I want to use this pic for, and ideally I would like at least 1 12 bit adc, preferably 2.

You also need to set adcon2 on the PIC18F4523. It might be better to try a 10 bit a to d converter first. Maybe a PIC18F452 once you get that working you move to a 12bit converter. PBP adcin command may not work since it is geared to 8 and 10bit adc's.
Good luck

Nothing like a quick search. See Bruce's example
http://www.picbasic.co.uk/forum/showthread.php?t=7479&highlight=12bit

thesparky
- 18th February 2011, 16:15
mackrackit

The compiler did not seem to like the commas.
e.g @ __CONFIG _CONFIG1H, _INTIO2_OSC_1H & _IESO_OFF_1H

I have just tried the line:
@__CONFIG_INTRC_OSC_NOCLKOUT

and it seems to compile with this. I do not know what this does and do not know if it applies to this PIC, but I was just seeing if would complie.

I am using MicroCode Studio, and when I go to View/Compile and Program Options/Compile Tab the use PBPL checkbox is ticked (although I have tried unticked) and when I go to the Assembler Tab the Use MPASM is ticked. I have not changed any of the settings (appart from PBPL) since I have been programming a 16F690 and this seems to program fine.


mark_s

I think I may be confusing the issue with the A-D convertor. I do not want any A-D stuff yet, I just want a simple?!? digital output to prove to myself that my setup works. I will look at ADCON2 now though. I tried another PIC18F4523 and this does not give me any output either. I have re-checked all the connections with a DVM and they all seem fine.


If I still have no luck over the weekend then I will buy some different pics on Monday.

mackrackit
- 18th February 2011, 20:30
No AD?
Take a look at this.
http://www.picbasic.co.uk/forum/content.php?r=154-All-Digital

thesparky
- 18th February 2011, 22:15
I looked into ALL DIGITAL and thought it would be the answer to all my prayers...

My Code is now as follows:

INCLUDE "ALLDIGITAL.pbp"
define SHOWDIGITAL 1
Main:
TOGGLE PORTE.0
PAUSE 1000
GOTO Main


Unfortunately I still cannot get any outputs.:mad:

When compiled I get the following messages in the results window:
ADCON1 = 0x0F
CMCON = 7

I have rechecked my software and hardware by programming another Pic, a 16F690, and this programs fine.

I am beginnig to think I may have a dodgy couple of IC's. I'll try and build another board up next Monday to rule out this, although I can program and read the pic with the setup I have now.

Any other things I can try?

mackrackit
- 19th February 2011, 00:33
I do not have this chip but give this a shot


'18F4523 INTERNAL OSC 8MHZ Internal oscillator block, port function on RA6 and RA7
@ __CONFIG _CONFIG1H, _OSC_INTIO67_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
@ __CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
@ __CONFIG _CONFIG3H, _CCP2MX_PORTC_3H & _PBADEN_OFF_3H & _LPT1OSC_OFF_3H & _MCLRE_ON_3H
@ __CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _XINST_OFF_4L
DEFINE OSC 8
OSCCON = %01110000
ADCON1 = %00001111
CMCON = 7

Main:
TOGGLE PORTE.0
PAUSE 1000
GOTO Main

cncmachineguy
- 19th February 2011, 01:46
FWIW, Dave your code works in sim, (I don't have that PIC either). 2 things I had to do: comment the configs cuz I was too lazy to go fix the .INC, and I had to tie MCLR high. So question for OP, Do you have MCLR enabled AND tied high? I ask because it's an easy to miss thing.

mackrackit
- 19th February 2011, 01:53
I think the configs are the key here if the OP wants to use the internal OSC.

cncmachineguy
- 19th February 2011, 01:59
Well I must admit, being new to proteus, blinking that LED was like I had done it with my first PIC!! I was thinking about MCLR since I assume when I commented the configs it used the PBP default's.

mackrackit
- 19th February 2011, 02:05
I may have to try a sim again, when I tried it a few years ago... well it just plan was not worth the effort.

The PBP inc configs for this chip are HS on the OSC.

Does your sim have a resonator or something attached?

And you may have hit it about MCLR.

cncmachineguy
- 19th February 2011, 02:15
Well see Dave its like this: I have no idea if there is a resonator or not. I am guessing not. I didn't add 1. my entire circuit is the chip, an led, and a wire to Vcc for MCLR. But there are built in things like automatic tie in to pwr and gnd, so MAYBE? and I didn't even put a resistor in series with the LED. So these things make me go Hmmm. But the LED was blinking at what appears 1 sec rate.

mackrackit
- 19th February 2011, 02:21
Very interesting.... :D

Does the LED burn out after awhile or do you have a place to set VDD to ~3 ?
Yes... I am being a smart _ _ _ now :)

cncmachineguy
- 19th February 2011, 02:31
in fact I think I could set it to ~3V, but I hooked her right from pin to ground, with uP at 5V. Left it running for 10 mins and still burning bright. :rolleyes:

This may be a case of don't fully trust the sim. Sorta like the LED thread where Darrels sim showed the different levels of brightness for different resistor values. but in real life, it works great as you and Dhuston? said.

I don't mind a little smart $$. makes things fun:)

thesparky
- 19th February 2011, 09:51
I have seen the light! The LED is now flashing! :D

mackrackit,

I tried the code you supplied but there were some problems in compiling with a message about overwriting (sorry, I cant remeber what it said), so I pasted these into the .inc file instead, only because cncmachineguy hinted at this. I'm not sure why or how this worked but it has. I'll have to spend a few days looking through all the files to work out in my head what is happening. I guess it wasn't using the internal crystal in the initial settings amongst other things.

So a big thanks to everyone who has helped over the past few days. I never would have got there on my own...

If anyone in the future finds this thread and wants a copy of the .inc file with the mackrackit mods then just get in touch, but it is fairly easy to do yourself. The file is in the pbp directory, called 18F4523.INC. Just load it into a text editor and comment out the original config lines, using a semi-colon, and add the four new config lines, but without the @ sign. I made a backup of the original file incase I messed something up....

Thanks again everyone, and I expect I'll be in touch when I try to do the 12 bit a-d conversion, if I cant find the info using the search.......

mackrackit
- 19th February 2011, 10:02
COOL!!!!!

You just about have the overwriting error solved too. When you commented out the inc file to make the mods, stop there. PBP looks to the inc file for the configs, so if you want to set them in code space commenting the inc is what you do.
http://www.picbasic.co.uk/forum/showthread.php?t=543

There is another *.inc file that is handy. It is in 'program files/ microchip/mpasam suite/'. These inc's have all of the available config settings at the end of the file. Do not modify this file. It is for reference only.