PDA

View Full Version : 18F2431 Learning Curve



Aussie Barry
- 22nd June 2012, 13:15
Hi All,

I got my 18F2431 today and I am trying to learn how to fly it.
I thought I would start with a very simple LCD routine:



'************************************************* ***************
'* Name : LCD_18F2431 *
'* Author : Barry Phillips *
'* Notice : Copyright (c) 2012 Baztronics *
'* : All Rights Reserved *
'* Date : 22/06/2012 *
'* Version : 1.0 *
'* Notes : LCD test program for 18F2431 *
'* : *
'************************************************* ***************

' Define LCD registers and bits
Define LCD_DREG PORTB
Define LCD_DBIT 0
Define LCD_RSREG PORTB
Define LCD_RSBIT 5
Define LCD_EREG PORTB
Define LCD_EBIT 4
Define LCD_BITS 4
Define LCD_LINES 2

Mainloop:
LCDOUT $FE, $01 ' Clear screen
LCDOUT $FE, $80, "Hello World"
Pause 1000 ' Pause 1 second
LCDOUT $FE, $C0, "Hello Again"
pause 5000 ' Pause 5 seconds
goto Mainloop ' Loop forever


When I tried to complie the program I get the following error messages

[ASM WARNING] Invalid RAM location specified. (0) : Warning [219]

repeated 13 times.

I was expecting a severe learning curve with this new device but I would have thought something as simple as this would have been straight forward.

Can someone please enlighten me on what needs to be done to correct this problem?

Cheers
Barry
VK2XBP

Aussie Barry
- 23rd June 2012, 00:25
Update:

It was a very long day yesterday!
I realised this morning that I had the wrong device selected in MCS. It compiles now but there ares still problems.
I did see "Hello World" appear on the first line of the LCD but it took a couple of seconds to get there...and it only happened once.
I obviously have the set-ups all messed up. I can't even do a simple LED Blink on RC0 :(

Back to the datasheet.

Cheers
Barry
VK2XBP

Demon
- 23rd June 2012, 02:44
Oscillator speed?

Or is that not required any more with v3.0 (I'm still on v2.60).

Robert

Aussie Barry
- 23rd June 2012, 02:55
I am using the internal oscillator which defaults to 8MHz
I also tried the program with

DEFINE OSC 8

preceeding the LCD definition statements, all to no avail :(

I was even more disappointed that my Blinky LED program didn't work



TRISA = 0 ' Set PORTA as output

LED VAR PORTA.0 ' Assign name "LED" to PORTA.0

mainloop:
High LED ' Turn on LED connected to PORTA.0
Pause 500 ' Delay for .5 seconds

Low LED ' Turn off LED connected to PORTA.0
Pause 500 ' Delay for .5 seconds

Goto mainloop ' Go back to loop and blink LED forever

End


I have +5V connecteds to pins 7 and 20 (AVdd and Vdd) and 0V connected to pins 8 and 19 (AVss and Vss). I have a 10k pull-up resistor from MCLR (Pin 1) to +5V and 330R in series with LED connected from RA0 (Pin 2) to 0V. I even tried fliping the LED just in case I put it in backwards!

Any ideas on why this thing won't work as expected?
Perhaps the default for RA0/AN0 is an analog input pin? I thought the TRISA = 0 statement would overide...

Cheers
Barry
VK2XBP

mackrackit
- 23rd June 2012, 03:39
How are the configs setup?

Demon
- 23rd June 2012, 03:42
I always stay away from analog pins until I get a basic program running. :)

Robert

Aussie Barry
- 23rd June 2012, 03:57
Thanks Robert - Unfortunatley this device has so many options per pin. If I stay away from the analog pins then I run into PWM pins, motion feedback pins and others that I haven't got my head around yet. I thought the RA0/AN0 option was the path of leas resistance.

Dave, I am using my MeProgrammer to configure the device at time of programming.
Details are as follows:
Oscillator = INTRC
Inernal External Switch Over = Enabled
Fail-Safe Clock Monitor - Enabled
Power-Up Timer = Disabled
Brown-Out Reset = Enabled
Brown-Out Reset Voltage = 2.0V
Watchdog Timer = Enabled
Watchdog Timer Postscaler = 1:512
Watchdog Timer Window = Enabled
PWM Pins = Disabled on Reset
Low-Side Transistor Polarity = Active High
High-Side Transistor Polarity = Active High
Special Event Reset = Disabled
MCLR Pin Function = Reset
Stack Overflow/Underflow Reset = Enabled
Low Voltage Programming = Disabled

All other memory options are "Not Protected"

Cheers
Barry
VK2XBP

Aussie Barry
- 23rd June 2012, 04:23
Update:

I have added the following lines to my Blinky LED program:



ANSEL0 = 0 ' Set all PORTA as digital I/O
ADCON0.0 = 0 ' Disable A/D module
TRISA = 0 ' Set PORTA as output


I was confident I would see a blinking LED after these changes but was sadly mistaken...

Cheers
Barry
VK2XBP

mackrackit
- 23rd June 2012, 05:07
Some will disagree, but I have never trusted the programmer to set the configs.
What version of PBP are you running?

Aussie Barry
- 23rd June 2012, 05:20
Ver 3.0.1.4

How should I manually set the configs and/or which configs are critical for my simple application?

I went back to the LCD test program and noticed that I do get two lines of text appearing BUT the timing is way out of kilter - maybe by a factor of 10 or 20?
I am pretty sure I have a clock issue. I don't have any crystals to drop in place but will try to get some tomorrow. I will also re-try the Blinky LED program and reduce the time between toggles to see if I can get something happening...

Cheers
Barry
VK2XBP

ScaleRobotics
- 23rd June 2012, 05:51
Hi Barry,

To include your configs in your pbp 3.0 code is pretty easy. Here are some examples: http://www.picbasic.co.uk/forum/showthread.php?t=543&p=110963#post110963

The bare bones ones to include would be these (though I have not tried running it on internal yet): Note it defaults to 4 mhz, unless you set the oscon bits for something else.


#CONFIG
__CONFIG _CONFIG1H, _OSC_IRCIO_1H
__CONFIG _CONFIG2H, _WDTEN_ON_2H & _WDPS_512_2H
__CONFIG _CONFIG4L, _LVP_OFF_4L

#ENDCONFIG

ADCON0 = %00000000
ADCON1 = %00000000

rsocor01
- 23rd June 2012, 06:30
Include the line

DEFINE OSC xx

And set all the options to disable (watchdog, brown-out, etc) to start with.

Robert

HenrikOlsson
- 23rd June 2012, 10:16
Hi,
Here are the CONFIGs I use for my 2431:

#CONFIG
CONFIG OSC = HSPLL
CONFIG FCMEN = ON
CONFIG IESO = OFF
CONFIG PWRTEN = ON
CONFIG BOREN = ON
CONFIG BORV = 42
CONFIG WDTEN = OFF
CONFIG WDPS = 512
CONFIG WINEN = OFF
CONFIG PWMPIN = OFF
CONFIG LPOL = HIGH
CONFIG HPOL = HIGH
CONFIG T1OSCMX = OFF
CONFIG MCLRE = ON
CONFIG STVREN = ON
CONFIG LVP = OFF
CONFIG DEBUG = OFF
CONFIG CP0 = ON
CONFIG CP1 = ON
CONFIG CP2 = ON
CONFIG CP3 = ON
CONFIG CPB = OFF
CONFIG CPD = OFF
CONFIG WRT0 = OFF
CONFIG WRT1 = OFF
CONFIG WRT2 = OFF
CONFIG WRT3 = OFF
CONFIG WRTC = OFF
CONFIG WRTB = OFF
CONFIG WRTD = OFF
CONFIG EBTR0 = OFF
CONFIG EBTR1 = OFF
CONFIG EBTR2 = OFF
CONFIG EBTR3 = OFF
CONFIG EBTRB = OFF
#ENDCONFIG

If you're running on the internal oscillator then you need to change from HSPLL to IRCIO.

The oscillator section of the datasheet indicates that the internal oscillator defaults to 4Mhz but looking at the initialization condition of the OSCCON register it seems like bits 4,5,6 are all zero on power up - which would give you 31kHz. If you then have the clock DEFINED to 8MHz your 500ms PAUSE will actually be 322 seconds....

So, without actually testing this I'd try something like

DEFINE OSC 8
OSCCON = %01110010 ' Internal oscillator, 8MHz

/Henrik.

Aussie Barry
- 23rd June 2012, 11:54
Thanks Henrik.

There is SO MUCH in the datasheet for this device!
Just when you think you have everything under control, something else raises its ugly head :)

I will try your configs and recommended changes over the next 24-48 hours and report back - hopefully with good news.

I went out this afternoon and bought a couple of crystals to play with - just in case. I now have 4, 8 and 20Mhz crystals to plug in, if and when required.

Cheers
Barry
VK2XBP

Aussie Barry
- 25th June 2012, 11:18
Hi Henrik,

I am pleased to advise that I now have a simple LCDOUT and LED Blink program operating as expected.
When I went through the '2431 datasheet regarding OSCON Register I selected OSCON = %0111110 which is somewhat different to what you suggested.
Both work but I am curious to know how, when and why bits 0-3 of the OSCON register should be used. Any comments?

My next stage of experimentation will be to get the serial LCD display working in the background using the HSEROUT statement. From there I will move onto ADCin to read a pot and follow up with the HPWM command.

Progress is slow but I am trying to understand "why" things are the way they are in conjunction with learning "how" to do things.

Cheers
Barry
VK2XBP

mackrackit
- 25th June 2012, 11:38
when and why bits 0-3 of the OSCON register should be used.
No different from any other chip, they are readable bits. Read if you want to know their state....

Aussie Barry
- 25th June 2012, 11:52
Oh dear....
I am really showing my ignorance

Thanks Dave.

HenrikOlsson
- 25th June 2012, 11:55
Hi Barry,
That's great news!

Bits 0 and 1 in the OSCCON register are the System Clock Select Bits. If you look at Figure 3-8 in the datasheet (http://ww1.microchip.com/downloads/en/DeviceDoc/39616d.pdf) (I link it here just to be sure we're looking at the same revision) you'll see in the upper right hand corner that they, together with the low four bits on CONFIH1H and via the MUX selects which of the various clock sources to use to "drive" the device. When bit 1 of OSCON is set the internal oscillator is selected and bit 0 of OSCON does not matter. You'll see the other options in the overview of the OSCCON register in page 36.

Bit 2 of OSCON (IOFS) is set by the device when the frequency of the internal oscillator block has stabilized, there should, AFAIK be no need for you to write that bit.

Bit 3 of OSCON (OSTS) is set by the device when the start-up timer for the primary oscillator (external x-tal, resonator or RC) has timed out and the oscillator can be considered running and stable. This is also a "read-bit", there should AFAIK be no need for you write this bit.

So, I wonder, didn't my suggested OSCCON=%01110010 work? In your message you say that you've set OSCCON to %0111110 which is only seven bits. I'm not sure if it's the forum messing up the format or if there's a typo?

Finally, you're probably not going to like this but I'm pretty sure that ADCIN doesn't work with the ADC on 18F2431. I know it didn't before and I can't remember seeing any updates regarding that. So, you'll have to resort to manually "running it". I believe Bruce has covered that too in one or more of his excellent posts so its shouldn't be too hard to figure out.

/Henrik.

mackrackit
- 25th June 2012, 12:07
Oh dear....
I am really showing my ignorance

Thanks Dave.
Not ignorance, just overwhelmed.
Do not be too hard on yourself.

Aussie Barry
- 25th June 2012, 12:25
Thanks Henrik.

I have downloaded the datasheet from your link - I have been working off a preliminary datasheet dated 2003...
At least we are on the same page now :)

Thanks also for the detailed explanation on OSCCON bits.

Just to clarify - I did mention in my previous post that both OSCCON setups worked. Also, there was a typo in my previous post; my setup was OSCCON = %01111110 (I left out one of the "1's")


ADCIN doesn't work with the ADC on 18F2431
Oh, man.... What a bummer!
Looks like I have more research to do on running it manually. I will do some searches and see what I can find from Bruce's previous posts.
I am almost too scared to ask why it doesn't work but I am sure as hell it caused some major grief for people in the past.

Cheers
Barry
VK2XBP

HenrikOlsson
- 25th June 2012, 15:41
I am almost too scared to ask why it doesn't work but I am sure as hell it caused some major grief for people in the past.
The reason is that the ADC is a bit different, and a bit more complicated, than on other chips. On the 18F2431 there are two sample and hold circuits so it can sample two analog voltages at the exact same time (and then convert them one after the other). It can also be setup to run continously in various configurations and put the results into a 4 word FIFO, an interrupt can be generated at various "fill levels" of the FIFO.

/Henrik.