PDA

View Full Version : 18F25J11 RTCC & remappable pin options



Bruce
- 25th January 2010, 21:02
I have received several emails asking for PBP code examples for using some of the newer 18F series with independent I/O mapping of onboard hardware peripherals, and for working with the new built-in RTCC hardware, so I'm posting a simple example here showing how to use the RTCC, and how to assign PWM to several pins.

These newer PICs that allow you to assign onboard peripherals like hardware PWM, USART, and a lot more, to different pins, are really nifty, and a lot like using the higher-end 24 series.


'************************************************* ***************
'* Name : TST_25J11_RTC.BAS *
'* Author : B. Reynolds *
'* Notice : Copyright (c) 2009 http://www.Rentron.com *
'* : All Rights Reserved *
'* Date : 1/25/2010 *
'* Version : 1.0 *
'* Notes : Testing 18F25J11 RTCC & remappable pin options. *
'* : *
'************************************************* ***************

' NOTE: CONFIG options below are placed in the PBP 18F25J11.INC file with
' default config options commented out.

' CONFIG OSC=INTOSC, LPT1OSC=OFF, CP0=OFF, WDTEN=ON, XINST=OFF, IOL1WAY=OFF
' CONFIG STVREN=OFF, T1DIG=ON, FCMEN=OFF, WDTPS=512, RTCOSC=T1OSCREF

' Connect a 32.768 kHz watch crystal between pins 11 & 12 T1CKI & T1OSI
' with caps to match your crystal load capacitance.
' 11 12
' |---||---|
' | xtal |
' _|_ _|_
' ___ ___ 2 x 12pF caps
' | |
' gnd gnd

DEFINE DEBUG_REG PORTB
DEFINE DEBUG_BIT 3 ' RB3 sends serial data to PC
DEFINE DEBUG_BAUD 19200 ' 19200 bps
DEFINE DEBUG_MODE 0 ' 1 = inverted, 0 = true

DEFINE OSC 8

X VAR BYTE
Hours VAR BYTE
Minutes VAR BYTE
Seconds VAR BYTE
Year VAR BYTE
Month VAR BYTE
Day VAR BYTE
Weekday VAR BYTE
Duty VAR WORD

OSCCON = %01110000 ' using 8MHz internal osc

' // A/D disable & setup Timer1 for RTCC //
ANCON0 = $ff
ANCON1 = $1f ' disable A/D on all analog pins
T1CON = %10001001 ' external xtal, 1:1 prescale, no synch, 8-bit, timer1 on

' // PWM & port setup //
Duty = 400 ' 50-50 PWM duty-cycle
T2CON = %00000100 ' timer2 on, 1:1 prescale
PORTA = 0
PORTB = 8 ' RB3 debug pin idles high
PORTC = 0 ' PWM outputs low at POR
TRISA = 0
TRISB = 0
TRISC = 0
CCP1CON = %00001100 ' setup for single-ended PWM output
PR2 = 199 ' setup for 10kHz
PSTR1CON = %00000001 ' P1A is driven by PWM
CCP1CON.4 = Duty.0 ' load 10-bit duty cycle into
CCP1CON.5 = Duty.1 ' duty registers
CCPR1L = Duty >> 2

' // re-map PWM output to RA0, RC4 and RC3 pins //
RPOR0 = 14 ' RA0, RP0 = P1A output
RPOR15 = 14 ' RC4. RP15 = P1A output
RPOR14 = 14 ' RC3, RP14 = P1A output

' // set initial time/date at POR //
GOSUB WRT_Enable ' run unlock sequence to enable RTCC writes
GOSUB LoadTime ' setup RTCC time/date
GOSUB WRT_Disable ' disable RTC writes after initial time & date setup

Main:
RTCCFG = 3 ' set pointer to Year
Year = RTCVALL ' read Year
RTCCFG = 2 ' set pointer to Month & Day
Day = RTCVALL ' read Day
Month = RTCVALH ' read Month & decrement pointer
Hours = RTCVALL ' read Hours
Weekday = RTCVALH ' read Weekday & decrement pointer
Seconds = RTCVALL ' read Seconds
Minutes = RTCVALH ' read Minutes (pointer already at 0)
PAUSE 990

' // date/time serial output format //
' Time: 03:33:00
' Monday:
' Date: 01:25:2010

' // display Hours, Minutes, Seconds //
DEBUG "Time: ",DEC (Hours>>4),DEC Hours & $0F,":", DEC (Minutes>>4),_
DEC Minutes & $0F,":", DEC (Seconds>>4),DEC Seconds & $0F,13,10

' // display day of week //
SELECT CASE Weekday
CASE 0
DEBUG "Sunday: ",13,10
CASE 1
DEBUG "Monday: ",13,10
CASE 2
DEBUG "Tuesday: ",13,10
CASE 3
DEBUG "Wednesday: ",13,10
CASE 4
DEBUG "Thursday: ",13,10
CASE 5
DEBUG "Friday: ",13,10
CASE 6
DEBUG "Saturday: ",13,10
END SELECT

' // display Month, Day, Year //
DEBUG "Date: ",DEC2 Month & $1F,":", DEC (Day>>4), DEC Day & $0F,":",_
"20",DEC (Year>>4), DEC Year & $0F,13,10,13,10,13,10

GOTO Main

LoadTime: ' // NOTE: time & date is in BCD format //
RTCCFG = %00100011 ' set pointer to Year
RTCVALL = %00010000 ' set Year to 2010
RTCCFG = %00100010 ' set pointer to Month & Day
RTCVALL = %00100101 ' set Day to the 25rd
RTCVALH = 1 ' set Month to Jan (decrements pointer to Weekday & Hours)
RTCVALL = %00000011 ' set Hour to 3
RTCVALH = 1 ' 0=Sun, 6=Sat. set Weekday (decrements pointer to Minutes & Seconds)
RTCVALL = 0 ' set Seconds to 0
RTCVALH = %00110011 ' set Minutes to 33
RTCCFG.7 = 1 ' RTCC enabled
RETURN

WRT_Enable: ' enable writes to RTCC registers
EECON2 = $55 ' unlock sequence
EECON2 = $AA '
RTCCFG.5 = 1 ' write enable RTCC register
RETURN

WRT_Disable: ' disable writes to RTCC registers
EECON2 = $55
EECON2 = $AA
RTCCFG.5 = 0
RETURN

END
This shows how to re-map PWM output to RA0, RC4 and RC3 pins, and use the built-in
RTCC clock functions. These PICs are definitely worth looking at, so have fun...;o)

It does take a bit of time to learn how to use these guys, but it's very cool. You can re-
map almost every onboard hardware peripheral to work on any pin.

rmteo
- 28th January 2010, 18:10
Yes, these newer PIC18's are really nice and they do give you a small hint as to the power and flexibility of the 24-bit core devices. Pity, they do not get more interest here.

Bruce
- 28th January 2010, 19:11
I'm waiting on a few 24 samples. Figured I would give them a whirl.

rmteo
- 28th January 2010, 23:57
I started using PIC24/dsPIC33 (in particular the PIC24F's for their rich peripheral set) 2 years ago and am really pleased with their power and flexibility - they are pretty much all I use these days. I have used PIC18's occasionally due to the excellent Swordfish compiler which isn't available yet for the 24-bit core devices. They only time I used a PIC16 in the last 2 years was when the app called for a small physical package and settled on an 8-pin MSOP.

Bruce
- 28th January 2010, 23:59
Which compiler are you using for the PIC24F's ?

rmteo
- 29th January 2010, 00:11
If you wan't to stay with BASIC (like me), I believe your only option for now is mikroBASIC Pro for dsPIC. Otherwise, there is the free C30 compiler from MicroCHIP. Also, Dave Barker says that he is working on a 24-bit version of Swordfish.

Bruce
- 29th January 2010, 00:16
I have the free version of C30 - so I'll probably start with that. How do you like the
mikroBASIC ?

I actually prefer C to BASIC. Do they have a version of C that supports this PIC? And,
do you consider it better than C30?

rmteo
- 29th January 2010, 00:26
I have been using mikroBASIC since I started with the PIC24/dsPIC33. mikroBASIC is closer to C than most any BASIC out there - with the exception of Swordfish. Realistically, there is nothing you can't do with mikroBASIC that can be done with C. There is a mikroC for dsPIC but I cannot comment on it as I am not a C user.

Bruce
- 29th January 2010, 04:00
Thanks for the feedback. I did try mikroBASIC several years back, and when comparing the
code it produced to PBP & Proton, it totally sucked. Especially compared to Proton.

Perhaps I should try it again, but I really do prefer C to BASIC - so I might give the mikroC
compiler another look. That one wasn't exactly up to par the last time I tried it either.

Swordfish totally rocks, but I'm a tad concerned about it being supported in the near
future, and it's really depressing that it only supports the 18F series & needs that USB
dongle.

My work requires me to get things out like yesterday, and I can't/won't pay full
boat for a replacement dongle in a pinch .. when it craps-out.

Proton rocks too, but I'm not into installing beta software, that's a real pain in the
pooter, for any compiler that hasn't offered registerd users an upgrade for many years.

That really killed Proton here in the US.

rmteo
- 21st February 2010, 01:12
I have been working with the NXP LPC11xx parts and they look really nice. A 32-bit part that is cheaper than most 8-bit PIC devices (as low as $0.65/10,000 for a 8kB/2kB/33-pin part). Some basic specs:

# ARM Cortex-M0 processor, running at frequencies of up to 50 MHz and 48DMips
# ARM Cortex-M0 built-in Nested Vectored Interrupt Controller (NVIC)
# 32 kB (LPC1114), 24 kB (LPC1113), 16 kB (LPC1112), or 8 kB (LPC1111) on-chip flash programming memory
# 8 kB, 4 kB, or 2 kB SRAM
# In-System Programming (ISP) and In-Application Programming (IAP) via on-chip bootloader software
# UART with fractional baud rate generation, internal FIFO, and RS-485 support
# Two SPI controllers with SSP features and with FIFO and multi-protocol capabilities (second SPI on LQFP48 and PLCC44 packages only)
# I2C-bus interface supporting full I2C-bus specification and Fast-mode Plus with a data rate of 1 Mbit/s with multiple address recognition and monitor mode
# Up to 42 General Purpose I/O (GPIO) pins with configurable pull-up/pull-down resistors
# Four general purpose 16/32-bit timers/counters with a total of four capture inputs and 13 match/PWM outputs
# Programmable WatchDog Timer (WDT)
# System tick timer
# Serial Wire Debug
# High-current output driver (20 mA) on one pin
# High-current sink drivers (20 mA) on two I2C-bus pins in Fast-mode Plus
# Integrated PMU (Power Management Unit) to minimize power consumption during Sleep, Deep-sleep, and Deep power-down modes
# Three reduced power modes: Sleep, Deep-sleep, and Deep power-down
# Single 3.3 V power supply (1.8 V to 3.6 V)
# 10-bit ADC (400ks/S) with input multiplexing among 8 pins
# GPIO pins can be used as edge and level sensitive interrupt sources
# Clock output function with divider that can reflect the system oscillator clock, IRC clock, CPU clock, and the Watchdog clock
# Processor wake-up from Deep-sleep mode via a dedicated start logic using up to 13 of the functional pins
# Brownout detect with four separate thresholds for interrupt and one threshold for forced reset
# Power-On Reset (POR)
# Crystal oscillator with an operating range of 1 MHz to 25 MHz
# 12 MHz internal RC oscillator trimmed to 1 % accuracy that can optionally be used as a system clock
# PLL allows CPU operation up to the maximum CPU rate without the need for a high-frequency crystal. May be run from the main oscillator, the internal RC oscillator, or the watchdog oscillator.
The software dev tools are C based but do come free with the purchase of the $30 LPC1114 LPCXpresso board. With this move, I am considering moving to MPLAB and C18/C30 and possibly abandoning BASIC altogether.

EE12212
- 6th October 2010, 16:24
hello
thanks bruce for this code!

i have a question for you.. if i want to change the frequency to 500Hz.. i do..

T2CON = %00000110 ' TIMER 2 ON/ PRESCALE 1:16
PR2 = 250 ' SETUP FOR 500HZ

but if i want to change to 100HZ i can't.. because prescale is in max (max=16) and PR2 is almost on max(max=255)..
how i could work with small frequencies?

best regards

Pedro Rodrigues

rmteo
- 30th October 2010, 19:49
but if i want to change to 100HZ i can't.. because prescale is in max (max=16) and PR2 is almost on max(max=255)..

how i could work with small frequencies?
You can't - this is a limitation of the CCP module which has only an 8-bit period register. This table is from the data sheet. With an 8MHz clock, the lowest frequency is 488Hz.

4898

Darrel Taylor
- 30th October 2010, 20:29
but if i want to change to 100HZ i can't.. because prescale is in max (max=16) and PR2 is almost on max(max=255)..
how i could work with small frequencies?

Use the newer 16F1 enhanced core devices.

Timer2 has a 1:64 prescaler, so you can get PWM down to 62hz with a 4mhz OSC.

__________________
DT
http://www.pbpgroup.com/files/SIGIMG/Pick_PIC.gif

jonas2
- 26th February 2011, 10:51
Hello

I want to use your example on a 18f26j50, but instead of displaying the date
I want to record data in an external eeprom.

Must convert it before writing to the eeprom or convert a reading
of the eeprom before sent to the PC? bcd format

and that the changes to make to pass a 16F628 to 18f26j50

Thank you for your help