📄 mucisd1720.c
字号:
#include <ht46r24.h>
#pragma vector isr_4 @ 0x4
#pragma vector isr_8 @ 0x8
#pragma vector isr_c @ 0xc
#define SET_BIT(x,y) (x|=(1<<y)) //x的y位置1
#define CLR_BIT(x,y) (x&=~(1<<y)) //清x的y位
#define GET_BIT(x,y) (x&(1<<y)) //读x的y位
#pragma rambank0
unsigned char key_value;
unsigned char SR0L @ 0x40;
unsigned char SR0H @ 0x41;
unsigned char SR1 @ 0x42;
unsigned char SR2 @ 0x43; //APC_H
unsigned char tmp @ 0x44;
unsigned char status @ 0x45;
#pragma norambank
void isr_4(){} // external ISR
void isr_8(){} // timer/event 0
void isr_c(){} // timer/event 1
//command define
#define _pac3 _13_3
#define _pac5 _13_5
#define PU 0x01
#define STOP 0x02
#define RESET 0x03
#define CLR_INT 0x04
#define RD_STATUS 0x05
#define RD_PLAY_PTR 0x06
#define PD 0x07
#define RD_REC_PTR 0x08
#define DEVID 0x09
#define PLAY 0x40
#define REC 0x41
#define ERASE 0x42
#define G_ERASE 0x43
#define RD_APC 0x44
#define WR_APC1 0x45
#define WR_APC2 0x65
#define WR_NVCFG 0x46
#define LD_NVCFG 0x47
#define FWD 0x48
#define CHK_MEM 0x49
#define EXTCLK 0x4a
#define SET_PLAY 0x80
#define SET_REC 0x81
#define SET_ERASE 0x82
#define PU_FLAG 1
#define EOM_FLAG 2
#define INT_FLAG 4
#define RDY_FLAG 0x10
#define ERASE_FLAG 0x20
#define PLAY_FLAG 0x40
#define REC_FLAG 0x80
void _delay10ms(unsigned char x){
unsigned char i;
for(i=0;i<x;i++)
{ _delay(10000);}
}
//MISO--→MCU
//MOSI←--MC
unsigned char isd1720_spio(unsigned char BUF_ISD)
{
unsigned char i,j;
_pb2=1; // SCLK=1;
_pb3=0; //ss
for(i=8;i>0;i--)
{
j=BUF_ISD;
_pb2=0; //SCLK
_delay(4);
if(!(GET_BIT(j,0)))
_pb1=0; //写ISD1720
else
_pb1=1;
BUF_ISD=BUF_ISD>>1; //右移 ,或者调用"intrins.h"里的_cror_(BUF_ISD,1)
_pb2=1;
if(!_pb0)
CLR_BIT(BUF_ISD,7); //读ISD1720,如果MISO为0,则BUF_ISD第7位为0
else
SET_BIT(BUF_ISD,7);
}
_pb1=0;
return BUF_ISD;
}
void isd1720_cmd(unsigned char cmd, unsigned char dat, unsigned char start, unsigned char end, unsigned char count)
{
_emi = 0; //禁止所有中断
_pb3 = 1;
_pb2 = 1;
_pb1 = 0;
_pb3 = 0;
_delay(20);
SR0L = isd1720_spio(cmd);
SR0H = isd1720_spio(dat);
if (count > 2)
{
SR1 = isd1720_spio(start);
}
if (count > 3)
{
SR2 = isd1720_spio(0x0);
}
if (count > 4)
{
SR0H = isd1720_spio(end);
}
if (count > 5)
{
SR0H = isd1720_spio(0x0);
}
if (count > 6)
{
SR0H = isd1720_spio(0x0);
}
_delay(20);
_pb3 = 1;
_emi = 1;
_delay(1000);
}
void isd1720_read_sr(void)
{
isd1720_cmd(RD_STATUS|0x10, 0x0, 0x0, 0x0, 3);
}
void isd1720_read_play(void)
{
isd1720_cmd(RD_PLAY_PTR|0x10, 0x0, 0x0, 0x0, 4);
}
void isd1720_read_rec(void)
{
isd1720_cmd(RD_REC_PTR|0x10, 0x0, 0x0, 0x0, 4);
}
void isd1720_set_erase(unsigned char addr)
{
isd1720_cmd(SET_ERASE|0x10, 0x0, addr, addr+0x0f, 7);
}
void isd1720_g_erase(void)
{
isd1720_cmd(G_ERASE|0x10,0x0,0x0,0x0,2);
}
void isd1720_reset(void)
{
isd1720_cmd(RESET, 0x0, 0x0, 0x0, 2);
}
void isd1720_powerup(void)
{
isd1720_cmd(PU, 0x0, 0x0, 0x0, 2);
}
void isd1720_stop_cmd(void)
{
isd1720_cmd(STOP, 0x0, 0x0, 0x0, 2);
}
void isd1720_powerdown(void)
{
isd1720_cmd(PD|0x10, 0x0, 0x0, 0x0, 2);
}
void isd1720_clear_int(void)
{
isd1720_cmd(CLR_INT, 0x0, 0x0, 0x0, 2);
}
void isd1720_write_apc2(void)
{
isd1720_cmd(WR_APC2, 0x40, 0x0C, 0x0, 3);
}
void isd1720_read_apc(void)
{
isd1720_cmd(RD_APC, 0x0, 0x0, 0x0, 4);
}
void isd1720_chk_mem(void)
{
isd1720_cmd(CHK_MEM, 0x0, 0x0, 0x0,2);
}
unsigned char isd1720_status_judge(unsigned char flag)
{
unsigned char time = 0;
status = 0;
SR1 = 0;
SR0L = 0;
while ((status & flag) == 0)
{
time++;
if (time > 8)
{
return 1; //>2s, time-out
}
isd1720_read_sr();
status = (SR1 << 4) + ((SR0L >> 2) & 0x0f);
}
return 0;
}
void isd1720_set_rec(unsigned char addr)
{
isd1720_cmd(SET_REC|0x10, 0x0, addr, addr+0x07, 7);
}
unsigned char isd1720_record(unsigned char addr)
{
if(addr>0x8f){
return 1;
}
if (isd1720_status_judge(PU_FLAG) != 0)
{
return 2; //time-out
}
isd1720_clear_int();
isd1720_write_apc2();
if (isd1720_status_judge(RDY_FLAG) != 0)
{
return 3; //time-out
}
isd1720_set_erase(addr);
if(SR0L&0x01!=0){
return 4;
}
_delay(20000); //延时20ms
isd1720_clear_int();
if (isd1720_status_judge(RDY_FLAG) != 0)
{
return 5; //time-out
}
isd1720_set_rec(addr);
_delay10ms(200); //延时2s
if (isd1720_status_judge(INT_FLAG) != 0)
{
return 6; //time-out
}
while ((SR0L&0x08== 0) && (SR1&0x08 ==0x08))
{
isd1720_read_sr(); //recing...
}
isd1720_clear_int();
isd1720_powerdown();
_delay(50000);
return 6;
}
void isd1720_set_play(unsigned char addr)
{
isd1720_cmd(SET_PLAY|0x10, 0x0, addr, addr+0x07, 7);
}
unsigned char isd1720_play(unsigned char addr)
{
if (addr > 0x9f)
{
return 5;
}
isd1720_powerup();
_delay(60000);
_delay(60000);
if (isd1720_status_judge(PU_FLAG)!= 0)
{
return 1; //time-out
}
isd1720_clear_int();
if (isd1720_status_judge(RDY_FLAG) != 0)
{
return 2; //time-out
}
isd1720_set_play(addr);
_delay10ms(120);
if (isd1720_status_judge(INT_FLAG) != 0)
{
return 3; //time-out
}
while ((SR0L&0x08 == 0) && (SR1&0x04 ==0x04))
{
isd1720_read_sr(); //playing...
}
isd1720_clear_int();
isd1720_powerdown();
_delay(60000);
return 4;
}
void safeguard_init(){
_intc0= 0;
_tmr0c = 0;
_tmr0h= 0;
_tmr0l = 0;
_tmr1c = 0;
_tmr1h = 0;
_tmr1l = 0;
_pac = 0xff;
_pbc = 0xff;
_pcc = 0xff;
_pdc = 0xff;
}
void wait_key_press(){
unsigned char key;
key=0;
while(!(key&(unsigned char)0x03)){
key=~_pa;
}
if(key&(unsigned char)0x01) key_value=0;
if(key&(unsigned char)0x02) key_value=1;
}
void wait_key_release(){
unsigned char key;
key=1;
while(key&(unsigned char)0x03){
key=~_pa;}
}
void csb_transmitt(){
_pac3=1;
_pa3=0;
_pac5=0;
_pa5=0;
_intc0=0x0d; //ET0I定时器0、ET0I定时器1 和 EMI总中断允许
_tmr0c=0x80; //定时模式
_tmr1c=0x80; //定时模式
_tmr0l=0xcb; //计算100次溢出
_tmr0h=0xff;
_tmr1h = 0;
_tmr1l = 0;
_pa3=1;
_t0on=1; //定时器0开启
_t1on=1; //定时器1开启
}
void main(){
safeguard_init();
_pbc=0xf1; //设置B口1、2、3口为输出其他为输入
_pb=0xfe;
_pac=0xf7; //设置PA4、3口为输出,接发光二级管
_pa=0xf7;
while(1){
wait_key_press();
isd1720_play(0x18);
isd1720_play(0x10);
wait_key_release();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -