|
|
View previous topic :: View next topic |
Author |
Message |
micro17
Joined: 02 Mar 2014 Posts: 14
|
Not work |
Posted: Tue Dec 16, 2014 8:44 am |
|
|
Hi
Please help me about that code compiles without errors, but does not work.
What is wrong?
Thank you
Code: | Blinking more LEDs
#include <12f683.h>
#fuses XT,NOMCLR
#use delay(clock=4M)
byte pins[4] = {0, 1, 2, 4};
byte i;
void init(void)
{
//set every pin from 0 to 4 in the array to OUTPUT mode
for(i=0; i<=4; i++)
{
output_high(pins[i]);
}
}
void main(void)
{
for(i=0; i<4; i++) //FOR loop goes from the first LED to last
{
output_high(pins[i]); //turn the LED on
delay_ms(200); //wait 200ms
output_low(pins[i]); //turn the LED off
}
for(i=4; i>0; i--) //FOR loop goes from the last LED to first
{
output_high(pins[i]); //turn the LED on
delay_ms(200); //wait 200ms
output_low(pins[i]); //turn the LED off
}
} |
Last edited by micro17 on Tue Dec 16, 2014 9:40 am; edited 1 time in total |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue Dec 16, 2014 9:24 am |
|
|
What are you expecting to happen?
What does happen?
Can you blink just one LED?
Why have you got an init() you don't call?
Which pins do you think will be controlled by your array?
Why are you missing pins[3] ?
Use the code button and indents to make your code more readable, as follows:- Code: | #include <12f683.h>
#fuses XT,NOMCLR
#use delay(clock=4M)
byte pins[4] = {0, 1, 2, 4};
byte i;
void init(void)
{
//set every pin from 0 to 4 in the array to OUTPUT mode
for(i=0; i<=4; i++)
{
output_high(pins[i]);
}
}
void main(void)
{
for(i=0; i<4; i++) //FOR loop goes from the first LED to last
{
output_high(pins[i]); //turn the LED on
delay_ms(200); //wait 200ms
output_low(pins[i]); //turn the LED off
}
for(i=4; i>0; i--) //FOR loop goes from the last LED to first
{
output_high(pins[i]); //turn the LED on
delay_ms(200); //wait 200ms
output_low(pins[i]); //turn the LED off
}
}
|
Mike |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Dec 16, 2014 9:27 am |
|
|
none of your pins[] values are valid for the PIC output_ function
look at the .h file for the part to see why. |
|
|
micro17
Joined: 02 Mar 2014 Posts: 14
|
|
Posted: Tue Dec 16, 2014 9:33 am |
|
|
asmboy
to be modified to work?
i am beginner in programming....
4 leds
pins A0,A1,A2,A4
pin 3 is only input
Original code is for arduino...
Code: | byte pins[8] = {2, 3, 4, 5, 6, 7, 8, 9};
byte i;
void setup()
{
//set every pin from 0 to 7 in the array to OUTPUT mode
for(i=0; i<=7; i++)
{
pinMode(pins[i], OUTPUT);
}
}
void loop()
{
for(i=0; i<7; i++) //FOR loop goes from the first LED to last
{
digitalWrite(pins[i], HIGH); //turn the LED on
delay(200); //wait 200ms
digitalWrite(pins[i], LOW); //turn the LED off
}
for(i=7; i>0; i--) //FOR loop goes from the last LED to first
{
digitalWrite(pins[i], HIGH); //turn the LED on
delay(200); //wait 200ms
digitalWrite(pins[i], LOW); //turn the LED off
}
} |
I wanted to change the code for pic microcontroller |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Tue Dec 16, 2014 9:47 am |
|
|
Also, given that the 683, is an 8pin PIC, is it very likely that he has a crystal attached?.....
That is what his fuses say.
If he has, the chip only has three output pins available. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue Dec 16, 2014 6:20 pm |
|
|
micro17 wrote: | asmboy
to be modified to work?
i am beginner in programming....
......
......
I wanted to change the code for pic microcontroller |
Start simple.
Try some of the CCS examples first.
Get one LED to flash.
CCS is a different beast.
Mike |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Wed Dec 17, 2014 4:54 am |
|
|
Though rather basic, lets start at the beginning:
You have an 8pin PIC.
The pins are:
+ve
GP5 or oscillator
CP4 or oscillator
GP3 or MCLR
GP2
GP1
GP0
Gnd
Now I've left out several other things that the pins may do, but included the ones that we need to talk about.
In your fuses 'XT' is the fuse to say 'use an external crystal'. So GP5, and GP4 are then used for the crystal. This is a 'read the data sheet' one. Look at what fuses are needed to use these pins for I/O?.
Then the next pin operates as both the MCLR pin, and the programming input. It _can_ be used for input only (when not used for other things). So this pin can't be an output.
So with your initial configuration, only three pins GP0 to GP2 are actually available for output.....
Big problem.
So you need to start by looking at what the settings are to allow GP5, and GP4 to be used.
Then, the numbering of PIC pins uses much 'larger' values than the Arduino code you are using. That code hides the physical addressing of the pins behind a routine that numbers them as simple numbers from 0. CCS instead uses 'names' for the pins. So an array to access the low 4 pins that can be used for output on your PIC would be:
int16 pins[5] = {PIN_A0,PIN_A1,PIN_A2,PIN_A4};
There is a slight 'complexity' here. When the little 8 pin PIC's arrived, Microchip called their pins GPx. All the other PIC's use Ax, Bx etc..
CCS uses the standard for the larger PIC's here, and all pins are referred to with names as shown. Microchip also changed to using this standard later, so PIC's like the 12F1822 (a more modern one), use the A0, A1 etc., naming. Really old CCS compilers will use the older naming, but all ones for several years have kept to the modern names.
You seem correctly to have spotted that you can't use A3/GP3 for output.
Now, once you sort out the oscillator setting, and use the pin array as shown, you should be able to start getting the code to work.
However the lesson is KISS. If you had started with the most basic program (just toggling one pin), you would have found the oscillator problem first. This is a general rule for programming. Take things one step at a time. |
|
|
micro17
Joined: 02 Mar 2014 Posts: 14
|
|
Posted: Mon Aug 03, 2015 6:17 am |
|
|
Hi
Now, the code it works, but LEDs light 1,2,3,4 (lights 2 times), 3, 2 and stop, why?
Code: |
#include <12f683.h>
#use delay(clock=4000000)
#fuses nowdt,intrc_io,put,noprotect,brownout,nomclr,nocpd
int16 pins[5] = {PIN_A0,PIN_A1,PIN_A2,PIN_A4};
byte i;
void init(void)
{
//set every pin from 0 to 4 in the array to OUTPUT mode
for(i=0; i<=4; i++)
{
output_high(pins[i]);
}
}
void main(void)
{
for(i=0; i<4; i++) //FOR loop goes from the first LED to last
{
output_high(pins[i]); //turn the LED on
delay_ms(200); //wait 200ms
output_low(pins[i]); //turn the LED off
}
for(i=4; i>0; i--) //FOR loop goes from the last LED to first
{
output_high(pins[i]); //turn the LED on
delay_ms(200); //wait 200ms
output_low(pins[i]); //turn the LED off
}
}
|
|
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Mon Aug 03, 2015 6:24 am |
|
|
why ?
you are entering SLEEP after the only 'pass' through the code .
try ...
Code: |
void main(void)
{
while(1){
for(i=0; i<4; i++) //FOR loop goes from the first LED to last
{
output_high(pins[i]); //turn the LED on
delay_ms(200); //wait 200ms
output_low(pins[i]); //turn the LED off
}
for(i=4; i>0; i--) //FOR loop goes from the last LED to first
{
output_high(pins[i]); //turn the LED on
delay_ms(200); //wait 200ms
output_low(pins[i]); //turn the LED off
}
delay_ms(200; // added to prevent a missing transition time when looping
}
} |
|
|
|
micro17
Joined: 02 Mar 2014 Posts: 14
|
|
Posted: Mon Aug 03, 2015 6:46 am |
|
|
asmboy, work in sequence 1, 2, 3, 4, 1, 2, 3, 4
I want to work in 1,2,3,4,3,2,1,2,3,4 etc
FOR loop goes from the first LED to last
FOR loop goes from the last LED to first |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Mon Aug 03, 2015 6:49 am |
|
|
try what i posted with the added instructions. without the WHILE()
you were doing one pass and sleeping.
the WHILE fixes what you complained about
i noted you only fill 4 of 5 elements in your PIN array too.
perhaps consider making 'i' be an INT8
the extra delay normalizes the transition from each iteration of you code.
Last edited by asmboy on Mon Aug 03, 2015 6:53 am; edited 1 time in total |
|
|
micro17
Joined: 02 Mar 2014 Posts: 14
|
|
Posted: Mon Aug 03, 2015 6:53 am |
|
|
I try to compile your code, but give me error... |
|
|
micro17
Joined: 02 Mar 2014 Posts: 14
|
|
Posted: Mon Aug 03, 2015 6:57 am |
|
|
I try this, but the same error 1,2,3,4 (blink 2 times),3,2 and stop
Code: |
void main(void)
{
for(i=0; i<4; i++) //FOR loop goes from the first LED to last
{
output_high(pins[i]); //turn the LED on
delay_ms(200); //wait 200ms
output_low(pins[i]); //turn the LED off
delay_ms(200); //wait 200ms
}
for(i=4; i>0; i--) //FOR loop goes from the last LED to first
{
output_high(pins[i]); //turn the LED on
delay_ms(200); //wait 200ms
output_low(pins[i]); //turn the LED off
delay_ms(200); //wait 200ms
}
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Mon Aug 03, 2015 7:41 am |
|
|
Of course it would. You have removed the while.....
Without this the loops execute _once_. |
|
|
micro17
Joined: 02 Mar 2014 Posts: 14
|
|
Posted: Mon Aug 03, 2015 9:21 am |
|
|
with the while the seq is 1,2,3,4,1,2,3,4 |
|
|
|
|
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
|