Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 11th January 2009, 09:38 AM   #1
Default BoostC Help

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 by usif; 11th January 2009 at 10:01 AM.
usif is offline  
Old 11th January 2009, 10:33 AM   #2
Default

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 by eng1; 11th January 2009 at 10:54 AM.
eng1 is offline  
Old 11th January 2009, 11:01 AM   #3
Default

My guess at this time of night 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.
Pommie is offline  
Old 11th January 2009, 11:47 AM   #4
Default

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);        //
      }
    }
Mike, K8LH is offline  
Old 11th January 2009, 11:55 AM   #5
Default

Quote:
Originally Posted by Mike, K8LH View Post
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.

Mike.
Pommie is offline  
Old 11th January 2009, 01:15 PM   #6
Default

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

Code:
    while(1)
    { porta = 0;             //  All LEDs off
      delay_ms(100);         //
      i = 1;                 // night rider effect
      while(i)               //
      { porta = i;           //
        delay_ms(50);        //
        i <<= 1;             // shift left
      }
      porta = 0;             // all LEDs off
      delay_ms(100);         //
      i = 128;               //
      while(i)               //
      { porta = i;           //
        i >>= 1;             // shift right
        delay_ms(50);        //
      }
    }
Mike, K8LH is offline  
Old 15th January 2009, 06:47 AM   #7
Default

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 by usif; 15th January 2009 at 06:48 AM.
usif is offline  
Old 15th January 2009, 07:25 AM   #8
Default

When you shift zero left it is still zero,

Try,
Code:
    while( 1 )
    {    
        for(i=1;i!=0;i<<=1){
            portb = i;
            delay_ms( 50 );
        }
             
        portb = 0;
        delay_ms( 15 ); 
        if(a){
            for(i=128;i!=0;i>>=1) {
                portb = i;
                delay_ms ( 50 );
             }                                                                            
             portb = 0;
             delay_ms ( 50 );
         }
     }
Mike.

Last edited by Pommie; 15th January 2009 at 07:47 AM. Reason: There was one too many braces!!
Pommie is offline  
Old 15th January 2009, 07:43 AM   #9
Default

This is a nice experiment I also like very much & leaned many things.

One thing I noticed that PORTA.0 input will act at the end of the pattern??
__________________
Gayan

My Website
http://gsmicro.blogspot.com/

Last edited by Gayan Soyza; 15th January 2009 at 07:45 AM.
Gayan Soyza is offline  
Old 15th January 2009, 09:17 AM   #10
Default

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 by usif; 15th January 2009 at 09:20 AM.
usif is offline  
Old 15th January 2009, 09:25 AM   #11
Default

Lines that start #define should not end in a semi colon.

Mike.
Pommie is offline  
Old 15th January 2009, 09:29 AM   #12
Default

Quote:
Originally Posted by Pommie View Post
Lines that start #define should not end in a semi colon.

Mike.
Fixed that problem, and it still wont compile. Giving me the same error.
usif is offline  
Old 15th January 2009, 09:36 AM   #13
Default

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.
Pommie is offline  
Old 15th January 2009, 09:56 AM   #14
Default

Quote:
Originally Posted by Pommie View Post
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.
Hi thanks Mike,I must very careful with braces alignment. I was searching wheres my post after I deleting it, you got it thanks for that
__________________
Gayan

My Website
http://gsmicro.blogspot.com/

Last edited by Gayan Soyza; 15th January 2009 at 09:57 AM.
Gayan Soyza is offline  
Old 15th January 2009, 10:08 AM   #15
Default

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.
Pommie is offline  
Reply

Tags
boostc

Thread Tools
Display Modes


Similar
Title Starter Forum Replies Latest
BoostC Charlieplexed PWM 32 Mike, K8LH Micro Controllers 140 27th March 2009 11:19 AM
Hardware SPI with BoostC weirdness futz Micro Controllers 0 23rd July 2008 06:57 AM
BoostC Flaw? AtomSoft Micro Controllers 7 19th July 2008 04:32 AM
BoostC USART 18F AtomSoft Micro Controllers 9 29th June 2008 05:02 PM
math.h and lib for BoostC? futz Micro Controllers 3 31st March 2008 06:29 AM



All times are GMT. The time now is 12:42 PM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker