Continue to Site

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.

  • 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.

Problems with Macro and functions?

Status
Not open for further replies.
Hey there,

I've finally developed some tone-generating algorithm to have the loudspeaker squeeze out some music notes with a frequency and time period.

I got that time-period correct, but I don't know why I couldn't transfer a constant to the function.

Here's the code:
Code:
#include <p18F1320.inc>
    
    CONFIG WDT=OFF; disable watchdog timer
    CONFIG MCLRE = OFF; MCLEAR Pin off
    CONFIG DEBUG = OFF; Enable Debug Mode
    CONFIG LVP = OFF; Low-Voltage programming disabled (necessary for debugging)
    CONFIG OSC = INTIO2;Internal oscillator, port function on RA6 
    CONFIG PWRT = ON
    
    org 0; start code at 0

cblock 0x20        
Delay1 
Delay2
Delay1T
count1
count2
freq
duration
endc    

[B][COLOR=Red]SND    macro    freq,duration

    movlw    freq
    movwf    Delay1
    
    movlw    duration
    movwf    count1
    
    call    Sound
    endm[/COLOR][/B] 
    
Start:
    movlw    b'01100000'
    movwf    OSCCON
    
    clrf    T0CON
    movlw    b'10000111'
    movwf    T0CON
    
    clrf    TRISA
    clrf    TRISB
    clrf    LATA
    clrf    LATB
    
    clrf    TMR0H
    clrf    TMR0L
    
;    movlw    .83
;    movwf    count1
    
main:
    [B][COLOR=Red]SND        .170, .83
    SND        .210, .83
    SND        .220, .83
    SND        .230, .83
    SND        .247, .83
    SND        .304, .83[/COLOR][/B]
    goto    $
        
Sound:
    bcf        INTCON, TMR0IF
    movlw    0xFF
    movwf    TMR0H
    movlw    0xF7
    movwf    TMR0L
sound1:
    bsf         LATA, 1
    call     Delay
    bcf         LATA, 1
    call     Delay
    btfss    INTCON,TMR0IF
    goto    sound1
    decfsz    count1,1
    goto    Sound
    return
    
[B][COLOR=Red]Delay:
    movwf    Delay1
DelayLoop:
    DECFSZ  Delay1,f
    GOTO     DelayLoop
    return[/COLOR][/B]

DelayT:
    movff    Delay1T,TMR0L
_DelayT:
    clrf    T0CON
    movlw    b'10000111'
    movwf    T0CON
    
    bcf        INTCON,TMR0IF
    DelayL:
    btfss    INTCON,TMR0IF
    goto    DelayL
    return

end
The problem is, I cannot put the value of the frequency into the Delay function (it's for the squarewave). I bold the instructions and the subroutines in red for clarity. I tried it many times, and it ended up without a sound at all. I believe that in C, there's something like "void function(int x, int y)" isn't it? :)

Almost got it working by a small inch, anything I've missed out there? :)
 
Last edited:
Hi,

Have a look at your Build Output Report - it does show an error - clue what is decimal for FF - what values have you used ?

Also, will this work " goto $ " ?
Plus using a " _ " as the first character not good practice as in " _DelayT: "
 
Hi,

Have a look at your Build Output Report - it does show an error - clue what is decimal for FF - what values have you used ?

Also, will this work " goto $ " ?
Plus using a " _ " as the first character not good practice as in " _DelayT: "

Well it did compile. But it didn't come out a sound, and I sent the values from the macro to the Delay1.

Ignore the _DelayT and the DelayT - it was a code fragment. :)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top