18F2520 - blinking led

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
Colin Mac
Posts: 4
Joined: Tue Jul 15, 2008 6:51 pm

18F2520 - blinking led

Post by Colin Mac » Tue Jul 15, 2008 7:02 pm

Hi I want to blink a led to test out a programmer but this code didn't work.
can anyone see a problem?

Code: Select all

Device = 18F2520
Clock = 8
Config OSC = INTIO67                // Use the Internal Oscillator

Include "utils.bas"

Dim LED As PORTA.0                  // Assign an alias for "LED"

// Start Of Program...
OSCCON = %01111111                  // Sets up the internal oscillator
SetAllDigital                       // Make all Pins digital I/O's
Low(LED)                            // Make the LED pin an output and set it low

While True

    LED = 1                         // Turn on the LED
    
    DelayMS(500)                    // Delay for half a second
    
    LED = 0                         // Turn off the LED
    
    DelayMS(500)                    // Delay for half a second
    
Wend

xor
Posts: 286
Joined: Sun Nov 05, 2006 1:15 pm
Location: NYC
Contact:

Post by xor » Tue Jul 15, 2008 9:12 pm

Try adding this to the beginning of the program:

Code: Select all

Dim LED As LATA.0      // LATx is the port output register

TRISA = 254       // or %11111110   // sets RA0 as output

Doj
Posts: 362
Joined: Wed Apr 11, 2007 10:18 pm
Location: East Sussex

Post by Doj » Wed Jul 16, 2008 1:09 am

From past experience do not set the whole of OSCCON register when using INTOSC, address the internal oscilator bits individually, why? well it stops the pic running, and no I do not know why:-

Code: Select all

OSCCON.6=1
OSCCON.5=1
OSCCON.4=1
This will give you 8MHz, other settings in the data sheet, the default is 4MHz if you want to do tests and not have to change OSCCON at all.

Also do not assume the all digital call will actually set all types of modules on all chips to digital.
It is a routine in a library and may not cover the specific hardware in your chip, this is a routine that requires constant updates with device specific information, microchip are not particularly consistent in this area.
The data sheet is the only place to find specifics for your device and is the only way to guarantee to set the device in a known state, such as all inputs being digital.

User avatar
blueroomelectronics
Posts: 46
Joined: Mon Apr 23, 2007 3:48 pm
Location: Toronto
Contact:

Post by blueroomelectronics » Wed Jul 16, 2008 3:05 am

I use
OSCCON = $72 // 8MHz
works like a charm

Colin Mac
Posts: 4
Joined: Tue Jul 15, 2008 6:51 pm

Post by Colin Mac » Wed Jul 16, 2008 4:27 pm

Will someone modify my code so that it will work?

Doj
Posts: 362
Joined: Wed Apr 11, 2007 10:18 pm
Location: East Sussex

Post by Doj » Wed Jul 16, 2008 5:13 pm

Mmmm, in your original post you say you want to test the programmer.

All programmers handle the config fuses differently, and you have only chosen to alter the OSC from standard.

Look in the 18F2520.bas file in the code explorer and at the bottom it will show all available fuse options and what the default ones are in the hex file.
Compare this with the ones you programmer shows and you will see what the chip is actually being programmed with.

I have run the code in my simulator which does exactly what I would expect and flashes the LED, but the simulator does not emulate the internal oscilator and apears to work whether the all digital line is used or not so is really of little use to you.

I do not have this type of hardware so can not help much more.

User avatar
blueroomelectronics
Posts: 46
Joined: Mon Apr 23, 2007 3:48 pm
Location: Toronto
Contact:

Post by blueroomelectronics » Thu Jul 17, 2008 2:06 am

You'll also need to turn the WDT off (WDT = OFF) in the CONFIG line.

Your program should work fine.

Colin Mac
Posts: 4
Joined: Tue Jul 15, 2008 6:51 pm

Post by Colin Mac » Thu Jul 17, 2008 2:43 pm

It's working now with the RC oscillator in case anyone reads this. 22pF cap and 3.3K resistor.

Code: Select all

Device = 18F2520
Clock = 3
Config OSC = RC, WDT = OFF               

Include "utils.bas"

Dim LED As PORTB.7      

TRISB = 0        

              
SetAllDigital                       // Make all Pins digital I/O's
Low(LED)                            // Make the LED pin an output and set it low

While True

    LED = 1                         // Turn on the LED
    
    DelayMS(2000)                    // Delay 
    
    LED = 0                         // Turn off the LED
    
    DelayMS(2000)                    // Delay
    
Wend

Doj
Posts: 362
Joined: Wed Apr 11, 2007 10:18 pm
Location: East Sussex

Post by Doj » Thu Jul 17, 2008 4:32 pm

Good, what happens if you do this:-

Code: Select all

Device = 18F2520 
Clock = 4 
Config OSC = INTIO67, WDT = OFF  
Providing the INTOSC is 4MHz by default it should work exactly as your current setting.
Also the fuses and their settings are shown in the 18F2520.bas, its a good idea to bring these into your code and set all of them to your chosen value rather than just rely on defaults, you then get used to what is in the chip and how different individual chips can be.

Colin Mac
Posts: 4
Joined: Tue Jul 15, 2008 6:51 pm

Post by Colin Mac » Thu Jul 17, 2008 5:24 pm

that works but its more like 6 seconds.

So I tried to set OSSCON to 72h and I got an error. END expected?

Code: Select all

Device = 18F2520
Clock = 8
Config OSC = INTIO67, WDT = OFF
Include "utils.bas"

OSCCON = $72
             
Dim LED As PORTB.7      

TRISB = 0        
PORTB = 0
              
SetAllDigital                       // Make all Pins digital I/O's
Low(LED)                            // Make the LED pin an output and set it low

While True

    LED = 1                         
    DelayMS(2000)                    
    LED = 0                        
    DelayMS(2000) 

Wend                  

Doj
Posts: 362
Joined: Wed Apr 11, 2007 10:18 pm
Location: East Sussex

Post by Doj » Thu Jul 17, 2008 5:50 pm

The error is caused by the position of OSCCON, and user executable code must come after you dimension variables, as you have done with tris.

When you say "its more like six seconds", that means very little, is it measured at 6 seconds or is it 4 or 8 seconds, this would make more sense.

If you set the clock to 8Mhz(this tells the compiler what to use as its calculations, it DOES NOT do anything to the PIC registers) and the OSSCON bits 4,5 and 6 to 1 then the 2 seconds should be exactly that.

Post Reply