CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Tris settings problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
theasus



Joined: 31 May 2009
Posts: 79

View user's profile Send private message

Tris settings problem
PostPosted: Sat Feb 12, 2011 6:48 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Feb 12, 2011 7:07 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Feb 12, 2011 7:08 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Feb 12, 2011 7:25 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Feb 12, 2011 10:01 am     Reply with quote

SO where ARE you ACCESSSING port A in the program you supplied in the OP ???
Ttelmah



Joined: 11 Mar 2010
Posts: 19498

View user's profile Send private message

PostPosted: Sat Feb 12, 2011 1:37 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Feb 13, 2011 6:36 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Feb 18, 2011 3:04 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Feb 18, 2011 6:20 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Feb 18, 2011 9:47 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Feb 18, 2011 10:28 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Feb 18, 2011 11:05 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Feb 21, 2011 2:17 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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