View previous topic :: View next topic |
Author |
Message |
Matrixx
Joined: 30 Nov 2005 Posts: 158
|
Posted: Sat Oct 21, 2017 7:53 pm Post subject: ISR not holding Timer1 preload, M328P |
|
|
Hello, I'm trying to implement a 3 simultaneous tone synthesizer based on the additive synthesis using a M328P.
Although the M328 is small for this project, I started with because I can program it fast with the boot loader & the FTDI cable.
(Upon getting it work, I will migrate to Xmega128A1U as I cannot simulate the Xmega).
I use the DAC MCP4921 to generate the audio signals, and the M328 running on the 8Mhz Xtall.
The DAC is updated at fixed 15.625 Khz this mean I have 64 uS to prepare the mix, produce the sample and transfer to DAC.
I have set Timer1 to prescale = 1 and preload it with 65024 (65536 - 65024 = 512 thus 125 nS x 512 = 64 uS).
I do reload the timer inside the ISR, of course.
I do all the simple math inside the ISR. the math code takes 28 uS. The SPI write to the DAC takes 25 uS.
So the operations inside the ISR last for 53 uS. This is enough to keep the thing working ok.
The problem is:
At some point adding more sums or shift left/right inside the ISR, the CS signal takes now 8 mS on the scope. Commenting out either one of the sum
or Shift gets back to ISR period of 64 uS. I was trying to figure out what happen, 8 mS is quite far from the preload value for the
timing I set. Then it is not related to sum in particular but about the number of operations done.
What I think is happening, is the Timer1 lost the reload value because 8 mS is likely the Timer1 is counting the whole 65536
before overflow (125 nS x 65536 = 8.192 mS). Because I reload the Timer1 at the beginning of the ISR and do all the math next,
to confirm my suspects I reloaded the Timer1 just before the ISR ends, and it worked! Of course this does not account for the
time of the operations so I would need to modify the Timer1 reload value...
Looks like the doing more than certain number of operations after Timer1 is loaded, mess with the Timer1 already set value...?
So the question is, why the Timer1 lost the load value if I do more or less operations inside the ISR?
-If you say is because the operations inside the ISR are taking longer time, I set a PortD.2 pin to measure just the math timing. And
the results are the math goes up to 34 uS, so 34 + 25 = 59 uS, enough to still work inside the ISR.
-If you say something about using Pushall/PopAll, welll... I'm not using it in my ISR so I did a Try. I added PushAll/PopAll but this
does not cure the situation. Growing up the Hwstack, Swstack & FrameSize from 80 to 200 didn't help either.
-If you say I can do the math in the Do..Nothing loop, setting a flag to know when the ISR has ended; this is a solution
but by now I'm concentrated in to know why this behavior happen, and how it can be fixed, or is a sort of bug..?
BascomAvr 2.0.7.9
Code: |
'Atmega328P Run with 8Mhz Xtall.
'Drives MCP4921 DAC, updating registers at 15,625 Hz (64 uS period).
$regfile = "m328pdef.dat"
$crystal = 8000000
$hwstack = 200
$swstack = 200
$framesize = 200
$Baud = 57600
Baud = 57600
'Setup SPI Harware Port
Config Spi = Hard , Interrupt = Off , Data_Order = Msb , Master = Yes , Polarity = Low, Phase = 0
Config PortB.1 = Output 'DAC CS
mCS alias PortB.1
Config PORTD.2 = Output 'Test pin for Scope
Test alias PORTD.2
'8Mhz, Tick = 125nS.
Config Timer1 = Timer , Prescale = 1
Enable Timer1
Stop Timer1
On Ovf1 _timer1
Config Base = 0 '0 based array
Dim Wave(256) as Byte
Dim mSPDR as Word
Dim Sample, Sample1, Sample2, Sample3 as Word
Dim Pos1, Pos2, Pos3 as Word
Dim Incr1, Incr2, Incr3 as Word
Dim T1, T2, T3 as Byte
Enable Interrupts
'init the spi pins
Spiinit
Set mCS 'Tie High MCP4921 CS pin.
Waitms 10
'0 Based Array.
Wave(000) = $80 : Wave(001) = $83 : Wave(002) = $86 : Wave(003) = $89
Wave(004) = $8C : Wave(005) = $8F : Wave(006) = $92 : Wave(007) = $95
Wave(008) = $98 : Wave(009) = $9B : Wave(010) = $9E : Wave(011) = $A2
Wave(012) = $A5 : Wave(013) = $A7 : Wave(014) = $AA : Wave(015) = $AD
Wave(016) = $B0 : Wave(017) = $B3 : Wave(018) = $B6 : Wave(019) = $B9
Wave(020) = $BC : Wave(021) = $BE : Wave(022) = $C1 : Wave(023) = $C4
Wave(024) = $C6 : Wave(025) = $C9 : Wave(026) = $CB : Wave(027) = $CE
Wave(028) = $D0 : Wave(029) = $D3 : Wave(030) = $D5 : Wave(031) = $D7
Wave(032) = $DA : Wave(033) = $DC : Wave(034) = $DE : Wave(035) = $E0
Wave(036) = $E2 : Wave(037) = $E4 : Wave(038) = $E6 : Wave(039) = $E8
Wave(040) = $EA : Wave(041) = $EB : Wave(042) = $ED : Wave(043) = $EE
Wave(044) = $F0 : Wave(045) = $F1 : Wave(046) = $F3 : Wave(047) = $F4
Wave(048) = $F5 : Wave(049) = $F6 : Wave(050) = $F8 : Wave(051) = $F9
Wave(052) = $FA : Wave(053) = $FA : Wave(054) = $FB : Wave(055) = $FC
Wave(056) = $FD : Wave(057) = $FD : Wave(058) = $FE : Wave(059) = $FE
Wave(060) = $FE : Wave(061) = $FF : Wave(062) = $FF : Wave(063) = $FF 'Peak
Wave(064) = $FF : Wave(065) = $FF : Wave(066) = $FF : Wave(067) = $FF 'Peak
Wave(068) = $FE : Wave(069) = $FE : Wave(070) = $FE : Wave(071) = $FD
Wave(072) = $FD : Wave(073) = $FC : Wave(074) = $FB : Wave(075) = $FA
Wave(076) = $FA : Wave(077) = $F9 : Wave(078) = $F8 : Wave(079) = $F6
Wave(080) = $F5 : Wave(081) = $F4 : Wave(082) = $F3 : Wave(083) = $F1
Wave(084) = $F0 : Wave(085) = $EE : Wave(086) = $ED : Wave(087) = $EB
Wave(088) = $EA : Wave(089) = $E8 : Wave(090) = $E6 : Wave(091) = $E4
Wave(092) = $E2 : Wave(093) = $E0 : Wave(094) = $DE : Wave(095) = $DC
Wave(096) = $DA : Wave(097) = $D7 : Wave(098) = $D5 : Wave(099) = $D3
Wave(100) = $D0 : Wave(101) = $CE : Wave(102) = $CB : Wave(103) = $C9
Wave(104) = $C6 : Wave(105) = $C4 : Wave(106) = $C1 : Wave(107) = $BE
Wave(108) = $BC : Wave(109) = $B9 : Wave(110) = $B6 : Wave(111) = $B3
Wave(112) = $B0 : Wave(113) = $AD : Wave(114) = $AA : Wave(115) = $A7
Wave(116) = $A5 : Wave(117) = $A2 : Wave(118) = $9E : Wave(119) = $9B
Wave(120) = $98 : Wave(121) = $95 : Wave(122) = $92 : Wave(123) = $8F
Wave(124) = $8C : Wave(125) = $89 : Wave(126) = $86 : Wave(127) = $83
Wave(128) = $80 : Wave(129) = $7D : Wave(130) = $7A : Wave(131) = $77
Wave(132) = $74 : Wave(133) = $71 : Wave(134) = $6E : Wave(135) = $6B
Wave(136) = $68 : Wave(137) = $65 : Wave(138) = $62 : Wave(139) = $5E
Wave(140) = $5B : Wave(141) = $59 : Wave(142) = $56 : Wave(143) = $53
Wave(144) = $50 : Wave(145) = $4D : Wave(146) = $4A : Wave(147) = $47
Wave(148) = $44 : Wave(149) = $42 : Wave(150) = $3F : Wave(151) = $3C
Wave(152) = $3A : Wave(153) = $37 : Wave(154) = $35 : Wave(155) = $32
Wave(156) = $30 : Wave(157) = $2D : Wave(158) = $2B : Wave(159) = $29
Wave(160) = $26 : Wave(161) = $24 : Wave(162) = $22 : Wave(163) = $20
Wave(164) = $1E : Wave(165) = $1C : Wave(166) = $1A : Wave(167) = $18
Wave(168) = $16 : Wave(169) = $15 : Wave(170) = $13 : Wave(171) = $12
Wave(172) = $10 : Wave(173) = $0F : Wave(174) = $0D : Wave(175) = $0C
Wave(176) = $0B : Wave(177) = $0A : Wave(178) = $08 : Wave(179) = $07
Wave(180) = $06 : Wave(181) = $06 : Wave(182) = $05 : Wave(183) = $04
Wave(184) = $03 : Wave(185) = $03 : Wave(186) = $02 : Wave(187) = $02
Wave(188) = $02 : Wave(189) = $01 : Wave(190) = $01 : Wave(191) = $01 'Valley
Wave(192) = $01 : Wave(193) = $01 : Wave(194) = $01 : Wave(195) = $01 'Valley
Wave(196) = $02 : Wave(197) = $02 : Wave(198) = $02 : Wave(199) = $03
Wave(200) = $03 : Wave(201) = $04 : Wave(202) = $05 : Wave(203) = $06
Wave(204) = $06 : Wave(205) = $07 : Wave(206) = $08 : Wave(207) = $0A
Wave(208) = $0B : Wave(209) = $0C : Wave(210) = $0D : Wave(211) = $0F
Wave(212) = $10 : Wave(213) = $12 : Wave(214) = $13 : Wave(215) = $15
Wave(216) = $16 : Wave(217) = $18 : Wave(218) = $1A : Wave(219) = $1C
Wave(220) = $1E : Wave(221) = $20 : Wave(222) = $22 : Wave(223) = $24
Wave(224) = $26 : Wave(225) = $29 : Wave(226) = $2B : Wave(227) = $2D
Wave(228) = $30 : Wave(229) = $32 : Wave(230) = $35 : Wave(231) = $37
Wave(232) = $3A : Wave(233) = $3C : Wave(234) = $3F : Wave(235) = $42
Wave(236) = $44 : Wave(237) = $47 : Wave(238) = $4A : Wave(239) = $4D
Wave(240) = $50 : Wave(241) = $53 : Wave(242) = $56 : Wave(243) = $59
Wave(244) = $5B : Wave(245) = $5E : Wave(246) = $62 : Wave(247) = $65
Wave(248) = $68 : Wave(249) = $6B : Wave(250) = $6E : Wave(251) = $71
Wave(252) = $74 : Wave(253) = $77 : Wave(254) = $7A : Wave(255) = $7D
'Expect a musical chord.
Incr1 = 1097
Incr2 = 1387
Incr3 = 1644
Pos1 = 0
Pos2 = 0
Pos3 = 0
Timer1 = 65024
Start Timer1
DO
'Do Nothing
Loop
End '----------------------------------
'We need this timer overflow each 64 uS.
_timer1:
Timer1 = 65024 '65536-65024 = 512. At 8 Mhz, 1/800000 = 129 nS x 512 = 64 uS !
Reset test 'Mark math code begin on scope
Pos1 = Pos1 + Incr1 : T1 = High(Pos1) 'Pahse pointer 1
Pos2 = Pos2 + Incr2 : T2 = High(Pos2) 'Pahse pointer 2
Pos3 = Pos3 + Incr3 : T3 = High(Pos3) 'Pahse pointer 3
Sample1 = Wave(T1) 'Extract phase
Sample2 = Wave(T2)
Sample3 = Wave(T3)
Shift Sample2, Right, 1 'Shift Right for Volume reduce
Shift Sample3, Right, 2 'Shift Right for Volume reduce
Sample = Sample1 + Sample2 'Mix down 3 audio tones
Sample = Sample + Sample3 '<-- commenting out either of these makes it work.
Shift Sample, Left, 2 'Shift Left for Mix Volume gain
Set Test 'Mark math code end on scope
Reset mCS 'This SPI transfer lasts for 25 uS using 8Mhz Xtall, no CkDiv.
mSPDR = High(Sample) OR $70 : Spiout mSpDR , 1
mSPDR = Low(Sample) : Spiout mSPDR , 1
Set mCS 'Vout is updated at rising edge of CS pin.
'Timer1 = 65024 '<---- preloading timer here Works.
Return
|
[/code]
(BASCOM-AVR version : 2.0.7.9 , Latest : 2.0.7.8 ) |
|
Back to top |
|
|
JC
Joined: 15 Dec 2007 Posts: 586 Location: Cleveland, OH
|
Posted: Sun Oct 22, 2017 4:47 am Post subject: |
|
|
Two thoughts:
You probably know this but I'll mention it anyway.
You have very little "extra" time in your ISR.
Using an O'scope is a great way to measure the time the ISR takes to run, and to see it execute as expected.
Know, however, that the O'scope markers start well after the ISR begins execution, and ends well before the ISR is actually over.
Bascom, when executing an ISR, automatically pushes and pops a LOT of registers.
See the On Interrupt instruction within the Help for the details.
That means that if your timing is very tight, you may well miss interrupts.
Next, I love the Xmegas!
If you intend to switch to the Xmega anyway, why not do so now?
At 32 MHz you have 4 times the processing power, (clock cycles), you have with the Mega at 8 MHz.
Tight timing inside the ISR is then not a problem at all.
JC |
|
Back to top |
|
|
i.dobson
Joined: 05 Jan 2006 Posts: 1570 Location: Basel, Switzerland
|
Posted: Sun Oct 22, 2017 7:32 am Post subject: |
|
|
Hi,
My tips would be:-
1) Try running the AVR at 16MHz
2) Try pushing/poping the Registers used in the ISR (rather than everything)
3) Replace the HIGH/LOW with variable overlays. Somthing like:
Code: |
dim MyWord as word
Dim MyWord_lo as Byte at MyWord overlay
Dim MyWord_hi as Byte at MyWord +1 overlay
|
Not sure if tip 3 will bring anything but it's worth a try.
Regards
Ian Dobson _________________ Walking on water and writing software to specification is easy if they're frozen. |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Sun Oct 22, 2017 8:52 am Post subject: |
|
|
With Clear_Timer (timer autoreload) and Nosave Interrupt option you should be safe to do your tasks
See Code1 and screenshot.
...but if your main loop do nothing then even saving/restore registers is not needed because you can use Timer flag for timing and not use interrupts (code2).
Code1
Code: |
'Atmega328P Run with 8Mhz Xtall.
'Drives MCP4921 DAC, updating registers at 15,625 Hz (64 uS period).
$regfile = "m328pdef.dat"
$crystal = 8000000
$hwstack = 200
$swstack = 200
$framesize = 200
$Baud = 57600
Baud = 57600
'Setup SPI Harware Port
Config Spi = Hard , Interrupt = Off , Data_Order = Msb , Master = Yes , Polarity = Low, Phase = 0
Config PortB.1 = Output 'DAC CS
mCS alias PortB.1
Config PORTD.2 = Output 'Test pin for Scope
Test alias PORTD.2
'8Mhz, Tick = 125nS.
Config Timer1 = Timer , Prescale = 1 , Compare_a = Disconnect , Compare_b = Disconnect , Clear_timer = 1
Compare1a = 512
Enable Compare1a
On Compare1a Timer1_isr Nosave ' <- NOSAVE is important here
Config Base = 0 '0 based array
Dim Wave(256) As Byte
Dim mSPDR as Word
Dim Sample As Word
Dim Sam_hi As Byte At Sample Overlay
Dim Sam_lo As Byte At Sample + 1 Overlay
Dim Sample1 As Word , T1 As Byte At Sample1 Overlay
Dim Sample2 As Word , T2 As Byte At Sample2 Overlay
Dim Sample3 As Word , T3 As Byte At Sample3 Overlay
Dim Pos1, Pos2, Pos3 as Word
Dim Incr1, Incr2, Incr3 as Word
Enable Interrupts
'init the spi pins
Spiinit
Set mCS 'Tie High MCP4921 CS pin.
Waitms 10
'0 Based Array.
Wave(000) = $80 : Wave(001) = $83 : Wave(002) = $86 : Wave(003) = $89
Wave(004) = $8C : Wave(005) = $8F : Wave(006) = $92 : Wave(007) = $95
Wave(008) = $98 : Wave(009) = $9B : Wave(010) = $9E : Wave(011) = $A2
Wave(012) = $A5 : Wave(013) = $A7 : Wave(014) = $AA : Wave(015) = $AD
Wave(016) = $B0 : Wave(017) = $B3 : Wave(018) = $B6 : Wave(019) = $B9
Wave(020) = $BC : Wave(021) = $BE : Wave(022) = $C1 : Wave(023) = $C4
Wave(024) = $C6 : Wave(025) = $C9 : Wave(026) = $CB : Wave(027) = $CE
Wave(028) = $D0 : Wave(029) = $D3 : Wave(030) = $D5 : Wave(031) = $D7
Wave(032) = $DA : Wave(033) = $DC : Wave(034) = $DE : Wave(035) = $E0
Wave(036) = $E2 : Wave(037) = $E4 : Wave(038) = $E6 : Wave(039) = $E8
Wave(040) = $EA : Wave(041) = $EB : Wave(042) = $ED : Wave(043) = $EE
Wave(044) = $F0 : Wave(045) = $F1 : Wave(046) = $F3 : Wave(047) = $F4
Wave(048) = $F5 : Wave(049) = $F6 : Wave(050) = $F8 : Wave(051) = $F9
Wave(052) = $FA : Wave(053) = $FA : Wave(054) = $FB : Wave(055) = $FC
Wave(056) = $FD : Wave(057) = $FD : Wave(058) = $FE : Wave(059) = $FE
Wave(060) = $FE : Wave(061) = $FF : Wave(062) = $FF : Wave(063) = $FF 'Peak
Wave(064) = $FF : Wave(065) = $FF : Wave(066) = $FF : Wave(067) = $FF 'Peak
Wave(068) = $FE : Wave(069) = $FE : Wave(070) = $FE : Wave(071) = $FD
Wave(072) = $FD : Wave(073) = $FC : Wave(074) = $FB : Wave(075) = $FA
Wave(076) = $FA : Wave(077) = $F9 : Wave(078) = $F8 : Wave(079) = $F6
Wave(080) = $F5 : Wave(081) = $F4 : Wave(082) = $F3 : Wave(083) = $F1
Wave(084) = $F0 : Wave(085) = $EE : Wave(086) = $ED : Wave(087) = $EB
Wave(088) = $EA : Wave(089) = $E8 : Wave(090) = $E6 : Wave(091) = $E4
Wave(092) = $E2 : Wave(093) = $E0 : Wave(094) = $DE : Wave(095) = $DC
Wave(096) = $DA : Wave(097) = $D7 : Wave(098) = $D5 : Wave(099) = $D3
Wave(100) = $D0 : Wave(101) = $CE : Wave(102) = $CB : Wave(103) = $C9
Wave(104) = $C6 : Wave(105) = $C4 : Wave(106) = $C1 : Wave(107) = $BE
Wave(108) = $BC : Wave(109) = $B9 : Wave(110) = $B6 : Wave(111) = $B3
Wave(112) = $B0 : Wave(113) = $AD : Wave(114) = $AA : Wave(115) = $A7
Wave(116) = $A5 : Wave(117) = $A2 : Wave(118) = $9E : Wave(119) = $9B
Wave(120) = $98 : Wave(121) = $95 : Wave(122) = $92 : Wave(123) = $8F
Wave(124) = $8C : Wave(125) = $89 : Wave(126) = $86 : Wave(127) = $83
Wave(128) = $80 : Wave(129) = $7D : Wave(130) = $7A : Wave(131) = $77
Wave(132) = $74 : Wave(133) = $71 : Wave(134) = $6E : Wave(135) = $6B
Wave(136) = $68 : Wave(137) = $65 : Wave(138) = $62 : Wave(139) = $5E
Wave(140) = $5B : Wave(141) = $59 : Wave(142) = $56 : Wave(143) = $53
Wave(144) = $50 : Wave(145) = $4D : Wave(146) = $4A : Wave(147) = $47
Wave(148) = $44 : Wave(149) = $42 : Wave(150) = $3F : Wave(151) = $3C
Wave(152) = $3A : Wave(153) = $37 : Wave(154) = $35 : Wave(155) = $32
Wave(156) = $30 : Wave(157) = $2D : Wave(158) = $2B : Wave(159) = $29
Wave(160) = $26 : Wave(161) = $24 : Wave(162) = $22 : Wave(163) = $20
Wave(164) = $1E : Wave(165) = $1C : Wave(166) = $1A : Wave(167) = $18
Wave(168) = $16 : Wave(169) = $15 : Wave(170) = $13 : Wave(171) = $12
Wave(172) = $10 : Wave(173) = $0F : Wave(174) = $0D : Wave(175) = $0C
Wave(176) = $0B : Wave(177) = $0A : Wave(178) = $08 : Wave(179) = $07
Wave(180) = $06 : Wave(181) = $06 : Wave(182) = $05 : Wave(183) = $04
Wave(184) = $03 : Wave(185) = $03 : Wave(186) = $02 : Wave(187) = $02
Wave(188) = $02 : Wave(189) = $01 : Wave(190) = $01 : Wave(191) = $01 'Valley
Wave(192) = $01 : Wave(193) = $01 : Wave(194) = $01 : Wave(195) = $01 'Valley
Wave(196) = $02 : Wave(197) = $02 : Wave(198) = $02 : Wave(199) = $03
Wave(200) = $03 : Wave(201) = $04 : Wave(202) = $05 : Wave(203) = $06
Wave(204) = $06 : Wave(205) = $07 : Wave(206) = $08 : Wave(207) = $0A
Wave(208) = $0B : Wave(209) = $0C : Wave(210) = $0D : Wave(211) = $0F
Wave(212) = $10 : Wave(213) = $12 : Wave(214) = $13 : Wave(215) = $15
Wave(216) = $16 : Wave(217) = $18 : Wave(218) = $1A : Wave(219) = $1C
Wave(220) = $1E : Wave(221) = $20 : Wave(222) = $22 : Wave(223) = $24
Wave(224) = $26 : Wave(225) = $29 : Wave(226) = $2B : Wave(227) = $2D
Wave(228) = $30 : Wave(229) = $32 : Wave(230) = $35 : Wave(231) = $37
Wave(232) = $3a : Wave(233) = $3c : Wave(234) = $3f : Wave(235) = $42
Wave(236) = $44 : Wave(237) = $47 : Wave(238) = $4A : Wave(239) = $4D
Wave(240) = $50 : Wave(241) = $53 : Wave(242) = $56 : Wave(243) = $59
Wave(244) = $5B : Wave(245) = $5E : Wave(246) = $62 : Wave(247) = $65
Wave(248) = $68 : Wave(249) = $6B : Wave(250) = $6E : Wave(251) = $71
Wave(252) = $74 : Wave(253) = $77 : Wave(254) = $7A : Wave(255) = $7D
'Expect a musical chord.
Incr1 = 1097
Incr2 = 1387
Incr3 = 1644
Pos1 = 0
Pos2 = 0
Pos3 = 0
Do
!nop
!nop
Loop
End
Timer1_isr:
$asm
!PUSH R10
!PUSH R16
!PUSH R17
!PUSH R20
!PUSH R21
!PUSH R24
!PUSH R25
!PUSH R26
!PUSH R27
!in R24, sreg
!PUSH R24
$end asm
Reset test 'Mark math code begin on scope
Pos1 = Pos1 + Incr1 'Pahse pointer 1
Pos2 = Pos2 + Incr2 'Pahse pointer 2
Pos3 = Pos3 + Incr3 'Pahse pointer 3
Sample1 = Wave(T1) 'Extract phase
Sample2 = Wave(T2)
Sample3 = Wave(T3)
Shift Sample2, Right, 1 'Shift Right for Volume reduce
Shift Sample3 , Right , 2 'Shift Right for Volume reduce
Sample = Sample1 + Sample2 'Mix down 3 audio tones
Sample = Sample + Sample3 '<-- commenting out either of these makes it work.
Shift Sample, Left, 2 'Shift Left for Mix Volume gain
Set Test 'Mark math code end on scope
Reset mCS 'This SPI transfer lasts for 25 uS using 8Mhz Xtall, no CkDiv.
Mspdr = Sam_hi Or $70 : Spiout Mspdr , 1
Mspdr = Sam_lo : Spiout Mspdr , 1
Set Mcs 'Vout is updated at rising edge of CS pin.
$asm
!POP R24
!out sreg, r24
!POP R27
!POP R26
!POP R25
!POP R24
!POP R21
!POP R20
!POP R17
!POP R16
!POP R10
$end asm
Return |
Code2
Code: |
'Atmega328P Run with 8Mhz Xtall.
'Drives MCP4921 DAC, updating registers at 15,625 Hz (64 uS period).
$regfile = "m328pdef.dat"
$crystal = 8000000
$hwstack = 200
$swstack = 200
$framesize = 200
$baud = 57600
Baud = 57600
'Setup SPI Harware Port
Config Spi = Hard , Interrupt = Off , Data_order = Msb , Master = Yes , Polarity = Low , Phase = 0
Config Portb.1 = Output 'DAC CS
Mcs Alias Portb.1
Config Portd.2 = Output 'Test pin for Scope
Test Alias Portd.2
'8Mhz, Tick = 125nS.
Config Timer1 = Timer , Prescale = 1 , Compare_a = Disconnect , Compare_b = Disconnect , Clear_timer = 1
Compare1a = 512
Config Base = 0 '0 based array
Dim Wave(256) As Byte
Dim Mspdr As Word
Dim Sample As Word
Dim Sam_hi As Byte At Sample Overlay
Dim Sam_lo As Byte At Sample + 1 Overlay
Dim Sample1 As Word , T1 As Byte At Sample1 Overlay
Dim Sample2 As Word , T2 As Byte At Sample2 Overlay
Dim Sample3 As Word , T3 As Byte At Sample3 Overlay
Dim Pos1 , Pos2 , Pos3 As Word
Dim Incr1 , Incr2 , Incr3 As Word
Enable Interrupts
'init the spi pins
Spiinit
Set Mcs 'Tie High MCP4921 CS pin.
Waitms 10
'0 Based Array.
Wave(000) = $80 : Wave(001) = $83 : Wave(002) = $86 : Wave(003) = $89
Wave(004) = $8c : Wave(005) = $8f : Wave(006) = $92 : Wave(007) = $95
Wave(008) = $98 : Wave(009) = $9b : Wave(010) = $9e : Wave(011) = $a2
Wave(012) = $a5 : Wave(013) = $a7 : Wave(014) = $aa : Wave(015) = $ad
Wave(016) = $b0 : Wave(017) = $b3 : Wave(018) = $b6 : Wave(019) = $b9
Wave(020) = $bc : Wave(021) = $be : Wave(022) = $c1 : Wave(023) = $c4
Wave(024) = $c6 : Wave(025) = $c9 : Wave(026) = $cb : Wave(027) = $ce
Wave(028) = $d0 : Wave(029) = $d3 : Wave(030) = $d5 : Wave(031) = $d7
Wave(032) = $da : Wave(033) = $dc : Wave(034) = $de : Wave(035) = $e0
Wave(036) = $e2 : Wave(037) = $e4 : Wave(038) = $e6 : Wave(039) = $e8
Wave(040) = $ea : Wave(041) = $eb : Wave(042) = $ed : Wave(043) = $ee
Wave(044) = $f0 : Wave(045) = $f1 : Wave(046) = $f3 : Wave(047) = $f4
Wave(048) = $f5 : Wave(049) = $f6 : Wave(050) = $f8 : Wave(051) = $f9
Wave(052) = $fa : Wave(053) = $fa : Wave(054) = $fb : Wave(055) = $fc
Wave(056) = $fd : Wave(057) = $fd : Wave(058) = $fe : Wave(059) = $fe
Wave(060) = $fe : Wave(061) = $ff : Wave(062) = $ff : Wave(063) = $ff 'Peak
Wave(064) = $ff : Wave(065) = $ff : Wave(066) = $ff : Wave(067) = $ff 'Peak
Wave(068) = $fe : Wave(069) = $fe : Wave(070) = $fe : Wave(071) = $fd
Wave(072) = $fd : Wave(073) = $fc : Wave(074) = $fb : Wave(075) = $fa
Wave(076) = $fa : Wave(077) = $f9 : Wave(078) = $f8 : Wave(079) = $f6
Wave(080) = $f5 : Wave(081) = $f4 : Wave(082) = $f3 : Wave(083) = $f1
Wave(084) = $f0 : Wave(085) = $ee : Wave(086) = $ed : Wave(087) = $eb
Wave(088) = $ea : Wave(089) = $e8 : Wave(090) = $e6 : Wave(091) = $e4
Wave(092) = $e2 : Wave(093) = $e0 : Wave(094) = $de : Wave(095) = $dc
Wave(096) = $da : Wave(097) = $d7 : Wave(098) = $d5 : Wave(099) = $d3
Wave(100) = $d0 : Wave(101) = $ce : Wave(102) = $cb : Wave(103) = $c9
Wave(104) = $c6 : Wave(105) = $c4 : Wave(106) = $c1 : Wave(107) = $be
Wave(108) = $bc : Wave(109) = $b9 : Wave(110) = $b6 : Wave(111) = $b3
Wave(112) = $b0 : Wave(113) = $ad : Wave(114) = $aa : Wave(115) = $a7
Wave(116) = $a5 : Wave(117) = $a2 : Wave(118) = $9e : Wave(119) = $9b
Wave(120) = $98 : Wave(121) = $95 : Wave(122) = $92 : Wave(123) = $8f
Wave(124) = $8c : Wave(125) = $89 : Wave(126) = $86 : Wave(127) = $83
Wave(128) = $80 : Wave(129) = $7d : Wave(130) = $7a : Wave(131) = $77
Wave(132) = $74 : Wave(133) = $71 : Wave(134) = $6e : Wave(135) = $6b
Wave(136) = $68 : Wave(137) = $65 : Wave(138) = $62 : Wave(139) = $5e
Wave(140) = $5b : Wave(141) = $59 : Wave(142) = $56 : Wave(143) = $53
Wave(144) = $50 : Wave(145) = $4d : Wave(146) = $4a : Wave(147) = $47
Wave(148) = $44 : Wave(149) = $42 : Wave(150) = $3f : Wave(151) = $3c
Wave(152) = $3a : Wave(153) = $37 : Wave(154) = $35 : Wave(155) = $32
Wave(156) = $30 : Wave(157) = $2d : Wave(158) = $2b : Wave(159) = $29
Wave(160) = $26 : Wave(161) = $24 : Wave(162) = $22 : Wave(163) = $20
Wave(164) = $1e : Wave(165) = $1c : Wave(166) = $1a : Wave(167) = $18
Wave(168) = $16 : Wave(169) = $15 : Wave(170) = $13 : Wave(171) = $12
Wave(172) = $10 : Wave(173) = $0f : Wave(174) = $0d : Wave(175) = $0c
Wave(176) = $0b : Wave(177) = $0a : Wave(178) = $08 : Wave(179) = $07
Wave(180) = $06 : Wave(181) = $06 : Wave(182) = $05 : Wave(183) = $04
Wave(184) = $03 : Wave(185) = $03 : Wave(186) = $02 : Wave(187) = $02
Wave(188) = $02 : Wave(189) = $01 : Wave(190) = $01 : Wave(191) = $01 'Valley
Wave(192) = $01 : Wave(193) = $01 : Wave(194) = $01 : Wave(195) = $01 'Valley
Wave(196) = $02 : Wave(197) = $02 : Wave(198) = $02 : Wave(199) = $03
Wave(200) = $03 : Wave(201) = $04 : Wave(202) = $05 : Wave(203) = $06
Wave(204) = $06 : Wave(205) = $07 : Wave(206) = $08 : Wave(207) = $0a
Wave(208) = $0b : Wave(209) = $0c : Wave(210) = $0d : Wave(211) = $0f
Wave(212) = $10 : Wave(213) = $12 : Wave(214) = $13 : Wave(215) = $15
Wave(216) = $16 : Wave(217) = $18 : Wave(218) = $1a : Wave(219) = $1c
Wave(220) = $1e : Wave(221) = $20 : Wave(222) = $22 : Wave(223) = $24
Wave(224) = $26 : Wave(225) = $29 : Wave(226) = $2b : Wave(227) = $2d
Wave(228) = $30 : Wave(229) = $32 : Wave(230) = $35 : Wave(231) = $37
Wave(232) = $3a : Wave(233) = $3c : Wave(234) = $3f : Wave(235) = $42
Wave(236) = $44 : Wave(237) = $47 : Wave(238) = $4a : Wave(239) = $4d
Wave(240) = $50 : Wave(241) = $53 : Wave(242) = $56 : Wave(243) = $59
Wave(244) = $5b : Wave(245) = $5e : Wave(246) = $62 : Wave(247) = $65
Wave(248) = $68 : Wave(249) = $6b : Wave(250) = $6e : Wave(251) = $71
Wave(252) = $74 : Wave(253) = $77 : Wave(254) = $7a : Wave(255) = $7d
'Expect a musical chord.
Incr1 = 1097
Incr2 = 1387
Incr3 = 1644
Pos1 = 0
Pos2 = 0
Pos3 = 0
Do
If Tifr1.ocf1a = 1 Then
Tifr1.ocf1a = 1
Reset Test 'Mark math code begin on scope
Pos1 = Pos1 + Incr1 'Pahse pointer 1
Pos2 = Pos2 + Incr2 'Pahse pointer 2
Pos3 = Pos3 + Incr3 'Pahse pointer 3
Sample1 = Wave(t1) 'Extract phase
Sample2 = Wave(t2)
Sample3 = Wave(t3)
Shift Sample2 , Right , 1 'Shift Right for Volume reduce
Shift Sample3 , Right , 2 'Shift Right for Volume reduce
Sample = Sample1 + Sample2 'Mix down 3 audio tones
Sample = Sample + Sample3 '<-- commenting out either of these makes it work.
Shift Sample , Left , 2 'Shift Left for Mix Volume gain
Set Test 'Mark math code end on scope
Reset Mcs 'This SPI transfer lasts for 25 uS using 8Mhz Xtall, no CkDiv.
Mspdr = Sam_hi Or $70 : Spiout Mspdr , 1
Mspdr = Sam_lo : Spiout Mspdr , 1
Set Mcs 'Vout is updated at rising edge of CS pin.
End If
Loop
End |
|
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5921 Location: Holland
|
Posted: Sun Oct 22, 2017 9:49 am Post subject: |
|
|
Best practice (IMO) is that you set the timer just before the ISR is terminated and not on entry as you do.
So normally you set the timer in the main code just before you let the timer run, and then again in the ISR before it ends.
Of course you can also use the compare mode of the timer so you do not need to load the reload value yourself. _________________ Mark |
|
Back to top |
|
|
Matrixx
Joined: 30 Nov 2005 Posts: 158
|
Posted: Mon Oct 23, 2017 1:50 am Post subject: |
|
|
I was playing a bit with the M328 yesterday and that is true. With 3 tones there is almost no time left inside the ISR for the rest of the features. I also consider the 16 Mhz option, but using the Pro Mini runs on 3.3V, I do not have the 16 Mhz 5V version around. But have some M2560 R3 boards that I could use.
Using Overlay bytes; a nice way to get High/Low !
It looks its time to use the Xmega now; I will start adding the rest of the features like drawbars and MIDI so it will need more time.
Xmega I have ran up to 44.2368 Mhz using a 7.3728 Mhz Xtall and a x6 multiplier.
This project I'm adapting from Roto; it produces cool sounds form the classic B3 organ.
Thank you all for your tips & clever code, I will do a try.
If someone want to test, this next settings produces nice tone assuming the ISR timing is well calibrated (I did reload the timer before ISR end, compensating for the math length) for the 64 uS.
At the Indexes:
Code: |
'A440 Organ Try:
'215 Hz (Below)
'440 Hz (Fund)
'2217 Hz
'Expect a musical chord.
Incr1 = 902
Incr2 = 1845
Incr3 = 6895
|
Inside ISR, remove or comment out this two lines (full volume for all 3 tones):
Code: |
'Shift Sample2, Left, 1 'Shift Right for Volume reduce
'Shift Sample3, Left, 1 'Shift Right for Volume reduce
|
|
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|