PDA

View Full Version : Moving on to 18F series.



Tina10
- 31st August 2012, 15:06
I do not know if this has been discussed before but I could not find much information about it. I worked on 16F877A for sometime and know about setting fuses, initial registers, setting digital/analog i/o etc etc. I am having big problems understanding the setting up of 18F4550. Are there any guidance/notes anywhere which can make it a bit easier to understand for not so technically inclined. Datasheet seems to be a bit complicated at this stage. Thanks in advance.

Demon
- 1st September 2012, 00:04
Click on ADVANCED SEARCH at the top right of the forum.

- enter 18F4550 and CONFIG as keywords.
- select ANY DATE and AND NEWER in FIND POSTS
- click SEARCH NOW

There's a bunch of threads that discuss this issue, like this one:
http://www.picbasic.co.uk/forum/showthread.php?t=14169&highlight=18F4550+config

Robert

Tina10
- 1st September 2012, 18:43
Thanks. Found some code relating to that.


DEFINE OSC 48
@ __CONFIG _CONFIG1L, _PLLDIV_1_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
@ __CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H
@ __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_512_2H
@ __CONFIG _CONFIG3H, _PBADEN_OFF_3H & _MCLRE_OFF_3H
@ __CONFIG _CONFIG4L, _LVP_OFF_4L & _ICPRT_OFF_4L &_XINST_OFF_4L

Opened the datasheet and tried hard so something enters my thick brain but :(
I want to ask:
What does _PLLDIV_1_1L mean? There are 3 fields PLLDIV0, PLLDIV1, PLLDIV2. If a 4MHz OSC is used, I guess these fields should be 000. What is PLL anyways? What is the difference between XT and XTPLL? If I am using XT OSC and a 4MHz crystal/resonator, and choose CPUDIV0/1 to be 00, does that mean the PIC will work at 4MHZ & NOT 4/4=1MHZ? If I do not want to use USB then what should be the setting? If I want to have an 8MHz oscillator which field should I change? Why on top it says DEFINE OSC 48 when the author used a 4MHz oscillator with the above settings?

I have many questions like this popping in my mind at this time, all I found is code but no explanation attached and even with datasheet, nothing is making perfect sense.

mackrackit
- 1st September 2012, 19:24
Take a look at the samples here
http://www.picbasic.co.uk/forum/showthread.php?t=5806&p=68359#post68359
and look at section 2.3 in the data sheet.

PLL = Phase Lock Loop.

By using the PLL you can use a low speed external OSC and have the chip run at a higher speed. This is needed for USB but can also be used with out USB.

Demon
- 1st September 2012, 21:06
... Why on top it says DEFINE OSC 48 when the author used a 4MHz oscillator with the above settings?

I have many questions like this popping in my mind at this time, all I found is code but no explanation attached and even with datasheet, nothing is making perfect sense.


Very good question Tina. I wish I could give you a perfect answer 'cause I don't know myself without doing extensive SEARCHes on the forum. Here is a working example I use, as you can see, I leave myself a LOT of comments because I quickly forget what does what:


'************************************************* **************************
'* Name : USBLabX1_RH.pbp *
'* Author : Steve Monfette/Darrel Taylor/Demon *
'* Date : Jan 11 2012 *
'* Version : 3.0 *
'* Notes : This is a re-creation of mister-e's USBdemo for DT_HID *
'* : Meant to work with mister-e's GUI *
'* : Modified for Lab X1 *
'* Hardware : PIC 18F4550, 20mhz crystal *
'* : Lab X1 Experimental Board *
'* : MeLabs U2 Programmer v4.32 *
'* Software : PIC Basic Pro v2.60C *
'* : MicroCode Studio Plus v2.2.1.1 *
'* : MPASM WIN Assembler v4.02 (mplab tools/MPASM Suite) *
'* PIC mods : plastic USB connector hot-glued onto top of PIC *
'* : wires soldered directly to top of pins *
'* : two 0.1uF ceramic caps in parallel across VUSB and VSS *
'* : D- wired to C4 (pin 23) *
'* : D+ wired to C5 (pin 24) *
'************************************************* **************************
'--- if you use these, you must comment the ones in the .inc file ---
@ __CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
@ __CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H
@ __CONFIG _CONFIG2L, _PWRT_OFF_2L & _BOR_ON_2L & _BORV_46_2L & _VREGEN_ON_2L
@ __CONFIG _CONFIG2H, _WDT_OFF_2H
@ __CONFIG _CONFIG3H, _CCP2MX_ON_3H & _PBADEN_OFF_3H & _LPT1OSC_OFF_3H & _MCLRE_ON_3H
@ __CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L

DEFINE OSC 48

' Using only 4 bit bus to control LCD
DEFINE LCD_DREG PORTD ' Set LCD data port
DEFINE LCD_DBIT 4 ' Set starting data bit
DEFINE LCD_RSREG PORTE ' Set LCD register select port
DEFINE LCD_RSBIT 0 ' Set LCD register select bit
DEFINE LCD_EREG PORTE ' Set LCD enable port
DEFINE LCD_EBIT 1 ' Set LCD enable bit
DEFINE LCD_BITS 4 ' Set LCD bus size
DEFINE LCD_LINES 2 ' Set number of lines on LCD
define LCD_COMMANDUS 2000 ' Set command delay time in microseconds
DEFINE LCD_DATAUS 50 ' Set data delay time in microseconds

;--- Setup Interrupts ------------------------------------------------------
INCLUDE "DT_INTS-18.bas" ; Base Interrupt System
INCLUDE "ReEnterPBP-18.bas" ; PBP Re-entry for external interrupt

ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler USB_Handler
INT_Handler INT_INT, _ToggleLED1, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
endasm

@ INT_ENABLE INT_INT ; enable external (INT) interrupts

;--- Setup USB -------------------------------------------------------------
INCLUDE "DT_HID260.pbp"

DEFINE USB_VENDORID 6017
DEFINE USB_PRODUCTID 2000
DEFINE USB_VERSION 1
DEFINE USB_VENDORNAME "Mr E/DT/RH"
DEFINE USB_PRODUCTNAME "USBLabX1"
DEFINE USB_SERIAL "001"
DEFINE USB_INSIZE 8 ; IN report is PIC to PC (8,16,32,64)
DEFINE USB_OUTSIZE 8 ; OUT report is PC to PIC
DEFINE USB_POLLIN 10 ; Polling times in mS, MIN=1 MAX=10
DEFINE USB_POLLOUT 10

; --- Each USB status LED is optional, comment them if not used ------------
; --- They can be assigned to any pin, and no further action is required ---
DEFINE USB_LEDPOLARITY 1 ; LED ON State [0 or 1] (default = 1)
DEFINE USB_PLUGGEDLED PORTD,0 ; LED indicates if USB is connected
DEFINE USB_TXLED PORTD,1 ; " " data being sent to PC
DEFINE USB_RXLED PORTD,2 ; " " data being received from PC

;--- Setup ADC -------------------------------------------------------------
DEFINE ADC_BITS 8 ; Number of bits in ADCIN result

TRISA = %00000011 ' Set port A pins to 6 output, 2 input
TRISB = %00001111 ' Set port B pins to 4 output, 4 input
' B0 = External Interrupt
TRISC = %00000000 ' Set all port C pins to output
TRISD = %00000000 ' Set all port D pins to output
TRISE = %00000000 ' Set all port E pins to output
;OUTPUT PORTC.1
;OUTPUT PORTC.2
;--- Initialize ------------------------------------------------------------
;CCPR1L = 0
;CCPR2L = 0
;CCP1CON = %00001100 ' CCP1, PWM mode
;CCP2CON = %00001100 ' CCP2, PWM mode
;PR2 = 249 ' 0-1000 duty range
;T2CON = %00000101 ' TMR2 on, prescaler 1:4

ADCON2.7 = 0 ; left justify (Change this if ADFM in diff register)
ADCON1 = %00001101 ; A/D Control
' Bit 7, 0 = n/a
' Bit 6, 0 = n/a
' Bit 5, Vref- Source
' 0 = VSS
' 1 = Vref- (AN2)
' Bit 4, Vref+ Source
' 0 = VSS
' 1 = Vref+ (AN3)
' Bit 3-0 1111 = All digital
' 1110 = All digital, AN0 analog
' 1101 = All digital, AN0 & AN1 analog

INTCON2 = %00000000
' Bit 7, 0 = enabled, Pull-up resistors
' Bit 6, 0 = falling edge, B0 interrupt
' Bit 5, 0 = falling edge, B1 interrupt
' Bit 4, 0 = falling edge, B2 interrupt
' Bit 3, 0 = n/a
' Bit 2, 0 = low priority, TMR0 interrupt
' Bit 1, 0 = n/a
' Bit 0, 0 = low priority, Port B on-change

;--- Variables -------------------------------------------------------------
Value0 VAR WORD : Value0 = 0
Value1 VAR WORD : Value1 = 0
DUTY1 VAR WORD
DUTY2 VAR WORD
'Old_PORTA VAR BYTE
'New_PORTA VAR BYTE
LED1 VAR PORTD.3
WS_CHAR VAR BYTE
WS_BYTE VAR BYTE
WS_LOOP VAR WORD

PORTB = %00000000 ' Set output on keypad
' to prevent floating pins

FOR WS_LOOP = 0 to 1000 ' Wait for PIC and LCD to stabilize
PAUSE 1
NEXT WS_LOOP
' LCDOUT $FE,1,"Initializing..."
' PORTD = 0 ' Clear LEDs from LCD operations
' FOR WS_LOOP = 0 to 500 ' Wait for LCD to initialize
' PAUSE 1
' NEXT WS_LOOP

LCDOUT $FE,1,"LabX1 18F4550 USB"
FOR WS_LOOP = 0 to 100 ' Short delay to process LCD operation
PAUSE 1
NEXT WS_LOOP
PORTD = 0 ' Clear LEDs from LCD operations

ARRAYWRITE USBTXBuffer, ["Lab X1 "]
IF Plugged THEN GOSUB DoUSBOut ; only send when USB is connected
FOR WS_LOOP = 0 to 500 ' Short delay to process USB operation
PAUSE 1
NEXT WS_LOOP
ARRAYWRITE USBTXBuffer, [Value0, Value1, PORTD.3, 0, 0, 0, 0, 0]
IF Plugged THEN GOSUB DoUSBOut ; only send when USB is connected

;--- The Main Loop ---------------------------------------------------------
Main:
FOR WS_LOOP = 0 to 1000 ; Check for incomming USB data while waiting
@ ON_USBRX_GOSUB _HandleRX
PAUSE 1
NEXT WS_LOOP

ADCIN 0, Value0 ; Send A/D about once/sec
ADCIN 1, Value1
ARRAYWRITE USBTXBuffer, [Value0, Value1, PORTD.3, 0, 0, 0, 0, 0]
IF Plugged THEN GOSUB DoUSBOut ; only send when USB is connected

GOTO Main
end

;---- Receive incoming data PORTB LEDS and CCP PWM dutycycle ---------------
HandleRX:
ARRAYREAD USBRXBuffer,[WS_BYTE, DUTY1.LowByte, DUTY1.HighByte, _
DUTY2.LowByte, DUTY2.HighByte]
LCDOUT $FE,$C0,BIN WS_BYTE
FOR WS_LOOP = 0 to 100 ' Short delay to process LCD operation
PAUSE 1
NEXT WS_LOOP
PORTD = 0 ' Clear LEDs from LCD operations
; CCP1CON.5=DUTY1.1 ' load CCP1 duty value
; CCP1CON.4=DUTY1.0 ' with Duty1
; CCPR1L=DUTY1>>2 '
;
; CCP2CON.5=DUTY2.1 ' load CCP2 duty value
; CCP2CON.4=DUTY2.0 ' with Duty2
; CCPR2L=DUTY2>>2 '
return

'---[INT - interrupt handler]---------------------------------------------------
ToggleLED1:
TOGGLE LED1
ARRAYWRITE USBTXBuffer, ["Button "]
IF Plugged THEN GOSUB DoUSBOut ; only send when USB is connected
PORTB = 0 ' Set output on keypad to prevent floating pins
FOR WS_LOOP = 0 to 250 ' Very basic debounce for keypress
PAUSE 1
NEXT WS_LOOP
@ INT_RETURN


I hope the formattting doesn't get all screwed up.

Robert


EDIT: This code is 99.9% stable. USB is a bit unstable because I am using external power source, PIC datasheet explains how to wire properly. I didn't have proper parts, I just wanted to have "something" that worked so I could get CONFIG set up and PIC would talk with PC.

PWM stuff is commented out, I didn't need it. Extra displays to LCD also commented out (I was debugging USB problem).

Small debounce loop at bottom is not a good idea, I will rewrite that later. PAUSE could "potentially" screw up USB interrupt, I think.

Darrel Taylor
- 2nd September 2012, 18:45
Long pauses are OK when you are using DT_HID260.pbp.

Fujimanme
- 4th September 2012, 05:50
The best advice is to download the latest data sheet for your PIC, grab a highlighter, check what features are available.
Then check the appropriate section, and change the registers as needed.
For example, your 18F4550 has a potential 13 ADC Ports.
If you look on pg 262, of the current data sheet, ADCON1 = $F '(%00001111) setting will turn all your ADC ports into Digital.

Unfortunately I don't know of any list that will clearly show all available.

If you are having difficulties, post your code, and someone can point you in the right direciton.

I do not know if this has been discussed before but I could not find much information about it. I worked on 16F877A for sometime and know about setting fuses, initial registers, setting digital/analog i/o etc etc. I am having big problems understanding the setting up of 18F4550. Are there any guidance/notes anywhere which can make it a bit easier to understand for not so technically inclined. Datasheet seems to be a bit complicated at this stage. Thanks in advance.