diff --git a/include/main.h b/include/main.h index e3c1cdc..374c7b2 100644 --- a/include/main.h +++ b/include/main.h @@ -1,7 +1,4 @@ #ifndef _MAIN #define _MAIN - -void lect(); - #endif diff --git a/include/sound4calc.h b/include/sound4calc.h index cec79c4..5575ab8 100644 --- a/include/sound4calc.h +++ b/include/sound4calc.h @@ -1,5 +1,14 @@ -#ifndef _SOUND4CALC -#define _SOUND4CALC +#ifndef _SOUND4CALC_H + #define _SOUND4CALC_H + +extern char* wave_signal = "-_"; // simple waveform* +extern unsigned short freq = 440; // frequency by default : to get La3 +extern unsigned short duration = 2000; // equivalent as 2000 ms duration sound + +void CallSequence(); +void PutPinState(char level); +void PlayNote(); +void InitPorts(); //SH4 addresses #define SH7305_PJCR 0xA4050110 @@ -18,19 +27,4 @@ #define SH7337_PGDR 0xA400012C #define SH7337_SCPDR 0xA4000136 - -void PutPinState(char level); -void PlayNote(); -void InitPorts(); - -static struct { - int freq; - int length_ms; - } seq[] = { - { 440, 500 }, - { 392, 500 }, - { 349, 500 }, - { -1, -1 } - }; - #endif diff --git a/src/main.c b/src/main.c index f04582a..9754de2 100644 --- a/src/main.c +++ b/src/main.c @@ -1,63 +1,50 @@ #include "main.h" -#include "sound4calc.h" - -#include // add timer's fonction -#include // add display's fonction +#include +//#include // add timer fonction +#include // add display fonction #include //#define PI 3.141592653584 -unsigned char bincod[25] = {0}; -unsigned char place = 0; -unsigned char range = 0; - -void lect() -{ - PutPinState(bincod[range]=='-'); - range++; - if(!(bincod[range])) range=0; -} - int main(void) { - static int freq = 440; - unsigned int key = 0; + unsigned int key = 0; //unsigned int i=0; - InitPorts(); + InitPorts(); - while(1) - { - dclear(); - - dprint(1, 1, "%d", freq); - dprint(1, 10, "%s", bincod); - dprint(1, 20, "%d", place); - dtext(1, 50, "F1 _ F2 -"); - - dupdate(); - - key = getkey(); - - switch(key) + while(1) { - case KEY_RIGHT : freq+=1; break; - case KEY_LEFT : freq-=1; break; - case KEY_UP : freq+=10; break; - case KEY_DOWN : freq-=10; break; + dclear(); - case KEY_F1 : *(bincod+place++) = '_'; break; - case KEY_F2 : *(bincod+place++) = '-'; break; - case KEY_DEL : place=(place>0 ? place - 1 : 0); *(bincod+place) = 0; break; + dprint(1, 1, "%d", freq); + dprint(1, 10, "%s", wave_signal); + //dprint(1, 20, "%d", place); + dtext(1, 50, "F1 _ F2 -"); - case KEY_EXE : - timer_start(TIMER_USER, freq*place, Clock_Hz, lect, 2*freq*place); - break; + dupdate(); - case KEY_EXIT : - timer_stop(TIMER_USER); - return 1; + key = getkey(); + + switch(key) + { + case KEY_RIGHT : freq+=1; break; + case KEY_LEFT : freq-=1; break; + case KEY_UP : freq+=10; break; + case KEY_DOWN : freq-=10; break; + + //case KEY_F1 : *(bincod+place++) = '_'; break; + //case KEY_F2 : *(bincod+place++) = '-'; break; + //case KEY_DEL : place=(place>0 ? place - 1 : 0); *(bincod+place) = 0; break; + + case KEY_EXE : + CallNote(); + break; + + case KEY_EXIT : + //timer_stop(TIMER_USER); + return 1; + } } - } - return 1; // this point is never reached + return 1; // this point is never reached } diff --git a/src/sound4calc.c b/src/sound4calc.c index f82ab2b..12095d7 100644 --- a/src/sound4calc.c +++ b/src/sound4calc.c @@ -2,45 +2,48 @@ #include // from Gint #include //from Gint -char* signal = "-_"; // simple waveform static struct { int freq; int length_ms; } seq[] = { - { 440, 500 }, + { 440, 500 }, { 392, 500 }, { 349, 500 }, { -1, -1 } }; - - void CallSequence() { - static int length_end = 0; - static int note = -1 ; + static int length_end = 0; + static int note = -1 ; - if(length_end<=0) - { - note++; - - //Stop the timer - if(seq[note].freq < 0) + if(length_end<=0) { - timer_stop(TIMER_USER); - return; + note++; + + //Stop the timer + if(seq[note].freq < 0) + { + timer_stop(TIMER_USER); + return; + } + + // update of the number of turn + length_end = seq[note].freq * seq[note].length_ms / 1000; + + timer_start(TIMER_USER, 2 * seq[note].freq, Clock_Hz, CallSequence, 0); } - length_end = seq[note].freq * seq[note].length_ms / 1000; - - timer_start(TIMER_USER, 2 * seq[note].freq, Clock_Hz, CallSequence, 0)); - } - - PlayNote(); - length_end--; + PlayNote(); + length_end--; } +void CallNote() +{ + unsigned int wavelength = strlen(wave_signal); + timer_start(TIMER_USER, freq * wavelength, Clock_Hz, PlayNote, freq * duration / 1000); +} /* PlayNote() @@ -48,11 +51,11 @@ void CallSequence() */ void PlayNote() { - static int x = 0; + static int x = 0; - PutPinState(signal[x]=='-'); - x = x + 1; - if(!(signal[x])) x = 0; + PutPinState(wave_signal[x]=='-'); + x = x + 1; + if(!(wave_signal[x])) x = 0; } /* @@ -68,16 +71,16 @@ void PlayNote() */ void PutPinState(char level) { - level = !!level; + level = !!level; - if(isSH3()) - { - *(volatile unsigned char*)SH7337_SCPDR = (*(volatile unsigned char*)SH7337_SCPDR & 0xFE) | level ; - } - else - { - *(volatile unsigned char*)SH7305_PJDR = (*(volatile unsigned char*)SH7305_PJDR & 0xF3) | (0x4 << level); - } + if(isSH3()) + { + *(volatile unsigned char*)SH7337_SCPDR = (*(volatile unsigned char*)SH7337_SCPDR & 0xFE) | level ; + } + else + { + *(volatile unsigned char*)SH7305_PJDR = (*(volatile unsigned char*)SH7305_PJDR & 0xF3) | (0x4 << level); + } } @@ -88,43 +91,43 @@ void PutPinState(char level) */ void InitPorts() { - if(isSH3()) - { + if(isSH3()) + { // initialisation of pin // *(volatile unsigned char*)SH7337_SCPDR |= 0x01; // SCIF2 clock on (STBCR3.MSTP31) - *(volatile unsigned char*)SH7337_STBCR3 &= ~0x02; + *(volatile unsigned char*)SH7337_STBCR3 &= ~0x02; // switch off SCSMR_2.TE and SCSMR_2.RE - *(volatile unsigned short*)SH7337_SCSCR2 &= ~0x0030; + *(volatile unsigned short*)SH7337_SCSCR2 &= ~0x0030; // SCIF2 clock off (STBCR3.MSTP31) - *(volatile unsigned char*)SH7337_STBCR3 |= 0x02; + *(volatile unsigned char*)SH7337_STBCR3 |= 0x02; // set bit 6 of port G to output mode - *(volatile unsigned short*)SH7337_PGCR = ( *(volatile unsigned short*)SH7337_PGCR & ~0x3000 ) | 0x1000; + *(volatile unsigned short*)SH7337_PGCR = ( *(volatile unsigned short*)SH7337_PGCR & ~0x3000 ) | 0x1000; // set bit 5 and 6 of port G - *(volatile unsigned char*)SH7337_PGDR |= 0x60; + *(volatile unsigned char*)SH7337_PGDR |= 0x60; // set port SC bit 0 to output - *(volatile unsigned short*)SH7337_SCPCR = ( *(volatile unsigned short*)SH7337_SCPCR & ~0x0003 ) | 0x0001; - } - else - { + *(volatile unsigned short*)SH7337_SCPCR = ( *(volatile unsigned short*)SH7337_SCPCR & ~0x0003 ) | 0x0001; + } + else + { // initialisation of pin // *(volatile unsigned char*)SH7305_PJDR |= 0x04; // SCIF2 clock on (MSTPCR0.MSTP007) - *(volatile unsigned int*)SH7305_MSTPCR0 &= ~0x00000080; + *(volatile unsigned int*)SH7305_MSTPCR0 &= ~0x00000080; // switch off SCSMR_2.TE and SCSMR_2.RE - *(volatile unsigned short*)SH7305_SCSCR &= ~0x0030; + *(volatile unsigned short*)SH7305_SCSCR &= ~0x0030; // SCIF2 clock off (MSTPCR0.MSTP007) - *(volatile unsigned int*)SH7305_MSTPCR0 |= 0x00000080; + *(volatile unsigned int*)SH7305_MSTPCR0 |= 0x00000080; // set bit 3 of port U to output mode - *(volatile unsigned short*)SH7305_PUCR = ( *(volatile unsigned short*)SH7305_PUCR & ~0x00C0 ) | 0x0040; + *(volatile unsigned short*)SH7305_PUCR = ( *(volatile unsigned short*)SH7305_PUCR & ~0x00C0 ) | 0x0040; // set bit 4 and 5 of port U - *(volatile unsigned char*)SH7305_PUDR |= 0x0C; + *(volatile unsigned char*)SH7305_PUDR |= 0x0C; // set port J bit 2 to output - *(unsigned short*)0xA4050110 = ( *(unsigned short*)0xA4050110 & ~0x0030 ) | 0x0010; + *(unsigned short*)0xA4050110 = ( *(unsigned short*)0xA4050110 & ~0x0030 ) | 0x0010; // set port J bit 3 to input - *(unsigned short*)0xA4050110 = ( *(unsigned short*)0xA4050110 & ~0x00C0 ) | 0x0080; + *(unsigned short*)0xA4050110 = ( *(unsigned short*)0xA4050110 & ~0x00C0 ) | 0x0080; /* // set port J bit 2 to output mode @@ -132,5 +135,5 @@ void InitPorts() // set port J bit 3 to output mode *(volatile unsigned short*)SH7305_PJCR = ( *(volatile unsigned short*)SH7305_PJCR & ~0x00C0 ) | 0x0040; */ - } + } }