Jump to content
Sign in to follow this  
SLSL

What am I doing wrong? (PIC16F77)

Recommended Posts

Hi!

 

I guess this is a typical beginner's question, but I really can't find the solution anywhere and hope that someone can help.

 

I started a few weeks ago with the 16F84A and successfully made it blink LEDs, control 7-segment LEDs, etc.

 

Then last week, I moved on to the 16F877 with disastrous results! The first thing I tried to do was something simple like blink LEDs with the different ports.....which I couldn't even do.

 

 

#pragma CLOCK_FREQ 10000000

 

void main (void)

{

  set_bit (STATUS, RP0) ;

  TRISB = 0x00 ;  //I tried set_tris_b and set_tris_c too

  clear_bit (STATUS, RP0) ;

 

  while (1)

  {

     output_high_port_b (0) ;  //I have also used PORTB = 1 ;

     delay_ms (200) ;

     output_low_port_b (0) ;  //I have also used PORTB = 0 ;

 

     output_high_port_b (1) ;

     delay_ms (200) ;

     output_low_port_b (1) ;

 

     output_high_port_b (2) ;

     delay_ms (200) ;

     output_low_port_b (2) ;

     

     //  etc etc for b3 to b6.....

 

     output_high_port_b (7) ;

     delay_ms (200) ;

     output_high_port_b (7) ;

  }

}

 

 

The above prog worked beautifully with the 16F84A. However, with the 16F877, sometimes one LED would permanently light up (not blink), sometimes a few would light up (or blink briefly), sometimes something else unexpected would happen.

 

I have checked the programming settings and the hardware and I'm convinced that all is well. I have left the wdt, brown_out reset, low-voltage programming and everthing else off.  I am using a 10 mHz crystal with 33 pF capacitors.

 

5 VDC power comes in and is forked out to both the 16F877's Vdd pins. Both Vss pins are connected to the wire, then to ground. MCLR pin is connected to the 5 VDC supply via a 11K (10K + 1 K) resistor. I have used an ohmmeter to go through every since connection and all is in order.

 

I am using C2C Version 4.00-something, which came with my 'C for PIC' CD-ROM.

 

This narrows me down to the last two suspicions, ie. (1) I have a faulty chip, or (2) I have left out some important 16F877 initialisation/control program lines.

 

What am I doing wrong? Or could this be just cruel retribution for all the naughty things I've done in my life?  

 

Please help....thanks.

 

 

SLSL

Share this post


Link to post
Share on other sites
Guest Pavel

Declare and use your own variables mapped on tris and port registers. There are multiple samples that show how to do this on the compiler Examples page. For example here or here.

 

Regards,

Pavel

Share this post


Link to post
Share on other sites

Hi Pavel,

 

I have now used my own variables mapped on the tris and port registers.  I thought I've got it right, but my PIC16F877 still doesn't run.  

 

Is the below correct?  Or am I missing out something again?

 

 

//DECLARE PORT

char _PORTA@0x05 ;

char _PORTB@0x06 ;

char _PORTC@0x07 ;

char _PORTD@0x08 ;

char _PORTD@0x09 ;

 

//DECLARE TRIS

char _TRISA@0x85 ;

char _TRISB@0x86 ;

char _TRISC@0x87 ;

char _TRISD@0x88 ;

char _TRISE@0x89 ;

 

char _ADCON1@0x9f ;

char _STATUS@0x03 ;

 

#define RP0 0x05 ;

 

 

//DELAY LOOP

void delay (void)

{

  unsigned int i ;

  for (i=0; i<32000 ; i++) ;

}

 

 

 

void main (void)

{

  set_bit (_STATUS, RP0) ;

  _ADCON1 = 0x06 ;           // SWITCH OFF ADC

  _TRISA = 0 ;                  // DECLARE ALL AS OUTPUT

  _TRISB = 0 ;

  _TRISC = 0 ;

  _TRISD = 0 ;

  _TRISE = 0 ;

  clear_bit (_STATUS, RP0) ;

 

  _PORTA = 0 ;                 //SWITCH OFF ALL OUTPUTS

  _PORTB = 0 ;

  _PORTC = 0 ;

  _PORTD = 0 ;

  _PORTE = 0 ;

 

  while (1)

  {

     // RUN FIRST 3 LEDS ON PORT B

 

     _PORTB = 1 ;

     delay () ;

     _PORTB = 0 ;

 

    _PORTB = 2 ;

    delay () ;

    _PORTB = 0 ;

 

   _PORTB = 4 ;

   delay () ;

   _PORTB = 0 ;

  }

}

 

 

Regards,

SLSL

Share this post


Link to post
Share on other sites
Guest msc

I am not sure if this is important, but I always change the target from 16F84 to 16F877 into C2C compiler when I move from one processor to other.

 

Regards

 

msc

Share this post


Link to post
Share on other sites
Guest BOB

Hi,

 

My uni also uses C2C compiler and iam having trouble with the compiler and a pic16f877.

 

is there a standard header file for use with the c2c or at very least a guide to getting the chip working ??

 

thanks

 

bob

Share this post


Link to post
Share on other sites

Hi MSC,

 

Thanks for the suggestion.  

 

Yes, I was careful to ensure that I did switch the target PIC to 16F877.

 

I really don't know what's wrong.  I've spent a few weeks troubleshooting this already.  I've also checked my hardware and all seems OK.  

 

I take it that my program looks OK?

 

SLSL

 

 

PS:  Bob, I think there are header files (and maybe inc. files too) written for the PIC16F877 in the "examples" page and the old forum.

Share this post


Link to post
Share on other sites
Guest Pavel
Yes, I was careful to ensure that I did switch the target PIC to 16F877.

 

I really don't know what's wrong.  I've spent a few weeks troubleshooting this already.  I've also checked my hardware and all seems OK. 

Maybe if you also post assembler generated from your c code readers will have more clues to figure out what might be wrong.

 

Regards,

Pavel

Share this post


Link to post
Share on other sites
Guest Pavel
PS:  Bob, I think there are header files (and maybe inc. files too) written for the PIC16F877 in the "examples" page and the old forum.

The latest compiler version 4.5.4 includes header files for almost all supported pic targets that can be included into the code trough system.h header like:

 

#include <system.h>

 

Regards,

Pavel

 

PS: "Almost all supported pics" is because I used the MPLAB 6.x .inc files to generated C headers using inc2h utility but MPLAB 6.x doesn't contain some .inc files which are present in MPLAB 5.x. The next compiler release will include missing headers as well but if you need them now you can generate those yourself using the inc2h utility.

Share this post


Link to post
Share on other sites

Good idea, Pavel.....I have include the asm file below. It is the same example as above, except that I'm flashing the first 3 LEDs of PORTC this time.

 

Sorry that I can't really comment on this asm file because I know nuts about assembly. :o

 

Does the asm file look OK?

 

There is something else I discovered today. Most of the time, when I power up the circuit, it doesn't work well. There were once or twice when it did work. Feels like there is some power-up reset affecting the chip.

 

(My power supply has been used successfully on the PIC16F84A, so I'm sure its OK). My PIC16F877 circuit is pretty simple, just with LEDs directly connected to the ports via current limiting resistors (of course the crystal and stabilising capacitors are included too).

 

 

 

 

; This file was generated by C2C-plus compiler version 4.00.7e

 

   include "C:\Program Files\MPLAB\P16f877.inc"

   ;Variables *****************************************

_i_delay                        equ 0x70

PORTB                         equ 0x06

PORTD                         equ 0x08

TRISA                         equ 0x85

TRISC                         equ 0x87

TRISE                         equ 0x89

STATUS                        equ 0x03

   ORG 0

   clrf PCLATH

   goto startcode

startcode

   bsf ADCON1

   clrf TRISB

   clrf TRISD

   clrf STATUS, D'5'

   bcf STATUS, RP0

   clrf PORTB

   clrf PORTD

   clrf PORTC

   call _delay

   bcf PCLATH, 3

   bcf PCLATH, 4

   bcf STATUS, RP0

   bcf STATUS, RP1

   clrf PORTC

   call _delay

   bcf PCLATH, 3

   bcf PCLATH, 4

   bcf STATUS, RP0

   bcf STATUS, RP1

   clrf PORTC

   call _delay

   bcf PCLATH, 3

   bcf PCLATH, 4

   bcf STATUS, RP0

   bcf STATUS, RP1

   clrf end

_delayend

   END

Share this post


Link to post
Share on other sites

PROBLEM SOLVED!!!  (I HOPE!)

 

My program and hardware were fine after all.

 

I solved the problem by fluke today. I was so frustrated after so many weeks of getting stuck that I told myself "I'm gonna try and click on every single setting on the PIC programmer and if it blows, bloody ####, so be it!!".

 

I am using the "PICALL" programmer (there's a webpage for it somewhere - do a search).  In my frustrations today, I just happened to click on an option called "Backgr. Debugger", which I believe is part of the Configuration Word.  And everything now works like a breeze!!

 

To be honest, I still haven't the slightest clue what on earth "Backgr. Debugger" is, but I'm gonna find out somehow.  I'm still very much a beginner.

 

I hope this things holds......if not, I'll be back again with more baffling questions.  :D

 

Thanks to all who have replied and tried to help....and so sorry for the bother!

 

 

SLSL

Share this post


Link to post
Share on other sites
Guest Pavel

Glad you have found and solved the problem. I also noticed that you use a very old compiler version (more that 2 years old isn't it) and recommend to upgrade to a new one. The latest C2C has much more features and many things fixed since 4.0 And the coming mayor release 5.0 will also have built-in debugger and simulator.

 

Regards,

Pavel

Share this post


Link to post
Share on other sites

This present C2C compiler I'm using came with my "C for PIC" CD-ROM from Matrix Multimedia a couple of months ago.

 

Yes, I will be getting the new C2C compiler. In fact, I've already downloaded the newest compiler, but haven't had the chance to play around with it yet.  Soon, soon.....

 

 

SLSL

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...