Jump to content

Help with coding...C

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
13 replies to this topic

#1
nelsonboy

nelsonboy

    Newbie

  • Members
  • Pip
  • 7 posts
Hi evy1.
i am new to this forum .
also noob in C language.
but still i have to complete my final year proejct.
so i have just written the C coding for my project.

dont nuke / flame me.
i know it is easy for some of you.
i am still learning.
i wll using MPLAB and PICC LITE software.

=========================
//==========================================================================
//	include
//==========================================================================
#include <pic.h>

//	configuration
//==========================================================================
__CONFIG (0x3F32);

//	define
//==========================================================================
#define	SevenSegment1	RA0
#define	SevenSegment2	RA1
#define	SevenSegment3	RA2
#define	SevenSegment4	RA3
#define	SevenSegment5	RA4
#define	SevenSegment6	RA5
#define	SevenSegment7	RC0



#define	SevenSegment1a	RC1
#define	SevenSegment2a	RC2
#define	SevenSegment3a	RC3
#define	SevenSegment4a	RC4
#define	SevenSegment5a	RC5
#define	SevenSegment6a	RC6
#define	SevenSegment7a	RC7


#define	stage1		RB0
#define	stage2		RB1
#define stage3 		RB2



//	main function
//==========================================================================
void main(void)
{
	unsigned char status;
	unsigned long tempA,tempB;

	ADCON1 = 0x06;							

	TRISA = 0b00000000;				
	TRISB = 0b11111111;			
	TRISC = 0b00000000;			

	SevenSegment1=0;	
	SevenSegment2=0;	
	SevenSegment3=0;	
	SevenSegment4=0;		
	SevenSegment5=0;		
	SevenSegment6=0;		
	SevenSegment7=0;		
	SevenSegment2a=0;		
	SevenSegment3a=0;		
	SevenSegment4a=0;		
	SevenSegment5a=0;		
	SevenSegment6a=0;		
	SevenSegment7a=0;		

	


	while(1)								
	{
		//scan input
		if(stage1==1)		
		{
			while(stage1==1);
			status=1;
			tempA=0;
		}

		if(stage3==1)		
			status=3;

		//processing output
		switch(status)
		{
			//Permitted period mode (15 seconds)
			case 1:
				
				SevenSegment1=1;
				SevenSegment2=1;
				SevenSegment3=1;
				SevenSegment4=1;
				SevenSegment5=1;
				SevenSegment6=1;
				SevenSegment1a=1;
				SevenSegment2a=1;
				SevenSegment4a=1;
				SevenSegment5a=1;
				SevenSegment7a=1;
				

				tempA+=1;
				if(tempA>1000000)
				{
					
					status=2;
				}
				else siren=0;
				break;

			//Scanning mode
			case 2:

			
				tempB+=1;
				if(tempB<25000)   
    
				SevenSegment1=1;
				SevenSegment3=1;
				SevenSegment4=1;
				SevenSegment6=1;
				SevenSegment7=1;
				SevenSegment1a=1;
				SevenSegment2a=1;
				SevenSegment3a=1;
				SevenSegment6a=1;

		
				else if(tempB<100000) 

				SevenSegment1=0;
				SevenSegment3=0;
				SevenSegment4=0
				SevenSegment6=0
				SevenSegment7=0
				SevenSegment1a=0;
				SevenSegment2a=0;
				SevenSegment3a=0;
				SevenSegment6a=0;

				else tempB=0;
				break;

			//Detected mode
			case 3:
				
				tempB+=1;
				if(tempB<40000) 			
				{
				SevenSegment1=1;
				SevenSegment2=1;
				SevenSegment3=1;
				SevenSegment4=1;
				SevenSegment5=1;
				SevenSegment6=1;
				SevenSegment3a=1;
				SevenSegment4a=1;
				SevenSegment5a=1;
			

		
				
					

				}
				else if(tempB<60000)
				{
				SevenSegment1=0;
				SevenSegment2=0;
				SevenSegment3=0;
				SevenSegment4=0;
				SevenSegment5=0;
				SevenSegment6=0;
				SevenSegment3a=0;
				SevenSegment4a=0;
				SevenSegment5a=0;



					

				}
				else tempB=0;
				break;
		}
	}
}

===========================
ok now.
let me explain abit about my project.
i have completed my motion detector project with PIC16F876A(first part)
now i wanna add 7segment display to my project with another PIC16F876A(part2)

assume that :-
when PIC16F876A(part2)

receive a continous "1" at pin RB0 , certain panel on 7segment will turn on
receive a continous "1" at pin RB1 , certain panel on 7segment will turn on/off(blinking)
receive a continous "1" at pin RB2 , certain panel on 7segment will turn on/off(blinking)

it has got 3 status.
status1 : permitted period 15seconds @ RB0 pin
status2 : scanning @ RB1 pin
status3 : trigger @ RB2 in

---------
i have manage to output "1" from each of the 3status from PIC16F876A(first part)

so now i need to connect the output "1" from PIC part 1 to PIC part2
in order to trigger the 7segment display for each status.

...
So is the above coding correct ?
i have not compile yet.
just wanna some guidance before i burn into my PIC.
thanksss... -)

Edited by WingedPanther, 17 April 2009 - 07:24 AM.
add code tags (the # button)


#2
Lance

Lance

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
Not sure what compiler you will be using, but standard C do not support binary number.

TRISA = 0b00000000;

TRISB = 0b11111111;

TRISC = 0b00000000;

will most likely trigger a compile-time error.

#3
Lance

Lance

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
Minor issues fixed in the while loop.

    while(1)
    {
        //scan input
        if(stage1==1)
        {
            while(stage1==1)
                ; // wait indefinitely until stage1 is other than 1
            status=1;
            tempA=0;
        }

        if(stage3==1)
            status=3;

        //processing output
        switch(status)
        {
        //Permitted period mode (15 seconds)
        case 1:

            SevenSegment1=1;
            SevenSegment2=1;
            SevenSegment3=1;
            SevenSegment4=1;
            SevenSegment5=1;
            SevenSegment6=1;
            SevenSegment1a=1;
            SevenSegment2a=1;
            SevenSegment4a=1;
            SevenSegment5a=1;
            SevenSegment7a=1;


            ++tempA;
            if(tempA>1000000)
                status=2;
            else
                siren=0;
            break;

        //Scanning mode
        case 2:
            ++tempB;
            if(tempB<25000)
            {

                SevenSegment1=1;
                SevenSegment3=1;
                SevenSegment4=1;
                SevenSegment6=1;
                SevenSegment7=1;
                SevenSegment1a=1;
                SevenSegment2a=1;
                SevenSegment3a=1;
                SevenSegment6a=1;

            }
            else if(tempB<100000)
            {
                SevenSegment1=0;
                SevenSegment3=0;
                SevenSegment4=0
                SevenSegment6=0
                SevenSegment7=0
                SevenSegment1a=0;
                SevenSegment2a=0;
                SevenSegment3a=0;
                SevenSegment6a=0;
            }
            else
                tempB=0;
            break;

        //Detected mode
        case 3:

            ++tempB;
            if(tempB<40000)
            {
                SevenSegment1=1;
                SevenSegment2=1;
                SevenSegment3=1;
                SevenSegment4=1;
                SevenSegment5=1;
                SevenSegment6=1;
                SevenSegment3a=1;
                SevenSegment4a=1;
                SevenSegment5a=1;
            }
            else if(tempB<60000)
            {
                SevenSegment1=0;
                SevenSegment2=0;
                SevenSegment3=0;
                SevenSegment4=0;
                SevenSegment5=0;
                SevenSegment6=0;
                SevenSegment3a=0;
                SevenSegment4a=0;
                SevenSegment5a=0;
            }
            else
                tempB=0;
            break;
        }
    }



#4
nelsonboy

nelsonboy

    Newbie

  • Members
  • Pip
  • 7 posts
while(1)
    {
        //scan input
        if(stage1==1)
        {
            while(stage1==1)
                ; // wait indefinitely until stage1 is other than 1
            status=1;
            tempA=0;
        }

        if(stage3==1)
            status=3;

=====================
what do you meant by waiting indefinitely until stage 1 is other than 1 ??
hmm.....
if stage1 = 1 ,
it will move to case 1 ?
if stage1=0 ?
keep looping ?

really noob in this.
-(

Edited by WingedPanther, 17 April 2009 - 07:25 AM.
add code tags (the # button)


#5
Lance

Lance

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts

Quote

=====================
what do you meant by waiting indefinitely until stage 1 is other than 1 ??
hmm.....
if stage1 = 1 ,
it will move to case 1 ?
if stage1=0 ?
keep looping ?

really noob in this.

did you write the code yourself , or are you borrowing somebody else code :)

I just move the ';' to a new line and add a comment cause that's exactly what you mean by your code. I wouldn't know the business logic as well as you do.

OK.
while (stage1==1);  // your code
is equivalent to
while (stage1==1)
     ;  // my re-format

And both means test stage1 until it's not 1.

I suspect there is a logical error there, that's why I made the comment to bring to your attention. But I wouldn't be able to know for sure.

stage1 is not declared as volatile and you didn't allow a chance for it to be modified in the loop body; I suspect this would be a infinite loop, and hence a logic error. It's upto you to decide.

Edited by Lance, 17 April 2009 - 07:55 AM.
typo


#6
Lance

Lance

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
If certain variable are read from port or something, you should declare them as volatile to prevent compiler from doing constant optimization. (Compiler may record your access to a variable, if it decide you didn't modify it between two reads, it may decide to use the cached version. If this is not desired, inform the compiler by declaring the variable as volatile.

#7
nelsonboy

nelsonboy

    Newbie

  • Members
  • Pip
  • 7 posts
yea. i borrowed from others and editted it to get the output that i wanted lolx
but i am still learning -)

erm
meaning stage1 =1
if stage1=0 what will happen ??

yes it is a infinite loop.
all i need is just 3 status only.
that why it keeps looping between those 3.

hmm.....kinda blur about volatile....-(

i hv define stage1 = RB0
RB0 will be receiving "1" from first PIC.

do u meant what if RB0 is receiving "0" from first PIC ???

#8
Lance

Lance

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
OIC.

Your code, even with my addition, would not work as intended unless you are using a compiler that does no optimization, or you turn off all optimization.

Right, RB0, I suppose, is some memory location that's associated with a hardware port, whose content will change as a consequence of hardware events, but the compiler would not know that. That's the whole point behind C/C++ keyword volatile. Without that, your program may not produce consistent/intended behavior under different compilers/compiling flags.

#9
nelsonboy

nelsonboy

    Newbie

  • Members
  • Pip
  • 7 posts
while(1)

    {

        //scan input

        if(stage1==1)

        {

            while(stage1==1) continues ; // would it be better ?

            status=1;

            tempA=0;

        }


        if(stage3==1)

            status=3;


Better ?
or how do i turn off the opt ?

i am using PICC LITE and MPLAB to compile.

#10
Lance

Lance

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
Makes absolutely no difference.

I don't have access to pic.h, chance is, RB0 and its kind have been declared as int volatile , than you don't have to worry about it.

So give it a try. The worst case, you can use a function to read RB0

int read_rb0()

{

        return RB0;

}


and in the loop

      while( read_rb0() == 1)

           ;

It's unlikely any optimization will break it in this case. And since it's in a idle loop, the cost of a function call is negligible.


BTW, here are missing some semi-colons

 case 2:

            ++tempB;

            if(tempB<25000)

            {


                SevenSegment1=1;

                SevenSegment3=1;

                SevenSegment4=1;

                SevenSegment6=1;

                SevenSegment7=1;

                SevenSegment1a=1;

                SevenSegment2a=1;

                SevenSegment3a=1;

                SevenSegment6a=1;


            }

            else if(tempB<100000)

            {

                SevenSegment1=0;

                SevenSegment3=0;

                SevenSegment4=0[B] <-------[/B]

                SevenSegment6=0 [B]<--------[/B]

                SevenSegment7=0[B] <-------[/B]

                SevenSegment1a=0;

                SevenSegment2a=0;

                SevenSegment3a=0;

                SevenSegment6a=0;

            }



#11
nelsonboy

nelsonboy

    Newbie

  • Members
  • Pip
  • 7 posts
Clean: Deleting intermediary and output files.
Clean: Deleted file "E:\Documents and Settings\Administrator\Desktop\Testing_2\7segment.p1".
Clean: Deleted file "E:\Documents and Settings\Administrator\Desktop\Testing_2\7Segment_Trial1.cof".
Clean: Deleted file "E:\Documents and Settings\Administrator\Desktop\Testing_2\7Segment_Trial1.hex".
Clean: Deleted file "E:\Documents and Settings\Administrator\Desktop\Testing_2\7Segment_Trial1.sym".
Clean: Deleted file "E:\Documents and Settings\Administrator\Desktop\Testing_2\7Segment_Trial1.map".
Clean: Deleted file "E:\Documents and Settings\Administrator\Desktop\Testing_2\7Segment_Trial1.hxl".
Clean: Deleted file "E:\Documents and Settings\Administrator\Desktop\Testing_2\startup.lst".
Clean: Deleted file "E:\Documents and Settings\Administrator\Desktop\Testing_2\startup.rlf".
Clean: Deleted file "E:\Documents and Settings\Administrator\Desktop\Testing_2\7Segment_Trial1.sdb".
Clean: Deleted file "E:\Documents and Settings\Administrator\Desktop\Testing_2\7Segment_Trial1.obj".
Clean: Deleted file "E:\Documents and Settings\Administrator\Desktop\Testing_2\7Segment_Trial1.lst".
Clean: Deleted file "E:\Documents and Settings\Administrator\Desktop\Testing_2\7Segment_Trial1.rlf".
Clean Warning: File "E:\Documents and Settings\Administrator\Desktop\Testing_2\doprnt.pre" doesn't exist.
Clean: Done.
Build E:\Documents and Settings\Administrator\Desktop\Testing_2\7Segment_Trial1 for device 16F876A
Using driver E:\Program Files\HI-TECH Software\PICC\PRO\9.65\bin\picc.exe

Executing: "E:\Program Files\HI-TECH Software\PICC\PRO\9.65\bin\picc.exe" --pass1 "E:\Documents and Settings\Administrator\Desktop\7segment.c" -q --chip=16F876A -P --runtime=default --opt=default -g --asmlist "--errformat=Error [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s"
Executing: "E:\Program Files\HI-TECH Software\PICC\PRO\9.65\bin\picc.exe" -o7Segment_Trial1.cof -m7Segment_Trial1.map --summary=default --output=default 7segment.p1 --chip=16F876A -P --runtime=default --opt=default -g --asmlist "--errformat=Error [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s"
HI-TECH C PRO for the PIC10/12/16 MCU family V9.65
Copyright © 1984-2009 HI-TECH SOFTWARE
licensed for evaluation purposes only
this licence will expire on Fri, 05 Jun 2009

Memory Summary:
Program space used DCh ( 220) of 2000h words ( 2.7%)
Data space used 9h ( 9) of 170h bytes ( 2.4%)
EEPROM space used 0h ( 0) of 100h bytes ( 0.0%)
Configuration bits used 1h ( 1) of 1h word (100.0%)
ID Location space used 0h ( 0) of 4h bytes ( 0.0%)

Loaded E:\Documents and Settings\Administrator\Desktop\Testing_2\7Segment_Trial1.cof.

********** Build successful! **********

#12
nelsonboy

nelsonboy

    Newbie

  • Members
  • Pip
  • 7 posts
repost.