Help with buzzer C code for Dragon12 HCS12

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Help with buzzer C code for Dragon12 HCS12

1,760 Views
dmek
Contributor I

Hello good afternoon everyone,

I am trying to write a C code for the HCS12 board that plays a tune at the prompt of a pushbutton. I have written individual C code for for the buzzer, the LCD, and the LED's. Separately they run fine but when I tried to combine the codes into a single program I am having issues with the buzzer. The buzzer only plays a single note instead of the full track. I could use a set of fresh eyes to spot any misplaced { }. If someone could please help it would be extremely grateful. 

<<<<<THE CODE>>>>>>>

# define c 2867
# define d 2554
# define e 2276
# define f 2148
# define g 1914
# define a 1705
# define b 1519
# define C 1434
# define D 1277
# define E 1138
# define F 1074
# define G 947
# define A 853
# define B 760
# define Bf 800
# define CC 717
# define DD 639

void oc5ISR(void);

void Setup (void);
void Draw (void);
void SetClk8(void);
int x;
int y;
int dlycnt;

////////////////////////////////////////////////////

void main(void)
{
SetClk8();
DDRJ |= 0xFF;
PTJ &= 0xFD;
DDRB |= 0xFF;
PTB |= 0xFF;
RTICTL = 0x40;
CRGINT |= RTIE;

DDRK = 0xFF;
Setup();
Draw();



TSCR1 = 0x90; // enable TCNT and fast timer flag clear
TSCR2 = 0x03; // set prescaler to TCNT to 1:8
TIOS |= BIT5; // enable OC5
TCTL1 = 0x04; // select toggle for OC5 pin action

dlycnt = c; // use high-frequency delay count first
TC5 = TCNT + dlycnt; // start an OC5 operation
TIE |= BIT5; // enable TC5 interrupt


asm("cli");

while(1) {

//stanza1
delayby100ms(1); // wait for half a second
dlycnt = e; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = d; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = e; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = d; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = e; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = b; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = d; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = c; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = a; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = c; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = e; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = a; // switch to high-frequency tone
//
// stanza2

delayby100ms(10); // wait for half a second
dlycnt = b; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = e; // switch to high-frequency tone

delayby100ms(5); // wait for half a second
dlycnt = G; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = b; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = C; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = e; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = E; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = d; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = E; // switch to low-frequency tone
delayby100ms(5); // wait for half a second
dlycnt = d; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = E; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = b; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = D; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = C; // switch to high-frequency tone
//
// stanza3

delayby100ms(1); // wait for half a second
dlycnt = a; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = c; // switch to high-frequency tone

delayby100ms(5); // wait for half a second
dlycnt = e; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = a; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = b; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = e; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = C; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = b; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = a; // switch to low-frequency tone
delayby100ms(5); // wait for half a second
dlycnt = E; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = d; // switch to low-frequency tone
//
// stanza4

delayby100ms(1); // wait for half a second
dlycnt = a; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = b; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = C; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = d; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = E; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = g; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = F; // switch to high-frequency tone

delayby100ms(5); // wait for half a second
dlycnt = E; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = d; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = f; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = E; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = d; // switch to low-frequency tone
//
// stanza5
delayby100ms(1); // wait for half a second
dlycnt = C; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = e; // switch to low-frequency tone
delayby100ms(5); // wait for half a second
dlycnt = d; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = C; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = b; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = e; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = E; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = e; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = E; // switch to high-frequency tone

delayby100ms(5); // wait for half a second
dlycnt = E; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = E; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = D; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = E; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = d; // switch to low-frequency tone
//
// stanza6

delayby100ms(1); // wait for half a second
dlycnt = E; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = d; // switch to low-frequency tone
delayby100ms(5); // wait for half a second
dlycnt = E; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = d; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = E; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = d; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = E; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = d; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = E; // switch to high-frequency tone

delayby100ms(5); // wait for half a second
dlycnt = b; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = D; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = C; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = a; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = c; // switch to low-frequency tone
delayby100ms(1); // wait for half a second
dlycnt = e; // switch to high-frequency tone

delayby100ms(1); // wait for half a second
dlycnt = a; // switch to low-frequency tone

//while(1); // Do we need to refresh at some point?
}
//}
while(1);

}


interrupt void rtiISR(void)
{
unsigned char SegPat[8] = {0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};

CRGFLG = 0x80;
x=0;

for(y=0;y<5;y++)
{
if(y<4)
{
PTB |= 0xFF;
delayby1ms(400);
PTB &= 0x00;
delayby1ms(400);
}
else
if(y<8)
{
for(x=0;x<8;x++)
{
PTB = SegPat[x];
delayby1ms(400);
}
}
else
if(y<12)
{
for(x=7;x>-1;x--)
{
PTB = SegPat[x];
delayby1ms(400);
}
}
}
}
////////////////////////////////////////////////////////////

interrupt void oc5ISR(void)
{
TC5 += dlycnt;
}

 

 

extern void near rtiISR(void);
#pragma CODE_SEG __NEAR_SEG NON_BANKED
#pragma CODE_SEG __DEFAULT
typedef void (*near tIsrFunc)(void);
const tIsrFunc _vect[] @0xFFF0 = {
rtiISR
};


void SetClk8 (void) {
SYNR = 0x02; // set SYSCLK to 24 MHz from a 4-MHz oscillator
REFDV = 0; // "
PLLCTL = 0x60; // turn on PLL, set automatic
while(!(CRGFLG & LOCK));
CLKSEL |= PLLSEL; // clock derived from PLL
}


void Setup(void) {
unsigned char init[7] = {
0x33,0x32,0x28,0x06,0x0E,0x01,0x80
};
int i;

for(i = 0; i < 7; i++) {
LCDControl(init[i]);
delayby1ms(1);
}
}


void Draw(void) {
unsigned char output[30] = {
'E','m','e','r','a','l','d',' ',
'H','i','l','l',' ',' ',' ',0xC0,
'b','y',':','S','o','n','i','c',
'.',' ',' ',' ',' ',' '
};
int i;
for (i = 0; i < 15; i++) {
delayby1ms(1);
LCDData(output[i]);
}
delayby1ms(1);
LCDControl(0xC0);
for(i=16; i<30; i++){
delayby1ms(1);
LCDData(output[i]);
}
}

 

<<<END>>>

Thanks again! i have attached a .zip file of the original files just in case

Tags (3)
0 Kudos
1 Reply

1,740 Views
lama
NXP TechSupport
NXP TechSupport

Hi,

could you please provide full code package with all project files as well as adjust the project in the way to be all user files placed in and included from this project folder. Now you include files from a cloud so the project is not fully downloadable to be able to check.

Which version of the CodeWarrior do you use? 5.1 / 5.2 / ???

One more thing I would like to ask is whether you tried to step the code in reality and/or the simulator and checked whether everything is written as expected before code starts and after each step. Have you found any discrepancy?

BTW; better and more readable is to use a little bit different definition of an interrupt function. However the decision is up to you and your approach.

An example:

//============================================================================#pragma CODE_SEG NON_BANKED
interrupt 8 void TIMER0_ISR(void);      // or selfcalc.: interrupt ((0xFE-0xEE)/2) void TIMER_ISR(void);
#pragma CODE_SEG DEFAULT          // 0xFE and 0xEE were get from vector table

      // 0xFE is lower byte of lower address of largest vector (0xFFFE,0xFFFF) ... reset vector
      // 0xEE is lower byte of lower address of ECT0 vector (0xFFEE,0xFFEF)

//============================================================================
//TIMER_ISR
//============================================================================
#pragma CODE_SEG NON_BANKED
interrupt 8 void TIMER0_ISR(void)
{
TC0 = TC0 + PERIOD0; //toggle in interval 0.5s (period = 31250 / timer clk = 0.5s)
PORTB_BIT0 = ~PORTB_BIT0; //toggle LED PB0
}
#pragma CODE_SEG DEFAULT

 

Best regards,

Ladislav

Tags (1)
0 Kudos