Sound2

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
No! it in bank 0 I already fix that...everything works ok exept the pushbutton...

maybe I forgot to edit the comment...

I am really tired with this but I got to make this work...
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
a response from the push button, here's the changes:

Rich (BB code):
tmr0_int bcf INTCON, T0IF  ; clear flag     
 
delay1s  decfsz  tmr0_cnt1, f  ; decrement interrupts count   
            goto    delay33ms  ;               
            movlw   245   ; reload count                   
            movwf   tmr0_cnt1  ; reset tmr0_cnt1 for 501.76msecs 
 
            decfsz  tmr0_cnt2, f  ; decrement interrupts count
            goto    delay33ms  ;    
            movlw   2   ; reload count                   
            movwf   tmr0_cnt2  ; reset tmr0_cnt for 1003.52msecs  
            bsf   t1000ms  ; set '1 second' flag for main program
instead of delay33ms I had isr_end. so I was not reaching there.

but the sound does not stop! have to fix stuff again but I am getting there!
 

Markd77

Joined Sep 7, 2009
2,806
Can you put the latest code up?
Also you said recently that you stop the interrupts to count to 1 second. I haven't seen that code, but doesn't the interrupt count up to a second?
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
A part from the change mentioned in earlier post...I added this at the end of tone2 right after 'call wait':

Rich (BB code):
clrf T2CON                     ; stop/clear timer2
 movlw 1<<GIE|1<<T0IE  ;                      
 movwf INTCON             ; enable global & TMR0 interrupts
we need to stop T2CON so tone2 stop buzzing after 1 second! and GIE, T0IE need to be re-enable for 'getkey' to work on the '2nd press' of the button! And they also must be initialized at program startup!

but each press sound a bit different! its like the timing is not constant...I think it because we creating two delay in the same timer0 interrupt...

That's why I like joeyd999 method of system timer with different timing..but can nomore use that at school as no one but me understands it...only use that for personal use!
 
Last edited:

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
I think the fact of disabling and re-enabling GIE and T0IE makes the timing not constant! will see how to fix that!
 

Markd77

Joined Sep 7, 2009
2,806
You have removed the section which resets the tmr0_cnt counters, so if the button is pressed partway through a second the first beep will be short.
Apart from the odd timing is it mainly working now?
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
Yeaha part from the odd timing it working all fine...

sometimes one press = 1 tone only, sometimes = 2 tones
2 quick press press = 3 tones

lol...I need to fix that...maybe what you just mentioned will fix that!

thanks again
 

Markd77

Joined Sep 7, 2009
2,806
Maybe you also need a flag that gets set when the button is pressed and cleared when the chime has finished. You can use that flag to skip checking the buttons.
 

MMcLaren

Joined Feb 14, 2010
861
Sorry Eric... My example, based on the code you posted, was setup for an active high switch on RB7 and I assumed you were using an external pull-down.

So, I modified the example program for an "active lo" switch on RB7 using internal weak pull-up. A Piezo speaker is attached between RA0 and ground. When I burn the program onto a 16F690 and plug it into a proto board, it works great.

Regards, Mike

Rich (BB code):
;******************************************************************
;                                                                 *
;   Filename: Sim Door Bell.asm                                   *
;     Author: Mike McLaren, K8LH                                  *
;    (C)2012: Micro Application Consultants                       *
;       Date: 30-Sep-12                                           *
;                                                                 *
;    16F690 (8-MHz INTOSC) Door Bell Demo'                        *
;                                                                 *
;                                                                 *
;      MPLab: 8.84    (tabs=8)                                    *
;      MPAsm: 5.44                                                *
;                                                                 *
;******************************************************************

        #include <p16f690.inc>
        radix dec
        list st=off
        errorlevel -302,-224

;--< config fuses >------------------------------------------------

  __config  _FCMEN_OFF& _IESO_OFF& _MCLRE_OFF& _WDT_OFF& _INTOSCIO

;  _FCMEN_OFF           ; -- fail safe clock monitor enable off
;  _IESO_OFF            ; -- int/ext switch over enable off
;  _BOR_ON              ; default, brown out reset on
;  _CPD_OFF             ; default, data eeprom protection off
;  _CP_OFF              ; default, program code protection off
;  _MCLR_OFF            ; -- use MCLR pin as digital input
;  _PWRTE_OFF           ; default, power up timer off
;  _WDT_OFF             ; -- watch dog timer off
;  _INTOSCIO            ; -- internal osc, OSC1 and OSC2 I/O

;--< variables >---------------------------------------------------

        cblock  0x20
swold                   ; switch state latch
delayhi                 ; DelayCy() subsystem variable
countlo                 ;
counthi                 ;
        endc

;--< constants >---------------------------------------------------

#define piezo   0       ; bit index for piezo spkr on RA0 pin
#define button  7       ; bit index for button on RB7 pin

;==================================================================
;  K8LH DelayCy() subsystem macro generates four instructions	  =
;==================================================================
        radix   dec
clock   equ     8               ; 4, 8, 12, 16, 20 (MHz), etc.
usecs   equ     clock/4         ; cycles/microsecond multiplier
msecs   equ     clock/4*1000    ; cycles/millisecond multiplier

DelayCy macro   delay           ; 11..327690 cycle range
        movlw   high((delay-11)/5)+1
        movwf   delayhi
        movlw   low ((delay-11)/5)
        call    uDelay-((delay-11)%5)
        endm

;==================================================================
;  K8LH Tone(frequency,duration) Macro                            =
;==================================================================
        radix   dec
tone    macro   freq,duration
        local   loop
        movlw   (duration/(500000/freq))%256
        movwf   countlo         ;
        movlw   (duration/(500000/freq))/256
        movwf   counthi         ;
loop    movlw   1<<piezo        ;
        xorwf   PORTA,F         ; toggle piezo spkr pin
        DelayCy(500000/freq*usecs-11)
        movf    countlo,F       ;
        skpnz                   ;
        decf    counthi,F       ;
        decf    countlo,F       ;
        movf    countlo,W       ;
        iorwf   counthi,W       ;
        skpz                    ;
        goto    loop            ;
        endm

;******************************************************************
;  reset vector							  *
;******************************************************************
	org	0x000
v_reset
  	clrf	STATUS		; force bank 0 and IRP = 0	  |B0
	goto	init		;				  |B0

;******************************************************************
;  interrupt vector						  *
;******************************************************************
	org	0x004
v_interrupt

;******************************************************************
;  main init                                                      *
;******************************************************************

init
        bsf     STATUS,RP1      ; bank 2                          |B2
        clrf    ANSEL           ; turn off ADC for digital I/O    |B2
        clrf    ANSELH          ;                                 |B2
;
;  configure INTOSC for 8-MHz
;
        bcf     STATUS,RP1      ; bank 0                          |B0
        bsf     STATUS,RP0      ; bank 1                          |B1
        movlw   b'01110000'     ; setup INTOSC for 8-MHz          |B1
        movwf   OSCCON          ;                                 |B1
stable  btfss   OSCCON,HTS      ; osc stable? yes, skip, else     |B1
        goto    stable          ; loop (wait until stable)        |B1
;
;  configure I/O ports
;
        bcf     OPTION_REG,7    ; enable pull-ups                 |B1
        clrf    TRISA           ; port A all outputs              |B1
        movlw   1<<button       ; bit index for switch on RB7     |B1
        movwf   TRISB           ; all others outputs              |B1
        clrf    TRISC           ; port C all outputs              |B1
        bcf     STATUS,RP0      ; bank 0                          |B0
        clrf    PORTA           ; clear port A output latches     |B0
        clrf    PORTB           ; clear port B output latches     |B0
        clrf    PORTC           ; clear port C output latches     |B0
;
        bsf     STATUS,RP1      ; bank 2                          |B2
        bsf     WPUB,WPUB7      ; enable RB7 (button) pull-up     |B2
        bcf     STATUS,RP1      ; bank 0                          |B0
;
;  initialize program variables
;
        clrf    swold           ; clear switch state latch        |B0

;******************************************************************
;  main loop                                                      *
;******************************************************************

loop
        DelayCy(16*msecs)       ; 16-msec 'debounce' intervals    |B0
;
;  K8LH parallel switch state logic (using "new press" filter)
;
;   wreg  ___---___---___---___   inverted active lo sample
;  swold  ____---___---___---__   switch state latch
;   wreg  ___-__-__-__-__-__-__   changes, press or release
;   wreg  ___-_____-_____-_____   filter out 'release' bits
;
        comf    PORTB,W         ; sample active lo switches       |B0
        andlw   b'10000000'     ; on the RB7 pin only             |B0
        xorwf   swold,W         ; changes, press or release       |B0
        xorwf   swold,F         ; update switch state latch       |B0
        andwf   swold,W         ; filter out 'release' bits       |B0
        skpnz                   ; a new press? yes, skip, else    |B0
        goto    loop            ; branch (wait for input)         |B0
;
;  generate a 1-second 661-Hz tone (toggle the piezo speaker at
;  756-usec intervals 1322 times)
;
        tone    661,1000000     ; 661-Hz tone for 1000000 usecs   |B0
;
;  generate a 496-Hz tone for 1 second (toggle the piezo speaker
;  at 1008-usec intervals 992 times)
;
        tone    496,1000000     ; 496-Hz tone for 1000000 usecs   |B0
        goto    loop            ;                                 |B0

;******************************************************************
;  K8LH DelayCy() 16-bit uDelay (11..327690 cycle) subroutine     *
;******************************************************************
        nop                     ; entry for (delay-11)%5 == 4     |B0
        nop                     ; entry for (delay-11)%5 == 3     |B0
        nop                     ; entry for (delay-11)%5 == 2     |B0
        nop                     ; entry for (delay-11)%5 == 1     |B0
uDelay  addlw   -1              ; subtract 5 cycle loop time      |B0
        skpc                    ; borrow? no, skip, else          |B0
        decfsz  delayhi,F       ; done?  yes, skip, else          |B0
        goto    uDelay          ; do another loop                 |B0
        return                  ;                                 |B0

;******************************************************************
        end
 
Last edited:

t06afre

Joined May 11, 2009
5,934
I still think you should use Mplab Simulator. The time it takes to learn it will save you lots of time. If you watch these and apply it you will probably be further ahead on the project by the end of the day than if you don't.
http://www.youtube.com/watch?v=pnkUOL0mmyA
http://www.youtube.com/watch?feature=endscreen&NR=1&v=T3OQsgAqdZs
You could probably have saved half an hour just now by putting a breakpoint in the interrupt, and seeing that it didn't happen.
I know from teaching that almost every student have some kind of strange reluctance against using debugger/simulator tools in programming. I do not know why. At the lab then a program is not working as expected they just ask for help. And our young apprentice Eric is no exception at all. It is always more easy to help. If you are given some basic information. Say something like this. I have placed a breakpoint in the ISR. But it looks like this breakpoint is not reached
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
Alright! everyting went Ok with this little project...

I was surprised that I had the best sounding 'doorbell'...I think the reason being most people avoided using PMW...they used toggling pin...if I may say they created a square wave *manually* lol...:)

I think PWM makes things much easier...just have to know how to use it...;) well I also got confused with it in one of my previous project but I am good with it now...

Also some of my comments in earlier posts was not totally correct!
You don't have to disable and re-enable GIE/T0IE in INTCON for a fresh count delay...just have to *reset* the registers for a fresh count...and let timer0 count continuously!

I also tried MMcLaren code and it works brilliantly!:)

Thanks All and on to the next one!
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
Congrats' Eric. Happy to hear you did good...
thanks!:)


So if the next one is due in 14 days, you're probably going to wait for 13 days before starting it, right (grin)?

Regards, Mike...
Hahahahaa...:D:D you know me well...

But I don't really do that on purpose there are too many things (courses) to take care of at the same time (tests, assignments, exams...).

Btw, the next and last one is due in 10 days...and I must make sure that from hardware to software it works brilliantly coz this is the *exam* if I mess up on this one I am dead for real!!

BTW, I took an exam this morning...but a big probem I think I have is *time management* ...if can fix that I would do better...

I clever enough :) but always late when submitting stuffs and it affect my marks...

thanks again!
 
Top