1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Fish Feeder Idea...Help Regards

Discussion in 'Microcontrollers' started by manoj soorya, Feb 20, 2018.

Tags:
  1. Dan Soze

    Dan Soze Member

    Joined:
    Dec 21, 2017
    Messages:
    38
    Likes:
    18
    Congratulations and well done.

    To go a bit further with this example I would like to mention that there are at least 8 places in the code that are style bugs in how the statements are written.

    Let me explain what a style bug is and why this is an important concept.

    A style bug occurs when the syntax used in a statement is inconsistent with other similar statements in the same project.

    Style bugs do not usually cause the application to fail to work properly. The problems that they cause are in the mind of the developer when projects become large. In a small project, like this one, inconsistent style is no real problem. In larger projects with hundreds of files, tens of thousands of lines of code and multiple developers then consistent usage of style is essential to maintain sanity.

    When anyone starts to learn how to develop code it is important to experiment with various styles then adopt the style that helps you create clear and consistent code for the computer language you will use. When working with a group there needs to be a style guide that all group members approve of and will use consistently.

    So for additional credit find at least 8 statements where I used correct but inconsistent syntax or formatting.

    For even more credit add comments, in any language you are most at home with, that clearly and concisely describes how this example code works. Warning: This is something of a trap because I used an advanced technique or two when implementing the Timer Zero interrupt service routine. When you understand the HOW and more importantly the WHY of the choices I made for this implementation you will have learned a lot of important things about the PIC12F675 Timer Zero hardware.
     
    • Like Like x 1
  2. manoj soorya

    manoj soorya Member

    Joined:
    Aug 23, 2014
    Messages:
    292
    Likes:
    4
    Location:
    India
    Hi Dan,


    But I dont understand some syntax.

    How to and where to use the correct syntax used in a statement is inconsistent with other similar statements... Its much difficult to me.

    But I will try to learn........I will keep It in my mind what you like to teach me.

    Thank you so much Dan.
     
  3. Dan Soze

    Dan Soze Member

    Joined:
    Dec 21, 2017
    Messages:
    38
    Likes:
    18
    In matters of style the notion of good or bad is based on experience. Often this experience is that of the person judging the style, in other cases it can be the collected experiences of many people.

    At present you may not have enough experience to have formed a useful sense of what style could mean in this context.

    This is my code with comments of where I went wrong in the style of the code I created:
    Code (PIC12F675 assembly):

        list n=0, c=150, r=dec
    ; File: main.asm
    ; Target: PIC12F675
    ; Created: 2018-02-23
    ; Author: Dan1138
    ;*********************************************************************
    ;                          ___ ___
    ;                   VDD ->|1  U  8|<- VSS
    ;                  LED1 <>|2     7|<> LED3
    ;                  LED2 <>|3     6|<> LED4
    ;                  SW1n ->|4     5|<> LED5
    ;                          ¯¯¯¯¯¯¯
    ;                         PIC12F675
    ;
    ;*********************************************************************
    ;
    ;  If the switch is Press and hold (My timer output is enable for 1 Minute minimum) either Press, all LED will be ON.
    ;  After 1 Second, LED 1 Turn Off,
    ;  After 2 Second, LED 2 Turn Off
    ;  After 4 Second, LED 3 Turn Off
    ;  After 6 Second, LED 4 Turn Off
    ;  After 8 Second, LED 5 Turn Off.
    ;
    ;  (5 LED is used for Manual Timer selection for the Motor will Enable..)
    ;
    ;*********************************************************************
    ; See: https://www.electro-tech-online.com/threads/fish-feeder-idea-help-regards.153045/
    ;*********************************************************************
        #include <p12f675.inc>
        ERRORLEVEL -302         ; Suppress warning: Register in operand not in bank 0.
     __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON & _CP_OFF & _CPD_OFF

    #define TIMER0_INTERRUPTS_PER_SECOND D'4000'
    ;
    ; BUG 1: LED defines not used should be removed
    ;
    ;#define LED1 GPIO,5
    ;#define LED2 GPIO,4
    ;#define LED3 GPIO,0
    ;#define LED4 GPIO,2
    ;#define LED5 GPIO,1

    #define SW1n GPIO,3

    ISR_DATA    UDATA_SHR  H'20' ; 0x20 ; BUG 2: Inconsistent syntax use H'20'
    WREG_save       res 1
    STATUS_save     res 1

    TIMER_DATA  UDATA_SHR
    IntsInOneSecond res 2
    Seconds         res 1

    RESET_CODE  CODE H'000' ; 0x000 ; BUG 3: Inconsistent syntax use H'000'
        goto    Start


    ISR_CODE    CODE H'004' ; 0x004 ; BUG 4: Inconsistent syntax use H'004'
        movwf   WREG_save
        movf    STATUS,W
        movwf   STATUS_save

        btfsc   INTCON,TMR0IE
        btfss   INTCON,TMR0IF
        goto    ISR_Exit
        bcf     INTCON,TMR0IF
        movlw   D'256'-D'250'+D'3' ;256-250+3  ; BUG 5: Inconsistent syntax use D'256'-D'250'+D'3'
        addwf   TMR0,F
        movlw   -1
        addwf   IntsInOneSecond,F
        skpc
        addwf   IntsInOneSecond+1,F
        skpnc
        goto    ISR_Exit
        movlw   HIGH(TIMER0_INTERRUPTS_PER_SECOND-1)
        movwf   IntsInOneSecond+1
        movlw   LOW(TIMER0_INTERRUPTS_PER_SECOND-1)
        movwf   IntsInOneSecond
        incf    Seconds,F

    ISR_Exit:
        movf    STATUS_save,W
        movwf   STATUS
        swapf   WREG_save,F
        swapf   WREG_save,W
        retfie
    START_CODE  CODE
    Start:
        btfsc   STATUS,RP0
        goto    OscillatorCalibrationFailed
        banksel OSCCAL
        call    GetOSCCAL
        movwf   OSCCAL
    OscillatorCalibrationFailed:
        banksel ANSEL
        clrf    ANSEL
        clrf    TRISIO
        movlw   B'11011111'
        movwf   OPTION_REG
        banksel INTCON
        clrf    INTCON
        clrf    GPIO
        movlw   H'07' ; 0x07 ; BUG 6: Inconsistent syntax use H'07'
        movwf   CMCON
        movlw   HIGH(TIMER0_INTERRUPTS_PER_SECOND-1)
        movwf   IntsInOneSecond+1
        movlw   LOW(TIMER0_INTERRUPTS_PER_SECOND-1)
        movwf   IntsInOneSecond
        clrf    Seconds
        clrf    TMR0
        movlw   D'256'-D'250'+D'3' ; 256-250+3  ; BUG 7: Inconsistent syntax use D'256'-D'250'+D'3'
        addwf   TMR0,F
        bsf     INTCON,TMR0IE
        bsf     INTCON,GIE
    ;
    ;
    ;
    AppStart:
        clrf    GPIO
    AppLoop:
        btfsc   SW1n
        goto    AppLoop
        bcf     INTCON,TMR0IE
        movlw   HIGH(TIMER0_INTERRUPTS_PER_SECOND-1)
        movwf   IntsInOneSecond+1
        movlw   LOW(TIMER0_INTERRUPTS_PER_SECOND-1)
        movwf   IntsInOneSecond
        clrf    Seconds
        movlw   B'00110111' ; LED1-5 on
        movwf   GPIO
        bsf     INTCON,TMR0IE
    AppTimeout:
        movf    Seconds,W
        xorlw   D'1'
        skpnz
        goto    LED1_off
        xorlw   D'1'^D'2'
        skpnz
        goto    LED2_off
        xorlw   D'2'^D'4'
        skpnz
        goto    LED3_off
        xorlw   D'4'^D'6'
        skpnz
        goto    LED4_off
        xorlw   D'6'^D'8'
        skpnz
        goto    LED5_off
        xorlw   D'8'
        sublw   D'8'
        skpc
        goto    WaitForButtonUp
    CheckSwitch:
        btfsc   SW1n
        goto    AppStart
        goto    AppTimeout
    ;
    ;
    ;
    WaitForButtonUp:
        btfsc   SW1n
        goto    AppStart
        goto    WaitForButtonUp
    ;
    ;
    ;
    LED1_off:
        movlw   B'00010111' ; LED1   off
        movwf   GPIO
        goto    CheckSwitch
    LED2_off:
        movlw   B'00000111' ; LED1-2 off
        movwf   GPIO
        goto    CheckSwitch
    LED3_off:
        movlw   B'00000110' ; LED1-3 off
        movwf   GPIO
        goto    CheckSwitch
    LED4_off:
        movlw   B'00000010' ; LED1-4 off
        movwf   GPIO
        goto    CheckSwitch
    LED5_off:
        movlw   B'00000000' ; LED1-5 off
        movwf   GPIO
        goto    CheckSwitch

    OSCCAL_CODE CODE H'3FF' ; 0x3ff ; BUG 8: Inconsistent syntax use H'3FF'
    GetOSCCAL:
        end
     
     
    • Like Like x 1
  4. dave miyares

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    2
    Likes:
    -10
  5. manoj soorya

    manoj soorya Member

    Joined:
    Aug 23, 2014
    Messages:
    292
    Likes:
    4
    Location:
    India
    Hi Dan..

    I mean I dont know what is movwf, movlw, skpc, goto etc..... Its difficult to me... No Idea about these type of syntax and its use.

    I am just an Electronics Hobbyest. Not no anything about programming
     
  6. Dan Soze

    Dan Soze Member

    Joined:
    Dec 21, 2017
    Messages:
    38
    Likes:
    18
    Everyone starts out just like you. You also have the advantage of learning to communicate using written English. That is so much harder than PIC assembly language. Today an electronics hobbyist will eventually get involved with writing code. It is impossible to avoid now because micro-controllers have replaced most of the special purpose circuits we used in the old days.
     
    • Like Like x 1
  7. manoj soorya

    manoj soorya Member

    Joined:
    Aug 23, 2014
    Messages:
    292
    Likes:
    4
    Location:
    India
    Hello Dan...

    In the above programme, How to change the OFF time to 5,10,15,20 and 25? It just for a curiosity....

    I try Much time, But no Idea. Think This is the part I need to Change... Is it?

    Code (text):


    movf    Seconds,W
        xorlw   D'1'
        skpnz
        goto    LED1_off
        xorlw   D'1'^D'2'
        skpnz
        goto    LED2_off
        xorlw   D'2'^D'4'
        skpnz
        goto    LED3_off
        xorlw   D'4'^D'6'
        skpnz
        goto    LED4_off
        xorlw   D'6'^D'8'
        skpnz
        goto    LED5_off
        xorlw   D'8'
        sublw   D'8'
        skpc
        goto    WaitForButtonUp

    [code end]



    Regards Manoj
     
  8. dave miyares

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    2
    Likes:
    -10
  9. Dan Soze

    Dan Soze Member

    Joined:
    Dec 21, 2017
    Messages:
    38
    Likes:
    18
    Just curious about how much time you spent when you "try Much time".

    Two days ago you posted that you "I mean I dont know what is movwf, movlw, skpc, goto etc.....", for me it took a lot more that two days to understand what statements in assembly language do and how to use them.

    I think that you may need to be more familiar with assembly language programming before you will understand the answer to your question.

    My best advise is to try it and see what happens.
     
    • Like Like x 1
  10. manoj soorya

    manoj soorya Member

    Joined:
    Aug 23, 2014
    Messages:
    292
    Likes:
    4
    Location:
    India
    Hi Dan..
    Really I tried and more time spend with MPLab.
    Try mean edit the previous code that you already given.

    Thank You..
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice