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 support@ccsinfo.com

#use rs232 and set_tris_b

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



Joined: 26 Feb 2012
Posts: 8

View user's profile Send private message

#use rs232 and set_tris_b
PostPosted: Sun Feb 26, 2012 11:04 am     Reply with quote

Hi to all,
I'm using pic16f88 uart, by code:
Code:
#use rs232(baud=9600,xmit=PIN_B5,rcv=PIN_B2,parity=N,bits=8,ERRORS)


I also need to use PIN_B3 as output to control a led.
So I use:
Code:
set_tris_b(4); //PIN_B2 serial input, others output


but nothing works. Where is the problem in set_tris_b?

Thanks
dyeatman



Joined: 06 Sep 2003
Posts: 1924
Location: Norman, OK

View user's profile Send private message

PostPosted: Sun Feb 26, 2012 11:36 am     Reply with quote

What fuses have you set? Show us your fuses line.
_________________
Google and Forum Search are some of your best tools!!!!
redicon



Joined: 26 Feb 2012
Posts: 8

View user's profile Send private message

PostPosted: Sun Feb 26, 2012 11:47 am     Reply with quote

Here is complete program:
Code:
#include <16F88.h>

#FUSES NOWDT       //No Watch Dog Timer
#FUSES INTRC_IO    //Internal RC Osc
#FUSES NOPUT       //No Power Up Timer
#FUSES NOMCLR       //Master Clear pin used for I/O
#FUSES NOBROWNOUT    //Reset when brownout detected
#FUSES NOLVP       //No low voltage programing, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD       //No EE protection
#FUSES NOWRT       //Program memory not write protected
#FUSES NODEBUG       //Debug mode for use with ICD
#FUSES NOPROTECT    //Code not protected from reading
#FUSES NOFCMEN       //Fail-safe clock monitor disabled
#FUSES NOIESO       //Internal External Switch Over mode disabled

#define TIMER0INIT 59286

#use delay(clock=2000000)
#use rs232(baud=9600,xmit=PIN_B5,rcv=PIN_B2,parity=N,bits=8,ERRORS)

#DEFINE LED   PIN_B3

int cont = 0;
#int_TIMER1
//interrupt each 0,1 s
void timer1_isr(void)
{
   cont++;
   set_timer1(TIMER0INIT);
}


main()
{

   setup_oscillator(OSC_2MHZ);
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_comparator(NC_NC_NC_NC);
   
     set_tris_b(4); // b2 serial input
   OUTPUT_LOW(LED);                           

     setup_timer_1 (T1_INTERNAL|T1_DIV_BY_8);
   enable_interrupts(GLOBAL);
   enable_interrupts(INT_TIMER1);
   set_timer1(TIMER0INIT);
         
   while(1)
   {
      if (cont == 30) // after 3sec
      {   
         cont =0;
         OUTPUT_HIGH(LED);
      }
   }
}


without #use rs232 this program is working fine, using rs232 there is a problem on portb and the led starts high.


Last edited by redicon on Thu Mar 01, 2012 7:29 am; edited 1 time in total
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Sun Feb 26, 2012 12:35 pm     Reply with quote

Unless you need to use fast_IO, which is rare, you should not need to set the TRIS registers at all. The compiler does it for you automatically.

Can you write a simple program to blink your LED on B3? Do you have a hardware problem?
_________________
The search for better is endless. Instead simply find very good and get the job done.
redicon



Joined: 26 Feb 2012
Posts: 8

View user's profile Send private message

PostPosted: Sun Feb 26, 2012 12:57 pm     Reply with quote

No hardware problem, my program can blink led well without #use rs232 statement.

Also removing the set_tris function the led starts high. Are you sure that i don't need to set tris?
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Sun Feb 26, 2012 1:45 pm     Reply with quote

Try instead of if (cont == 30) // after 3sec this
if (cont >= 30) // after 3sec
redicon



Joined: 26 Feb 2012
Posts: 8

View user's profile Send private message

PostPosted: Sun Feb 26, 2012 5:49 pm     Reply with quote

Nothing changes.
If I add "#use rs232" statement the program stop working.

The problem is in set_tris+use rs232 (i've also tried #use fast_io(B), nothing change)
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 26, 2012 6:40 pm     Reply with quote

Post your compiler version. It's given at the top of the .LST file, which is
in your project directory. Your version may have a bug. Example of
version numbers:
http://www.ccsinfo.com/devices.php?page=versioninfo
redicon



Joined: 26 Feb 2012
Posts: 8

View user's profile Send private message

PostPosted: Sun Feb 26, 2012 8:49 pm     Reply with quote

My version is 4.016, quite old.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 26, 2012 11:06 pm     Reply with quote

I don't have vs. 4.016, but I do have 4.014 and 4.017. So I copied and
pasted your program into MPLAB and compiled it and saved the .LST
file in another directory. Then I commented out the #use rs232 line and
re-compiled it. Then I compared the two .LST files. The only real
differences that I saw, was that the file with #use rs232() in it had the
following lines to setup the UART.
Quote:

0046: MOVLW 0C
0047: MOVWF SPBRG
0048: MOVLW A6
0049: MOVWF TXSTA
004A: MOVLW 90
004B: BCF STATUS.RP0
004C: MOVWF RCSTA

I didn't see any lines in the .LST file that would change the TRIS on pin B3
to be an input, which would prevent it from driving the LED.

Here's the code in vs. 4.014. CCS does do something a little weird.
They use the obsolete TRIS instruction even though the 16F88 data sheet
says not to use it. But it shouldn't matter anyway because in the next
lines, CCS sets the TRISB.3 bit = 0, which makes it an output pin.
Code:

....................      set_tris_b(4); // b2 serial input 
006C:  MOVLW  04
006D:  TRIS   6
....................    OUTPUT_LOW(LED);                             
006E:  BSF    03.5  // Bank 1
006F:  BCF    06.3  // TRISB.3 = 0
0070:  BCF    03.5  // Bank 0
0071:  BCF    06.3  // PortB.3 = 0


Maybe I missed something, but do you guarantee that merely
un-commenting the #use rs232() line will make the LED fail to turn on ?

If it does, I think you should strip down the program to almost nothing.
Get rid of 90% of the code and make a test program for the LED and
#use rs232() only. See if adding #use rs232() really makes the LED
fail to turn on.
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Mon Feb 27, 2012 2:26 am     Reply with quote

There's a lot of confusion here... set_tris_x() only has any effect if fast_io is set for that port. As has pointed out you are not using fast_io. Even if you were, your set_tris_b() will do precisely the opposite of what you say it does. All IOs are inpus by default (which is why your LED "starts high" when you omit the set_tris, in fact it will always "start high", i.e,. be pulled high by the LED, until you set the tris in fast io mode, or output to it the first time in standard IO mode) and must be actively set as outputs by setting the relevant tris bit high. So your statement:

set_tris_b(4); //PIN_B2 serial input, others output

should read

set_tris_b(251); //PIN_B2 serial input, others output

This also illustrates a problem with using decimal constants in set_tris(): its very confusing. Its probably best here to use binary constants as its much clearer. Hex is better than decimal, but not as clear as binary:

set_tris_b(0b11111011); //PIN_B2 serial input, others output

Also you must be aware that manually setting the tristate control for a port can conflict with other uses for that port, in this case the UART. Also your use rs232 may well override your tris setting, or part of it. In any case your application doesn't need fast io, and all these confusions.

RF Developer
redicon



Joined: 26 Feb 2012
Posts: 8

View user's profile Send private message

PostPosted: Mon Feb 27, 2012 5:39 am     Reply with quote

RF_Developer in TRIS register 0 make pin as output, 1 as input.
This line is wrong
Code:
set_tris_b(0b11111011); //PIN_B2 serial input, others output

Because all pin is set as input except B2 that is an output
But this is not my problem because i do not use fast_io.

PCM programmer wrote:
Maybe I missed something, but do you guarantee that merely
un-commenting the #use rs232() line will make the LED fail to turn on ?

Yes, i guarantee. Only removing #use rs232 the program starts working.

PCM programmer wrote:
If it does, I think you should strip down the program to almost nothing.
Get rid of 90% of the code and make a test program for the LED and
#use rs232() only. See if adding #use rs232() really makes the LED
fail to turn on.
I'll do. Let me to buy some new PICs to test it in a new enviorment.
redicon



Joined: 26 Feb 2012
Posts: 8

View user's profile Send private message

PostPosted: Thu Mar 01, 2012 7:35 am     Reply with quote

New pic, ccs 4.093.
This code works fine:
Code:
#include <16F88.h>

#FUSES NOWDT       //No Watch Dog Timer
#FUSES INTRC_IO    //Internal RC Osc
#FUSES NOPUT       //No Power Up Timer
#FUSES NOMCLR       //Master Clear pin used for I/O
#FUSES NOBROWNOUT    //Reset when brownout detected
#FUSES NOLVP       //No low voltage programing, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD       //No EE protection
#FUSES NOWRT       //Program memory not write protected
#FUSES NODEBUG       //Debug mode for use with ICD
#FUSES NOPROTECT    //Code not protected from reading
#FUSES NOFCMEN       //Fail-safe clock monitor disabled
#FUSES NOIESO       //Internal External Switch Over mode disabled

#define TIMER0INIT 59286

#use delay(clock=2000000)
//#use rs232(baud=9600,xmit=PIN_B5,rcv=PIN_B2,parity=N,bits=8,ERRORS)

#DEFINE LED   PIN_B3

int cont = 0;
#int_TIMER1
//interrupt each 0,1 s
void timer1_isr(void)
{
   cont++;
   set_timer1(TIMER0INIT);
}


main()
{

   setup_oscillator(OSC_2MHZ);
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_comparator(NC_NC_NC_NC);
   
   OUTPUT_LOW(LED);                           

     setup_timer_1 (T1_INTERNAL|T1_DIV_BY_8);
   enable_interrupts(GLOBAL);
   enable_interrupts(INT_TIMER1);
   set_timer1(TIMER0INIT);
         
   while(1)
   {
      if (cont == 30) // after 3sec
      {   
         cont =0;
         OUTPUT_HIGH(LED);
      }
   }
}


This code is wrong, the led starts high:
Code:
#include <16F88.h>

#FUSES NOWDT       //No Watch Dog Timer
#FUSES INTRC_IO    //Internal RC Osc
#FUSES NOPUT       //No Power Up Timer
#FUSES NOMCLR       //Master Clear pin used for I/O
#FUSES NOBROWNOUT    //Reset when brownout detected
#FUSES NOLVP       //No low voltage programing, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD       //No EE protection
#FUSES NOWRT       //Program memory not write protected
#FUSES NODEBUG       //Debug mode for use with ICD
#FUSES NOPROTECT    //Code not protected from reading
#FUSES NOFCMEN       //Fail-safe clock monitor disabled
#FUSES NOIESO       //Internal External Switch Over mode disabled

#define TIMER0INIT 59286

#use delay(clock=2000000)
#use rs232(baud=9600,xmit=PIN_B5,rcv=PIN_B2,parity=N,bits=8,ERRORS)

#DEFINE LED   PIN_B3

int cont = 0;
#int_TIMER1
//interrupt each 0,1 s
void timer1_isr(void)
{
   cont++;
   set_timer1(TIMER0INIT);
}


main()
{

   setup_oscillator(OSC_2MHZ);
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_comparator(NC_NC_NC_NC);
   
   OUTPUT_LOW(LED);                           

     setup_timer_1 (T1_INTERNAL|T1_DIV_BY_8);
   enable_interrupts(GLOBAL);
   enable_interrupts(INT_TIMER1);
   set_timer1(TIMER0INIT);
         
   while(1)
   {
      if (cont == 30) // after 3sec
      {   
         cont =0;
         OUTPUT_HIGH(LED);
      }
   }
}


Where is the problem?
Ttelmah



Joined: 11 Mar 2010
Posts: 19365

View user's profile Send private message

PostPosted: Thu Mar 01, 2012 10:05 am     Reply with quote

The code runs fine, with your compiler version....

The one thing that changes, is that pin B5, is driven high in the second case.
Are you sure you don't have a wiring short, or are testing the wrong pin?.

Best Wishes
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