Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

BoostC Help

Status
Not open for further replies.

usif

New Member
Hi guys, I have got a PIC dev kit with loads of LED's on it, and I have been successful in having them chase after each other. I have included the code I used for my 16F877A. I need help in creating a more efficient program.

Code:
// PIC16F877A
#include<system.h>

// _LVP_OFF disables Low Voltage Programming, enabling use of the RB3 output.
#pragma DATA _CONFIG, _CP_OFF & _PWRTE_OFF & _WDT_OFF & _HS_OSC & _LVP_OFF

#pragma CLOCK_FREQ 5000000

void main()
{
    // - turn portb inputs that we are using into digital mode.
    adcon1 = 00000110b;
    trisb = 0x00;
    trisc = 0x00;
    trisa = 0x00;
    while( 1 )
    {
            porta = 0x01;
        delay_ms( 50 );
        porta = 0x02;
        delay_ms( 50 );
        porta = 0x04;
        delay_ms( 50 );
        porta = 0x08;
        delay_ms( 50 );
        porta = 0x10;
        delay_ms( 50 );
        porta = 0x20;
        delay_ms( 50 );
        porta = 0x40;
        delay_ms( 50 );
        porta = 0x80;
        delay_ms( 50 );
        porta = 0x00;
    
        portb = 0x01;
        delay_ms( 50 );
        portb = 0x02;
        delay_ms( 50 );
        portb = 0x04;
        delay_ms( 50 );
        portb = 0x08;
        delay_ms( 50 );
        portb = 0x10;
        delay_ms( 50 );
        portb = 0x20;
        delay_ms( 50 );
        portb = 0x40;
        delay_ms( 50 );
        portb = 0x80;
        delay_ms( 50 );
        portb = 0x00;

        portc = 0x01;
        delay_ms( 50 );
        portc = 0x02;
        delay_ms( 50 );
        portc = 0x04;
        delay_ms( 50 );
        portc = 0x08;
        delay_ms( 50 );
        portc = 0x10;
        delay_ms( 50 );
        portc = 0x20;
        delay_ms( 50 );
        portc = 0x40;
        delay_ms( 50 );
        portc = 0x80;
        delay_ms( 50 );
        portc = 0x00;
        
    }
    
}

I was thinking of having i as a variable and implementing this statement, but when I try it doesn't work:

Code:
for (i=8;i>0;i--)

How can I use this statement in my program.

Code:
// PIC16F877A
#include<system.h>

// _LVP_OFF disables Low Voltage Programming, enabling use of the RB3 output.
#pragma DATA _CONFIG, _CP_OFF & _PWRTE_OFF & _WDT_OFF & _HS_OSC & _LVP_OFF

#pragma CLOCK_FREQ 5000000

void main()
{
    // - turn portb inputs that we are using into digital mode.
    adcon1 = 00000110b;
    trisb = 0x00;
    trisc = 0x00;
    trisa = 0x00;
    while( 1 )
    {
    
		int i
		
		for(i=8;i>0;i--)
		
        porta = i;
        delay_ms( 50 );
             
         
        
    }
    
}

I know im awfully wrong with the second code:(. Also ignore the fact that there are no portb, or portc in the second code
 
Last edited:
Code:
    while( 1 )
    {
    
        int i;   // a semi-colon is required to tell the compiler that
                 // you're at the end of a command 
         
        for(i=8;i>0;i--)
        {
           // braces are required when you want to include
           // more lines in the body of the for instruction
           porta = i;
           delay_ms( 50 );
             
        }
        
    }
Now the code should compile correctly, but you might need to change your program using shift operators in the for loop to get the desired effect.

Your for loop sets PORTA pins as follows:
PORTA = 8 = b'00001000'
PORTA = 7 = b'00000111'
PORTA = 6 = b'00000110'
PORTA = 5 = b'00001001'
PORTA = 4 = b'00001000'
PORTA = 3 = b'00000011'
PORTA = 2 = b'00000010'
PORTA = 1 = b'00000001'
 
Last edited:
My guess at this time of night :D is,

Code:
void main()
{
char i;
    // - turn portb inputs that we are using into digital mode.
    adcon1 = 00000110b;
    trisb = 0x00;
    trisc = 0x00;
    trisa = 0x00;
    while( 1 )
    {
	for(i=1;i!=0;i<<=1){
	    porta = i;
            delay_ms( 50 );
        }
        porta=0;
        delay_ms( 50 );
    }  
}

Mike.
 
You might do it this way as well;

Code:
    while(1)
    { i = 1;                 // night rider effect
      while(i)               //
      { porta = i;           //
        delay_ms(50);        //
        i <<= 1;             // shift left
      }
      i = 128;               //
      while(i)               //
      { porta = i;           //
        i >>= 1;             // shift right
        delay_ms(50);        //
      }
    }
 
You might do it this way as well;

Code:
    while(1)
    { i = 1;                 // night rider effect
      while(i)               //
      { porta = i;           //
        delay_ms(50);        //
        i <<= 1;             // shift left
      }
      i = 128;               //
      while(i)               //
      { porta = i;           //
        i >>= 1;             // shift right
        delay_ms(50);        //
      }
    }

Hi Mike,

You missed out the all LEDs off state.:D

Mike.
 
If you want LEDs off between the back-and-forth scans, you might add a few more lines;

Code:
    while(1)
    { [COLOR=Red]porta = 0;             //  All LEDs off
      delay_ms(100);         //[/COLOR]
      i = 1;                 // night rider effect
      while(i)               //
      { porta = i;           //
        delay_ms(50);        //
        i <<= 1;             // shift left
      }
      [COLOR=Red]porta = 0;             // all LEDs off
      delay_ms(100);         //[/COLOR]
      i = 128;               //
      while(i)               //
      { porta = i;           //
        i >>= 1;             // shift right
        delay_ms(50);        //
      }
    }
 
Can anyone tell me whats wrong with this code? I am trying to get an input from porta.0 so that it would change direction of the LED chase.

Code:
#include<system.h>

// _LVP_OFF disables Low Voltage Programming, enabling use of the RB3 output.
#pragma DATA _CONFIG, _CP_OFF & _PWRTE_OFF & _WDT_OFF & _HS_OSC & _LVP_OFF

#pragma CLOCK_FREQ 20000000

void main()
{

	#define a porta.0;  // Define porta.0,1.2 as variable a,b,c
	#define b porta.1;  // These will be used as the input variables
	#define c porta.2;


    // - turn portb inputs that we are using into digital mode.
    adcon1; = 00000110b; //digital
    trisb = 0x00; //Output
    trisc = 0x00; // Output
    trisa = 0xFF; // Turn Porta A as an Input
    
    char i; // introduce I as a variable for the flash
       
    while( 1 )
    {
    
		for(i=0;i!=0;i<<=1){
			portb = i;
            delay_ms( 50 );
            }
			 
			portb = 0;
			delay_ms( 15 );
			
			
				if(a){

			for(i=0;i!=0;i>>=1) {
			portb = i;
			delay_ms ( 50 );
			 }                                                                            
			 portb = 0;
			 delay_ms ( 50 );
			 			 }
			 }
			 }
		
}
 
Last edited:
When you shift zero left it is still zero,

Try,
Code:
    while( 1 )
    {    
        for(i=[COLOR="red"]1[/COLOR];i!=0;i<<=1){
            portb = i;
            delay_ms( 50 );
        }
             
        portb = 0;
        delay_ms( 15 ); 
        if(a){
            for(i=[COLOR="Red"]128[/COLOR];i!=0;i>>=1) {
                portb = i;
                delay_ms ( 50 );
             }                                                                            
             portb = 0;
             delay_ms ( 50 );
         }
     }

Mike.
 
Last edited:
Mike, Thanks for your reply, and you pointed out another error which I hadnt found yet (normally I would compile, and look at any of the oddities of the project, and edit again)

However, what I failed to say in my previous post was that the code will not compile. Ive double checked for any oddities and the usual ";" forgotten (as a noob, I guess), and there are none. I get these errors:
Code:
Building...
BoostC Optimizing C Compiler Version 6.90 (for PIC16 architecture)
http://www.sourceboost.com
Copyright(C) 2004-2008 Pavel Baranov
Copyright(C) 2004-2008 David Hobday

Single user Lite License (Unregistered) for 0 node(s)
Limitations: PIC12,PIC16 max code size:2048 words, max RAM banks:2, Non commercial use only


SourceBoost1.c
C:\Documents and Settings\Yousif\Desktop\SourceBoost1.c(11): error: general error

failure
"C:\Program Files\SourceBoost\boostc.pic16.exe" SourceBoost1.c -t PIC16F877A 
Exit code was 1.
Removing target: SourceBoost1.obj
C:\Documents and Settings\Yousif\Desktop\SourceBoost1.c(11): error:  failure
Done

Line 11 is the opening brace after "void main ()". Also it gave me an error earlier about the "adcon1 = 00000110b;" line, which it said a semicolon was missing. I had used that line earlier in the previous project, and it compiled fine. I gave a semicolon after
"adcon1" and it shut up about that error. I have my doubts on this - is this usual?
 
Last edited:
Gayan,

It is always easier to follow C if the indentation is correct. Every { you indent 4 more characters and every } you indent 4 less characters.

Code:
    while(a==1) //active low type switch
    {
        PORTB = i;
        delay_ms(50);
        i=i<<1;
        if(i==0)
        {
            PORTB = 0;
            delay_ms(50);    
            i = 1;
        }
    }
    i = 128;
    while(b==1) //active low type switch
    {
        PORTB = i;
        delay_ms(50);
        i=i>>1;
        if(i==1)
        {
            PORTB = 0;
            delay_ms(50);    
            i = 128;
        }
    }

To make the light go one way and reverse whenever the button is pressed I would do,
Code:
    i=1;
    while(1)
    {
        if(a)
        {
            i>>=1;
            if(i==0)
                i=128;
        }else
        {
            i<<=1;
            if(i==0)
                i=1;
        }
        PORTB=i;
    }

Mike.
 
It took a while to find that error as the compiler gives a nonsense error message, the line
Code:
    adcon1; = 00000110b; //digital
should be,
Code:
    adcon1 = 0b00000110; //digital

Mike.
 
Oh, forgot, as well you have a brace too many.

Here is your code rearanged so it's more readable for me,
Code:
#include <system.h>

// _LVP_OFF disables Low Voltage Programming, enabling use of the RB3 output.
#pragma DATA _CONFIG, _CP_OFF & _PWRTE_OFF & _WDT_OFF & _HS_OSC & _LVP_OFF

#pragma CLOCK_FREQ 20000000

#define a porta.0   // Define porta.0,1.2 as variable a,b,c
#define b porta.1   // These will be used as the input variables
#define c porta.2

void main(void)
{
char i;                     // introduce I as a variable for the flash
                            // - turn portb inputs that we are using into digital mode.
    adcon1 = 0b00000110;    //digital
    trisb = 0x00;           //Output
    trisc = 0x00;           // Output
    trisa = 0xFF;           // Turn Porta A as an Input
           
    while( 1 )
    {
        for(i=0;i!=0;i<<=1){
            portb = i;
            delay_ms( 50 );
        }
        portb = 0;
        delay_ms( 15 );
    
        if(a){
        for(i=0;i!=0;i>>=1) {
            portb = i;
            delay_ms ( 50 );
        }                                                                            
        portb = 0;
        delay_ms ( 50 );
        }
    }
}

Mike.
 
Mike, I have to thank you. You have taught me very valuable stuff. I managed to do it myself before seeing your last post. I opened up the BoostC manual, and decided to use the "?" conditional for the first time, and it worked.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top