View previous topic :: View next topic |
Author |
Message |
theasus
Joined: 31 May 2009 Posts: 79
|
Tris settings problem |
Posted: Sat Feb 12, 2011 6:48 am |
|
|
I couldn't set tris settings of 18f14k50,properly.When I didn't use tris setting my program works and I can use SPI module.But when I set tris register,it doesn't work,properly and I can't get correct value from Master side.My program is;(my compiler version is 4.114)
Code: |
#include <18F14K50.H>
#include <PIC18F14K50_registers.h>
#fuses INTRC_IO, NOWDT, PUT, BROWNOUT, NOLVP, CPUDIV1,NOMCLR
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_b7,rcv=PIN_b5)
int a;
#int_ssp
void ssp_isr(void)
{
a=spi_read();
}
//======================================
void main()
{
SET_TRIS_B( 0b01110000 );
SET_TRIS_C( 0x00 );
setup_spi(SPI_SLAVE | SPI_SS_DISABLED | SPI_L_TO_H);
clear_interrupt(INT_SSP);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
while(1)
{
if(a==58)
{
disable_interrupts(INT_SSP);
output_high(pin_c3);
delay_ms(1000);
output_low(pin_c3);
delay_ms(1000);
enable_interrupts(INT_SSP);
}
printf("\r%d",a);
}
}
|
I also looked C/ASM list.But I didn't see any wrong setting related with TRIS register;
Code: |
.................... SET_TRIS_B( 0b01110000 );
020C: MOVLW 70
020E: MOVWF F93
....................
.................... SET_TRIS_C( 0x00 );
0210: MOVLW 00
0212: MOVWF F94
|
So How can I set tris register?And How can this register effect SPI module? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Sat Feb 12, 2011 7:07 am |
|
|
Repeat after me:
If I'm going to set the tris myself, and not just let the compiler do it, I _must_ read the data sheet, and work out which pins need to be inputs, and which pins need to be outputs.....
Hint. SCK, is an _input_ on a slave device.
Best Wishes |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Sat Feb 12, 2011 7:08 am |
|
|
Since you're NOT using fast_IO mode, why not just let the compiler handle the TRIS details ?
It'll do everything automatically for you. |
|
|
theasus
Joined: 31 May 2009 Posts: 79
|
|
Posted: Sat Feb 12, 2011 7:25 am |
|
|
Ttelmah wrote: | Repeat after me:
If I'm going to set the tris myself, and not just let the compiler do it, I _must_ read the data sheet, and work out which pins need to be inputs, and which pins need to be outputs.....
Hint. SCK, is an _input_ on a slave device.
Best Wishes |
I read datasheet,And I used these codes for PIC16f877.It is not related with not reading datasheet.
I need to add some codes,And I need to define other pins as input or output.(for button control or something like that).I think it is related with TRIS register.Because when I used TRISA register,It didn't work again.And As you know SPI pins aren't related with portA. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Sat Feb 12, 2011 10:01 am |
|
|
SO where ARE you ACCESSSING port A in the program you supplied in the OP ??? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Sat Feb 12, 2011 1:37 pm |
|
|
theasus wrote: | Ttelmah wrote: | Repeat after me:
If I'm going to set the tris myself, and not just let the compiler do it, I _must_ read the data sheet, and work out which pins need to be inputs, and which pins need to be outputs.....
Hint. SCK, is an _input_ on a slave device.
Best Wishes |
I read datasheet,And I used these codes for PIC16f877.It is not related with not reading datasheet.
I need to add some codes,And I need to define other pins as input or output.(for button control or something like that).I think it is related with TRIS register.Because when I used TRISA register,It didn't work again.And As you know SPI pins aren't related with portA. |
The data sheet for the 18F14K50, specifically says that you must _set_ the tris bit for SCK to 1, on a slave device, yet you have this bit set to 0.
If you are going to control the tris, you _must_ get it right.
Best Wishes |
|
|
theasus
Joined: 31 May 2009 Posts: 79
|
|
Posted: Sun Feb 13, 2011 6:36 am |
|
|
I have already defined SCK pin is an input.As it shown in my codes;
SET_TRIS_B( 0b01110000 );
And I couldn't solve my problem yet.
Ttelmah wrote: | theasus wrote: | Ttelmah wrote: | Repeat after me:
If I'm going to set the tris myself, and not just let the compiler do it, I _must_ read the data sheet, and work out which pins need to be inputs, and which pins need to be outputs.....
Hint. SCK, is an _input_ on a slave device.
Best Wishes |
I read datasheet,And I used these codes for PIC16f877.It is not related with not reading datasheet.
I need to add some codes,And I need to define other pins as input or output.(for button control or something like that).I think it is related with TRIS register.Because when I used TRISA register,It didn't work again.And As you know SPI pins aren't related with portA. |
The data sheet for the 18F14K50, specifically says that you must _set_ the tris bit for SCK to 1, on a slave device, yet you have this bit set to 0.
If you are going to control the tris, you _must_ get it right.
Best Wishes |
|
|
|
theasus
Joined: 31 May 2009 Posts: 79
|
|
Posted: Fri Feb 18, 2011 3:04 am |
|
|
Could you purpose anything? My problem is still continuing.
Couldn't I set tris register when I used SPI or uart? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Fri Feb 18, 2011 6:20 am |
|
|
Why do you want to control the TRIS register manually? In your OP you said your program ran fine when you allowed the compiler to do it's job, making your life easier!
If you do want to play with TRIS, you'll have to read the datasheet on the PIC you're using,dump the listing and see what you're doing wrong. Comparing the listings of your working code (where the compiler does the settings) and your new code, you'll see what you're doing wrong.
As you've found out, incorrect settings of the register's can cause bad things to happen.
Even after 20+ years of programming PICs, I let the compiler do it's job. Only twice in that time was it necessary to manually configure the TRIS registers. |
|
|
theasus
Joined: 31 May 2009 Posts: 79
|
|
Posted: Fri Feb 18, 2011 9:47 am |
|
|
I'm not using only SPI module. I will also receive some signal from peripheral device. So I have to define pins as inputs or outputs. And "SET_TRIS_XX" is a CCSC fuction, Isn't it? |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Fri Feb 18, 2011 10:28 am |
|
|
You can have as many set_tris_X() statements you want but if you do not have #use fast_io(X) in your code the tris will not be changed. This is a must.
Ronald |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Fri Feb 18, 2011 11:05 am |
|
|
Yes,.."SET_TRIS_XX" is a function, however unless you are very, very good at programming and never intend on changing the function of I/O pins it is a LOT better to not use the set-tris-xx function.
It is not necessary to use it (and the mandatory fast_io() ). Heck there are dozens of supplied functions I've never needed or used. Just because CCS supplies them doesn't mean you have to use them.
Let the compiler automatically handle those details, after all that's probably why most people bought it!
You're making your task much harder than required. If, and it's a big IF, you need faster program execution, you might gain a little speed by using fast_io() and the tris..() functions BUT a lot depends on the overall code you create. |
|
|
theasus
Joined: 31 May 2009 Posts: 79
|
|
Posted: Mon Feb 21, 2011 2:17 am |
|
|
RS232 settings have some bugs for 18f14k50. Because another PICS like 16f877 didn't give an error for this operation. And When I don't use SPI module with RS232, It is worked well. This setting shown below effect SPI module and tris setting:
Code: |
#use rs232(baud=9600, xmit=PIN_b7, rcv=PIN_b5)
|
It seems unlogical, but I couldn't find any other reason for this. |
|
|
|