 |

|
Elektronika.lt portalo forumas
Jūs esate neprisijungęs lankytojas. Norint dalyvauti diskusijose, būtina užsiregistruoti ir prisijungti prie forumo.
Prisijungę galėsite kurti naujas temas, atsakyti į kitų užduotus klausimus, balsuoti forumo apklausose.
Administracija pasilieka teisę pašalinti pasisakymus bei dalyvius,
kurie nesilaiko forumo taisyklių.
Pastebėjus nusižengimus, prašome pranešti.
Dabar yra 2025 06 29, 14:07. Visos datos yra GMT + 2 valandos.
|
|
|
 |
Forumas » Mikrovaldikliai » Digital guitar tuner
|
Jūs negalite rašyti naujų pranešimų į šį forumą Jūs negalite atsakinėti į pranešimus šiame forume Jūs negalite redaguoti savo pranešimų šiame forume Jūs negalite ištrinti savo pranešimų šiame forume Jūs negalite dalyvauti apklausose šiame forume
|
|
|
 |
 |
 |
Digital guitar tuner |
Parašytas: 2011 05 23, 02:23 |
|
|
|
Sveiki,
jau kuris laikas bandau pasidaryti gitarai tuneri pagal sita http://www.myplace.nu/avr/gtuner/index.htm
viskas atrodo paprasta, bet vietoj originalioje schemoj naudojamo mikrovaldiklio Atmel 2323 dabar gamina Attiny25, tai reikia perdaryti koda. bandau pritaikyt naujam valdikliui, bet vistiek neveikia normaliai. galbut del to, kad pats kodas irgi senas. kadangi mazai patirties ir ziniu programavime, tai nebezinau ka daryti :/
imetu originalu ir perdaryta koda, gal kas izvelgsite akivaizdziu klaidu.
Kodas: |
/*
Jesper Hansen <jesperh@telia.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
Simple Digital Guitar Tuner
---------------------------
version 1.0 2001-02-12 jesper
PIN assignements on the 2323
PB0 High LED
PB1 Input pin
PB2 Low Led
*/
#include <io.h>
#include <signal.h>
#define F_CPU 11059200 // CPU clock frequency
#define PRESCALER 64 // CPU prescaler value
#define BASE_FREQUENCY (F_CPU/PRESCALER) // counter frequency
#define TUNING_FORK_A 440.0 // "base" A
#define NOTE_DIFF 1.05946309436 // the 12'th root of 2
#define E_STRING 164.81 // top string (1)
#define A_STRING 220.00
#define D_STRING 293.66
#define G_STRING 391.99
#define B_STRING 493.88
#define EH_STRING 659.26 // bottom string (6)
// The guitar note span
// # # # # # # # # # #
//EF G A BC D EF G A BC D E
//1 2 3 4 * 5 6
/////////////////////////////
unsigned int Center_Count[] =
{
BASE_FREQUENCY/EH_STRING, // High E
BASE_FREQUENCY/B_STRING, // B
BASE_FREQUENCY/G_STRING, // G
BASE_FREQUENCY/D_STRING, // D
BASE_FREQUENCY/A_STRING, // A
BASE_FREQUENCY/E_STRING, // Low E
};
unsigned int Transition_Count[] =
{
BASE_FREQUENCY/(B_STRING+(EH_STRING-B_STRING)/2), // E to B
BASE_FREQUENCY/(G_STRING+(B_STRING-G_STRING)/2), // B to G
BASE_FREQUENCY/(D_STRING+(G_STRING-D_STRING)/2), // G to D
BASE_FREQUENCY/(A_STRING+(D_STRING-A_STRING)/2), // D to A
BASE_FREQUENCY/(E_STRING+(A_STRING-E_STRING)/2), // A to E
};
volatile unsigned char count_hi; // overflow accumulator
//
//timer 0 overflow interrupt
//
SIGNAL(SIG_OVERFLOW0)
{
count_hi++; // increment overflow count
}
//----------------------------------------------------------------------------
// Main Lupe
//----------------------------------------------------------------------------
int main(void)
{
unsigned int i;
unsigned int count;
//------------------------------
// Initialize
//------------------------------
cbi(DDRB, 1); // PB1 is input
cbi(PORTB, 1); // no pullups active
sbi(DDRB, 0); // PB0 is ouput, High LED
sbi(DDRB, 2); // PB2 is ouput, Low LED
outp(0x03,TCCR0); // set prescaler to f/64 (172.8 kHz @ 11.0592 MHz)
sbi(TIMSK,TOIE0); // enable interrupt on timer overflow
asm volatile ("sei"); // global interrupt enable
//----------------------------------------------------------------------------
// Let things loose
//----------------------------------------------------------------------------
while (1) // loop forever
{
count = 0; // clear sample count
loop_until_bit_is_set(PINB,1); // wait for something to happen
// got a high edge
// start sampling
outp(0,TCNT0); // clear counter
count_hi = 0; // clear hi count
// sample loop
for (i=0;i<32;i++)
{
while (bit_is_set(PINB,1)) // ignore hi->lo edge transitions
if (count_hi > 80) // skip if no edge is seen within
break; // a reasonable time
while (bit_is_clear(PINB,1)) // wait for lo->hi edge
if (count_hi > 80) // skip if no edge is seen within
break; // a reasonable time
count += (count_hi << 8) + inp(TCNT0); // get counter value
outp(0,TCNT0); // clear counter
if (count_hi > 80) // skip if counter has accumulated a
break; // too high value
count_hi = 0; // clear hi count
}
// initially turn off both leds
sbi(PORTB,0);
sbi(PORTB,2);
if (count_hi <= 80) // if count is reasonable
{
count = count >> 5; // average accumulated count by dividing with 32
// now we have to find the correct string
// go through transition frequencies
for (i=0;i<sizeof(Transition_Count)/sizeof(Transition_Count[0]);i++)
{
if (count < Transition_Count[i]) // stop if lower than this transition count
break;
}
// i now holds the string index
// check the count for a match, allowing
// 1 extra count "hysteresis" to avoid too
// much LED flickering
if (count-1 <= Center_Count[i]) // if count <= this string count
cbi(PORTB,0); // light "Too High" LED
if (count+1 >= Center_Count[i]) // if count >= this string count
cbi(PORTB,2); // light "Too Low" LED
}
}
}
//////////////////////////////////////////
/////////Pakeistas kodas//////////////////
//////////////////////////////////////////
#include <iotiny25.h>
#include <dlib/signal.h>
#include <dlib/stdint.h>
#define _MMIO_BYTE(mem_addr) (*(volatile uint8_t *)(mem_addr))
#define _SFR_MEM_ADDR(sfr) (sfr)
#define _SFR_ADDR(sfr) _SFR_MEM_ADDR(sfr)
#define _SFR_BYTE(sfr) _MMIO_BYTE(_SFR_ADDR(sfr))
#define _BV(bit) (1 << (bit))
#define inb(sfr) _SFR_BYTE(sfr)
#define bit_is_set(sfr, bit) (inb(sfr) & _BV(bit))
#define bit_is_clear(sfr, bit) (!(inb(sfr) & _BV(bit)))
#define loop_until_bit_is_set(sfr, bit) do { } while (bit_is_clear(sfr, bit))
#define loop_until_bit_is_clear(sfr, bit) do { } while (bit_is_set(sfr, bit))
#define inp(sfr) inb(sfr)
#define F_CPU 11059200 // CPU clock frequency
#define PRESCALER 64 // CPU prescaler value
#define BASE_FREQUENCY (F_CPU/PRESCALER) // counter frequency
#define TUNING_FORK_A 440.0 // "base" A
#define NOTE_DIFF 1.05946309436 // the 12'th root of 2
#define E_STRING 164.81 // top string (1)
#define A_STRING 220.00
#define D_STRING 293.66
#define G_STRING 391.99
#define B_STRING 493.88
#define EH_STRING 659.26 // bottom string (6)
// The guitar note span
// # # # # # # # # # #
//EF G A BC D EF G A BC D E
//1 2 3 4 * 5 6
/////////////////////////////
unsigned int Center_Count[] =
{
BASE_FREQUENCY/EH_STRING, // High E
BASE_FREQUENCY/B_STRING, // B
BASE_FREQUENCY/G_STRING, // G
BASE_FREQUENCY/D_STRING, // D
BASE_FREQUENCY/A_STRING, // A
BASE_FREQUENCY/E_STRING, // Low E
};
unsigned int Transition_Count[] =
{
BASE_FREQUENCY/(B_STRING+(EH_STRING-B_STRING)/2), // E to B
BASE_FREQUENCY/(G_STRING+(B_STRING-G_STRING)/2), // B to G
BASE_FREQUENCY/(D_STRING+(G_STRING-D_STRING)/2), // G to D
BASE_FREQUENCY/(A_STRING+(D_STRING-A_STRING)/2), // D to A
BASE_FREQUENCY/(E_STRING+(A_STRING-E_STRING)/2), // A to E
};
volatile unsigned char count_hi; // overflow accumulator
//
//timer 0 overflow interrupt
//
SIGNAL(SIG_OVERFLOW0)
{
count_hi++; // increment overflow count
}
//----------------------------------------------------------------------------
// Main Lupe
//----------------------------------------------------------------------------
int main(void)
{
unsigned int i;
unsigned int count;
//------------------------------
// Initialize
//------------------------------
DDRB &= ~(_BV(1)); // PB1 is input
PORTB &= ~(_BV(1)); // no pullups active
DDRB |= _BV(0); // PB0 is ouput, High LED
DDRB |= _BV(2); // PB2 is ouput, Low LED
TCCR0B = 0x03; // set prescaler to f/64 (172.8 kHz @ 11.0592 MHz)
SREG_Bit7 = 1; // enable interrupt on timer overflow
TIMSK_TOIE1 = 1;
asm ("sei"); // asm volatile ("sei"); kazkodel meta klaida ir neleidzia daryt "volatile" // global interrupt enable
//----------------------------------------------------------------------------
// Let things loose
//----------------------------------------------------------------------------
while (1) // loop forever
{
count = 0; // clear sample count
loop_until_bit_is_set(PINB,1); // wait for something to happen
// got a high edge
// start sampling
TCNT0 = 0; // clear counter
count_hi = 0; // clear hi count
// sample loop
for (i=0;i<32;i++)
{
while (bit_is_set(PINB,1)) // ignore hi->lo edge transitions
if (count_hi > 80) // skip if no edge is seen within
break; // a reasonable time
while (bit_is_clear(PINB,1)) // wait for lo->hi edge
if (count_hi > 80) // skip if no edge is seen within
break; // a reasonable time
count += (count_hi << 8) + inp(TCNT0); // get counter value
TCNT0 = 0; // clear counter
if (count_hi > 80) // skip if counter has accumulated a
break; // too high value
count_hi = 0; // clear hi count
}
// initially turn off both leds
PORTB |= _BV(0);
PORTB |= _BV(2);
if (count_hi <= 80) // if count is reasonable
{
count = count >> 5; // average accumulated count by dividing with 32
// now we have to find the correct string
// go through transition frequencies
for (i=0;i<sizeof(Transition_Count)/sizeof(Transition_Count[0]);i++)
{
if (count < Transition_Count[i]) // stop if lower than this transition count
break;
}
// i now holds the string index
// check the count for a match, allowing
// 1 extra count "hysteresis" to avoid too
// much LED flickering
if (count-1 <= Center_Count[i]) // if count <= this string count
PORTB &= ~(_BV(0)); // light "Too High" LED
if (count+1 >= Center_Count[i]) // if count >= this string count
PORTB &= ~(_BV(2)); // light "Too Low" LED
}
}
}
|
siaip nesigavo man padaryti asm volatile ("sei");, tai naudoju asm ("sei");..., nors zinau, kad cia ne tas pats, bet IAR Embedded Workbench for Atmel AVR 5.51 neturi interrupt.h (apkarpyta versija)
dar galvoju, kad su "CPU clock frequency" gali buti negerai kazkas, nes kiek testavau, tai atrodo kodas sustoja loop'int toje vietoje, kur pradeda skaiciuoti signalo dazni.
siaip naudoju isorini 4Mhz kvarca, kaip originale ir fuse atitinkamai nustatau tik STU1 ir CKSEL3 (nzn, ar teisingai). bet gal butu galima panaudoti ir vidini laikrodi?
buciau dekingas uz bet kokia pagalba  |
|
|
|
|
 |
 |
Digital guitar tuner |
Parašytas: 2011 05 23, 08:45 |
|
|
|
aciu, sopkis, sita buvau uzgaudes ir pats, reikes dar karta perziureti sita datasheet'a, bet vien anglu kalbos ziniu nepakanka visiems atsakymams rasti sakau, kazkaip atrodo, kad code kazkas negerai. gal klaidinga funkcija vietoj senos nebenaudojamos kokia esu ikises ar pan.
siaip IAR nemeta kritiniu klaidu, tik warning'us. dauguma
"Warning[Pa092]: implicit conversion to integer does not preserve the value"
o sitoj eiluteje "count += (count_hi << + inp(TCNT0);" rodo "Warning[Pa082]: undefined behavior: the order of volatile accesses is undefined in this statement", tai gal cia problema? |
|
|
|
|
|
 |
Google paieška forume |
|
 |
Naujos temos forume |
|
 |
FS25 Tractors
Farming Simulator 25 Mods,
FS25 Maps,
FS25 Trucks |
 |
ETS2 Mods
ETS2 Trucks,
ETS2 Bus,
Euro Truck Simulator 2 Mods
|
 |
FS22 Tractors
Farming Simulator 22 Mods,
FS22 Maps,
FS25 Mods |
 |
VAT calculator
VAT number check,
What is VAT,
How much is VAT |
 |
FS25 Mods
FS25 Harvesters,
FS25 Tractors Mods,
FS25 Maps Mods |
 |
Dantų protezavimas
All on 4 implantai,
Endodontija mikroskopu,
Dantų implantacija |
 |
FS25 Mods
FS25 Maps,
FS25 Cheats,
FS25 Install Mods |
 |
FS25 Mods
Farming Simulator 25 Mods,
FS25 Maps |
 |
ATS Trailers
American Truck Simulator Mods,
ATS Trucks,
ATS Maps |
 |
Football Training Kit
Football Training Equipment,
Football Skills,
Football Training |
|

|
 |