C Programming blinking LED on PIC16F628A

Discussion in 'Embedded Systems and Microcontrollers' started by viva_unix, May 8, 2010.

  1. viva_unix

    Thread Starter New Member

    Apr 24, 2010
    26
    0
    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:

    Code ( (Unknown Language)):
    1.  
    2. #include <16F628A.h>
    3. #use delay(clock=4000000)
    4. #fuses NOWDT, HS, PUT, NOPROTECT, BROWNOUT, MCLR, NOLVP, NOCPD
    5. void main(void)
    6. {
    7.  setup_comparator(NC_NC_NC_NC);
    8.  setup_vref(FALSE);
    9.  set_tris_b(0b11111110);
    10.  
    11.  while( 1 )
    12.  {
    13.   output_low(PIN_A2);
    14.   delay_ms(500);
    15.   output_high(PIN_A2);
    16.   delay_ms(500);
    17.  }
    18. }
    19.  
    20.  
    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!
     
  2. bertus

    Administrator

    Apr 5, 2008
    15,645
    2,344
    Hello,

    Wich C compiler are you using?
    Did you declare the "PIN_A2"?

    Bertus
     
  3. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    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.
     
  4. viva_unix

    Thread Starter New Member

    Apr 24, 2010
    26
    0
    I am using CCS C Compiler
    The PIN_A2 is declared in .h file
    The application is compiled, just nothing happened.
     
  5. viva_unix

    Thread Starter New Member

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

    Code ( (Unknown Language)):
    1.  
    2. #include <16F628A.h>
    3. #FUSES NOWDT //No Watch Dog Timer
    4. #FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
    5. #FUSES NOPUT //No Power Up Timer
    6. #FUSES NOPROTECT //Code not protected from reading
    7. #FUSES NOBROWNOUT //No brownout reset
    8. #FUSES MCLR //Master Clear pin enabled
    9. #FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
    10. #FUSES NOCPD //No EE protection
    11. #use delay(clock=4000000)
    12. #define LED PIN_A1 //CHANGE PIN_XX TO YOUR LED PIN NUMBER, EX: PIN_A5
    13. void main()
    14. {
    15. setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
    16. setup_timer_1(T1_DISABLED);
    17. setup_timer_2(T2_DISABLED,0,1);
    18. setup_ccp1(CCP_OFF);
    19. setup_comparator(NC_NC_NC_NC);
    20. setup_vref(FALSE);
    21. //Example blinking LED program
    22. while(true)
    23. {
    24. output_low(LED);
    25. delay_ms(500);
    26. output_high(LED);
    27. delay_ms(500);
    28. }
    29. }
    30.  
     
  6. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    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.
     
  7. viva_unix

    Thread Starter New Member

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

    Code ( (Unknown Language)):
    1.  
    2. #include <16F628A.h>
    3. #use delay(clock=4000000)
    4. #fuses NOWDT, HS, PUT, NOPROTECT, BROWNOUT, MCLR, NOLVP, NOCPD
    5. void main(void)
    6. {
    7.  setup_comparator(NC_NC_NC_NC);
    8.  setup_vref(FALSE);
    9.  set_tris_b(0b11111110);
    10.  
    11.  while( 1 )
    12.  {
    13.   //output_low(PIN_B0);
    14.   set_tris_b(0b11111111);
    15.   delay_ms(500);
    16.   //output_high(PIN_B0);
    17.   set_tris_b(0b00000000);
    18.   delay_ms(500);
    19.  }
    20. }
    21.  
    I have also connected the LED to the B0 pin. Still does not work. What fuses options should I use?
     
  8. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Should you be using one of the intosc settings instead of HS in the fuses?
    I can't see a crystal.
     
  9. viva_unix

    Thread Starter New Member

    Apr 24, 2010
    26
    0
    yes, I was thinking to use internal crystal. Do you know what should I set?
     
  10. viva_unix

    Thread Starter New Member

    Apr 24, 2010
    26
    0
    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...

    Code ( (Unknown Language)):
    1.  
    2. #include <16F628A.h>
    3. #use delay(clock=4000000)
    4. #fuses NOWDT, PUT, NOPROTECT, BROWNOUT, NOMCLR, NOLVP, NOCPD, INTRC
    5. void main(void)
    6. {
    7.  setup_comparator(NC_NC_NC_NC);
    8.  setup_vref(FALSE);
    9.  set_tris_b(0b11111110);
    10.  
    11.  while( 1 )
    12.  {
    13.   //output_low(PIN_B0);
    14.   set_tris_b(0b11111111);
    15.   delay_ms(500);
    16.   //output_high(PIN_B0);
    17.   set_tris_b(0b00000000);
    18.   delay_ms(500);
    19.  }
    20. }
    21.  
     
  11. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    I don't know C but in asm I would put _INTRC_OSC_NOCLKOUT in the CONFIG (fuses) instead of HS
     
  12. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
    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.
     
  13. viva_unix

    Thread Starter New Member

    Apr 24, 2010
    26
    0
    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?
     
  14. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
    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. ;)
     
  15. kingdano

    Member

    Apr 14, 2010
    377
    19
    wise words
     
Loading...