Bruce
- 25th January 2010, 20: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.
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.