My Attempt at a Square Wave keyboard

Discussion in 'Embedded Systems and Microcontrollers' started by MusicTech, Sep 11, 2008.

  1. MusicTech

    Thread Starter Active Member

    Apr 4, 2008
    144
    0
    I discovered an interest in MCUing a couple weeks ago, and with the help of others on this site and hours of google searches, I think I know some basic stuff now. I wanted to see if I could make a square wave keyboard. Here's the code, if people could help me with my code to make it, well good, that would be much appreciated, please note, anything in asterisks are troubled areas and I realize time hasn't been set to values as I do not know the method by which I will delay:

    ;Author Jake Wood
    ;Project Square Wave Keyboard
    ;Date 9 11 08

    device PIC16F882
    *What other info do I need up here, and what is the syntax to set the Hz of the built in 31kHz-8Mhz oscillator?*

    origin 0

    clrf PORTA *I would be surprised if this syntax were right
    clrf PORTB
    clrf PORTC
    bsf STATUS, RP0; to bank 1
    movlw b'11111111' ;sets 255 to W
    movfw TRISA ;all RA set to input
    movfw TRISB ;all RB set to input
    movlw b'11111110' ;sets 255 to W
    movfw TRISC ;RC0-6 set to input RC7 to out
    bcf STATUS, RP0 ;back to bank 0

    int time

    buttons:
    movlw 0
    movfw RC7 ;turn RC7 off
    While(1){
    If(RA0==1){ ;waits for button to be pressed, goto appropriate tone
    time=
    goto tone}
    If(RA1==1){
    time=
    goto tone}
    If(RA2==1){
    time=
    goto tone}
    If(RA3==1){
    time=
    goto tone}
    If(RA4==1){
    time=
    goto tone}
    If(RA5==1){
    time=
    goto tone}
    If(RA6==1){
    time=
    goto tone}
    If(RA7==1){
    time=
    goto tone}
    If(RB0==1){
    time=
    goto tone}
    If(RB1==1){
    time=
    goto tone}
    If(RB2==1){
    time=
    goto tone}
    If(RB3==1){
    time=
    goto tone}
    If(RB4==1){
    time=
    goto tone}
    If(RB5==1){
    time=
    goto tone}
    If(RB6==1){
    time=
    goto tone}
    If(RB7==1){
    time=
    goto tone}
    If(RC0==1){
    time=
    goto tone}
    If(RC1==1){
    time=
    goto tone}
    If(RC2==1){
    time=
    goto tone}
    If(RC3==1){
    time=
    goto tone}
    If(RC4==1){
    time=
    goto tone}
    If(RC5==1){
    time=
    goto tone}
    If(RC6==1){
    time=
    goto tone}
    }
    tone:
    while(*The Bit that got us here*== 1)
    movlw 1
    movfw RC7
    *delay for time*
    movlw 0
    movfw RC7
    *delay for time*
    }

    end

    http://www.mastincrosbie.com/mark/electronics/pic/delay.html Would this work for the delays? also, where would I put that lib file so I can use those commands?

    ;THANKS A LOT GUYS!!!!!!!!!!!!
     
    Last edited: Sep 15, 2008
  2. n9352527

    AAC Fanatic!

    Oct 14, 2005
    1,198
    4
    There are a few mistakes, like movfw instructions, which really should be movwf instead. Also, to set a port pin as input/output you need to write to the corresponding TRISx register, not PORTx.

    To set the frequency, have a look at OSCCON register in the datasheet. Also make sure the config bit is set to INT_OSC (either with or without clock out as required).

    My suggestion at this point is to go through the tutorials and examples that come with MPASM and familiarise yourself with the instructions and program structure first. Then you could write a few simple codes and run them with MPSIM and see what's going on.
     
  3. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    What language are you using to write your program?

    You seem start off in assembler then jump into something which might be BASIC (or might not) then end up with a BASIC?? "while" loop but running what look to be assembler commands.
     
  4. MusicTech

    Thread Starter Active Member

    Apr 4, 2008
    144
    0
    http://ww1.microchip.com/downloads/en/DeviceDoc/41291C.pdf Data Sheet (Pg16 has pins)

    What is the difference between movfw movwf. I am not using an external clock (CLKIN)

    So it says OSC1 is the crystal, what do you mean about the config bit? Is it just OSCCON or is it OSC1 .032 I know oscillations are measured in MHz. So what would the variable be that stores or gets number of oscillations? is it T1OSO?

    I am using C.

    MPASM?

    Thanks
     
  5. n9352527

    AAC Fanatic!

    Oct 14, 2005
    1,198
    4
    movfw copies the value in register F to W. The usual usage is movf F, d, instead. Where d = 0 will copy the value in register F to W, while d = 1 will copy the value in register F to register F itself (changing the flags in the process).

    movwf copies the value in W to register F.

    To use the internal oscillator, you have to configure the PIC oscillator to use the internal oscillator. This is done through the config bits. Look for INT_OSC_NOCLKOUT or similar words in the datasheet.

    The frequency is controlled through OSCCON, look at the corresponding page in the datasheet, it will tell you what value to write for your desired frequency.

    MPASM is microchip assembler and MPSIM is Microchip simulator, free from Microchip.

    You are not using C, movfw, movwf, movlw, bsf, bcf, clrf, org are typical PIC assembler mnemonics.

    I strongly suggest you to go through the tutorials, MPASM and MPSIM first.
     
  6. MusicTech

    Thread Starter Active Member

    Apr 4, 2008
    144
    0
    Ok, I will ruminate over that mov jazz until I get it straight, thanks for the advice
     
Loading...