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

weird occurance dont understand

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







weird occurance dont understand
PostPosted: Tue Sep 09, 2008 8:54 am     Reply with quote

Hello

I have encountered a problem which i really dont understand please help
below is a code snippet 1 with its associated header file.

First is a main.h file that contains a some simple micros that set TRIS directions and set them high of low. In this instance I am using or's and ands to turn bits high and low.

main1.h file
Code:


/**********************************************************/
/*                                                                            */
/*    PROJECT SCOPE SECTION                                                   */
/*                                                                            */



/**********************************************************/
/*                                                                            */
/*    CONSTANTS/MACROS (#Def's) AVAILABLE TO WHOLE PROJECT                    */
/*                                                                            */
/**********************************************************/
#define bitTest(var,bitNo)  ((var & (1<<bitNo)) != 0)
#define bitSet(var,bitNo)   (var |= (1<<bitNo))
#define bitClear(var,bitNo) (var &= ~(1<<bitNo))



/************************************************************/
/*                                                                            */
/*    DEFINES (#Def's) AVAILABLE TO FILE                             */
/*                                                                            */
/************************************************************/
#define   BIT0   0x01
#define   BIT1   0x02
#define   BIT2   0x04
#define   BIT3   0x08
#define   BIT4   0x10
#define   BIT5   0x20
#define   BIT6   0x40
#define   BIT7   0x80


#define BUTTON1_B   BIT0
#define BUTTON1_P   PORTB
#define BUTTON1_D   INPUT
#define BUTTON1_SET_TRIS  (TRISB&= ~(BUTTON1_B & BUTTON1_D))
#define SET_BUTTON1   (BUTTON1_P |= BUTTON1_B)
#define CLR_BUTTON1   (BUTTON1_P &= ~BUTTON1_B)
#define BUTTON1_PRESSED   (BUTTON1_P & BUTTON1_B)




main1.c contains a main entry file that sets the tris directions only
Code:

***********************************************************
*
*
*
************************************************************/

#define _MAIN1_C_


/******************************************************
;          Include files. Application dependent
******************************************************/

#include<18F45K20.h>
#fuses H4                     /* Config bit. HSPLL x4 enabled */
#fuses CCP2B3                   /* Config bit. Multiplex PWM pins to use B3 */ 
//#fuses NODEBUG
#DEVICE ADC=10                  // CCS why of setting adc to 10 bit         
//#DEVICE ICD=FALSE
#priority int_timer0,int_timer3       /* CCS why of setting up interrupt priorities */     
#use delay(clock=64000000)         /* CCS Used for inbuilt functions. used for delay_X() functions */
#use rs232(baud = 115000,xmit=PIN_C6,rcv=PIN_c7)   /* CCS Used for inbuilt functions. used for printf functions */

   #include "PIC18F45K20_registers.h"
   #include "main3.h"
   #include "tdef.h"



void main()
{
  //TRISB = 0x00;

  BUTTON1_SET_TRIS;

   while(1)
   {
   }

}



Now here is the problem. In the main.c file you can see that if i set the TRISB = 0x00; then use the macro BUTTON1_SET_TRIS;

the trisb1 should go high and the reset should go low. however it does not do this. the macro does not seem to have any affect. Why is this.

please note:- If i declare the BUTTON1_SET_TRIS to be TRISB1 = 1; this seems to work.

I would like to understand why as, in that If before I set any TRIstates, i put TRISB = 0x00; then use these macros with ors and ands it does not seem to work. if I dont have this TRISB = 0x00; in the beginning then the BUTTON1_SET_TRIS seems to work.

regards
newbie333
Guest







PostPosted: Tue Sep 09, 2008 8:58 am     Reply with quote

SOrry forgot to mention. I am using CCS version 4.077 amd mplab 8.10.
Guest








PostPosted: Tue Sep 09, 2008 9:30 am     Reply with quote

This is simple enough an example - look in the lst file and see if the compiler is doing what you ask. You might simplify the macro some to make it easier to see what the compiler is doing.

HTH - Steve H.
newbie333
Guest







PostPosted: Tue Sep 09, 2008 9:57 am     Reply with quote

You read my mind steve.

the disassembly show me

Snippet 1 of disassembly....
In the code below is the disassembly with BUTTON1_SET_TRIS defined as

#define BUTTON1_SET_TRIS (TRISB1 = 1)


Code:


  0004    6AF8     CLRF 0xff8, ACCESS
  0006    9ED0     BCF 0xfd0, 0x7, ACCESS
  0008    6AEA     CLRF 0xfea, ACCESS
  000A    6AE9     CLRF 0xfe9, ACCESS
  000C    86B8     BSF 0xfb8, 0x3, ACCESS
  000E    0E8A     MOVLW 0x8a
  0010    6EAF     MOVWF 0xfaf, ACCESS
  0012    0E00     MOVLW 0
  0014    6EB0     MOVWF 0xfb0, ACCESS
  0016    0EA6     MOVLW 0xa6
  0018    6EAC     MOVWF 0xfac, ACCESS
  001A    0E90     MOVLW 0x90
  001C    6EAB     MOVWF 0xfab, ACCESS
  001E    507E     MOVF 0xf7e, W, ACCESS
  0020    0BE0     ANDLW 0xe0
  0022    6E7E     MOVWF 0xf7e, ACCESS
  0024    98C1     BCF 0xfc1, 0x4, ACCESS
  0026    9AC1     BCF 0xfc1, 0x5, ACCESS
  0028    0E00     MOVLW 0
  002A    6E7F     MOVWF 0xf7f, ACCESS
  002C    9A79     BCF 0xf79, 0x5, ACCESS
  002E    9879     BCF 0xf79, 0x4, ACCESS
  0030    6A7B     CLRF 0xf7b, ACCESS
  0032    6A7A     CLRF 0xf7a, ACCESS
32:                  //TRISB = 0x00;
33:               
34:                  BUTTON1_SET_TRIS;
  0034    9293     BCF 0xf93, 0x1, ACCESS
35:               
36:                   while(1)
37:                   {
38:                   }
  0036    D7FF     BRA 0x36
39:               
  0038    0003     SLEEP


The above code produces expected code:: (line 0034)



Snippet 2 of disassembly....
In the code below is the disassembly with BUTTON1_SET_TRIS defined as

#define BUTTON1_SET_TRIS (TRISB&= ~(BUTTON1_B & BUTTON1_D))


Code:


30:                void main()
31:                {
  0004    6AF8     CLRF 0xff8, ACCESS
  0006    9ED0     BCF 0xfd0, 0x7, ACCESS
  0008    6AEA     CLRF 0xfea, ACCESS
  000A    6AE9     CLRF 0xfe9, ACCESS
  000C    86B8     BSF 0xfb8, 0x3, ACCESS
  000E    0E8A     MOVLW 0x8a
  0010    6EAF     MOVWF 0xfaf, ACCESS
  0012    0E00     MOVLW 0
  0014    6EB0     MOVWF 0xfb0, ACCESS
  0016    0EA6     MOVLW 0xa6
  0018    6EAC     MOVWF 0xfac, ACCESS
  001A    0E90     MOVLW 0x90
  001C    6EAB     MOVWF 0xfab, ACCESS
  001E    507E     MOVF 0xf7e, W, ACCESS
  0020    0BE0     ANDLW 0xe0
  0022    6E7E     MOVWF 0xf7e, ACCESS
  0024    98C1     BCF 0xfc1, 0x4, ACCESS
  0026    9AC1     BCF 0xfc1, 0x5, ACCESS
  0028    0E00     MOVLW 0
  002A    6E7F     MOVWF 0xf7f, ACCESS
  002C    9A79     BCF 0xf79, 0x5, ACCESS
  002E    9879     BCF 0xf79, 0x4, ACCESS
  0030    6A7B     CLRF 0xf7b, ACCESS
  0032    6A7A     CLRF 0xf7a, ACCESS
32:                  //TRISB = 0x00;
33:               
34:                  BUTTON1_SET_TRIS;
35:               
36:                   while(1)
37:                   {
38:                   }
  0034    D7FF     BRA 0x34
39:               




as you can see nothing appears for the

#define BUTTON1_SET_TRIS (TRISB&= ~(BUTTON1_B & BUTTON1_D))


why??
Ttelmah
Guest







PostPosted: Tue Sep 09, 2008 10:01 am     Reply with quote

As shown, the macros, don't make sense. If you expand your line as posted, you get:

BUTTON1_SET_TRIS =>

Which expands as:

TRISB = TRISB & (~(0x1 & INPUT))

Input what?.

If you '&' something, you can only get a bit set in the output, if there is a '1' in the same place on both sides of the &. So, if you start with TRISB=0, you can never get a '1' in any bit of the result from this operation, whatever appears on the other side. Exactly what you are seeing....

Best Wishes
newbie333
Guest







PostPosted: Tue Sep 09, 2008 10:11 am     Reply with quote

INPUT is = 1
and
OUTPUT = 0

Therefore I think the state should be

TRISB = TRISB & (~(0x1 &1))

however it seems that i have been been an idiot, in CCS INPUT is a reserved word therefore it is getting confused.. need to change
INPUT to something else ie
#define DIR_INPUT 1
#define BUTTON1_D DIR_INPUT
and then

BUTTON1_SET_TRIS (TRISB&= ~(BUTTON1_B & BUTTON1_D))


it seems to work now.

thanks
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