Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC 18F1320 problems in assembly!

Status
Not open for further replies.
Hey there,

I received a couple of PIC 18F1320, and I'm continuing to do ASM inside because the Pic Kit 2 Demo board is quite limited, so I expanded it by using a full bread board instead and with 8 LEDs each with a current limiting resistor.

However, there's a very strange issue: I'm trying to test all of the ports whether is it working or not, but when I tried to turn on all 7 LEDs or 8 LEDs, the RB5's LED is always off. The RB5's all on when I tried to turn on below 7 LEDs.

And when I import the compiled HEX file to the Pic Kit 2 programmer, it ended up shouting "Warning: No configuration words in Hex file, In MPLAB, use File-Export to save hex with config".

How am I going to 'port' from 16F690 to 18F1320? I've read the documents, but none of them seem to give me ideas. Guidances perhaps? :)

I'm now familiar with teh 16F690, can do some bank switch and some simple functions. But now, I have to move to 18F1320 because I have these only. I hope their mechanisms are all the same anyway.
 
Last edited:
hi,
Do you have the full assembler code that you could post.?:)
 
hi,
Do you have the full assembler code that you could post.?:)

Ok, actually this one I ripped out from my 16F690 practise sheets. I took out all the CONFIG flags and let the MPLAB determine for me.

Code:
cblock 0x20
Delay1              ; Assign an address to label Delay1
Delay2
endc

Main:
clrf    LATB
clrf    WREG
clrf    TRISB

Loop:
movlw    b'11111111'
movwf    LATB
call    Delay
movlw   b'00001111'
movwf    LATB
call    Delay
goto    Loop

Delay:     
     ;movwf     Delay2         ;
DelayLoop:
     decfsz    Delay1,f       ; Waste time.  
     goto      DelayLoop      ; The Inner loop takes 3 instructions per loop * 256 loopss = 768 instructions
     decfsz    Delay2,f       ; The outer loop takes and additional 3 instructions per lap * 256 loops
     goto      DelayLoop      ; (768+3) * 256 = 197376 instructions / 1M instructions per second = 0.197 sec.
                              ; call it a two-tenths of a second.
     return
The very strange thing is, it's not a need to initialize the ports anymore, especially the BSF STATUS, RP0 and stuff like that.

In MPLAB C Compiler, I can get all the LEDs to light up using the LATB, but in any of the other languages liek ASM, it's like the PIC18F1320 totally ignored my request when I put "movlw b'11111111', movwf PORTB". And they behaved eratically. :p

And of course, these rows of LEDs didn't change. It just got stuck there, while doing the same in the PIC16F690, I can see it's working. :)
 
Last edited:
Hi,
You still have to initialize the port by enabling/disabling the analog and port direction (input or output). You don't have to do bank select as you can access to all of the SFR with access bank. You can't let MPLAB to determine the configuration for you, just set to whatever that suit to your application. Example of the configuration, you can do like this:
Code:
    __config    _CONFIG1H, _INTIO2_OSC_1H
    __config    _CONFIG2L,  _PWRT_ON_2L
    __config    _CONFIG2H, _WDT_OFF_2H
    __config    _CONFIG3H, _MCLRE_OFF_3H
    __config    _CONFIG4L, _LVP_OFF_4L
or like this:
Code:
    CONFIG    OSC = INTIO2, PWRT = ON, WDT = OFF, MCLRE = OFF, LVP = OFF
Read the datasheet for the detail of the configuration settings under section 19: special features of the CPU, page 171.
 
The ADCON1 reg controls the ability of ports to be analog or digital eg.

Code:
movlw    B'01111111'        ; Set all analog ports to digital only (both porta & portb)
movwf    ADCON1
You will need to read the manual to see which ADCON bits to set if you want a mixture of analog and digital ports.
 
Ok, got all of the LEDs turned on as a start.

But strange, the Delay functions I took from the PIC Kit 2 Demoboard as a start with a 0.2 sec delay has surprisingly changed. i don't know why, maybe it's the oscillator being at 40MHz instead of the PIC Kit's 2 Demoboard 4Mhz. :)
 
I was just giving examples :)
If you're using internal oscillator, then you'll have to select the oscillation frequency via OSCCON register, IRCF2:IRCF0. See page 17 of the datasheet for detail.
 
Oh, I'm using an internal oscillator, as I've given this "CONFIG OSC = INTIO2" before the main program loop. :)
bananasiong said:
If you're using internal oscillator, then you'll have to select the oscillation frequency via OSCCON register, IRCF2:IRCF0. See page 17 of the datasheet for detail.
As bananasiong says, be sure to kick the osc speed up from the very, very slow default clock rate. Default is 31kHz. That caught me out when I was starting with the chip. Things that I wrote assuming 8MHz ran so slowly that I thought my program wasn't working. :p
 
I was just giving examples :)
If you're using internal oscillator, then you'll have to select the oscillation frequency via OSCCON register, IRCF2:IRCF0. See page 17 of the datasheet for detail.

Oh I'm reading the page 17. However, how am I going to implement the 'setting of the bits' in the OSCCON register into the program code? Is it something like the 'BCF STATUS, RP0' (for example in the STATUS register manipulation) thingy in the PIC16F? :)
 
Hmm.. don't you know, all the registers are 8-bit register. From the datasheet, each bit of OSCCON are:
bit 7 IDLEN: Idle Enable bits
1 = Idle mode enabled; CPU core is not clocked in power managed modes
0 = Run mode enabled; CPU core is clocked in Run modes, but not Sleep mode
bit 6-4 IRCF2:IRCF0: Internal Oscillator Frequency Select bits
111 = 8 MHz (8 MHz source drives clock directly)
110 = 4 MHz
101 = 2 MHz
100 = 1 MHz
011 = 500 kHz
010 = 250 kHz
001 = 125 kHz
000 = 31 kHz (INTRC source drives clock directly)
bit 3 OSTS: Oscillator Start-up Time-out Status bit
1 = Oscillator Start-up Timer time-out has expired; primary oscillator is running
0 = Oscillator Start-up Timer time-out is running; primary oscillator is not ready
bit 2 IOFS: INTOSC Frequency Stable bit
1 = INTOSC frequency is stable
0 = INTOSC frequency is not stable
bit 1-0 SCS1:SCS0: System Clock Select bits
1x = Internal oscillator block (RC modes)
01 = Timer1 oscillator (Secondary modes)
00 = Primary oscillator (Sleep and PRI_IDLE modes)
Note 1: Depends on state of the IESO bit in Configuration

so for the frequency selection, say 8MHz, you just have to set bit 6 to bit 4 (or IRCF2:IRCF0) to 111, which can be made like this:
movlw 0x70 ; or b'01110000'
movwf OSCCON
 
Hmm.. don't you know, all the registers are 8-bit register. From the datasheet, each bit of OSCCON are:


so for the frequency selection, say 8MHz, you just have to set bit 6 to bit 4 (or IRCF2:IRCF0) to 111, which can be made like this:
movlw 0x70 ; or b'01110000'
movwf OSCCON

Ok, got it already. But not sure about the other bits than the bit 4-6, maybe I'll leave them to zero if I have to.

Sorry that I'm still learning this PIC, so bear with me for a moment. Quite tough stuff in ASM. ;)
 
Just take your time, it's good for learning ASM, IMO (I'm not trying to start programming language war :D)
Some other bits are read only, it won't affect anything for read only bits.
 
Just take your time, it's good for learning ASM, IMO (I'm not trying to start programming language war :D)
Some other bits are read only, it won't affect anything for read only bits.

Yeah I know - ASM is totally crazy and new for me, who has already did C/C++ for 2 semesters. I have to go back to square one in ASM too.

And worse, my next sem's course isn't pertaining PIC - it's the 68HC11. it could be a torture since, 68HC11 is all totally different.

In the meantime I got the code working as it should be. Thanks. :)
 
Last edited:
And worse, my next sem's course isn't pertaining PIC - it's the 68HC11. it could be a torture since, 68HC11 is all totally different.
Not torture at all. :D Motorola (Freescale) CPU's are really nice to program. You'll just have to get used to having more (and better) commands available (the HC's are CISC, not RISC), and of course learn a different architecture and memory map.
 
Not torture at all. :D Motorola (Freescale) CPU's are really nice to program. You'll just have to get used to having more (and better) commands available (the HC's are CISC, not RISC), and of course learn a different architecture and memory map.

Hehe... Well, I'm ordering the 68HC11 training kit, since my college seems to be running short of these programmers, and there's a ton of students who will be fighting to use them, that's why I have to get that one.

And I'm killing two birds in one stone, PIC and 68HC11... :)
 
It seems lots of the university/college use 68HC11, I don't like it after getting myself into PIC.
That's because, though they're kinda comfy once you get used to them, PICs are freaky strange compared to "normal" CPUs. By learning PIC first, you learned "bad" habits. The PIC way of doing things works great for what they do, but it's quite different from other CPUs (PIC is Harvard arch - virtually no (user accessible) stack - severely limited addressing modes - etc.). IMHO, most programmers would be better off starting out programming a PC or something else before learning the PIC way and thinking it's the norm.
 
It seems lots of the university/college use 68HC11, I don't like it after getting myself into PIC.

That's because they all use antique equipment rather than upgrade to anything remotely modern, India seems to still be stuck using 8051's and such, and a lot of America seems to still have 68HC11 kit - which was (and is) a fine processor.

Really is makes very little difference, programming is programming, and it's the techniques rather than specific devices you're supposed to be learning.

Where it does make a difference is asking for help, where there's far more PIC and AVR support out there - but really they should pay attention in class, work hard, and not need to ask for help :D
 
Status
Not open for further replies.

Latest threads

Back
Top