jump to navigation

Holtek HT48C50 C code for Piano September 10, 2007

Posted by eepu in Microcontroller.
trackback

Currently I (Fandi Gunawan) am playing around with Holtek’s microcontroller series. This is the example of Piano’s code in C language since I do not want to touch assembly language.

//Piano.c
//
//Body: HT48C50-1
//Mask option
//BZ/BZB : All Disable
//the others use the default value

#include <ht48c50-1.h>

#pragma vector isr_4 @ 0×4
#pragma vector isr_8 @ 0×8
#pragma vector isr_c @ 0xc

//ISR for safequard
void isr_4(){} // external ISR
void isr_8(){} // timer/event 0

//initialize registers for safeguard
void safeguard_init(){
        _intc = 0;
        _tmr0c = 0;
        _tmr0 = 0;
        _tmr1c = 0;
        _tmr1h = 0;
        _tmr1l = 0;
        _pac = 0xff;
        _pbc = 0xff;
        _pcc = 0xff;
        _pdc = 0xff;
}

#define _tmr1c4 _11_4  //timer1 enable bit

const unsigned char frq[16] = {
0×21, 0xfe, 0×58, 0xfe, 0×84, 0xfe, 0×99, 0xfe,
0xc1, 0xfe, 0xe3, 0xfe, 0×2, 0xff, 0×11, 0xff};

unsigned char frq_idx;

void initial();
void wait_key_press();
void wait_key_release();
void start_sound();
void stop_sound();

void main(){

        safeguard_init();
        initial();

        while(1){
                wait_key_press();
                start_sound();
                wait_key_release();
                stop_sound();
        }
}

void wait_key_press(){
        unsigned char i, key;

        key = 0;
        while(!key)
                key = ~_pa;

        for(i=0; i<8; i++){
                if (key & 0×1){
                        frq_idx = i << 1;                                                  
                        break;
                }
                key >>= 1;
        }
}

void wait_key_release(){
        unsigned char key;
        key = 1;
        while(key)
                key = ~_pa;
}

void start_sound(){
        _intc = 9;              //enable timer1
        _tmr1c = 0×80;          //timer mode
        _tmr1l = frq[frq_idx];  //load sound freq.
        _tmr1h = frq[frq_idx+1];
        _tmr1c4 = 1;            //start timer1
}

void stop_sound(){
        _tmr1c4 = 0;            //stop timer1
        _pb = 0;
}

void isr_c(){                   // timer1                  
        _pb = ~_pb;             // generate square wave
}

void initial(){
        _pac = 0xff;    //set port A to input port
        _pbc = 0;       //set port B to output port
        _pb = 0;
}

Comments»

No comments yet — be the first.