• AVR Freaks

Helpful ReplyComplete NOOB Requires some Explaination - PIC 16F877A

Page: < 123 > Showing page 2 of 3
Author
Johnnycrash
New Member
  • Total Posts : 18
  • Reward points : 0
  • Joined: 2010/12/22 19:15:58
  • Location: 0
  • Status: offline
Re:Complete NOOB Requires some Explaination - PIC 16F877A 2010/12/23 08:54:40 (permalink)
0
That makes sense using the ADCON0 reg, but how would i apply that to the PORTA bits. since they dont have lables like go_done or ad_on etc.
#21
1and0
Access is Denied
  • Total Posts : 12099
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re:Complete NOOB Requires some Explaination - PIC 16F877A 2010/12/23 09:01:29 (permalink)
0
Johnnycrash
If i do this,
movlw b'111111'
movwf PORTA
 
it will set a hex number of 16 in the register
 
How do I set it so RA1 goes high?

Some PORTA pins are multiplexed with analog function.  You have to configure these pins to digital with the ADCON1 register.
 
#22
1and0
Access is Denied
  • Total Posts : 12099
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re:Complete NOOB Requires some Explaination - PIC 16F877A 2010/12/23 09:31:50 (permalink)
0
Johnnycrash
That makes sense using the ADCON0 reg, but how would i apply that to the PORTA bits. since they dont have lables like go_done or ad_on etc.

You can create labels/symbols with the #define directive, such as
 
#define LED1  PORTA,1
 
Wherever LED1 is used in the assembly code, PORTA,1 will be substituted.
#23
Johnnycrash
New Member
  • Total Posts : 18
  • Reward points : 0
  • Joined: 2010/12/22 19:15:58
  • Location: 0
  • Status: offline
Re:Complete NOOB Requires some Explaination - PIC 16F877A 2010/12/23 09:36:49 (permalink)
0
I thought that was what I did here?
 
;*****Set Up The Port*******
 clrf  PORTA
BANKSEL  TRISA
 
 movlw  0x0F
 movwf ADCON1
 
BANKSEL  PORTA 
  
 movlw 00h
 movwf COUNT1  ;Clears data from count1 on startup
 movwf COUNT2  ;Clears data from count2 on startup
 
 
Then the next part of the program will try and switch RA1 "1"
 
START
 
 movlw 03h
 movwf PORTA  ;copies working reg to PORTA
 
 
But this does not enter a value into PORTA register for some reason. is there something I am missing?
As far as I can tell I am Initializing PORTA, Setting ADCON1 turns off the AD converters, and setting TRISA sets PORTA as OUTPUT only.
 
 
#24
1and0
Access is Denied
  • Total Posts : 12099
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re:Complete NOOB Requires some Explaination - PIC 16F877A 2010/12/23 09:50:49 (permalink)
0
Post your entire asm source code within [ code] and [ /code] tags -- without the spaces in the [ ].
#25
Johnnycrash
New Member
  • Total Posts : 18
  • Reward points : 0
  • Joined: 2010/12/22 19:15:58
  • Location: 0
  • Status: offline
Re:Complete NOOB Requires some Explaination - PIC 16F877A 2010/12/23 11:19:59 (permalink)
0
I think I have to Working Now, This starts up, zeros everything, then when it gets to turn on, sets PORTA to 0x02 continues, then
once the loop is up, sets PORTA to 00, then starts over.
 
The only constants being used are for the counters, I found I could remove the rest. just semicoloned out right now.
 
Here is the whole lot 
 
The file is attached at the bottom because this is screwing the formatting up bigtime
     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;DEC 21 2010 LED TEST PROGRAM ON 16F877A
;
;
;
;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  Processor 16F877A  List p=16F877A
 #include <p16F877A.INC>  __config _XT_OSC & _WDT_OFF & _LVP_OFF & _CP_OFF
  ;Set up constants
;STATUS equ 03h  ;Assigns the word STATUS to the Value 03h
     ;which is the address of the status register.
     
;TRISA equ 85h  ;Assigns the word TRISA to the value 85h
     ;which is the address of the TRi-State reg of Port A
     
;PORTA equ 05h  ;assigns the word PORTA the value of 05h
     ;which is the address of the PORTA register
     
     
COUNT1 equ 20h  ;Assigns the word COUNT the value of 85h which is
     ;the address of the TRISA reg, also the value of 255
      COUNT2 equ 21h  ;Assigns the word COUNT2 the value of 09h which is the
     ;address of the PORTE Reg, also the value of 255
 org 0x0000
;*****Set Up The Port*******
 clrf  PORTA
BANKSEL  TRISA
 movlw 00h
 movwf TRISA
 movlw  0x0F
 movwf ADCON1
 
BANKSEL  PORTA 
  
 movlw 00h
 movwf COUNT1  ;Clears data from count1 on startup
 movwf COUNT2  ;Clears data from count2 on startup ;*****Turn LED On******** START
 
 movlw 02h
 movwf PORTA  ;copies working reg to PORTA
;*****Start of Delay Loop 1*******  call DELAY ;*****End Of Delay Loop 1********
;*****Turn Led Off********  movlw  00h  ;Loads zero data into working reg 00000
 movwf PORTA  ;Copies zero data to Port A   ;*****Off Delay Loop******  call DELAY
;*****Back to Start of Program******  goto START
;*****Delay Routine***** DELAY
LOOP1 decfsz COUNT1,1 ;Subtract 1 from 255, If count is Zero carry on
  goto LOOP1  ;Count is not zero, repeat DECFSZ
  decfsz COUNT2,1  ;Subtract 1 from 255
  goto  LOOP1  ;Continues decrementing Count1
 RETURN
;*****End of Program******  end
 
post edited by Johnnycrash - 2010/12/23 11:41:32
#26
MBedder
Circuit breaker
  • Total Posts : 6973
  • Reward points : 0
  • Joined: 2008/05/30 11:24:01
  • Location: Zelenograd, Russia
  • Status: offline
Re:Complete NOOB Requires some Explaination - PIC 16F877A 2010/12/23 12:07:33 (permalink) ☄ Helpful
+2 (1)
Some further recommendations and notes:

1. You do not need to use the Processor and list p = directives - the processor model should be selected in MPLAB before build, and thus will be passed to assembler via command line.

2. All SFRs are already defined in the processor include file, so you should never try to define them yourself.

3. At the very beginning of your code place the radix dec directive, and then use the human readable decimal numbers in their native form wherever possible.

4. Do not use multiple representation formats for hex numbers, i.e. H'ABCD' and 0ABCDh. There is a most common hex number representation format used in most programming languages - 0xABCD, so use only this one.

5. Learn and use the relocatable type of code generating. It's much more flexible and convenient than an old style absolute code type.

6. Make your routines reusable and flexible by passing the parameters to them instead of e.g. writing several delay routines with fixed parameters for every delay value required.

7. Do not calculate the numerical values manually, let the assembler do it automatically for you.

8. Try to avoid using numerical values unless it's absolutely necessary - use #define/equ to assign the numerical values to the named constants and use these named constants instead of "magic numbers".

9. Place a colon sign (:) after a label.

10. Comment your code, make the label and variable names self explanatory.

11. Put a whitespace between a semicolon and a comment text - ; Comment, not ;Comment.

12. Use tabulation instead of whitespace(s) to separate the command fields.

13. Separate functionally completed code blocks with empty lines.

14. Put labels at the beginning of SEPARATE lines, write commands and directives after tabulation.

15. Define an interrupt vector location and GOTO command even if you're not using interrupts yet - you will need them soon or later.

Here is an example of your code modified according to the above recommendations:


 
;--------------------------------------------------------------------------------------
;      LED LOOP TEST PROGRAM
;--------------------------------------------------------------------------------------
        RADIX   dec                     ; Set native decimal numbers representation
#include        <p16F877A.INC>          ; CPU specific definitions

;--------------------------------------------------------------------------------------
; Define named constants and assign values to them:

fosc            EQU     5000000         ; Crystal frequency, Hz
fcpu            EQU     fosc/4          ; CPU clock frequency, Hz

clks_1ms        EQU     fcpu/1000       ; Number of CPU clock cycles per 1 ms (1000 Hz)

tledon          EQU     50              ; Time to keep a LED on, ms (1..255 range)
tledoff         EQU     250             ; Time to keep a LED off, ms (1..255 range)

LED1            EQU     2               ; LED1 is connected to this port pin

;--------------------------------------------------------------------------------------
; Initialize the nonvolatile configuration register(s):

        __CONFIG        _XT_OSC & _WDT_OFF & _LVP_OFF & _CP_OFF

;--------------------------------------------------------------------------------------
; Start a data section and declare register file variables here:

        UDATA                           ; Unitialized data section directive

; RES N directive reserves N bytes in a register file for variable(s):

mscnt:  RES     1                       ; Milliseconds counter for DELAY_MS routine
intcnt: RES     1                       ; Internal loop counter for DELAY_MS routine
;--------------------------------------------------------------------------------------

;--------------------------------------------------------------------------------------
; Code sections follow:

;--------------------------------------------------------------------------------------
; Reset vector - code gets executed from here after a reset.

reset   CODE    0
        goto    start
;--------------------------------------------------------------------------------------

;--------------------------------------------------------------------------------------
; Interrupt vector - in-place (no GOTO) interrupt service routine (stub) code.

isr     CODE    4
        retfie
;--------------------------------------------------------------------------------------

;--------------------------------------------------------------------------------------
; We get here from a Reset vector.

start:
        BANKSEL ADCON1                  ; Switch to an ADCON1 and TRISA bank
        movlw   6                       ; Configure PORTA for digital i/o
        movwf   ADCON1                  ;

        bcf     TRISA,LED1              ; Configure a LED pin as output
        BANKSEL PORTA                   ; Switch to a PORTA bank


;--------------------------------------------------------------------------------------
; Main program loop:

main:
        bsf     PORTA,LED1              ; Turn a LED on for TLEDON ms
        movlw   tledon                  ;
        call    delay_ms                ;

        bcf     PORTA,LED1              ; Turn a LED off for TLEDOFF ms
        movlw   tledoff                 ;
        call    delay_ms                ;

        goto    main                    ; Loop
;--------------------------------------------------------------------------------------

;--------------------------------------------------------------------------------------
; Subroutines.

;--------------------------------------------------------------------------------------
; Delay for W milliseconds (W = 1..255 on entry):

delay_ms:
        movwf   mscnt           ; Store a W value to a milliseconds counter

loop_1ms:
        movlw   clks_1ms/5      ; Initialize internal loop counter to get 1 ms delay
        movwf   intcnt          ;

int_loop:
        clrwdt                  ; Waste a CPU cycle clearing a watchdog
        nop                     ; Waste a CPU cycle doing nothing
        decfsz  intcnt,f        ; Decrement INTCNT until zero
        goto    int_loop        ; Must be 5 CPU cycles per single int_loop

        decfsz  mscnt,f         ; Decrement MSCNT until zero
        goto    loop_1ms        ;

        return                  ; Done
;--------------------------------------------------------------------------------------

        END                     ; End of program directive


post edited by MBedder - 2010/12/23 12:10:30
#27
Johnnycrash
New Member
  • Total Posts : 18
  • Reward points : 0
  • Joined: 2010/12/22 19:15:58
  • Location: 0
  • Status: offline
Re:Complete NOOB Requires some Explaination - PIC 16F877A 2010/12/23 12:54:06 (permalink)
0
WOW,
Thanks for that, its very nice and clean and orderly.
Ive only even known this stuff existed for 2 days, so that really helps in furthering my understaning.
 
I see what you have done with the decimal, it makes it easier to think using that.
I had no idea it could be written like that.
 
INTCNT and MSCNT are those part of the pic or part of the assembler or are they just lables you assigned a value to?
 
Sorry if these questions are very basic, and thank you for your help,
any other examples/advice/info is more than welcome..
 
 
#28
MBedder
Circuit breaker
  • Total Posts : 6973
  • Reward points : 0
  • Joined: 2008/05/30 11:24:01
  • Location: Zelenograd, Russia
  • Status: offline
Re:Complete NOOB Requires some Explaination - PIC 16F877A 2010/12/23 13:04:33 (permalink)
0
INTCNT and MSCNT are the PIC's RAM (or so called "register file") locations. In 877 there are 368 RAM bytes which you can freely use for whatever purpose - just give them names as I did in the example.
#29
Johnnycrash
New Member
  • Total Posts : 18
  • Reward points : 0
  • Joined: 2010/12/22 19:15:58
  • Location: 0
  • Status: offline
Re:Complete NOOB Requires some Explaination - PIC 16F877A 2010/12/23 13:28:14 (permalink)
0
Ok, but in that program you didn't assign a register, you just put MSCNT RES 1 and INTCNT RES 1
So how does that work if there is no address for them, or does it just pick the first 2 available GPR's in BANK0?
They are coming up as GPR 20h & 21h which are the first 2 registers after ADCON0 in BANK0?
 
 
#30
Stefan Uhlemayr
Super Member
  • Total Posts : 4292
  • Reward points : 0
  • Joined: 2005/05/12 12:25:46
  • Location: Germany
  • Status: offline
Re:Complete NOOB Requires some Explaination - PIC 16F877A 2010/12/23 13:36:53 (permalink)
0
Johnnycrash

Ok, but in that program you didn't assign a register, you just put MSCNT RES 1 and INTCNT RES 1
So how does that work if there is no address for them, or does it just pick the first 2 available GPR's in BANK0?
They are coming up as GPR 20h & 21h which are the first 2 registers after ADCON0 in BANK0?
See here:
Stefan UhlemayrORIGINAL: post #18 of this thread

An additional recommend reading is the MPASM/MPLINK-User's Guide
Read it. wink

Greetings,
Stefan
#31
Johnnycrash
New Member
  • Total Posts : 18
  • Reward points : 0
  • Joined: 2010/12/22 19:15:58
  • Location: 0
  • Status: offline
Re:Complete NOOB Requires some Explaination - PIC 16F877A 2010/12/23 15:19:40 (permalink)
0
Stefan Uhlemayr

Johnnycrash

Ok, but in that program you didn't assign a register, you just put MSCNT RES 1 and INTCNT RES 1
So how does that work if there is no address for them, or does it just pick the first 2 available GPR's in BANK0?
They are coming up as GPR 20h & 21h which are the first 2 registers after ADCON0 in BANK0?
See here:
Stefan UhlemayrORIGINAL: post #18 of this thread

An additional recommend reading is the MPASM/MPLINK-User's Guide
Read it. wink

Greetings,
Stefan

 
Printed, Bound and Reading Now!
#32
Johnnycrash
New Member
  • Total Posts : 18
  • Reward points : 0
  • Joined: 2010/12/22 19:15:58
  • Location: 0
  • Status: offline
Re:Complete NOOB Requires some Explaination - PIC 16F877A 2010/12/23 17:22:29 (permalink)
0
Ok so I used the code that was provided by Mbedder, loaded it onto the pic, and I still get nothing.
The basic schematic is attached.
 
I am using a 5Mhz xtal, with 2 x 15pF caps to ground, and yea, not sure if that is correct, please take a look and tell me what you think.
Thanks
 
 
 

Attached Image(s)

#33
Ian.M
Super Member
  • Total Posts : 13273
  • Reward points : 0
  • Joined: 2009/07/23 07:02:40
  • Location: UK
  • Status: offline
Re:Complete NOOB Requires some Explaination - PIC 16F877A 2010/12/23 17:47:04 (permalink)
0
Modify his code:
       __CONFIG        _XT_OSC & _WDT_OFF & _LVP_OFF & _CP_OFF
to:
       __CONFIG        _HS_OSC & _WDT_OFF & _LVP_OFF & _CP_OFF & _ BODEN_ON
as you are using a faster crystal than is permitted in XT mode.

The _BODEN_ON enables Brown Out Reset which resets the PIC if the supply voltage is below about 4V. This means that if your supply is slow ramping up, the pic will still start correctly.  You can only use this feature on this PIC with a 5V supply, newer PICs have a configurable brownout voltage so can use it with a lower supply.

You MUST have decoupling capacitors. If you haven't got them in your actual circuit, add a 0.1uF ceramic from pins 11 to 12 and another from pins 31 to 32

N.B. the line:
LED1            EQU     2               ; LED1 is connected to this port pin
 specifies RA2, but your circuit shows the LED is on RA1.  You probably need to fix that!


#34
Johnnycrash
New Member
  • Total Posts : 18
  • Reward points : 0
  • Joined: 2010/12/22 19:15:58
  • Location: 0
  • Status: offline
Re:Complete NOOB Requires some Explaination - PIC 16F877A 2010/12/24 10:39:33 (permalink)
0
Well I added the 2 caps across Vdd and Vss, changed the config bits, and i still get diddly. When I meter the pins Im showing anywhere between 0v and .2v with no real activity. This is the same across all pins except vdd and vss which respectivly show 4.985 and 0.002

Im starting to wonder if maybe I damaged the chip some how.
also is there any way to verify that the program is actually loading onto the pic? Im using a Canakit pickit 2 programmer with slots, no ICSP right now.
#35
Ian.M
Super Member
  • Total Posts : 13273
  • Reward points : 0
  • Joined: 2009/07/23 07:02:40
  • Location: UK
  • Status: offline
Re:Complete NOOB Requires some Explaination - PIC 16F877A 2010/12/24 14:52:47 (permalink)
0
If the Canakit is a proper PICkit 2 clone, it will run with the stand-alone GUI software 'PICkit 2 V2.61' that you will find on the PICkit 2 page.

That software will let you read the chip and verify it against  the .hex file from your project without MPLAB's complexities.  If MPLAB is open you must make sure that the PICkit 2 is NOT selected either as programmer or debugger in MPLAB while you are using the standalone software.

If you need it, we can put together a very simple MPASM Assembler program using the external RC oscillator mode that makes every pin of the PIC toggle to let you test the chip is OK.

Do you have an oscilloscope?  If you do have, I think the next step should be checking the signal at the OSC2 pin to make sure the oscillator is actually running.
#36
Johnnycrash
New Member
  • Total Posts : 18
  • Reward points : 0
  • Joined: 2010/12/22 19:15:58
  • Location: 0
  • Status: offline
Re:Complete NOOB Requires some Explaination - PIC 16F877A 2010/12/24 19:19:33 (permalink)
0
Well in MPLAB there is a verify option next to the program button, that i have done and it comes back fine.

\i have the pickit2 gui aswell and it comes back fine aswell.

yes i have a scope
how does the test program work?
#37
MBedder
Circuit breaker
  • Total Posts : 6973
  • Reward points : 0
  • Joined: 2008/05/30 11:24:01
  • Location: Zelenograd, Russia
  • Status: offline
Re:Complete NOOB Requires some Explaination - PIC 16F877A 2010/12/25 04:24:39 (permalink)
0
You have to make sure the oscillator is running. If you do not have a scope, make a primitive rectifier with a capacitor, diode and resistor, connect it to the OSC2 output and measure the rectified DC voltage - it should be at least 1V if the osc is running and 0V otherwise:

 
OSC2 >--||--*----*--> VDC+ to multimeter
        C  _|_   |
    0.1 uF /_\   \ R
          D |    / 100k
     1N4148 |    \
            |    |
VSS  >------*----*--> VDC- to multimeter


#38
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re:Complete NOOB Requires some Explaination - PIC 16F877A 2010/12/25 08:19:22 (permalink)
0
Yeah,
I usually used a simple Tester (analog) set for dbOutput measurement. Basically an AC measurer with AC coupling

GENOVA :D :D ! GODO
#39
Johnnycrash
New Member
  • Total Posts : 18
  • Reward points : 0
  • Joined: 2010/12/22 19:15:58
  • Location: 0
  • Status: offline
Re:Complete NOOB Requires some Explaination - PIC 16F877A 2010/12/25 13:30:36 (permalink)
0
I have ac and dc freq option on my meter would that work?
#40
Page: < 123 > Showing page 2 of 3
Jump to:
© 2021 APG vNext Commercial Version 4.5