Model Boat Mayhem

Please login or register.

Login with username, password and session length.
Pages: 1 2 3 [4] 5 6 7   Go Down

Author Topic: Help writing pic code  (Read 22863 times)

TurboTyne

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 113
  • Location: Newcastle upon Tyne, England
Re: Help writing pic code
« Reply #75 on: September 18, 2014, 06:56:02 AM »

Hi Stew
Yes I do use MPLAB. I'm away from home at present and cannot remember the exact version but it is the latest of the versions that preceed the X version. I have been planning to switch to X at some point as I guess this will get best support from MP in the future, but I've not tried it yet. I think all versions may look a bit challenging when you start out but like many things, its not as bad as it first looks.
I really like the simulator tool. It lets you look at all the various registers as you step through your code.
If you downloaded yours from the MPweb site you'll already realise that the various versions are all available.

Regards  Mike
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #76 on: September 19, 2014, 11:27:08 AM »

For my 20ms loop using timer0 with a prescaler of 256, how do I know when TMR0 = 78?

Stew
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #77 on: September 19, 2014, 01:37:10 PM »

I'm off to zap a few million brain cells at York beer festival.
See you all in a couple of days.
Stew
Logged

TurboTyne

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 113
  • Location: Newcastle upon Tyne, England
Re: Help writing pic code
« Reply #78 on: September 19, 2014, 02:48:49 PM »

For my 20ms loop using timer0 with a prescaler of 256, how do I know when TMR0 = 78?

Stew

Hi Stew

Follow guidelines posted by Ian already. The following waffle expands a little upon what Ian said:

Since you have pre-scaler set at 256 (i.e. by setting bits in the OPTION register), the timer increments every time the instruction cycle (1 usec) goes through 256 cycles i.e. once every 256 usecs. The TMR0 register can be read and written to. If you clear it before starting the timer, it starts counting up from zero. It is a 1 byte timer, so when it reaches value 255 it cannot go any higher and rolls over to zero and immediately re-starts the process. When it rolls over it causes the interrupt flag TOIF in the INTCON register to be set. If you don't have interrupts enabled this will not do anything and the only way to respond to it is by keep testing the flag bit to see when it gets set. Once that happens you must clear the flag and then do what you want to do. However, the best way is to enable interrupts and set up an interrupt service routine (ISR) which would also need to clear the interrupt flag and then carry out the response you want. This way the PIC can get on with other tasks but when the interrupt occurs its current processes are halted temporarily while it reponds in the way specified in the ISR.
In order to time for ca 20 msec you need to stop the TMR0, move a starting value into TMR0 register and then re-start the timer. The starting value is 256 - 78 = 178. This means that when the timer has counted from 178 to 255 + 1 (i.e. 78 * 256 usecs) it will roll over to zero and trigger the IF flag to be set.

Enjoy the beers s s s!!

Mike
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #79 on: September 21, 2014, 02:15:35 PM »

Hi Mike
Thanks for advice.
I'll try making a simple loop that turns a servo either right or left depending on a switch being pressed. This will test the timings.
I don't think I'll need to use an interrupt as the loop will be waiting for the time to reach 20ms, I'll simply keep testing the interrupt flag.
I'll also be able to alter this loop as I add a second servo and use the sensors as inputs. Should be plain sailing from there.
regards
Stew
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #80 on: September 21, 2014, 06:57:16 PM »

Can you see any problems with this code

;CONFIGURATION SECTION

START    MOVLW    B'00011111'    ;5bits of PORTA are I/P
        MOVWF    TRISA

        MOVLW    B'00000000'               
        MOVWF    TRISB        ;PORTB is OUTPUT

        MOVLW    B'00000111'    ;Prescaler is /256
        MOVWF    OPTION_REG

        CLRF    PORTA        ;Clears PortA.
        CLRF    PORTB        ;Clears PortB.

;SUBROUTINE SECTION.

WAIT1MS   MOVLW    d'250'       ;Initial value - tweak if req.
          NOP
LOOP1MS   ADDLW    d'255'       ;dec W
          BTFSS    STATUS, Z    ;Zero flag set?
          GOTO     LOOP1MS      ;  No, keep looping
          RETURN                ;  Yes, 1ms done



;Program starts now.

BEGIN
    CLRF    TMR0    ;clears tmr0
    CLRW        ;clears working register
    ADDLW    177    ;256-177=78
    ADDWF    TMR0    ;counts for 20ms
    BCF    INTCON,2    ;clear overflow flag
    BCF    OPTION_REG,5    ;start timer mode
    BTFSS    PORTA,0        ;test for switch press
    GOTO    SERVOR
    GOTO    SERVOL
SERVOR    BSF    PORTB,0        ;turn on power to servo
CALL    WAIT1MS           
    BCF    PORTB,0        ;turn off power to servo
    GOTO    WAIT20MS   
SERVOL    BSF    PORTB,0        ;turn on power to servo
    CALL    WAIT1MS       
    CALL    WAIT1MS       
    BCF    PORTB,0        ;turn off power to servo
    GOTO    WAIT20MS   
WAIT20MS
    BTFSS    INTCON,2    ;wait until timer0 reaches 20ms   
    GOTO    WAIT20MS
    BSF    OPTION_REG,5    ;stop timer mode
    GOTO    BEGIN        ;keep looping
END

Stew
Logged

TurboTyne

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 113
  • Location: Newcastle upon Tyne, England
Re: Help writing pic code
« Reply #81 on: September 21, 2014, 08:52:43 PM »

Can you see any problems with this code

 
Stew

Hi Stew

I assume this is just the meat of your prohramme? i.e. do you have

    list      p=16F????    ; list directive to define processor
    #include <P16F????.inc>        ; processor specific variable definitions

also do you have a

    __CONFIG

statement to define clock source etc?
and staements to set clock fequency.

I always include the following which suppresses trivial error messages:

    errorlevel  -302              ; suppress message 302 from list file


Minor point but instead of         
        MOVLW    B'00000000'               
        MOVWF    TRISB        ;PORTB is OUTPUT
you could just write 
       CLRF         TRISB

Instead of
  CLRW        ;clears working register
    ADDLW    177    ;256-177=78
    ADDWF    TMR0    ;counts for 20ms

I would just write:

MOVLW    d'177'
MOVWF   TMR0

When reading in values as literals you must always indicate the type of value (i.e. decimal, hex, binary)
Also you want to move the value into TMR0 not add anything together.

I don't know what PORTB pin 0 is connected to. Maybe an LED via a resistor? I don't see how it would influence a servo.

Testing for switch presses can be problematic but maybe not in this case. It is common to include code to stop detecting switch bounce.

One final thing, as a precaution it is common to include BANKSEL directives. I don't know if this is necessary for your PIC but I've wasted ages tracing bugs which turn out to be omitted BANKSELs at unfortunate points in the programme. If all the registers you are using are in the same bank you can forget this for the time being.

I've had a quick look but have not checked the details of which bits are relevant in INTCON etc. I hope these comments are helpful (and also that they are accurate  %) ).

Mike
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #82 on: September 21, 2014, 10:25:36 PM »

Hi Mike
I've added a few lines and made the alterations you suggest, it now looks like the following.

; EQUATES SECTION

TMR0        EQU    1        ;means TMR0 is file 1.   
STATUS        EQU    3        ;means STATUS is file 3.   
PORTA        EQU    5        ;means PORTA  is file 5.   
PORTC        EQU    7        ;means PORTB is file 6.
INTCON        EQU    08H        ;INTCON is file 08H
TRISA        EQU    85H     ;TRISA (the PORTA I/O selection) is file 85H
TRISC        EQU    87H     ;TRISC (the PORTC I/O selection) is file 86H
OPTION_REG     EQU    81H        ;the OPTION register is file 81H   
ZEROBIT        EQU    2        ;means ZEROBIT is bit 2.   
;*********************************************************

    LIST    P=16F684    ; we are using the 16F684.
    ORG    0                ; the start address in memory is 0
    GOTO    START        ; goto start!

;**********************************************************************
; Configuration Bits


;*********************************************************


;CONFIGURATION SECTION

START   
    MOVLW    B'00011111'    ;5bits of PORTA are I/P
**MOVWF    TRISA
**CLRF    TRISC        ;PORTC is OUTPUT
    MOVLW    B'00000111'    ;Prescaler is /256
**MOVWF    OPTION_REG
    CLRF    PORTA        ;Clears PortA.
    CLRF    PORTC        ;Clears PortC.

;SUBROUTINE SECTION.

WAIT1MS   MOVLW    d'250'       ;Initial value - tweak if req.
          NOP
LOOP1MS   ADDLW    d'255'       ;dec W
          BTFSS    STATUS,2            ;Zero flag set?
          GOTO     LOOP1MS      ;  No, keep looping
          RETURN                ;  Yes, 1ms done



;Program starts now.

BEGIN
    CLRF    TMR0    ;clears tmr0
    MOVLW    d'178'    ;256-178=78 counts for 20ms
    MOVWF    TMR0   
    BCF    INTCON,2    ;clear overflow flag
**BCF    OPTION_REG,5    ;start timer mode
    BTFSS    PORTA,0        ;test for switch press
    GOTO    SERVOR
    GOTO    SERVOL
SERVOR    BSF    PORTC,0        ;turn on power to servo
    CALL    WAIT1MS           
    BCF    PORTC,0        ;turn off power to servo
    GOTO    WAIT20MS   
SERVOL    BSF    PORTC,0        ;turn on power to servo
    CALL    WAIT1MS       
    CALL    WAIT1MS       
    BCF    PORTC,0        ;turn off power to servo
    GOTO    WAIT20MS   
WAIT20MS
    BTFSS    INTCON,2    ;wait until timer0 reaches 20ms   
    GOTO    WAIT20MS
**BSF    OPTION_REG,5    ;stop timer mode
    GOTO    BEGIN        ;keep looping
    END

When I try to do a quickbuild I get the following message.


Clean: Deleting intermediary and output files.
Clean: Done.
Executing: "C:\Program Files\Microchip\MPASM Suite\MPASMWIN.exe" /q /p16F684 "20msloop.asm" /l"20msloop.lst" /e"20msloop.err"
Message[302] C:\PIC PROJECTS\20MSLOOP\20MSLOOP.ASM 29 : Register in operand not in bank 0.  Ensure that bank bits are correct.
Message[302] C:\PIC PROJECTS\20MSLOOP\20MSLOOP.ASM 30 : Register in operand not in bank 0.  Ensure that bank bits are correct.
Message[302] C:\PIC PROJECTS\20MSLOOP\20MSLOOP.ASM 32 : Register in operand not in bank 0.  Ensure that bank bits are correct.
Message[302] C:\PIC PROJECTS\20MSLOOP\20MSLOOP.ASM 54 : Register in operand not in bank 0.  Ensure that bank bits are correct.
Message[302] C:\PIC PROJECTS\20MSLOOP\20MSLOOP.ASM 70 : Register in operand not in bank 0.  Ensure that bank bits are correct.
Executing: "C:\Program Files\Microchip\MPASM Suite\mplink.exe" /p16F684 "20msloop.o" /z__MPLAB_BUILD=1 /o"20msloop.cof" /M"20msloop.map" /W /x


error lines are as follows

29    MOVWF    TRISA
30    CLRF    TRISC
32    MOVWF    OPTION_REG
54    BCF    OPTION_REG,5    ;start timer mode
70    BSF    OPTION_REG,5    ;stop timer mode

I've marked them with asterix


I have looked at how to change banks but can't get my head around it.
Also for some reason this chip uses PortA and PortC not PortB.
I have seen programs with an include statement but have no idea what they are.
I'll try to figure out how to do the __config statement.

PortC,0 will be connected to the signal wire of the servo, I think it just needs a tiny current.

Stew
Logged

flashtwo

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 521
  • Location: Sevenoaks, Kent, England
Re: Help writing pic code
« Reply #83 on: September 21, 2014, 11:09:36 PM »

Hi Stew,

Before you use a Register the PIC needs to know which memory bank it is located by setting the STATUS word. All is fine until you wish to use another register in the other memory bank, in which case you have to switch banks by resetting the STATUS word, otherwise the PIC will just refer to the wrong register.

From the 684 documentation Fig 2-2 you can see that the Special Function Registers (the ones that control the functions like Timers and I/O etc.) and General Purpose Registers (the ones that you control and give names to) are spread between the two Memory Banks.

To switch between the two banks, one must set bits in the STATUS word, which appears in both banks. Not being in the correct bank causes the most numerous bugs in a program and needs a bit of discipline to avoid. To simplify switching between banks, I place routines at the start of the code which can be called avoiding repetition of the switching code, viz:

BANK_0_P0   BCF   STATUS,RP0   ; Switch to register bank 0.
              BCF   STATUS,RP1   ;      
              RETURN

BANK_1_P0   BSF   STATUS,RP0   ; Switch to register bank 1.
              BCF   STATUS,RP1   ;      
              RETURN

BANK_2_P0   BCF   STATUS,RP0   ; Switch to register bank 2.
              BSF   STATUS,RP1   ;      
              RETURN

BANK_3_P0   BSF   STATUS,RP0   ; Switch to register bank 3.
              BSF   STATUS,RP1   ;      
              RETURN

On the larger PICs there are four banks, so I have shown routines for all four.

I know the 684 has only one memory page, but for larger PICs the routines have to be repeated on each page and have a routine name dedicated to that page; hence my routines for page zero are called "BANK_0_P0", "BANK_1_P0" etc.

Part of the programers art is to group the variables (registers) that are used together in the same memory bank to avoid lots of confusing switching.

Ian
Logged

TurboTyne

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 113
  • Location: Newcastle upon Tyne, England
Re: Help writing pic code
« Reply #84 on: September 21, 2014, 11:10:55 PM »

Hi Stew

You should be able to find in the files provided by MP that come with the MPLAB IDE a file that is a typical programme file for the PIC16F684 device. This is called PIC16F684.temp. Included in it are all the routine setting up stuff that can be really tricky to work out from scratch. Also there will be a file called PIC16F684.INC. This contains definitions of all the special function registers so there is no need to use EQU directives to define registers such as TMR0 and PORTA.

I am using MPLAB IDE v8.92. The INC files were in the directory "MPASM suite" and TEWMP files in the directory "MPASM suite\template\code". If you can't find the files in your library then let me know and I'll send them off thread.

Mike
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #85 on: September 21, 2014, 11:45:21 PM »

Hi Mike
I've tried putting my code into the template and including the .inc file but I get the same errors as before.

    list        p=16f684        ; list directive to define processor
    #include    <P16F684.inc>        ; processor specific variable definitions
   
    __CONFIG    _CP_OFF & _CPD_OFF & _BOD_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _MCLRE_ON & _FCMEN_OFF & _IESO_OFF


; '__CONFIG' directive is used to embed configuration data within .asm file.
; The labels following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.






;***** VARIABLE DEFINITIONS
w_temp        EQU    0x7E            ; variable used for context saving
status_temp    EQU    0x7F            ; variable used for context saving





;**********************************************************************
    ORG        0x000            ; processor reset vector
      goto        main            ; go to beginning of program


    ORG        0x004            ; interrupt vector location
    movwf        w_temp            ; save off current W register contents
    movf        STATUS,w        ; move status register into W register
    movwf        status_temp        ; save off contents of STATUS register

; isr code can go here or be located as a call subroutine elsewhere

    movf        status_temp,w        ; retrieve copy of STATUS register
    movwf        STATUS            ; restore pre-isr STATUS register contents
    swapf        w_temp,f
    swapf        w_temp,w        ; restore pre-isr W register contents
    retfie                    ; return from interrupt


main


START   
    MOVLW    B'00011111'    ;5bits of PORTA are I/P
    MOVWF    TRISA
    CLRF    TRISC        ;PORTC is OUTPUT
    MOVLW    B'00000111'    ;Prescaler is /256
    MOVWF    OPTION_REG
    CLRF    PORTA        ;Clears PortA.
    CLRF    PORTC        ;Clears PortC.

;SUBROUTINE SECTION.

WAIT1MS   MOVLW    d'250'       ;Initial value - tweak if req.
          NOP
LOOP1MS   ADDLW    d'255'       ;dec W
          BTFSS    STATUS,2  ;Zero flag set?
          GOTO     LOOP1MS      ;  No, keep looping
          RETURN                ;  Yes, 1ms done



;Program starts now.

BEGIN
    CLRF    TMR0    ;clears tmr0
    MOVLW    d'178'    ;256-178=78 counts for 20ms
    MOVWF    TMR0   
    BCF    INTCON,2    ;clear overflow flag
    BCF    OPTION_REG,5    ;start timer mode
    BTFSS    PORTA,0        ;test for switch press
    GOTO    SERVOR
    GOTO    SERVOL
SERVOR    BSF    PORTC,0        ;turn on power to servo
    CALL    WAIT1MS           
    BCF    PORTC,0        ;turn off power to servo
    GOTO    WAIT20MS   
SERVOL    BSF    PORTC,0        ;turn on power to servo
    CALL    WAIT1MS       
    CALL    WAIT1MS       
    BCF    PORTC,0        ;turn off power to servo
    GOTO    WAIT20MS   
WAIT20MS
    BTFSS    INTCON,2    ;wait until timer0 reaches 20ms   
    GOTO    WAIT20MS
    BSF    OPTION_REG,5    ;stop timer mode
    GOTO    BEGIN        ;keep looping




    ORG    0x2100                ; data EEPROM location
    DE    1,2,3,4                ; define first four EEPROM locations as 1,2,3,&4




    END                       ; directive 'end of program'
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #86 on: September 22, 2014, 12:08:00 AM »

Hi
This is getting more confusing by the minute.
Can someone please tell me what code I need to put in and where.
Maybe then I can work out what is going on.
I've looked for banksel and can't find any mention of it in the datasheet or the mplab user guide.
Thanks
Stew
Logged

TurboTyne

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 113
  • Location: Newcastle upon Tyne, England
Re: Help writing pic code
« Reply #87 on: September 22, 2014, 07:06:49 AM »

Hi Stew

Another important document you need to download from here:

http://www.microchip.com/pagehandler/en-us/family/mplabx/home.html

Scroll down to documentation section and get:

MPASM/MPLINK users guide

This explains about BANKSEL etc.


You can either do it Ian's way or use BANKSEL but you must do something because your error messages are saying the bank is wrong.

Simplest thing to do is, before each and every instruction that refers to any register place a line like this example:

e.g. instead of: 

    CLRF    TMR0    ;clears tmr0
    MOVLW    d'178'    ;256-178=78 counts for 20ms
    MOVWF    TMR0   
    BCF    INTCON,2    ;clear overflow flag
**BCF    OPTION_REG,5    ;start timer mode
    BTFSS    PORTA,0        ;test for switch press


you should write:

    BANKSEL      TMR0
    CLRF    TMR0    ;clears tmr0
    MOVLW    d'178'    ;256-178=78 counts for 20ms
    MOVWF    TMR0   
    BANKSEL    INTCON
    BCF    INTCON,2    ;clear overflow flag
    BANKSEL    OPTION
**BCF    OPTION_REG,5    ;start timer mode
    BANKSEL  PORTA
    BTFSS    PORTA,0        ;test for switch press



BANKSEL instructs the compiler to insert extra commands to make the PIC read from the correct memory bank for the register in question. i.e. it inserts lines equivalent to the one's suggested by Ian.


If you know that the next register to be used is in the same bank as the last you can omit the BANKSEL, but it is safest to always use it, especially when starting out. Downside of extra BANKSELs is that you end up inserting unnecessary code, but for short programmes like yours this does not matter.

Regards  Mike

Logged

flashtwo

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 521
  • Location: Sevenoaks, Kent, England
Re: Help writing pic code
« Reply #88 on: September 22, 2014, 09:11:29 AM »

Hi Stew,

Regarding the _CONFIG file, I forgo any #include statements and set it up from scratch. It encourages a complete understanding of what is happening and removes the "mystery" and "magic" of using other peoples code. The only document I refer to is the PIC's data sheet and derive all my code from there. This is RAW programing, but it enables understandable code and removes unnecessary library code.

The _CONFIG is only a register with lots of "switches" that sets up the basic operation of the PIC; here is my version for an 16F877 PIC:-

;==========================================================================
;
;       Configuration Bits
;
;==========================================================================

_BODEN_ON                    EQU     H'3FFF'
_BODEN_OFF                   EQU     H'3FBF'
_CP_ALL                      EQU     H'03FF'
_CP_75                       EQU     H'17FF'
_CP_50                       EQU     H'2BFF'
_CP_OFF                      EQU     H'3FFF'
_DATA_CP_ON                  EQU     H'3EFF'
_DATA_CP_OFF                 EQU     H'3FFF'
_PWRTE_OFF                   EQU     H'3FFF'
_PWRTE_ON                    EQU     H'3FF7'
_WDT_ON                      EQU     H'3FFF'
_WDT_OFF                     EQU     H'3FFB'
_LVP_ON                      EQU     H'3FFF'
_LVP_OFF                     EQU     H'3F7F'
_MCLRE_ON                    EQU     H'3FFF'
_MCLRE_OFF                   EQU     H'3FDF'
_ER_OSC_CLKOUT               EQU     H'3FFF'
_ER_OSC_NOCLKOUT             EQU     H'3FFE'
_INTRC_OSC_CLKOUT            EQU     H'3FFD'
_INTRC_OSC_NOCLKOUT          EQU     H'3FFC'
_EXTCLK_OSC                  EQU     H'3FEF'
_LP_OSC                      EQU     H'3FEC'
_XT_OSC                      EQU     H'3FFD'
_HS_OSC                      EQU     H'3FEE'

   __CONFIG        _BODEN_ON & _CP_OFF & _DATA_CP_OFF & _PWRTE_ON & _WDT_OFF & _LVP_OFF  & _XT_OSC

(the Tabbing doesn't seem to transfer - sorry)

Ian
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #89 on: September 22, 2014, 12:11:54 PM »

Hi Mike & Ian
At the moment I feel like I am searching down blind alleys and I don't even know what I am searching for. Unfortunately the more information I'm given the more blind alleys I'm searching down.
Would it be possible for one of you to turn the following code into working code.
I feel I would learn quicker if I had some working code to try and understand rather than code that doesn't work which is battering my brain.
This code seems to build ok and it seems to run ok in MPLAB SIM, although very slowly, but when I burn it to a chip I'm not getting any output.

; EQUATES SECTION

TMR0        EQU        1        ;means TMR0 is file 1.   
STATUS      EQU        3        ;means STATUS is file 3.   
PORTA       EQU        5        ;means PORTA  is file 5.   
PORTC       EQU        7        ;means PORTB is file 6.
INTCON      EQU        08H        ;INTCON is file 08H
TRISA       EQU        85H     ;TRISA (the PORTA I/O selection) is file 85H
TRISC       EQU        87H     ;TRISC (the PORTC I/O selection) is file 86H
OPTION_REG  EQU        81H        ;the OPTION register is file 81H   
ZEROBIT     EQU        2        ;means ZEROBIT is bit 2.   
OSCCON        EQU        8FH
;*********************************************************

    LIST    P=16F684    ; we are using the 16F684.
    ORG        0                ; the start address in memory is 0
    GOTO    START        ; goto start!

;**********************************************************************
; Configuration Bits
    __CONFIG    H'34C4'

;*********************************************************
;SUBROUTINE SECTION.

WAIT1MS   MOVLW    d'250'       ;Initial value - tweak if req.
          NOP
LOOP1MS   ADDLW    d'255'       ;dec W
          BTFSS    STATUS,2            ;Zero flag set?
          GOTO    LOOP1MS      ;  No, keep looping
          RETURN                ;  Yes, 1ms done


;CONFIGURATION SECTION

START   
    BANKSEL    TRISA   
    MOVLW   B'00011111'    ;5bits of PORTA are I/P
    MOVWF   TRISA
    CLRF    TRISC        ;PORTC is OUTPUT
    MOVLW   B'00000111'    ;Prescaler is /256
    MOVWF   OPTION_REG
    MOVLW    B'01100111'
    MOVWF    OSCCON   
    BANKSEL    PORTA
    CLRF    PORTA        ;Clears PortA.
    CLRF    PORTC        ;Clears PortC.




;Program starts now.

BEGIN
        CLRF    TMR0    ;clears tmr0
        MOVLW    d'178'    ;256-178=78 counts for 20ms
        MOVWF    TMR0   
        BCF        INTCON,2    ;clear overflow flag
        BANKSEL    OPTION_REG
        BCF        OPTION_REG,5    ;start timer mode
        BANKSEL    PORTA
        BTFSS    PORTA,0        ;test for switch press
        GOTO    SERVOR
        GOTO    SERVOL
SERVOR  BSF        PORTC,0        ;turn on power to servo
        CALL    WAIT1MS           
        BCF        PORTC,0        ;turn off power to servo
        GOTO    WAIT20MS   
SERVOL  BSF        PORTC,0        ;turn on power to servo
        CALL    WAIT1MS       
        CALL    WAIT1MS       
        BCF        PORTC,0        ;turn off power to servo
        GOTO    WAIT20MS   
WAIT20MS
        BTFSS    INTCON,2    ;wait until timer0 reaches 20ms   
        GOTO    WAIT20MS
        BANKSEL    OPTION_REG
        BSF        OPTION_REG,5    ;stop timer mode
        BANKSEL    TMR0
        GOTO    BEGIN        ;keep looping
        END
Logged

flashtwo

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 521
  • Location: Sevenoaks, Kent, England
Re: Help writing pic code
« Reply #90 on: September 22, 2014, 01:53:42 PM »

Hi Stew,

We're working on it.

I'm finding it a useful revision course.

Tenacious is the best software tool!

Ian.
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #91 on: September 22, 2014, 02:13:05 PM »

Thanks a lot Ian
I thought climbing Skiddaw was steep but not half as steep as the learning curve here.
My brain hurts.
Stew
Logged

flashtwo

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 521
  • Location: Sevenoaks, Kent, England
Re: Help writing pic code
« Reply #92 on: September 22, 2014, 02:52:31 PM »

Hi Stew,

Initial thought regards the "EQU" statement.

Looking at document DS33014L-page 68, the number format is either H'3' or 0x003, hexadecimal being the default "radix".

E.g. STATUS   EQU  H'3' or STATUS   EQU  0x003,  personally I prefer the H'3' version.

How did you arrive at the config H'34C4', which suggests to me that you are using an external clock source?

Ian

Ian
Logged

flashtwo

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 521
  • Location: Sevenoaks, Kent, England
Re: Help writing pic code
« Reply #93 on: September 22, 2014, 03:04:15 PM »

Hi Stew,

Try putting the ORG after the __Config , viz:-

      ORG           H'0000'      ;Reset vector address.
      GOTO   START             ;goto START routine on bootup.
      ORG           H'0004'      ; Interrupt vector address.
      NOP         ; No operation.
                 place your Interrupt Service routine here (if used)

               END of the interrupt service routine

               your routines
               START

Ian
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #94 on: September 22, 2014, 04:19:21 PM »

Hi Ian
I used mplab to set the config bits and that's the result it gave me.

FOSC   internal oscillator, I/O on rc4 and RA5
WDTE  watchdog timer disabled
PWRTE  power up timer enabled
CP       code protection disabled
CPD     data code protection disabled
BOREN  brown out detect disabled
IESO     internal external switchover mode enabled
FCMEN  fail safe clock monitor disabled



What is ORG, there's no mention of it in datasheet?
Please try and keep this as simple as possible.
I don't think this is a complicated bit of code, it should be easy to sort out.

Stew
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #95 on: September 22, 2014, 04:29:12 PM »

Ian
I added the ORG lines and when I tried to build it got the following error

Error[118] C:\PICPROJECTS\20MSLOOP\TEST.ASM 24:Overwriting previous address contents(0000)

Line 24 is GOTO START

Stew
Logged

flashtwo

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 521
  • Location: Sevenoaks, Kent, England
Re: Help writing pic code
« Reply #96 on: September 22, 2014, 04:58:43 PM »

Hi Stew,

The ORG is describe (in somewhat cryptic language!) in the MPASM programmer document DS33014L (page 141) and is the memory location 000h where the PIC starts from when it is RESET (i.e. switched on or manually reset). After 000h, there are three memory locations (before the Interrupt Vector) where some instructions can be placed to handle the RESET, in our case GOTO START.

To put it simply, it is the first in-tray you see when the office lights are switched on and contains the first order of the day - GOTO.

It may be as well to have the most simple cyclic program to start with:

START     NOP
              GOTO START

and see if the system clock is working by putting the 'scope on Pin 3 CLKOUT and looking for a square wave - RA4 will have to be designated as an output. If the clock is not there then __CONFIG may need some attention.

Regarding Error[118] - could you post the code again?

I shall be looking at config again - hang on there!

Ian
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #97 on: September 22, 2014, 05:23:16 PM »

Ian
The code has changed that much I don't know where I am.

Can we start from the beginning ie what do I need to put in the equates section?
Originally it looked like this. Is this correct or do I need to change it?

; EQUATES SECTION

TMR0         EQU        1        ;means TMR0 is file 1.   
STATUS      EQU        3        ;means STATUS is file 3.   
PORTA       EQU        5        ;means PORTA  is file 5.   
PORTC       EQU        7        ;means PORTB is file 6.
INTCON     EQU        08H        ;INTCON is file 08H
TRISA        EQU        85H     ;TRISA (the PORTA I/O selection) is file 85H
TRISC        EQU        87H     ;TRISC (the PORTC I/O selection) is file 86H
OPTION_REG  EQU        81H        ;the OPTION register is file 81H   
ZEROBIT     EQU        2        ;means ZEROBIT is bit 2.   
OSCCON        EQU        8FH
Logged

flashtwo

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 521
  • Location: Sevenoaks, Kent, England
Re: Help writing pic code
« Reply #98 on: September 22, 2014, 06:32:50 PM »

Hi Stew,

I think the register number after EQU should be of the form H'3' etc., to ensure the programmer (MPASM) knows it is dealing with an hexadecimal number.

To be pedantic, your files should really be referred to as the registers; it avoids future confusion.

My mistake regarding the config number - I was looking at the wrong PIC data sheet.

Regarding the use of a 'scope on pin 3, the config number would have to change the FOSC to INTOSC rather than INTOSCIO and that will allow the CLKOUT to be monitored. The config number will then change from H'34C4' to H'34C5'.

Regarding getting lost with all the changes, always copy your code into a new version number and make the changes in the copy and keep the old code (it may be better than the new one!). At the top of the code page, put your version comments in and your reasons and objectives for change, plus the date - example:-

;*  CONTROLLER_V32 copy of CONTROLLER_V31.               *
;*      The feed pump Master is now manually selectable from the Display*
;*      Unit instead of swopping over the ESC connectors and the RPM   *
;*      connectors.                     *
;*      Placed PORTC,ACTIVE in CONTROL routine to give a better      *
;*      oscilloscope square wave.               *
;*      The "A" and "B" Pump displacements are now amendable from the    *
;*      Display Unit.                     *
;*                              *
;*  MODIFIED BY: I GERRARD     23/03/2012               *

Excuse the flying stars, tabbing not consistent.

Have you got two ORG statements that could be causing that error message?

Ian

Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #99 on: September 22, 2014, 08:12:29 PM »

Progress

The following code gives me a lovely 5v 1ms pulse every 2ms.
It's not a lot but at least it's something to work from, and it tells me the pic and config is ok.
The problem seems to be in the 20ms delay routine.

; EQUATES SECTION

TMR0         EQU        H'01'        ;means TMR0 is file 1.   
STATUS      EQU        H'03'        ;means STATUS is file 3.   
PORTA       EQU        H'05'        ;means PORTA  is file 5.   
PORTC       EQU        H'07'        ;means PORTB is file 6.
INTCON     EQU        H'08'        ;INTCON is file 08H
TRISA        EQU        H'85'        ;TRISA (the PORTA I/O selection) is file 85H
TRISC        EQU        H'87'        ;TRISC (the PORTC I/O selection) is file 86H
OPTION_REG  EQU   H'81'        ;the OPTION register is file 81H   
ZEROBIT     EQU       H'02'        ;means ZEROBIT is bit 2.   

;*********************************************************

    LIST    P=16F684    ; we are using the 16F684.
    ORG        0                ; the start address in memory is 0
    GOTO    START        ; goto start!

;**********************************************************************
; Configuration Bits
    __CONFIG    H'30C4'

;*********************************************************
;SUBROUTINE SECTION.

WAIT1MS   MOVLW    d'250'       ;Initial value - tweak if req.
                  NOP
LOOP1MS   ADDLW    d'255'       ;dec W
                  BTFSS    STATUS,2            ;Zero flag set?
                  GOTO    LOOP1MS      ;  No, keep looping
                  RETURN                ;  Yes, 1ms done


;CONFIGURATION SECTION

START   
    BANKSEL    TRISA   
    MOVLW   B'00011111'    ;5bits of PORTA are I/P
    MOVWF   TRISA
    CLRF    TRISC        ;PORTC is OUTPUT
    MOVLW   B'00000111'    ;Prescaler is /256
    MOVWF   OPTION_REG
       
    BANKSEL    PORTA
    CLRF    PORTA        ;Clears PortA.
    CLRF    PORTC        ;Clears PortC.




;Program starts now.

BEGIN
    BSF        PORTC,0
    CALL    WAIT1MS
    BCF        PORTC,0
    CALL    WAIT1MS
    GOTO    BEGIN
    END



Happier now
Stew
Logged
Pages: 1 2 3 [4] 5 6 7   Go Up