C Programming blinking LED on PIC16F628A

Thread Starter

viva_unix

Joined Apr 24, 2010
26
Hello!
I have found an application for blinking LED here:
http://www.oz1bxm.dk/PIC/628LED.htm
The application has been uploaded and working. I have also recompiled code and uploaded my HEX file, works also...

The problem appears when I try to code in C. This is what I get:

Rich (BB code):
#include <16F628A.h>
#use delay(clock=4000000)
#fuses NOWDT, HS, PUT, NOPROTECT, BROWNOUT, MCLR, NOLVP, NOCPD
void main(void)
{
 setup_comparator(NC_NC_NC_NC);
 setup_vref(FALSE);
 set_tris_b(0b11111110);
 
 while( 1 )
 {
  output_low(PIN_A2);
  delay_ms(500);
  output_high(PIN_A2);
  delay_ms(500);
 }
}
It does not work... :(

The code in the attachment. I have also included the 16F628A.h file. You can see my bread board on the image included. I do not have a pullup resistor. How critical is it?

I know assembler, but would preffer to write in C for now. Could you help me to rewrite the code into C? I am new in microcontroller programming.

Thank you!
 

Attachments

mik3

Joined Feb 4, 2008
4,843
You have set the fuse MCLR which means pin 4 is used as a reset pin. This pin must be connected as shown in the diagram you posted. If not the PIC will reset in unknown times because pin 4 will be floating. Otherwise set the MCLR fuse to NOMCLR.

Also, you have to set the tris_a register as to define the inputs and outputs.
 

Thread Starter

viva_unix

Joined Apr 24, 2010
26
Originally I have also tried this code taking from PIC-C as a startup example:

Rich (BB code):
#include <16F628A.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES MCLR //Master Clear pin enabled
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#use delay(clock=4000000)
#define LED PIN_A1 //CHANGE PIN_XX TO YOUR LED PIN NUMBER, EX: PIN_A5
void main()
{
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_ccp1(CCP_OFF);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
//Example blinking LED program
while(true)
{
output_low(LED);
delay_ms(500);
output_high(LED);
delay_ms(500);
}
}
 

AlexR

Joined Jan 16, 2008
732
Why are you setting up the TRISB register and then using port A?
Make life easy for yourself and get the LED flashing on one of the port B pins for a start. That way you won't have to worry about analogue port settings.
Other than your failure to set up port A, I see no glaring errors and as long as it compiles without errors it should work.
 

Thread Starter

viva_unix

Joined Apr 24, 2010
26
OK. Now my application looks like that:

Rich (BB code):
#include <16F628A.h>
#use delay(clock=4000000)
#fuses NOWDT, HS, PUT, NOPROTECT, BROWNOUT, MCLR, NOLVP, NOCPD
void main(void)
{
 setup_comparator(NC_NC_NC_NC);
 setup_vref(FALSE);
 set_tris_b(0b11111110);
 
 while( 1 )
 {
  //output_low(PIN_B0);
  set_tris_b(0b11111111);
  delay_ms(500);
  //output_high(PIN_B0);
  set_tris_b(0b00000000);
  delay_ms(500);
 }
}
I have also connected the LED to the B0 pin. Still does not work. What fuses options should I use?
 

Thread Starter

viva_unix

Joined Apr 24, 2010
26
interesting with the following code if I am touching the resistor I can see a led flashing but very-very week. I am defenetly missing something small, I just do not know what...

Rich (BB code):
#include <16F628A.h>
#use delay(clock=4000000)
#fuses NOWDT, PUT, NOPROTECT, BROWNOUT, NOMCLR, NOLVP, NOCPD, INTRC
void main(void)
{
 setup_comparator(NC_NC_NC_NC);
 setup_vref(FALSE);
 set_tris_b(0b11111110);
 
 while( 1 )
 {
  //output_low(PIN_B0);
  set_tris_b(0b11111111);
  delay_ms(500);
  //output_high(PIN_B0);
  set_tris_b(0b00000000);
  delay_ms(500);
 }
}
 

retched

Joined Dec 5, 2009
5,207
I think your code is fine.

You have a bad connection on your breadboard.

Move the circuit down a few holes and try again.

It may be a bad ground, but it sounds like a intermittent connection. Happens ALL the time with solderless breadboards.

Make sure your led is in the correct way and check the output pin for voltage with a scope or meter.
 

Thread Starter

viva_unix

Joined Apr 24, 2010
26
OK!
Thank you everytone! As always it was a stupied mistake. I have used PIC16LF628A option in my programmer instead of PIC16F628A. As the result the voltage was 2.0V and I could not see the light blinking. It is working now.

How to use A (Analog) Pins? Is it the same?
 

retched

Joined Dec 5, 2009
5,207
I like a lot of tones but not everytone. ;)

Those simple mistakes will have you pulling you hair out.
Checking proper output with a meter is the first thing you should do. That will lead to discoveries much quicker.

And then there is AAC. ;)
 
Top