EmbeddedRelated.com
Forums

Microchip Music example

Started by Stephen D. Barnes July 14, 2003
Hi all,
I have a couple of questions concerning Microchip's code for "adding music
to your
next project". I attached the code but you can see their article with a
functional description at:
http://www.microchip.com/1000/edit/proceed/archive/9_179/index.htm

1) Their code includes the instruction 'addwf PCL,F'. What does the 'F'
designate as I cannot find this in any of the instruction descriptions
for the 16F84 or 84A? Is that 'F' supposed to be the destination bit for
the result of the add and if so, where do we want it to go? (Does the
compiler look at the 'F' and interpret that as putting the result in the
specified file register?)

2) When they say the code is interrupt driven, does that mean that I have to
provide an input to generate an interrupt to begin the play of music?

When I compile the code and program the chip and connect RA0 as they have
described, nothing happens. I simulated the code in MPLAB and found that
after the Main routine is called from org 0x000, Initialize is called where
a movlw 0x00 is performed before calling Notes where the addwf PCL,F occurrs
with the result being PCL set to 0x00 (which I thought was strange since
adding 0 to any number should result in the original number and not 0). This
takes the program back to the beginning and it starts this all over again in
a loop.

Any comments or corrections would be appreciated as I am relatively new to
PIC programming (about two months experience) and I am still trying to learn
all I can.

Thanks for your time,
Stephen D. Barnes


Attachment (not stored)
sound.asm
Type: application/octet-stream


1) Yes, the 'F' is the destination of the result - either W or F
depending on the requirement

2) The code is interrupt driven in that TMR0 is being used to
generate the timing by causing an interrupt. Remember this code is
set for 4 MHz.

You are correct, the code at Check4 does not work. They have set
PCLATH to 0x00 when, in fact, the table is at 0x100. They have taken
the easy way out on Table Lookup by limiting the tables to 255
elements and starting on a 256 byte boundary (don't know if you were
following along on the lengthy discussion re: Table Lookup we had
last week). In any event, when PCLATH is being loaded before the
call to Notes (and Durations) it should be set to HIGH(Notes) or HIGH
(Durations) before the call. It isn't even set for Durations so that
lookup will NEVER work.

Worse yet, they don't save PCLATH when the interrupt code is
entered. In fact, they don't save FSR either. The thing is,
interrupt code MUST leave the machine in the same condition on exit
that it had on entrance. In this case it is not a serious problem
because the Main code does nothing so it doesn't care what the
interrupt routine does.

Also, if PCLATH is set correctly during the interrupt routine and
then restored properly, the goto $ in Main might actually work.

If I get a chance I will try to get the thing to work - probably on a
20 MHz 16F877 and I'll post the results.
--- In , "Stephen D. Barnes"
<stephendbarnes@h...> wrote:
> Hi all,
> I have a couple of questions concerning Microchip's code
for "adding music
> to your
> next project". I attached the code but you can see their article
with a
> functional description at:
> http://www.microchip.com/1000/edit/proceed/archive/9_179/index.htm
>
> 1) Their code includes the instruction 'addwf PCL,F'. What does
the 'F'
> designate as I cannot find this in any of the instruction
descriptions
> for the 16F84 or 84A? Is that 'F' supposed to be the destination
bit for
> the result of the add and if so, where do we want it to go? (Does
the
> compiler look at the 'F' and interpret that as putting the result
in the
> specified file register?)
>
> 2) When they say the code is interrupt driven, does that mean that
I have to
> provide an input to generate an interrupt to begin the play of
music?
>
> When I compile the code and program the chip and connect RA0 as
they have
> described, nothing happens. I simulated the code in MPLAB and found
that
> after the Main routine is called from org 0x000, Initialize is
called where
> a movlw 0x00 is performed before calling Notes where the addwf
PCL,F occurrs
> with the result being PCL set to 0x00 (which I thought was strange
since
> adding 0 to any number should result in the original number and not
0). This
> takes the program back to the beginning and it starts this all over
again in
> a loop.
>
> Any comments or corrections would be appreciated as I am relatively
new to
> PIC programming (about two months experience) and I am still trying
to learn
> all I can.
>
> Thanks for your time,
> Stephen D. Barnes




Thanks for the reply!
I'm not up to speed on setting PCL and register save and restore for interrupt routines yet. I could learn from reading some good, well commented code (and this example would also be useful to me). If you get around to it I would appreciate seeing the results you achieve.
 
Regards,
Stephen D. Barnes
----- Original Message -----
From: rtstofer
To: p...@yahoogroups.com
Sent: Monday, July 14, 2003 8:28 AM
Subject: [piclist] Re: Microchip Music example

1) Yes, the 'F' is the destination of the result - either W or F
depending on the requirement

2) The code is interrupt driven in that TMR0 is being used to
generate the timing by causing an interrupt.  Remember this code is
set for 4 MHz.

You are correct, the code at Check4 does not work.  They have set
PCLATH to 0x00 when, in fact, the table is at 0x100.  They have taken
the easy way out on Table Lookup by limiting the tables to 255
elements and starting on a 256 byte boundary (don't know if you were
following along on the lengthy discussion re: Table Lookup we had
last week).  In any event, when PCLATH is being loaded before the
call to Notes (and Durations) it should be set to HIGH(Notes) or HIGH
(Durations) before the call.  It isn't even set for Durations so that
lookup will NEVER work.

Worse yet, they don't save PCLATH when the interrupt code is
entered.  In fact, they don't save FSR either.  The thing is,
interrupt code MUST leave the machine in the same condition on exit
that it had on entrance.  In this case it is not a serious problem
because the Main code does nothing so it doesn't care what the
interrupt routine does.

Also, if PCLATH is set correctly during the interrupt routine and
then restored properly, the goto $ in Main might actually work.

If I get a chance I will try to get the thing to work - probably on a
20 MHz 16F877 and I'll post the results.
--- In p...@yahoogroups.com, "Stephen D. Barnes"
<stephendbarnes@h...> wrote:
> Hi all,
> I have a couple of questions concerning Microchip's code
for "adding music
> to your
> next project". I attached the code but you can see their article
with a
> functional description at:
> http://www.microchip.com/1000/edit/proceed/archive/9_179/index.htm
>
> 1) Their code includes the instruction 'addwf PCL,F'. What does
the 'F'
> designate as I cannot find this in any of the instruction
descriptions
> for the 16F84 or 84A? Is that 'F' supposed to be the destination
bit for
> the result of the add and if so, where do we want it to go? (Does
the
> compiler look at the 'F' and interpret that as putting the result
in the
> specified file register?)
>
> 2) When they say the code is interrupt driven, does that mean that
I have to
> provide an input to generate an interrupt to begin the play of
music?
>
> When I compile the code and program the chip and connect RA0 as
they have
> described, nothing happens. I simulated the code in MPLAB and found
that
> after the Main routine is called from org 0x000, Initialize is
called where
> a movlw 0x00 is performed before calling Notes where the addwf
PCL,F occurrs
> with the result being PCL set to 0x00 (which I thought was strange
since
> adding 0 to any number should result in the original number and not
0). This
> takes the program back to the beginning and it starts this all over
again in
> a loop.
>
> Any comments or corrections would be appreciated as I am relatively
new to
> PIC programming (about two months experience) and I am still trying
to learn
> all I can.
>
> Thanks for your time,
> Stephen D. Barnes



to unsubscribe, go to http://www.yahoogroups.com and follow the instructions

">Yahoo! Terms of Service.




I'll post it because I have some interest in the result - perhaps a
little song from one of my robot toys.

I have started working over the code but I have to deal with the 4
MHz vs 20 MHz issue. I'm starting to think about that now - I really
want it to be a configuration thing, not some complex process to
select between the two.

Or I have to put a 4 MHz crystal in one of my prototype boards - that
would be acceptable for proof-of-concept.

More to follow...

--- In , "Stephen D. Barnes"
<stephendbarnes@h...> wrote:
> Thanks for the reply!
> I'm not up to speed on setting PCL and register save and restore
for interrupt routines yet. I could learn from reading some good,
well commented code (and this example would also be useful to me). If
you get around to it I would appreciate seeing the results you
achieve.
>
> Regards,
> Stephen D. Barnes
> ----- Original Message -----
> From: rtstofer
> To:
> Sent: Monday, July 14, 2003 8:28 AM
> Subject: [piclist] Re: Microchip Music example > 1) Yes, the 'F' is the destination of the result - either W or F
> depending on the requirement
>
> 2) The code is interrupt driven in that TMR0 is being used to
> generate the timing by causing an interrupt. Remember this code
is
> set for 4 MHz.
>
> You are correct, the code at Check4 does not work. They have set
> PCLATH to 0x00 when, in fact, the table is at 0x100. They have
taken
> the easy way out on Table Lookup by limiting the tables to 255
> elements and starting on a 256 byte boundary (don't know if you
were
> following along on the lengthy discussion re: Table Lookup we had
> last week). In any event, when PCLATH is being loaded before the
> call to Notes (and Durations) it should be set to HIGH(Notes) or
HIGH
> (Durations) before the call. It isn't even set for Durations so
that
> lookup will NEVER work.
>
> Worse yet, they don't save PCLATH when the interrupt code is
> entered. In fact, they don't save FSR either. The thing is,
> interrupt code MUST leave the machine in the same condition on
exit
> that it had on entrance. In this case it is not a serious
problem
> because the Main code does nothing so it doesn't care what the
> interrupt routine does.
>
> Also, if PCLATH is set correctly during the interrupt routine and
> then restored properly, the goto $ in Main might actually work.
>
> If I get a chance I will try to get the thing to work - probably
on a
> 20 MHz 16F877 and I'll post the results. >
> --- In , "Stephen D. Barnes"
> <stephendbarnes@h...> wrote:
> > Hi all,
> > I have a couple of questions concerning Microchip's code
> for "adding music
> > to your
> > next project". I attached the code but you can see their
article
> with a
> > functional description at:
> >
http://www.microchip.com/1000/edit/proceed/archive/9_179/index.htm
> >
> > 1) Their code includes the instruction 'addwf PCL,F'. What does
> the 'F'
> > designate as I cannot find this in any of the instruction
> descriptions
> > for the 16F84 or 84A? Is that 'F' supposed to be the
destination
> bit for
> > the result of the add and if so, where do we want it to go?
(Does
> the
> > compiler look at the 'F' and interpret that as putting the
result
> in the
> > specified file register?)
> >
> > 2) When they say the code is interrupt driven, does that mean
that
> I have to
> > provide an input to generate an interrupt to begin the play of
> music?
> >
> > When I compile the code and program the chip and connect RA0 as
> they have
> > described, nothing happens. I simulated the code in MPLAB and
found
> that
> > after the Main routine is called from org 0x000, Initialize is
> called where
> > a movlw 0x00 is performed before calling Notes where the addwf
> PCL,F occurrs
> > with the result being PCL set to 0x00 (which I thought was
strange
> since
> > adding 0 to any number should result in the original number and
not
> 0). This
> > takes the program back to the beginning and it starts this all
over
> again in
> > a loop.
> >
> > Any comments or corrections would be appreciated as I am
relatively
> new to
> > PIC programming (about two months experience) and I am still
trying
> to learn
> > all I can.
> >
> > Thanks for your time,
> > Stephen D. Barnes >
>
> to unsubscribe, go to http://www.yahoogroups.com and follow the
instructions
>
> Your use of Yahoo! Groups is subject to the Yahoo! Terms of
Service.


Fantastic! I'm working with 16F84A's at 10MHz and 16F628's at 4 and 10MHz so the 4MHz version would be great (changing prescale values isn't that big a deal though to get the right pitch). This code looked promising to me because it appeared to be more elegant than my kludged together tone generation routines!
 
Regards,
Stephen D. Barnes
----- Original Message -----
From: rtstofer
To: p...@yahoogroups.com
Sent: Monday, July 14, 2003 3:41 PM
Subject: [piclist] Re: Microchip Music example


I'll post it because I have some interest in the result - perhaps a
little song from one of my robot toys.

I have started working over the code but I have to deal with the 4
MHz vs 20 MHz issue.  I'm starting to think about that now - I really
want it to be a configuration thing, not some complex process to
select between the two.

Or I have to put a 4 MHz crystal in one of my prototype boards - that
would be acceptable for proof-of-concept.

More to follow...

--- In p...@yahoogroups.com, "Stephen D. Barnes"
<stephendbarnes@h...> wrote:
> Thanks for the reply!
> I'm not up to speed on setting PCL and register save and restore
for interrupt routines yet. I could learn from reading some good,
well commented code (and this example would also be useful to me). If
you get around to it I would appreciate seeing the results you
achieve.
>
> Regards,
> Stephen D. Barnes
>   ----- Original Message -----
>   From: rtstofer
>   To: p...@yahoogroups.com
>   Sent: Monday, July 14, 2003 8:28 AM
>   Subject: [piclist] Re: Microchip Music example>   1) Yes, the 'F' is the destination of the result - either W or F
>   depending on the requirement
>
>   2) The code is interrupt driven in that TMR0 is being used to
>   generate the timing by causing an interrupt.  Remember this code
is
>   set for 4 MHz.
>
>   You are correct, the code at Check4 does not work.  They have set
>   PCLATH to 0x00 when, in fact, the table is at 0x100.  They have
taken
>   the easy way out on Table Lookup by limiting the tables to 255
>   elements and starting on a 256 byte boundary (don't know if you
were
>   following along on the lengthy discussion re: Table Lookup we had
>   last week).  In any event, when PCLATH is being loaded before the
>   call to Notes (and Durations) it should be set to HIGH(Notes) or
HIGH
>   (Durations) before the call.  It isn't even set for Durations so
that
>   lookup will NEVER work.
>
>   Worse yet, they don't save PCLATH when the interrupt code is
>   entered.  In fact, they don't save FSR either.  The thing is,
>   interrupt code MUST leave the machine in the same condition on
exit
>   that it had on entrance.  In this case it is not a serious
problem
>   because the Main code does nothing so it doesn't care what the
>   interrupt routine does.
>
>   Also, if PCLATH is set correctly during the interrupt routine and
>   then restored properly, the goto $ in Main might actually work.
>
>   If I get a chance I will try to get the thing to work - probably
on a
>   20 MHz 16F877 and I'll post the results.>
>   --- In p...@yahoogroups.com, "Stephen D. Barnes"
>   <stephendbarnes@h...> wrote:
>   > Hi all,
>   > I have a couple of questions concerning Microchip's code
>   for "adding music
>   > to your
>   > next project". I attached the code but you can see their
article
>   with a
>   > functional description at:
>   >
http://www.microchip.com/1000/edit/proceed/archive/9_179/index.htm
>   >
>   > 1) Their code includes the instruction 'addwf PCL,F'. What does
>   the 'F'
>   > designate as I cannot find this in any of the instruction
>   descriptions
>   > for the 16F84 or 84A? Is that 'F' supposed to be the
destination
>   bit for
>   > the result of the add and if so, where do we want it to go?
(Does
>   the
>   > compiler look at the 'F' and interpret that as putting the
result
>   in the
>   > specified file register?)
>   >
>   > 2) When they say the code is interrupt driven, does that mean
that
>   I have to
>   > provide an input to generate an interrupt to begin the play of
>   music?
>   >
>   > When I compile the code and program the chip and connect RA0 as
>   they have
>   > described, nothing happens. I simulated the code in MPLAB and
found
>   that
>   > after the Main routine is called from org 0x000, Initialize is
>   called where
>   > a movlw 0x00 is performed before calling Notes where the addwf
>   PCL,F occurrs
>   > with the result being PCL set to 0x00 (which I thought was
strange
>   since
>   > adding 0 to any number should result in the original number and
not
>   0). This
>   > takes the program back to the beginning and it starts this all
over
>   again in
>   > a loop.
>   >
>   > Any comments or corrections would be appreciated as I am
relatively
>   new to
>   > PIC programming (about two months experience) and I am still
trying
>   to learn
>   > all I can.
>   >
>   > Thanks for your time,
>   > Stephen D. Barnes>        
>             
>       
>       
>
>   to unsubscribe, go to http://www.yahoogroups.com and follow the
instructions
>
>   ">Yahoo! Terms of Service.



Stephen,
 
The attached file is for the 16F877 at 4MHz.  You should be able to change the chip description in the first 3 lines to 16F628 and it should work.  I don't know about the 16F84.
 
Enjoy!
Richard


Attachment (not stored)
Music.zip
Type: application/x-zip-compressed


Thanks.
I must be overlooking something that is staring me right in the eyes! I changed the info in the file for the F628 and loaded it up......got one click only. Simulated the code....goes to Main, calls Initialize which calls Notes(which returns to Initialize) where Initialize executes the next five instructions then returns to Main then falls into Loop where it remains. I'm lost.
What am I doing wrong?
 
----- Original Message -----
From: R.Stofer
To: p...@groups.yahoo.com
Sent: Monday, July 14, 2003 9:41 PM
Subject: [piclist] Re: Microchip Music Example

Stephen,
 
The attached file is for the 16F877 at 4MHz.  You should be able to change the chip description in the first 3 lines to 16F628 and it should work.  I don't know about the 16F84.
 
Enjoy!
Richard


to unsubscribe, go to http://www.yahoogroups.com and follow the instructions

">Yahoo! Terms of Service.




All of the work is performed in the InterruptEntry code. I don't
know how you can simulate this, the code is entered when Timer 0
overflows. You can see in the code with TMR0 is being loaded with
various values. At every clock / 4 / prescaler, the count in TMR0 is
incremented. When it overflows from 255 to 0 an interrupt T0IF is
generated.

But you are having difficulty with the F628 so let me take a look - I
have a prototype board that is being used for something else but I
can knock it down pretty quick. --- In , "Stephen D. Barnes"
<stephendbarnes@h...> wrote:
> Thanks.
> I must be overlooking something that is staring me right in the
eyes! I changed the info in the file for the F628 and loaded it
up......got one click only. Simulated the code....goes to Main, calls
Initialize which calls Notes(which returns to Initialize) where
Initialize executes the next five instructions then returns to Main
then falls into Loop where it remains. I'm lost.
> What am I doing wrong?
>
> ----- Original Message -----
> From: R.Stofer
> To: piclist@g...
> Sent: Monday, July 14, 2003 9:41 PM
> Subject: [piclist] Re: Microchip Music Example > Stephen,
>
> The attached file is for the 16F877 at 4MHz. You should be able
to change the chip description in the first 3 lines to 16F628 and it
should work. I don't know about the 16F84.
>
> Enjoy!
> Richard >
> to unsubscribe, go to http://www.yahoogroups.com and follow the
instructions
>
> Your use of Yahoo! Groups is subject to the Yahoo! Terms of
Service.


Stephen,
 
Attached is code for the 16F628 using the internal 4MHz oscillator.  You can change the __CONFIG for your application.
 
I also initialized CMCON in 'Initialize' but I can not figure you why I have to toggle RA1 in the Loop code to make this thing work.  I also defined PORTA as having 8 bits with RA0 and RA1 as output.
 
I usually put toggle code in various spots like Loop and the beginning of InterruptEntry so I can check things with a scope.  That's how I discovered the code required at Loop.
 
It's late, I'll get back to it tomorrow.  In the meantime, the code works for me!
 
Richard


Attachment (not stored)
Music.zip
Type: application/x-zip-compressed


Hello, Can anybody help me?
I need a LCD and Keypad Interface code for pic16F876.
The LCD interface code i have. I dont know like i put
Keypad interface in my code.
Thank you
Marcela

_______________________________________________________________________
Yahoo! Mail
O melhor e-mail gratuito da internet: 6MB de espa, antivus, acesso POP3, filtro contra spam.
http://br.mail.yahoo.com/