PIC16F676 <= Why does this program not work?

Discussion in 'Embedded Systems and Microcontrollers' started by arashian1, Sep 28, 2016.

  1. arashian1

    Thread Starter Member

    Sep 9, 2016
    39
    0
    void main() {
    ANSEL = 0x00;
    TRISC = 0;
    PORTC = 0;
    while(1){
    PORTC.RC4 = 1;
    delay_ms(500);
    PORTC.RC4 = 0;
    delay_ms(500);
    }
    }

    upload_2016-9-28_22-24-27.png

    who knows why this program do not work?
    i did test in proteus and output always is high
     
  2. nerdegutta

    Moderator

    Dec 15, 2009
    2,517
    785
    Hi,

    which compiler do you use?
    Is this your whole program? Where's the configuration bits? You miss a resistor in series with the LED.

    You need to provide a bit more info...
     
  3. dannyf

    Well-Known Member

    Sep 13, 2015
    1,819
    363
    It works perfectly.
     
  4. arashian1

    Thread Starter Member

    Sep 9, 2016
    39
    0
    it must operate like flasher each 500ms off and on but it always is on
     
  5. arashian1

    Thread Starter Member

    Sep 9, 2016
    39
    0
    this program with other PIC series for example 16F877 works perfectly
    compiler is mikroC
     
    Last edited: Sep 28, 2016
  6. dannyf

    Well-Known Member

    Sep 13, 2015
    1,819
    363
    Compare the datasheet of those devices and you will know why.
     
  7. arashian1

    Thread Starter Member

    Sep 9, 2016
    39
    0
    there is nothing in datasheet that different with the other types
     
  8. nerdegutta

    Moderator

    Dec 15, 2009
    2,517
    785
    If the LED is always on, you could have a timing issue? Perhaps the LED is flashing too fast, so it looks like its on all the time?
     
  9. jayanthd

    Member

    Jul 4, 2015
    274
    29
    In mikroC PRO PIC "Edit Project" dialog box set Osc type to XT if you are using 4 MHz and HS if you are using more than 4 MHz Crystal.
     
  10. nerdegutta

    Moderator

    Dec 15, 2009
    2,517
    785
    I'm not into Proteus, but do you have to put a valid value on the Crystal? Like you've done with the resistor and caps?
     
  11. jayanthd

    Member

    Jul 4, 2015
    274
    29
    Try attached project. See attached video.
     
  12. nerdegutta

    Moderator

    Dec 15, 2009
    2,517
    785
    While comparing the c file in the *.rar and the code on top, your are missing this:

    Code (C):
    1.  
    2. CMON=0x00;
    3.  
    This is from the attached file:

    Code (C):
    1.  
    2. sbit LED at RC4_bit;
    3. void main() {
    4.   CMCON = 0x00;
    5.   ANSEL = 0x00;
    6.   TRISA = 0x31;
    7.   TRISC = 0x00;
    8.   PORTA = 0x00;
    9.   PORTC = 0x00;
    10.    
    11.   while(1) {
    12.    
    13.   LED = ~LED;
    14.   Delay_ms(500);
    15.   }
    16. }
    17.  
     
  13. jayanthd

    Member

    Jul 4, 2015
    274
    29
    It is better to make

    Code (C):
    1.  
    2. CMCON = 0x07;
    3.  
    as Comparators are not used.


    Code (C):
    1.  
    2. sbit LED at RC4_bit;
    3.  
    4. void main() {
    5.  
    6.     CMCON = 0x07;
    7.     ANSEL = 0x00;
    8.     TRISA = 0x38;
    9.     TRISC = 0x00;
    10.     PORTA = 0x00;
    11.     PORTC = 0x00;
    12.  
    13.     while(1) {
    14.  
    15.         LED = ~LED;
    16.         Delay_ms(500);
    17.     }
    18. }
    19.  
     
  14. nerdegutta

    Moderator

    Dec 15, 2009
    2,517
    785
    So you got the program working?
     
  15. jayanthd

    Member

    Jul 4, 2015
    274
    29
    @nerdegutta

    You asked me ? Yes, my project works. I have attached Simulation Video. It will surely work in hardware also. Crystal and associated Capacitors should be added. A 10 uF 6.3V and 100 nF 16V has to be added across PIC power pins and close to PIC on the PCB.
     
  16. dannyf

    Well-Known Member

    Sep 13, 2015
    1,819
    363
    Two things make or break a good embedded programmer: programming skills, and your willingness and ability to comprehend the datasheet.

    If you aren't willing to read and understand the datasheet, you will have a miserable time in this business.
     
  17. arashian1

    Thread Starter Member

    Sep 9, 2016
    39
    0
    upload_2016-9-30_13-45-28.png

    upload_2016-9-30_13-46-59.png

    sbit LCD_RS at RC0_bit;
    sbit LCD_EN at RC1_bit;
    sbit LCD_D4 at RC2_bit;
    sbit LCD_D5 at RC3_bit;
    sbit LCD_D6 at RC4_bit;
    sbit LCD_D7 at RC5_bit;
    sbit LCD_RS_Direction at TRISC0_bit;
    sbit LCD_EN_Direction at TRISC1_bit;
    sbit LCD_D4_Direction at TRISC2_bit;
    sbit LCD_D5_Direction at TRISC3_bit;
    sbit LCD_D6_Direction at TRISC4_bit;
    sbit LCD_D7_Direction at TRISC5_bit;

    void main() {
    ANSEL = 0x00;
    CMCON = 0x07;
    TRISC = 0x00;
    LCD_INIT();
    LCD_CMD(_lcd_clear);
    LCD_CMD(_lcd_cursor_off);

    while(1){
    LCD_OUT(1,1,"IMAN");

    }
    }
    upload_2016-9-30_13-48-55.png

    who knows during programming the programmer message OSCCAL what does mean?
     
  18. jayanthd

    Member

    Jul 4, 2015
    274
    29
    Are you using Internal RC Oscillator 4 MHz or External 4 MHz Crystal ?
     
  19. arashian1

    Thread Starter Member

    Sep 9, 2016
    39
    0
    i set OSCCAL = 0b11111100; for internal 4MHz
     
  20. jayanthd

    Member

    Jul 4, 2015
    274
    29
    Why did you set OSCCAL in code. mikroE used IDE to set config bits. Set proper type oscillator in Edit Project dialog box.
     
Loading...