PDA

View Full Version : 16F1827 setup



Macgman2000
- 5th February 2010, 19:35
Hello All,

I have looked high and low for 16F1827 code examples. Basically I am looking for a template that initializes common hardware features I want to use...not the other 400 pages of features in the data sheet.

I want to use the ADC, HWPWM1 and 2, and some digital only I/O's. Some how I don't think copying and pasting my old 16F877A code will work. I think the configuration registers don't have the same naming convention.....or do they? I suppose I could drop the code in, sit back and gawk at the never ending list of errors.....I would rather not though, there should be a better way :)



Nick

Archangel
- 5th February 2010, 19:40
Hello All,

I have looked high and low for 16F1827 code examples. Basically I am looking for a template that initializes common hardware features I want to use...not the other 400 pages of features in the data sheet.

I want to use the ADC, HWPWM1 and 2, and some digital only I/O's. Some how I don't think copying and pasting my old 16F877A code will work. I think the configuration registers don't have the same naming convention.....or do they? I suppose I could drop the code in, sit back and gawk at the never ending list of errors.....I would rather not though, there should be a better way :)



Nick
Hi Nick,
go to your MPASM Suite root directory and find the file named P16F1827.inc , open it using either notepad or MCS. There you will find all the available configs, which usually are near the bottom, also; you will see all the available registers too.

Acetronics2
- 5th February 2010, 20:02
Hi, Joe

I do think this file means ...



'************************************************* ***************
'* 16F1827.BAS *
'* *
'* By : Leonard Zerman, Jeff Schmoyer *
'* Notice : Copyright (c) 2009 microEngineering Labs, Inc. *
'* All Rights Reserved *
'* Date : 06/23/09 *
'* Version : 2.60 *
'* Notes : *
'************************************************* ***************


Somebody already did the job ...



I want to use the ADC, HWPWM1 and 2, and some digital only I/O's

and the dedicated library is here ...

Alain

Bruce
- 6th February 2010, 16:51
Most peripherals on the 16F1827 are the same as previous parts, but you will need to
spend some time going through the 1827 data sheet to find out which ones are the same,
and which ones have changed.

Definitely have a look at the APFCON0 & APFCON1 registers.

Also, check your PI14EEXT.BAS to make sure it has APFCON0 VAR BYTE EXT and not
APFCON VAR BYTE EXT.

There should also be these definitions in the PI14EEXT.BAS file;


MDCON VAR BYTE EXT
MDSRC VAR BYTE EXT
MDCARH VAR BYTE EXT
MDCARL VAR BYTE EXT

If these aren't in there, just add them.

Then you can do cool stuff like this with the new Data Signal Modulator...:o.


'************************************************* ***************
'* Name : DSM.BAS *
'* Author : B. Reynolds *
'* Notice : Copyright (c) 2010 http://www.Rentron.com *
'* : All Rights Reserved *
'* Date : 2/5/2010 *
'* Version : 1.0 *
'* Notes : Using the 16F1827 Data Signal Modulator for IR *
'* : serial communications with onboard USART. *
'************************************************* ***************

' MODOUT, RB3, outputs whatever you send with HSEROUT as a 40kHz
' modulated data stream.

ASM
__config _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
__config _CONFIG2, _PLLEN_OFF & _LVP_OFF & _VCAPEN_OFF & _LVP_OFF & _STVREN_OFF
ENDASM

DEFINE OSC 8
DEFINE HSER_BAUD 2400
Duty VAR WORD
TRISB = 0

Init:
PR2 = 49 ' Set PWM for ~40kHz
CCP1CON = %00001100 ' Mode select = PWM
T2CON = %00000100 ' %00000100 = TMR2 ON 1:1 prescale

Duty = 100 ' Roughly 50% duty cycle
CCP1CON.4 = duty.0 ' Setup 10-bit duty cycle as
CCP1CON.5 = duty.1 ' a 10-bit word
CCPR1L = DUTY >> 2
APFCON0 = 1 ' CCP1 PWM output on RB0
OSCCON = %01110000 ' 8MHz internal
ANSELA = 0 ' all digital. A/D disabled
ANSELB = 0
MDCON = %11100000 ' modulator, MODOUT pin, slew rate limiting enabled
' Note: set MDSRC.7 to eliminate the TX signal output on RB2/TX
MDSRC = %00001010 ' USART TX is modulation source, TX output still active
MDCARH = %00000000 ' carrier OFF during idle periods
MDCARL = %00000100 ' carrier ON only when sending data

Main:
HSEROUT [$55] ' sends USART data out modulated at 40kHz
PAUSE 50
GOTO Main
END

The output of your 40kHz IR module spits out serial data you send with HSEROUT.

Macgman2000
- 7th February 2010, 00:06
Hello Bruce,

I am not all that familiar with this chip yet. How is it different than the 16F877A using PWM1 at 38.8Khz and sending serial data out on say RB4 data out? The IR LED would then be placed between PWM1 out and RB4 to modulate the data stream.

Just wondering if the 1827 has a nifty way of doing it... Do you have a schematic of how the IR led connects to the I/O, using the code you attached?

Best Regards,
Nick

Bruce
- 7th February 2010, 01:05
I am not all that familiar with this chip yet. Neither am I. That's the 1st bit of code I've compiled or tested on this one.

How is it different than the 16F877A using PWM1 at 38.8Khz and sending serial data out on say RB4 data out?
Quite a lot. With the 16F1827 you only need a single pin. Here's a schematic;
RB3 ----/\/\/\-----|>|---GND.

Or you could use an NPN transistor if you need more range. Look at the logic picture below.

The top signal is the modulated data output from RB3 MODOUT. The lower signal is
the USART TX output.

Here's a copy of the previous code with better explanations, and both the USART TX out,
and PWM output pins disabled.


' MODOUT, RB3, outputs whatever you send with HSEROUT as a 40kHz
' modulated data stream.

ASM
__config _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
__config _CONFIG2, _PLLEN_OFF & _LVP_OFF & _VCAPEN_OFF & _LVP_OFF & _STVREN_OFF
ENDASM

DEFINE OSC 8
DEFINE HSER_BAUD 2400
Duty VAR WORD
TRISB = 0

Init:
CCPTMRS = %00000000 ' CCP1 is based off Timer 2 in PWM Mode
PR2 = 49 ' Set PWM for ~40kHz
CCP1CON = %00001100 ' Mode select = PWM
T2CON = %00000100 ' %00000100 = TMR2 ON 1:1 prescale

Duty = 100 ' Roughly 50% duty cycle
CCP1CON.4 = duty.0 ' Setup 10-bit duty cycle as
CCP1CON.5 = duty.1 ' a 10-bit word
CCPR1L = DUTY >> 2
APFCON0 = 1 ' Assigns CCP1 PWM output to RB0 (RB3 is used for MODOUT)
OSCCON = %01110000 ' 8MHz internal
ANSELA = 0 ' all digital. A/D disabled
ANSELB = 0

' Data Signal Modulator setup
' Notes:
' MDSRC.7 = 1, which eliminates the USART TX signal output on RB2/TX.
' The PWM output signal, on CCP1 pin, which is set to RB0 with APFCON0=1
' has been disabled by setting MDCARL.7
' So even with the hardware USART outputting data, and PWM on, thier output pins
' are disabled. PWM and the serial data output from the USART are "mixed" internally
' by the Data Signal Modulator.

MDCON = %11100000 ' modulator, MODOUT pin, slew rate limiting enabled
MDSRC = %10001010 ' USART TX is modulation source, TX output "pin" disabled
MDCARH = %00000000 ' carrier OFF during idle periods
MDCARL = %10000100 ' carrier ON only when sending data, CCP1 PWM "pin" output disabled

Main:
HSEROUT [$55] ' sends USART data out modulated at 40kHz
PAUSE 500
GOTO Main
END
Even with PWM on, and the USART sending data, there is no PWM output on the CCP1 pin
or data output on the USART TX pin. Both signals are mixed internally by the DSM,
and output only on RB3 or MODOUT.

I only had the USART TX pin output enabled to capture the logic analyzer traces below to
show the MODOUT signal compared to the USART TX output signal.

Macgman2000
- 17th February 2010, 16:14
Hello Bruce,

I tried to compile your code, it had many errors. I have PBP 2.60, using PB assembler (not MASM). I went through the PI14EEXT.BAS and made the changes you advised. I have 1 error left "CCPTMRS". This does not exist in the PI14EEXT.BAS, only CCPTMRS0 and CCPTMRS1.

Which of these did you intend?

thanks!
Nick

Bruce
- 17th February 2010, 16:51
Hi Nick,

Add CCPTMRS to your PI14EEXT.BAS file. There were several register definitions missing.

Macgman2000
- 18th February 2010, 16:22
Hello Bruce,

I made the changes, still nothing. I am getting a bunch of errors saying that the 16F1827.inc could not be found. It is giving me a BUNCH of errors. Errors for symbol not previously defined portA, PortB, portC...etc.

I found the 16F1827.inc within the PBP directory, opened it up and it says not to use PB assembler, but rather MPASM. So I selected MPASM as my assembler, still I am getting a ton of errors using MPASM. I have never seen this many errors ever...wow.

Nick

Bruce
- 18th February 2010, 16:40
Does your version of MPASM support the PIC16F1827?

If it does you should have a P16F1827.inc file in your MPASM directory. If not, you need to
update to the latest version. Without the include file it will return a ton of errors.

Acetronics2
- 18th February 2010, 17:01
Hi, Bruce

Sorry for Nick,

But once mods done in the EEXT and 1827.inc files ... everything fine ( may I say : as usual .... )

BTW: no CCPTMRSx alert ...

Alain

Macgman2000
- 18th February 2010, 18:51
Bruce,

I have IDE 7.52. No didn't see the 16F1827.INC in the MPASM directory....sigh. If I upgrade now will it screw up all my other working code for other chip bodies?

ggrrrrr.

Best Regards,
Nick

Acetronics2
- 18th February 2010, 18:59
I have IDE 7.52. No didn't see the 16F1827.INC in the MPASM directory....sigh. If I upgrade now will it screw up all my other working code for other chip bodies?

ggrrrrr.

You're joking Nick ???

MPLAB is @ 8.43 Version !!! ... Ok if PBP 2.60 already is on your computer.

v 8.15 if not ...

No soucy when Upgrading MPLAB ...

Not forbidden ( read recommended ) to save your projects files BEFORE upgrading ...

Alain

Macgman2000
- 19th February 2010, 16:01
Alain,

Still nothing. I upgraded to 8.46. Now when I compile it opens up a DOS window and does something REALLY quick using MPASMwin.exe then returns with a bunch of errors.....my code is below....

Is it sufficient to choose the device in MicroStudio or do I need to call it within the ASM section in the code?



DEFINE OSC 8

ASM
__config _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
__config _CONFIG2, _PLLEN_OFF & _LVP_OFF & _VCAPEN_OFF & _LVP_OFF & _STVREN_OFF
ENDASM



TRISA = 0
TRISB = 0


Duty VAR WORD


Init:
CCPTMRS = %00000000 ' CCP1 is based off Timer 2 in PWM Mode
PR2 = 49 ' Set PWM for ~40kHz
CCP1CON = %00001100 ' Mode select = PWM
T2CON = %00000100 ' %00000100 = TMR2 ON 1:1 prescale

Duty = 100 ' Roughly 50% duty cycle
CCP1CON.4 = duty.0 ' Setup 10-bit duty cycle as
CCP1CON.5 = duty.1 ' a 10-bit word
CCPR1L = DUTY
APFCON0 = 1 ' Assigns CCP1 PWM output to RB0 (RB3 is used for MODOUT)
OSCCON = %01110000 ' 8MHz internal
ANSELA = 0 ' all digital. A/D disabled
ANSELB = 0

' Data Signal Modulator setup
' Notes:
' MDSRC.7 = 1, which eliminates the USART TX signal output on RB2/TX.
' The PWM output signal, on CCP1 pin, which is set to RB0 with APFCON0=1
' has been disabled by setting MDCARL.7
' So even with the hardware USART outputting data, and PWM on, thier output pins
' are disabled. PWM and the serial data output from the USART are "mixed" internally
' by the Data Signal Modulator.

MDCON = %11100000 ' modulator, MODOUT pin, slew rate limiting enabled
MDSRC = %10001010 ' USART TX is modulation source, TX output "pin" disabled
MDCARH = %00000000 ' carrier OFF during idle periods
MDCARL = %10000100 ' carrier ON only when sending data, CCP1 PWM "pin" output disabled

Main:
HSEROUT [$55] ' sends USART data out modulated at 40kHz
PAUSE 500
GOTO Main
END

Acetronics2
- 19th February 2010, 16:51
Ok ...

1) Did you make the mods Bruce pointed at in his 6th February post ???

you DO have to make them ...




Also, check your PI14EEXT.BAS to make sure it has APFCON0 VAR BYTE EXT and not
APFCON VAR BYTE EXT.

There should also be these definitions in the PI14EEXT.BAS file;

Code:
MDCON VAR BYTE EXT
MDSRC VAR BYTE EXT
MDCARH VAR BYTE EXT
MDCARL VAR BYTE EXT

If these aren't in there, just add them.




2) just Paste Bruce's code in MCS:




'************************************************* ***************
'************************************************* ***************
'* Name : DSM.BAS *
'* Author : B. Reynolds *
'* Notice : Copyright (c) 2010 http://www.Rentron.com *
'* : All Rights Reserved *
'* Date : 2/5/2010 *
'* Version : 1.0 *
'* Notes : Using the 16F1827 Data Signal Modulator for IR *
'* : serial communications with onboard USART. *
'************************************************* ***************

' MODOUT, RB3, outputs whatever you send with HSEROUT as a 40kHz
' modulated data stream.

ASM
__config _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
__config _CONFIG2, _PLLEN_OFF & _LVP_OFF & _VCAPEN_OFF & _LVP_OFF & _STVREN_OFF
ENDASM

DEFINE OSC 8
DEFINE HSER_BAUD 2400
Duty VAR WORD
TRISB = 0

Init:
PR2 = 49 ' Set PWM for ~40kHz
CCP1CON = %00001100 ' Mode select = PWM
T2CON = %00000100 ' %00000100 = TMR2 ON 1:1 prescale

Duty = 100 ' Roughly 50% duty cycle
CCP1CON.4 = duty.0 ' Setup 10-bit duty cycle as
CCP1CON.5 = duty.1 ' a 10-bit word
CCPR1L = DUTY >> 2
APFCON0 = 1 ' CCP1 PWM output on RB0
OSCCON = %01110000 ' 8MHz internal
ANSELA = 0 ' all digital. A/D disabled
ANSELB = 0
MDCON = %11100000 ' modulator, MODOUT pin, slew rate limiting enabled
' Note: set MDSRC.7 to eliminate the TX signal output on RB2/TX
MDSRC = %00001010 ' USART TX is modulation source, TX output still active
MDCARH = %00000000 ' carrier OFF during idle periods
MDCARL = %00000100 ' carrier ON only when sending data

Main:
HSEROUT [$55] ' sends USART data out modulated at 40kHz
PAUSE 50
GOTO Main
END


MUST compile without any error in MCS if 1) has been done.

As simple as that.

Alain

Macgman2000
- 19th February 2010, 17:13
Alain,

I recopied the code from your post. Still getting

Symbol not previously defined (PortC)
Symbol not previously defined (TRISC)
etc etc etc

Acetronics2
- 19th February 2010, 17:39
Hi,

Got it ...



Devices affected: 16F1826, 16F1827
Date encountered: 08/2009
Error class: safe (always reported)

There are invalid port aliases in the PBP header file for these parts. The following assembly errors will be generated:

Symbol not previously defined (PORTC)
Symbol not previously defined (TRISC)
Workaround:

Edit the files 16F1826.BAS and 16F1827.BAS (found in the PBP install folder) as follows:

PORTL VAR PORTB
PORTH VAR PORTA ' PORTC invalid, change to PORTA
TRISL VAR TRISB
TRISH VAR TRISA ' TRISC invalid, change to TRISA


This is from a Melab's errata sheet Darrel Pointed at some times ago ....
I made the mod a while ago ... so couln't see what's wrong.

Have a nice evening

Alain

Macgman2000
- 19th February 2010, 20:41
Alain,

Ok getting somewhere now. When I comment out all the fuse configurations between the ASM and ENDASM I get no errors. With all the mods that you suggested.

I am down to 3 errors.

overwriting previous address contents (8007)
symbol not previously defined (_CAPEN_OFF)
overwriting previous address contents (8008)

What other files do I have to modify to get rid of these errors?

Nick

Macgman2000
- 22nd February 2010, 15:29
Alain & Bruce,

I almost resolved all the errors by modifying the files you and Bruce suggested, except for 1. I went into 16F1827.INC (PBP file) and commented out existing fuse settings and let the main code define those functions. So of the 3 remaining errors, 1 is left _VCAPEN_OFF symbol not previously defined.

I opened up the P16F1827.INC (MPASM file) to see if there is an address for _VCAPEN_OFF....it is not listed at all...not even under Config1 or Config2.

Wow this has been an uphill battle getting this thing to compile. Any suggestions on how to resolve this last issue would be appreciated.

thanks,
Nick

Bruce
- 22nd February 2010, 15:57
Just remove _VCAPEN_OFF from your config line.

It's definitely in my P16F1827.INC file, but if Microchip has updated include files, it may
not be in the one you have. It should work either way.

Macgman2000
- 22nd February 2010, 17:13
Bruce,

Thanks for the help. It compiled without errors. Now off to testing! I will post back my results later.

Nick

RussMartin
- 24th February 2010, 18:28
I'll bet this thread has already saved me much frustration and gnashing of teeth.

I assume that, when the device is erased, the CONFIGx registers are all 1s; and that all I have to specify are the parameters that would require a 0 to get what I want. Anything wrong with that assumption?

J. Mark Wolf
- 15th November 2010, 12:35
Bruce,

Thanks for the help. It compiled without errors. Now off to testing! I will post back my results later.

Nick

Hello Macgman2000

Were you ever able to get anything working? If so how?

I've followed the suggestions in this thread, although all my files were already updated in my v2.60a. I still get the errors. I have the latest and greatest Mplab and Bruces' code simply will not compile.

Ioannis
- 9th March 2011, 21:26
It's definitely in my P16F1827.INC file, but if Microchip has updated include files, it may
not be in the one you have. It should work either way.

Hi Bruce.

I searched on two MPLAB installations for this VCAPEN thing.

It is not in my *.inc files and also it is absent from the DS41391C pdf file too.

I wonder what is this for. Just curious.

Ioannis

Bruce
- 9th March 2011, 21:45
Hi Ioannis,

In an early data sheet (and silicon I assume?) this was a config2, bit 4 option like in the 16F193x series for the voltage regulator capacitor enable/disable.

It magically vanished when the newer data sheet & .inc files were released, so I would just ignore it.

If you look in the CONFIG 2 section you'll see where they changed bit #4 to Reserved. Not sure what happened?

Ioannis
- 9th March 2011, 23:04
Hmm, maybe ghosts in the bits...

Who knows?

Thanks anyway.

Ioannis