⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 1507op1104_avr.c

📁 华虹TYPEA射频芯片1507的控制程序
💻 C
📖 第 1 页 / 共 2 页
字号:
//************************************************************//
//1507op1104_AVR 
//------------------------------------------------------------
//Data:			08/04/21
//File name:	1507op1104_AVR.C
//Hardware:		ATMega32
//compiler:		GCC
//Author:		Gamma
//Version:		v1.0.0
//Description:
//E-Mail:		togamma@hotmail.com
//************************************************************// 

#include <avr/io.h>
#include <avr/pgmspace.h>
#include <1507op1104_AVR.h>

//************************************************************// 
//数据类型定义
//************************************************************// 
#define uchar unsigned char
#define uint  unsigned int

//************************************************************// 
//SHC1507寄存器定义
//注意:请根据系统调整寄存器基地址CDMADDR
//************************************************************// 
#define CDMADDR	0x0000

#define DATAREG 	CDMADDR+0x00
#define STACON		CDMADDR+0x01
#define ENABLE		CDMADDR+0x02
#define BCNS		CDMADDR+0x03
#define BCNR		CDMADDR+0x04
#define BAUDRAT		CDMADDR+0x05
#define TOC			CDMADDR+0x06
#define MODE		CDMADDR+0x07
#define	SOFC		CDMADDR+0x08
#define EGTC		CDMADDR+0x09
#define	KEYDATA		CDMADDR+0x0a
#define KEYSTAC		CDMADDR+0x0b
#define KEYADR		CDMADDR+0x0c
#define RCODE		CDMADDR+0x0e
#define DATACNT		CDMADDR+0x0f
#define GAINCTR		CDMADDR+0x10
#define TRANS0L		CDMADDR+0x11
#define TRANS0H		CDMADDR+0x12
#define	TRANS1L		CDMADDR+0x13
#define TRANS1H		CDMADDR+0x14
#define RFSEL		CDMADDR+0x15


//************************************************************// 
//错误标志位定义
//************************************************************// 
#define OK				0x00
#define FE_1507			0x02
#define AE_1507			0x04
#define BE_1507			0x08
#define CE_1507			0x10
#define PE_1507			0x20
#define TE_1507			0x40
#define DV_1507			0x80

#define XE_1507			0xa0
#define RE_1507			0xa1


//************************************************************// 
//模拟并口定义
//************************************************************// 
#define LPTPORT		PORTB	//并口
#define LPTDDR		DDRB
#define LPTPIN		PINB

#define ALEPORT		PORTC	//ALE脚	
#define ALEDDR		DDRC
#define ALE			PC2	

#define NWRPORT		PORTC	//NWR脚
#define NWRDDR		DDRC
#define NWR			PC3	

#define NRDPORT		PORTC	//NRD脚
#define NRDDDR		DDRC
#define NRD			PC4	

#define NIRQPORT	PORTC	//NIRQ口
#define NIRQDDR		DDRC
#define NIRQPIN		PINC
#define NIRQ		PC5	

//************************************************************// 
//宏指令
//************************************************************// 
#define _NOP() asm("nop")


//************************************************************// 
//SHC1507传输密钥
//************************************************************// 
prog_char TransKey1507[]=
{
 0xAA, 0x1D, 0xA2, 0xEB, 0x1B, 0x52 ,  //sector0
 0xFC, 0x4B, 0xF4, 0xBD, 0x4D, 0x04 ,  //sector1 
 0x70, 0xC7, 0x78, 0x31, 0xC1, 0x88 ,  //sector2  
 0x26, 0x91, 0x2E, 0x67, 0x97, 0xDE ,  //sector3
 0x11, 0xA6, 0x19, 0x50, 0xA0, 0xE9 ,  //sector4
 0x47, 0xF0, 0x4F, 0x06, 0xF6, 0xBF ,  //sector5 
 0xCB, 0x7C, 0xC3, 0x8A, 0x7A, 0x33 ,  //sector6 
 0x9D, 0x2A, 0x95, 0xDC, 0x2C, 0x65 ,  //sector7
 0x1E, 0xAD, 0x1A, 0x63, 0xB5, 0xF0 ,  //sector8
 0x5C, 0x0F, 0x58, 0x21, 0x13, 0xCE ,  //sector9
 0xE8, 0x93, 0xF4, 0xAD, 0x9F, 0x6A ,  //sector10
 0xC2, 0x59, 0xCE, 0x07, 0x59, 0xA4 ,  //sector11
 0xE7, 0x54, 0xF3, 0x2E, 0x60, 0xA9 ,  //sector12
 0x41, 0x9A, 0x4D, 0x08, 0x9A, 0x63 ,  //sector13
 0xDD, 0x2E, 0xD9, 0xA4, 0x36, 0xEF ,  //sector14
 0x9B, 0x10, 0x97, 0xE2, 0x14, 0x4D ,  //sector15
};


/******************************************/
//函数:SendLPT
//说明:向模拟并口写数据
/******************************************/
void SendLPT(uchar add, uchar data)
{
	//----------端口配置-------------//
	LPTDDR = 0xff;				//并口输出
	ALEDDR |= (0x01<<ALE);		//ALE脚输出
	NWRDDR |= (0x01<<NWR);		//NWR脚输出

	//----------输出地址-------------//
 	ALEPORT &= ~(0x01<<ALE);	//ALE=0	
	NWRPORT |= (0x01<<NWR);		//nWR=1;
	_NOP();

	ALEPORT |= (0x01<<ALE);		//ALE=1
	LPTPORT = add;				//写寄存器地址
	_NOP();

	ALEPORT &= ~(0x01<<ALE);	//ALE=0
	_NOP();

	//----------输出数据-------------//
	LPTPORT = data;				//DATA 写数据
	_NOP(); 

	NWRPORT &= ~(0x01<<NWR);	//nWR=0;
	_NOP();	_NOP();	_NOP();

	NWRPORT |= (0x01<<NWR);		//nWR=1;
	_NOP();
}

/******************************************/
//函数:RcvLPT
//说明:从模拟并口读数据
/******************************************/
uchar RcvLPT(uchar add)
{
	uchar data;

	//----------端口配置-------------//
	LPTDDR = 0xff;				//并口输出
	ALEDDR |= (0x01<<ALE);		//ALE脚输出
	NRDDDR |= (0x01<<NRD);		//NRD脚输出

	//----------输出地址-------------//
 	ALEPORT &= ~(0x01<<ALE);	//ALE=0	
	NRDPORT |= (0x01<<NRD);		//nRD=1;
	_NOP();

	ALEPORT |= (0x01<<ALE);		//ALE=1
	LPTPORT = add;					//写寄存器地址
	_NOP();

	ALEPORT &= ~(0x01<<ALE);	//ALE=0
	_NOP();

	//----------并口配置------------//
	LPTDDR = 0x00;				//并口输入
	LPTPORT = 0xff;				//并口上拉
	
 	//----------读出数据------------//
	NRDPORT &= ~(0x01<<NRD);	//nRD=0;
	_NOP();
 
	NRDPORT |= (0x01<<NRD);		//nRD=1;
  
  	data = LPTPIN;				//读出数据
	_NOP();
 
	return data;				//返回读出的数据
}

/******************************************/
//函数:DVStatus
//说明:检查1507DV位状态,函数返回状态值
/******************************************/
uchar DVStatus(void)
{
	uchar temp;
	uint i;

	NIRQDDR &= ~(0x01<<NIRQ);			//NIRQ输入
	NIRQPORT |= (0x01<<NIRQ);			//NIRQ上拉

	i = 0x9000;
	temp = 0x01<<NIRQ;
	while( NIRQPIN & temp ){
		if(i-- == 0)	return	TE_1507;	//等待DV跳高即NIRQ跳低
	}

	return RcvLPT(STACON);
}

/******************************************/
//函数:Delay_1ms
//说明:延时函数,延时multix1ms
/******************************************/
void Delay_1ms(uchar multi)
{
	uint i,j;

	for(i=0;i<multi;i++)
		for(j=0;j<0x500;j++);
}

/******************************************/
//函数:PowerDown1507
//说明:SHC1507休眠
/******************************************/
void PowerDown1507(void)
{
	//关闭发射管
	SendLPT(TRANS0L,0x00);
	SendLPT(TRANS0H,0x00);
	SendLPT(TRANS1L,0x00);
	SendLPT(TRANS1H,0x00);

	SendLPT(STACON,0x12);	//SHC1507休眠
}

/******************************************/
//函数:PowerOn1507
//说明:SHC1507唤醒
/******************************************/
void PowerOn1507(void)
{
	SendLPT(STACON,0x8c);	//唤醒SHC1507
}

/******************************************/
//函数:ClsSHC1507RF
//说明:关闭SHC1507的射频输出
/******************************************/
void ClsSHC1507RF(void)
{
	//关闭发射管
	SendLPT(TRANS0L,0x00);
	SendLPT(TRANS0H,0x00);
	SendLPT(TRANS1L,0x00);
	SendLPT(TRANS1H,0x00);

	SendLPT(STACON,0x0e);	//关闭射频输出
	Delay_1ms(1);			//延时1ms
}

/******************************************/
//函数:Request_04
//说明:寻卡指令,返回2字节寻卡返回值
/******************************************/
uchar Request_04(uchar *outbuf)
{
	uchar status;	
	
	SendLPT(GAINCTR,0x07);//接收增益

	SendLPT(TRANS0L,0x00);//设为100%调制模式
	SendLPT(TRANS0H,0x00);
	SendLPT(TRANS1L,0xff);
	SendLPT(TRANS1H,0xff);

	SendLPT(RFSEL,0x0);//使能数字和模拟部分
	SendLPT(BAUDRAT,0x0e);//设置速率106kbps
	SendLPT(MODE,0xc1);//设置nPause宽度
	SendLPT(RCODE,0x03);//设置解码

	SendLPT(STACON,0x8c);//复位CDM,选择TYPEA模式
	SendLPT(STACON,0x0c);

	Delay_1ms(1);	

	SendLPT(ENABLE,0xc1);//复位各错误标识

	SendLPT(BCNS,0x07);//发送bit数

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -