View previous topic :: View next topic |
Author |
Message |
Surfer83
Joined: 25 Nov 2011 Posts: 7
|
Silly question blinking a led (delay_ms) |
Posted: Fri Nov 25, 2011 4:19 am |
|
|
I just started using C for programming by running some simple code. I have some experience using assembly but seems like everyone telling me I should go to C. SO here I'm stuck with the simplest code haha.
I'm trying to blink a LED on port A1. I had run a simple code to just turn on the LED and it was ok and this show that my hardware should be ok. Hope that someone could give me some help here.
I did try Googling and searching the forum but can't find solution for my problem.
Code: |
#include <16F628.h>
#fuses INTRC_IO,NOWDT,NOPUT,NOPROTECT,NOCPD
#use delay(internal=4000000)
void main()
{
while(true)
{
output_high(PIN_A1);
delay_ms(500);
output_low(PIN_A1)
delay_ms(500);
}
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Fri Nov 25, 2011 4:34 am |
|
|
How is the LED wired?.
One 'classic', is not having a current limiting resistor, so when the LED turns on, it hangs the chip. This wouldn't be obvious with code that just turned the LED 'on'.
I'd add:
setup_adc_ports(NO_ANALOGS);
at the start of the main, and the fuse 'NOLVP'. Otherwise the chip may get hung thinking you want to program it!....
What is actually happening?.
Best Wishes |
|
|
Surfer83
Joined: 25 Nov 2011 Posts: 7
|
|
Posted: Fri Nov 25, 2011 6:18 am |
|
|
I have 1K ohm current limiter, positive side of the LED connected from 1K ohm end and the negative side of the LED to the ground. I have 10k pull up to Vdd on the MCLR PIN. Supply is 5 volt from a 5volt regulator. Sorry cant bring a proper sch atm.
After I turn on the power the LED just won't turn on. As I said earlier I did try a simple code to just turn a LED on and it did work. I have a feeling there probably something wrong when I used the "delay_ms".
Anyway I will try you suggestions latter thank you. |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Fri Nov 25, 2011 11:42 am |
|
|
1- have U tested led GOOD with measured Vf of 1.6V or more ?
2- is led POLARITY correct ?? |
|
|
Surfer83
Joined: 25 Nov 2011 Posts: 7
|
|
Posted: Wed Nov 30, 2011 7:59 am |
|
|
Code: |
#include <16F628.h>
#use delay(internal=4M)
#fuses INTRC_IO, NOWDT, BROWNOUT, NOPUT, NOLVP, ,NOCPD
void main()
{
while(1)
{
output_high(PIN_B5);
delay_ms(10);
output_low(PIN_B5);
delay_ms(10);
}
}
|
My new code which I lowered the timing and change pin to B5 which is purely a digital I/0 pin.
Quote: | 1- have U tested led GOOD with measured Vf of 1.6V or more ?
2- is led POLARITY correct ?? |
Yeah LED good and my first attempt to just turn on a LED was fine and still using the same LED |
|
|
Surfer83
Joined: 25 Nov 2011 Posts: 7
|
|
Posted: Wed Nov 30, 2011 8:14 am |
|
|
BTW I'm using a ICD2,version 4. CCS I'm using and a solderless protoboard.
Man this really sucks, just to blink this LED and I had a bad feeling there must be some silly thing that I missed.
Anyway thanks in advance to the people who give the time to help and currently helping me.
Appreciate it very much. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Wed Nov 30, 2011 10:43 am |
|
|
You talk about the ICD2. Does this mean you have the 18 to 28pin adapter AC162053?. If not, you are aware the ICD, is _not_ available on the 18pin device?.
Hopefully you are just using the ICD2 as a programmer.
You don't show the NOMCLR fuse. Do you have the MCLR pin pulled up?.
Best Wishes |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Wed Nov 30, 2011 8:13 pm |
|
|
you said :
Quote: |
just to blink this led
|
why did you write so such code ? LOL
try this:
Code: |
void main(){ while(1) { output_toggle(PIN_B5); delay_ms(100);}}
|
now THATS the easy way |
|
|
PICman
Joined: 02 Nov 2007 Posts: 26
|
|
Posted: Wed Nov 30, 2011 8:58 pm |
|
|
Doesn't the code need oscillator setup ?
setup_oscillator(OSC_4MHZ|OSC_INTRC|OSC_PLL_OFF,0); _________________ The ideal electronic world: A place where the words VERSION and REVISION do NOT exist ! |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Wed Nov 30, 2011 9:34 pm |
|
|
Hard to improve on that code i fear.
Code should be as simple and transportable as possible , no?
But specifying the master clock matters to simulation
and the real world alike.
I agree that a device definitions setup would be required beyond
that omission in order for this circuit to make its way out of simulation.
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Dec 01, 2011 10:10 am |
|
|
What is your compiler version number? It looks something like 4.xxx, i.e. 3 digits after the dot.
NOMCLR fuse is used by default for this chip when you specify nothing, but best to specific about your choice and add it to the list of fuses.
A delay of 10ms is way too short for debugging as your eye is to slow to see it. Even 100ms is tricky. Be safe and use something like 500ms.
I tried it in the MPLab simulator with CCS v4.077 and got a delay as expected. |
|
|
rovtech
Joined: 24 Sep 2006 Posts: 262
|
Me too with a 16F722 |
Posted: Sat Dec 03, 2011 8:23 pm |
|
|
I have similar problems and resorted to the simplest code (not the most elegant, that just makes more problems). Note the delays in the comments as seen on a 'scope.
Code: |
#include <16F722.H>
#fuses XT, NOWDT, PUT, NOPROTECT, MCLR
#use delay (clock = 4000000) // using 4 MHz XTAL
void main(void)
{
set_tris_c (0x00); //all outputs
while (true)
{
output_low (pin_C4); // low for about 190 uS
delay_ms (1);
output_high (pin_C4); // high for about 200 uS
delay_ms (10);
}
}
|
My programming for the 16F722 was working OK, suddenly it has problems.
Selecting #fuses INTRC and #use delay (clock = 8MHZ) gives an osc of 500 kHz and I had to resort to an XTAL and the above to get the correct freq. – 4 MHz on pin 10. Other options did not work.
The timers seem to be haywire and the delay does not work. A second delay after line 17 of 20 ms just doubles the delay to about 380 us. This is not even close. Changing from 1 to 20 ms in the delays has no effect!
MPLAB IDE v8.60 and CCS C v 4.114, Lenovo T61 running latest updates of XP pro.
The only thing that has changed (I think) is a switch to ICD-3 from Picstart Plus, and XP updates.
Relevant part of .lst is below. I can read most of it but do not understand the 20.4 etc after BCF, I guess it clears bit 4 of register 20? and call 004 is the delay routine? but is it correct?
Code: |
.................... while (true)
.................... {
.................... output_low (pin_C4);
002D: BCF 20.4
002E: MOVF 20,W
002F: BSF 03.5
0030: MOVWF 07
0031: BCF 03.5
0032: BCF 07.4
.................... delay_ms (1);
0033: MOVLW 01
0034: MOVWF 21
0035: CALL 004
.................... output_high (pin_C4);
0036: BCF 20.4
0037: MOVF 20,W
0038: BSF 03.5
0039: MOVWF 07
003A: BCF 03.5
003B: BSF 07.4
.................... delay_ms (10);
003C: MOVLW 0A
003D: MOVWF 21
003E: CALL 004
.................... }
003F: GOTO 02D |
|
|
|
rovtech
Joined: 24 Sep 2006 Posts: 262
|
|
Posted: Sun Dec 04, 2011 9:55 am |
|
|
This works and gives a square wave of 20 milliseconds period:
Code: |
#include <16F722.H>
#fuses XT, NOWDT, PUT, NOPROTECT, MCLR
#use delay (clock = 4000000) // using 4 MHz XTAL
void main()
{
while(1)
{
output_toggle(PIN_C4);
delay_ms(10);
}
} |
This does NOT work and gives a square wave of about 400 microseconds period. Note that it should NOT be a square wave.
Code: |
#include <16F722.H>
#fuses XT, NOWDT, PUT, NOPROTECT, MCLR
#use delay (clock = 4000000) // using 4 MHz XTAL
void main()
{
while(1)
{
output_low (pin_C4); // low for about 190 uS
delay_ms (1);
output_high (pin_C4); // high for about 200 uS
delay_ms (10);
}
} |
The only difference I can see is the use of upper case and a space before the brackets. I will experiment with these. |
|
|
rovtech
Joined: 24 Sep 2006 Posts: 262
|
delay_ms is not working |
Posted: Sun Dec 04, 2011 11:22 am |
|
|
I connected a freq counter to measure the time periods of the negative pulse and changed only the first delay in us or ms
10 us = 16 us out
50 us = 56 us out
100 us = 106 us out
500 us = 506 us out
1000 us = 192 us out
1 ms = 192 us out and any value in ms from 1 to 100 gives 192 us out
500 ms = 36.9 ms out
So microsecond delays are close unless a value over 1000 (1ms) is used. Even then the constant 6 us error is suspicious. I think I have a version of CCS C (4.114) that has a bug in it and will have to upgrade.
Typical code showing last change:
Code: |
while(1)
{
output_low (PIN_C4); // low for about 190 uS
delay_ms(500);
output_high (PIN_C4); // high for about 200 uS
delay_ms(1);
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
|