芯片KC89C72 可编程声音发生器(PSG)

一片从游戏器上拆下来的KC89C72。MCU使用的51。

/* \file main.c - Keil C v8.02
 *   Project id: 00595cf9-8de6-4a57-b940-eb0347ac9e13
 *
 * \details This file is part of the KC89C72 project.
 *
 * History:
 *   Date        Auther      Description
 *   -------------------------------------------------------------
 *   2013-01-06  Perry       Initial created.
 *
 * Note:
 * This source code can be used, modified, and redistributed under the
 * terms of the license agreement that is included in the lcd_h018in01 package
 * By continuing to use, modify, or redistributed this code you indicate
 * that you have read the license and understand and accept it fully.
 */


#include "main.h"

#define PSG_KC89C72_REG_R0              0x00  // 通道A频率设置,低8位。
#define PSG_KC89C72_REG_R1              0x01  // 通道A频率设置,高4位。
#define PSG_KC89C72_REG_R2              0x02  // 通道B频率设置,低8位。
#define PSG_KC89C72_REG_R3              0x03  // 通道B频率设置,高4位。
#define PSG_KC89C72_REG_R4              0x04  // 通道C频率设置,低8位。
#define PSG_KC89C72_REG_R5              0x05  // 通道C频率设置,高4位。
#define PSG_KC89C72_REG_R6              0x06  // 噪音频率设置,5位。
#define PSG_KC89C72_REG_R7              0x07  // I/O端口与混音设置。
#define PSG_KC89C72_REG_R8              0x08  // 通道A电平设置。
#define PSG_KC89C72_REG_R9              0x09  // 通道B电平设置。
#define PSG_KC89C72_REG_RA              0x0A  // 通道B电平设置。
#define PSG_KC89C72_REG_RB              0x0B  // 包络频率。微调,8位。
#define PSG_KC89C72_REG_RC              0x0C  // 包络频率。粗调,8位。
#define PSG_KC89C72_REG_RD              0x0D  // 包络形状。低4位(CONT/ATT/ALT/HOLD)。
#define PSG_KC89C72_REG_RE              0x0E  // 扩展/外设端口A数据寄存器,8位。
#define PSG_KC89C72_REG_RF              0x0F  // 扩展/外设设口B数据寄存器,8位。

#define chip_addr  PSG_KC89C72_DA
#define chip_data  PSG_KC89C72_DA

sbit key = P1^0;

sbit bc1      = PSG_KC89C72_BC1;
sbit bc2      = PSG_KC89C72_BC2;
sbit bdir     = PSG_KC89C72_BDIR;
sbit sel      = PSG_KC89C72_SEL;
sbit a8       = PSG_KC89C72_A8;
sbit a9       = PSG_KC89C72_A9;
sbit reset    = PSG_KC89C72_RESET;
sbit clk      = PSG_KC89C72_CLK;

#define set_addr_mode() \
  bc1 = 1; \
  bc2 = 1; \
  bdir = 1; \
  a8 = 1; \
  a9 = 0

#define set_read_mode() \
  bc1 = 1; \
  bc2 = 1; \
  bdir = 0; \
  a8 = 0

#define set_write_mode() \
  bc1 = 0; \
  bc2 = 1; \
  bdir = 1; \
  a8 = 0

#define set_inactive_mode() \
  bc1 = 0; \
  bc2 = 1; \
  bdir = 0; \
  a8 = 0

void chip_write(uint8 reg, uint8 val)
{
  set_addr_mode();
  delay_xms(1);
  chip_addr = reg & 0xf;
  delay_xms(1);

  set_write_mode();
  delay_xms(1);
  chip_data = val;
  delay_xms(1);

  set_inactive_mode();
  delay_xms(1);
  
  chip_data = 0xff;
}

void chip_reset()
{
  reset = 0;
  delay_xms(1);
  reset = 1;
}

void chip_init()
{
  chip_reset();
  sel = 1;
  
  chip_write(PSG_KC89C72_REG_R0, 150);
  chip_write(PSG_KC89C72_REG_R1, 10);
  
  chip_write(PSG_KC89C72_REG_R2, 0xff);
  chip_write(PSG_KC89C72_REG_R3, 0xf);
  
  chip_write(PSG_KC89C72_REG_R4, 8);
  chip_write(PSG_KC89C72_REG_R5, 0);
  
  chip_write(PSG_KC89C72_REG_R6, 1);
  chip_write(PSG_KC89C72_REG_R7, 0xf8);
  
  chip_write(PSG_KC89C72_REG_R8, 0x2);
  chip_write(PSG_KC89C72_REG_R9, 0xf);
  chip_write(PSG_KC89C72_REG_RA, 0x1f);
  
  chip_write(PSG_KC89C72_REG_RB, 0x28);
  chip_write(PSG_KC89C72_REG_RC, 0x0);
  
  chip_write(PSG_KC89C72_REG_RD, 0xe);
}

void main()
{
  uint8 vol = 0xf;
  
  chip_init();
  
  while (1)
  {
    if (!key)
    {
      delay_xms(500);
      if (vol = 0)
        vol = 0xf;
      vol--;
  chip_write(PSG_KC89C72_REG_R1, vol & 0xf);
  chip_write(PSG_KC89C72_REG_R3, vol & 0xf);
  chip_write(PSG_KC89C72_REG_R5, vol & 0xf);
    }
  }
}

// 延时子程序毫秒单位。
void delay_xms(uint16 ms)
{
  while (ms--)
    delay_xus(109);
}

// 延时子程序微秒单位。
void delay_xus(uint16 us)
{
  while (us--);
}


 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值