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

Need simple code help please.

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



Joined: 01 May 2004
Posts: 49

View user's profile Send private message

Need simple code help please.
PostPosted: Sun May 02, 2004 1:01 am     Reply with quote

I'm using a PIC16F77.A to control a small electronics project of mine, this is my first time trying this and it seems my one semester of C Language isn't helping much.

I just need a set of push button switches to give me an output to control some relays, but I just just can't seem to get it right. I'm pretty sure I have the push buttons and relays set up correctly (electronics I understand) but I just can't seem to get the code right. Could someone look at this for me and tell me what I'm doing wrong?

Code:
#include <16f877A.h>
#device ICD=TRUE
#fuses HS,NOWDT,NOPROTECT
//#byte TRISA = 0x85
//#byte PORTA = 0x05
#byte TRISB = 0x86
#byte PORTB = 0x06
#byte TRISD = 0x88
#byte PORTD = 0x08

#byte ADCON0 = 0x1F
#byte ADCON1 = 0x9F
#byte ADRESH = 0x1E
#byte OPTION = 0x81
#bit T0IF = 0x0B.2
#bit GO = 0x1F.2
#bit ADIF = 0x0c.6


/* Define Outputs */
#bit Paint = PORTD.0
#bit Preheat = PORTD.1
#bit Plastic = PORTD.2
#bit Metal = PORTD.3

/* Define Inputs */
#bit PB_Paint = PORTB.0
#bit PB_Preheat = PORTB.1
#bit PB_Plastic = PORTB.2
#bit PB_Metal = PORTB.3


#use delay(clock=20000000)
#use rs232(DEBUGGER)


   void main(void)

{


   ADCON0 = 0x41;      /* Fosc/8, A/D enabled */
   OPTION = 0x07;      /* TMR0 prescaller, 1:256, PORTB pullups */
   ADCON1 = 0x0E;      /* Left justify, 1 analog channel, VDD and Vss references */


   //TRISA = 0;      /* PORTA all outputs */
   TRISB = 0xFF;      /* PORTD all inputs */
   TRISD = 0;      /* PORTB all 0utputs */
   Paint = 0;     /* Clear Paint */
   Preheat = 0;      /* Clear Preheat */
   Plastic = 0;      /* Clear Plastic */
   Metal = 0;      /* Clear Metal */

BACK:   do
{;}
   while (PB_Paint&PB_Preheat&PB_Plastic&PB_Metal == 1);

   if (PB_Paint = 0)
   {
      Paint = 1;
      printf("Paint");
 
   }


   else if (PB_Preheat = 0)
   {
      Preheat = 1;
      printf("Preheat");
     
   }


   else if (PB_Plastic = 0)
   {
      Plastic = 1;
      printf("Plastic");
     
   }


   else if (PB_Metal = 0)
   {
      Metal = 1;
      printf("Metal");
     
   }
   
   

goto BACK;         /* Check again */

}


And here's part of the assembly code, it seems that my if/else statements don't do anything.

Code:
....................
.................... BACK:   do
.................... {;}
....................    while (PB_Paint&PB_Preheat&PB_Plastic&PB_Metal == 1);
....................
....................    if (PB_Paint = 0)
0020:  MOVLW  00
0021:  BTFSC  06.0
0022:  MOVLW  01
0023:  MOVWF  21
0024:  MOVLW  00
0025:  BTFSC  06.1
0026:  MOVLW  01
0027:  ANDWF  21,W
0028:  MOVWF  22
0029:  MOVLW  00
002A:  BTFSC  06.2
002B:  MOVLW  01
002C:  ANDWF  22,W
002D:  MOVWF  23
002E:  MOVLW  00
002F:  BTFSC  06.3
0030:  MOVLW  01
0031:  SUBLW  01
0032:  BTFSC  03.2
0033:  GOTO   036
0034:  MOVLW  00
0035:  GOTO   037
0036:  MOVLW  01
0037:  ANDWF  23,W
0038:  XORLW  00
0039:  BTFSS  03.2
003A:  GOTO   020
....................    {
....................       Paint = 1;
....................       printf("Paint");
....................
....................    }
....................
....................
....................    else if (PB_Preheat = 0)
....................    {
....................       Preheat = 1;
....................       printf("Preheat");
....................
....................    }
....................
....................
....................    else if (PB_Plastic = 0)
....................    {
....................       Plastic = 1;
....................       printf("Plastic");
....................
....................    }
....................
....................
....................    else if (PB_Metal = 0)
....................    {
....................       Metal = 1;
....................       printf("Metal");
....................
....................    }
....................
....................
....................
.................... goto BACK;         /* Check again */



Thank you for any help you can give, I've spent all day trying to figure this out, and I've searched every where for answers.
hillcraft



Joined: 22 Sep 2003
Posts: 101
Location: Cape Town (South africa)

View user's profile Send private message Send e-mail Visit poster's website

Need simple code help please.
PostPosted: Sun May 02, 2004 1:32 am     Reply with quote

This is an age old common mistake.

>>else if (PB_Preheat = 0)

All that you are doing is setting the PB_Preheat variable equal to zero.

You must use == to test for equality in C

So all the else if must be changed to ==
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Sun May 02, 2004 11:16 am     Reply with quote

Code:
if (PB_Paint = 0)


This will always evaluate to TRUE because you are using the assignment operator "=" instead of the equality operator "==". You seem to have done this in several places.

Also, unless you are intending to use more than one brand of compiler you don't need to go to all the trouble of setting up registers yourself. The CCS compiler has a rich set of built-in functions for dealing with perpherals.

And finally, typically, you don't see the "goto" statement used much as it is considered bad practice. Use a do-while-, while- or for-loop instead.

Code:

do {
// all my code to repeat over and over and over
} while(1);


--- or ---

Code:

while (1) {
// all my code to repeat over and over and over
}


The key difference is that a do-while is a "bottom tested loop" and so is guaranteed to run at least once and the while{} loop is a "top tested loop" and depending on what you put in as the expression, may not run at all.

A third choice is the for{} loop

Code:

for(;;;)
{
}

This you can make behave like the while{} loop by fiddling with the expression at the top or like the do-while loop by including an if-test at the bottom with a "break" statement.

Right off the top of my head I don't remember which form the CCS compiler is the best at implementing. But since you have figured out that the LST file is your friend you can play around and decide for yourself! :-)

A classroom is fine but the C language (and any programming language for that matter) has enough little gotcha's that the best way to learn is to practice!
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
RatFink



Joined: 01 May 2004
Posts: 49

View user's profile Send private message

PostPosted: Sun May 02, 2004 1:31 pm     Reply with quote

Thank you guys, I can't believe I misses the "=" instead of "==", I was tearing my hair out wondering why it wouldn't work.

Quote:
Also, unless you are intending to use more than one brand of compiler you don't need to go to all the trouble of setting up registers yourself. The CCS compiler has a rich set of built-in functions for dealing with perpherals.


But that would require research, which is work Rolling Eyes I'll look into it, simple would be best.

Also, I'm not sure what you mean by the goto statement, I thought I was using a "do/while" statement, I'll check and see what you mean.


All I've had in school for C programming was just a quick overview, not really to teach me how to write it, mostly just so that if I ran into it I would know what I was looking at.
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Sun May 02, 2004 1:45 pm     Reply with quote

Invest in a copy of C Programming Language (2nd Edition) by Kernighan and Ritchie, ISBN 0131103628. Should be less than $40 new and you can probably find a used copy for less. If you get a used one, just make sure it is the 2nd edition and not the first.

Lots of other good C books out there but this one still holds up well after 26 years (16 for the 2nd edition)!
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
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