|
|
View previous topic :: View next topic |
Author |
Message |
Will Reeve
Joined: 30 Oct 2003 Posts: 209 Location: Norfolk, England
|
PIC18 Port D |
Posted: Wed Jun 30, 2004 11:34 am |
|
|
Hi,
I have had to switch to a 18F442 for speed and am trying to output a byte to port D. I'ved used:
#byte AD = 0x83
and a
set_tris_d(0x00);
but when I set AD to a value it's not translated to the pins of port D. This is quite frustrating as the code works in a 16F877 with #byte changed. Am I missing something obvious, is 0x83 the correct addess for PORT D?
Will |
|
|
Will Reeve
Joined: 30 Oct 2003 Posts: 209 Location: Norfolk, England
|
|
Posted: Wed Jun 30, 2004 11:50 am |
|
|
I love replying to myself :-) I�ve dug some deeper. It�s the set_tris_d(0x00); line it sets the whole of PORTD to 1.
If I manually use output_high(PIN_D0); output_low(PIN_D0); without the set_tris_d line everything works fine which at least confirms the PIC is working!
However adding set_tris_d(0x00); causes port D to lock at 1.
Here is my code:
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
set_tris_d(0x00);
while(true) {
delay_ms(1);
output_low(PIN_D0);
delay_ms(1);
output_high(PIN_D0);
}// while(true)
}
Remove the set_tris_d(0x00) line and it works, leave it in and it doesn�t. Here is the list file for the code after set_tris_d(0x00);
0070: MOVLW 00
0072: MOVWF F95
Any ideas?
Will |
|
|
dvsoft
Joined: 28 Nov 2003 Posts: 46
|
|
Posted: Wed Jun 30, 2004 11:53 am |
|
|
bonjour,
PortD is 0xF83
DS39564B-page 45 TABLE 4-1: SPECIAL FUNCTION REGISTER MAP
Alain |
|
|
Will Reeve
Joined: 30 Oct 2003 Posts: 209 Location: Norfolk, England
|
|
Posted: Wed Jun 30, 2004 1:03 pm |
|
|
Very frustrated now, off to sleep on it! This code works:
CCS PCH C Compiler, Version 3.190, 18637
Filename: E:\pic18.LST
ROM used: 142 (1%)
Largest free fragment is 142
RAM used: 5 (1%) at main() level
6 (1%) worst case
Stack: 1 locations
*
0000: GOTO 002E
.................... #include "pic18.h"
.................... #include <18F442.h>
.................... //////// Standard Header file for the PIC18F442 device ////////////////
.................... #device PIC18F442
.................... #list
....................
.................... #device ICD=TRUE
.................... #device adc=8
.................... #use delay(clock=20000000)
0004: CLRF FEA
0006: MOVLW 06
0008: MOVWF FE9
000A: MOVF FEF,W
000C: BTFSC FD8.2
000E: GOTO 002C
0012: MOVLW 06
0014: MOVWF 01
0016: CLRF 00
0018: DECFSZ 00,F
001A: BRA 0018
001C: DECFSZ 01,F
001E: BRA 0016
0020: MOVLW 7B
0022: MOVWF 00
0024: DECFSZ 00,F
0026: BRA 0024
0028: DECFSZ FEF,F
002A: BRA 0012
002C: RETLW 00
.................... #fuses NOWDT,WDT128,HS,NOPROTECT,NOOSCSEN,NOBROWNOUT,BORV20,NOPUT,NOSTVREN,DEBUG,NOLVP,NOWRT,NOWRTD,NOWRTB,NOWRTC,NOCPD,NOCPB,NOEBTR,NOEBTRB
....................
....................
....................
.................... void main()
.................... {
002E: CLRF FF8
0030: BCF FD0.7
0032: CLRF FEA
0034: CLRF FE9
0036: MOVLW 06
0038: MOVWF FC1
.................... setup_adc_ports(NO_ANALOGS);
003A: MOVLW 06
003C: MOVWF FC1
.................... setup_adc(ADC_OFF);
003E: BCF FC1.6
0040: MOVF FC2,W
0042: ANDLW 38
0044: MOVWF FC2
.................... setup_psp(PSP_DISABLED);
0046: BCF F96.4
.................... setup_spi(FALSE);
0048: BCF FC6.5
004A: BCF F94.5
004C: BSF F94.4
004E: BCF F94.3
0050: MOVLW 00
0052: MOVWF FC6
0054: MOVWF FC7
.................... setup_wdt(WDT_OFF);
0056: BCF FD1.0
.................... setup_timer_0(RTCC_INTERNAL);
0058: MOVLW 80
005A: MOVWF FD5
.................... setup_timer_1(T1_DISABLED);
005C: CLRF FCD
.................... setup_timer_2(T2_DISABLED,0,1);
005E: MOVLW 00
0060: MOVWF 01
0062: MOVWF FCA
0064: MOVLW 00
0066: MOVWF FCB
.................... setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
0068: MOVF FB1,W
006A: ANDLW 48
006C: MOVWF FB1
.................... // set_tris_d(0x00);
....................
.................... while(true) {
.................... delay_ms(1);
006E: MOVLW 01
0070: MOVWF 06
0072: CALL 0004
.................... output_low(PIN_D0);
0076: BCF F95.0
0078: BCF F8C.0
.................... delay_ms(1);
007A: MOVLW 01
007C: MOVWF 06
007E: CALL 0004
.................... output_high(PIN_D0);
0082: BCF F95.0
0084: BSF F8C.0
.................... }// while(true)
0086: GOTO 006E
....................
.................... }
....................
008A: GOTO 008A
Configuration Fuses:
Word 1: 2200 HS NOOSCSEN
Word 2: 0E0C NOBROWNOUT WDT128 NOWDT BORV20 PUT
Word 3: 0100 CCP2C1
Word 4: 0001 STVREN DEBUG NOLVP
Word 5: C00F NOPROTECT NOCPD NOCPB
Word 6: E00F NOWRT NOWRTD NOWRTB NOWRTC
Word 7: 400F NOEBTR NOEBTRB
and this code Doesn't:
CCS PCH C Compiler, Version 3.190, 18637
Filename: E:\pic18.LST
ROM used: 146 (1%)
Largest free fragment is 146
RAM used: 5 (1%) at main() level
6 (1%) worst case
Stack: 1 locations
*
0000: GOTO 002E
.................... #include "pic18.h"
.................... #include <18F442.h>
.................... //////// Standard Header file for the PIC18F442 device ////////////////
.................... #device PIC18F442
.................... #list
....................
.................... #device ICD=TRUE
.................... #device adc=8
.................... #use delay(clock=20000000)
0004: CLRF FEA
0006: MOVLW 06
0008: MOVWF FE9
000A: MOVF FEF,W
000C: BTFSC FD8.2
000E: GOTO 002C
0012: MOVLW 06
0014: MOVWF 01
0016: CLRF 00
0018: DECFSZ 00,F
001A: BRA 0018
001C: DECFSZ 01,F
001E: BRA 0016
0020: MOVLW 7B
0022: MOVWF 00
0024: DECFSZ 00,F
0026: BRA 0024
0028: DECFSZ FEF,F
002A: BRA 0012
002C: RETLW 00
.................... #fuses NOWDT,WDT128,HS,NOPROTECT,NOOSCSEN,NOBROWNOUT,BORV20,NOPUT,NOSTVREN,DEBUG,NOLVP,NOWRT,NOWRTD,NOWRTB,NOWRTC,NOCPD,NOCPB,NOEBTR,NOEBTRB
....................
....................
....................
.................... void main()
.................... {
002E: CLRF FF8
0030: BCF FD0.7
0032: CLRF FEA
0034: CLRF FE9
0036: MOVLW 06
0038: MOVWF FC1
.................... setup_adc_ports(NO_ANALOGS);
003A: MOVLW 06
003C: MOVWF FC1
.................... setup_adc(ADC_OFF);
003E: BCF FC1.6
0040: MOVF FC2,W
0042: ANDLW 38
0044: MOVWF FC2
.................... setup_psp(PSP_DISABLED);
0046: BCF F96.4
.................... setup_spi(FALSE);
0048: BCF FC6.5
004A: BCF F94.5
004C: BSF F94.4
004E: BCF F94.3
0050: MOVLW 00
0052: MOVWF FC6
0054: MOVWF FC7
.................... setup_wdt(WDT_OFF);
0056: BCF FD1.0
.................... setup_timer_0(RTCC_INTERNAL);
0058: MOVLW 80
005A: MOVWF FD5
.................... setup_timer_1(T1_DISABLED);
005C: CLRF FCD
.................... setup_timer_2(T2_DISABLED,0,1);
005E: MOVLW 00
0060: MOVWF 01
0062: MOVWF FCA
0064: MOVLW 00
0066: MOVWF FCB
.................... setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
0068: MOVF FB1,W
006A: ANDLW 48
006C: MOVWF FB1
.................... set_tris_d(0x00);
006E: MOVLW 00
0070: MOVWF F95
....................
.................... while(true) {
.................... delay_ms(1);
0072: MOVLW 01
0074: MOVWF 06
0076: CALL 0004
.................... output_low(PIN_D0);
007A: BCF F95.0
007C: BCF F8C.0
.................... delay_ms(1);
007E: MOVLW 01
0080: MOVWF 06
0082: CALL 0004
.................... output_high(PIN_D0);
0086: BCF F95.0
0088: BSF F8C.0
.................... }// while(true)
008A: GOTO 0072
....................
.................... }
....................
008E: GOTO 008E
Configuration Fuses:
Word 1: 2200 HS NOOSCSEN
Word 2: 0E0C NOBROWNOUT WDT128 NOWDT BORV20 PUT
Word 3: 0100 CCP2C1
Word 4: 0001 STVREN DEBUG NOLVP
Word 5: C00F NOPROTECT NOCPD NOCPB
Word 6: E00F NOWRT NOWRTD NOWRTB NOWRTC
Word 7: 400F NOEBTR NOEBTRB
I can't see anything wrong with it!
Will |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jun 30, 2004 1:46 pm |
|
|
I think the first thing you should do is to strip your test program
down to essentials. Get rid of all code that is unnecessary to
test the problem.
For example, you have got one million fuses in your fuse statement.
If you do something like this, you are essentially saying:
"Dear CCS, I would like to be an alpha tester (or omega tester ?)
for your fuses statement. I will dump one million parameters into
your #fuses directive, in an attempt to break it".
My two cents:
Do not do this.
Strip it down to something like this for the 18F442:
#fuses HS, NOWDT, WDT128, BROWNOUT, PUT, NOLVP
I would get rid of these statements:
#include "pic18.h"
#device adc=8
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
Reasons:
You shouldn't need some extra pic18.h include file for this test.
You don't need to use the adc in this test.
The compiler will automatically insert startup code to turn off the ADC.
(Or, at least it should if you have a good version of the compiler).
The PSP is disabled at power-up by default.
So is SPI.
WDT is disabled at power-up, with the NOWDT, WDT128 setting.
All Timers are disabled at power-up by default.
Once you've got all this extra stuff out of there, it's much easier
to scan the .LST file and notice any problems. |
|
|
Ttelmah Guest
|
Re: PIC18 Port D |
Posted: Wed Jun 30, 2004 2:30 pm |
|
|
Will Reeve wrote: | Hi,
I have had to switch to a 18F442 for speed and am trying to output a byte to port D. I'ved used:
#byte AD = 0x83
and a
set_tris_d(0x00);
but when I set AD to a value it's not translated to the pins of port D. This is quite frustrating as the code works in a 16F877 with #byte changed. Am I missing something obvious, is 0x83 the correct addess for PORT D?
Will |
No. 0x83, is _not_ the address for Port D on the 18 chips. On the 18F, 'Port D', is 0xF83. You can also access the output latch seperately on the 18 chips (at 0xF8C).
I suggest you get a copy of the chips data sheet, before doing anything trying to access the registers directly....
Best Wishes |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Jun 30, 2004 5:02 pm |
|
|
From your list file it doesn't make any sense as to why setting tris_d would cause your application to fail to output a value at PIN_D0. Studying your code everything looks ok, I only have two questions:
1) After reset all port D pins are set as inputs. Your setting of the tris_d register makes all port-D pins outputs with undefined values..... What is connected to these port-D pins? Is here something connected that might cause your processor to stall or reset continuously? Perhaps some undefined output values causing a short circuit?
2) Why do you want to set tris_d? It is not required as the CCS compiler will automatically set the corresponding tris-bits at every I/O operation. The only reason for manually setting the tris register on a PIC18 I can think of is when used in combination with #use fast_io(d) to save a few bytes, but this is missing from your code. |
|
|
Guest
|
|
Posted: Thu Jul 01, 2004 8:40 am |
|
|
Hi,
What I really want to do is map PORTD to a C variable (using #byte), set TRISD manually and then bit bash as quickly as possible! A 16F877 isn�t quite fast enough hence the move to PIC18F442 and 40MHz.
When that didn�t work I tried to go back to basics and use the inbuilt commands (with and without #use fast_io(D)). I then ran strangely up against this problem, I got it down to adding the tris line and port D went �high� without the tris everything worked!
I had already removed all the external circuitry from port D as someone said.
I reprogrammed the PIC after the system was switched off overnight and it worked!
I think I was getting a little frustrated last night and glad my wife dragged me away from the PC before I ran out of hair.
I have done as PCM suggested and got rid of all that unnecessary code, it was generated by the PIC Wizard but I appreciate it�s not all needed. Thanks.
I now have my code working as it should (well nearly, but that�s a different thread!). Thanks guys,
Will |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|