![]() | ![]() | ![]() |
| |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
![]() |
| | Tools |
| | #1 |
|
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;
}
}
Code: for (i=8;i>0;i--) 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 );
}
}
. 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. | |
| |
| | #2 |
| 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 );
}
}
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. | |
| |
| | #3 |
|
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 );
}
}
| |
| |
| | #4 |
|
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); //
}
}
| |
| |
| | #5 | |
| Quote:
You missed out the all LEDs off state. ![]() Mike. | ||
| |
| | #6 |
|
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); //
}
}
| |
| |
| | #7 |
|
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. | |
| |
| | #8 |
|
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 );
}
}
Last edited by Pommie; 15th January 2009 at 07:47 AM. Reason: There was one too many braces!! | |
| |
| | #9 |
|
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?? Last edited by Gayan Soyza; 15th January 2009 at 07:45 AM. | |
| |
| | #10 |
|
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 "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. | |
| |
| | #11 |
|
Lines that start #define should not end in a semi colon. Mike. | |
| |
| | #12 |
| | |
| |
| | #13 |
|
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;
}
}
Code: i=1;
while(1)
{
if(a)
{
i>>=1;
if(i==0)
i=128;
}else
{
i<<=1;
if(i==0)
i=1;
}
PORTB=i;
}
| |
| |
| | #14 | |
| Quote:
Last edited by Gayan Soyza; 15th January 2009 at 09:57 AM. | ||
| |
| | #15 |
|
It took a while to find that error as the compiler gives a nonsense error message, the line Code: adcon1; = 00000110b; //digital Code: adcon1 = 0b00000110; //digital | |
| |
|
| 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 |