已阅读5页,还剩8页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
#include #include #include #include ADDR.H#include SI5338.H /clockbuilder产生寄存器值头文件#define uchar unsigned char#define uint unsigned int#define TRUE1#define FALSE 0sbit SCL=P26;sbit SDA=P21;bit I2CErr = FALSE;bit LOCK_PLL = TRUE;/*-函数名称:延时-*/void DELAY(void) _nop_(); _nop_(); _nop_(); _nop_();/*-函数名称:10us延时-*/void DELAY_10(uchar n) do _nop_(); _nop_(); _nop_(); _nop_(); _nop_();while(-n);/*-函数名称:启动-*/void I2C_Start(void) SDA=1;SCL=1; DELAY();SDA=0; DELAY();SCL=0;/*-函数名称:停止-*/void I2C_Stop(void) SCL=0; SDA=0; DELAY(); SCL=1; DELAY();SDA=1;/*-函数名称:测试应答-*/bit I2C_WaitAck(void) uchar times=255;/避免故障,设定错误次数SDA=1; _nop_();SCL=1;while(SDA)times-;if(!times)/超时值为255 I2C_Stop();I2CErr=TRUE;return FALSE; DELAY();SCL=0; I2CErr=FALSE;return TRUE;/*-函数名称:发送应答-*/void I2C_SendAck(void) SDA=0;DELAY();SCL=1;DELAY();SCL=0;/*-函数名称:发送非应答-*/void I2C_SendNoAck(void) SDA=1;DELAY();SCL=1;DELAY();SCL=0;/*-函数名称:发送1字节数据-*/void I2C_SendByte(uchar dat)uchar i; for(i=0;i=7;i+) _nop_();if(dat&0x80)=0x80)SDA=1;elseSDA=0;dat=1;SCL=1;DELAY();SCL=0;/*-函数名称:接收1字节数据-*/void I2C_RcvByte(uchar *pRdDat)uchar i; SDA=1; for(i=0;i=7;i+)SCL=1;DELAY();*pRdDat=1;*pRdDat|=SDA;SCL=0;DELAY();/*-函数名称:写N字节数据-*/void I2C_WrBytes(uchar wrDAdr1,uchar wordAdr1,uchar *pWrDat,uchar num1) uchar i;I2C_Start();I2C_SendByte(wrDAdr1); I2C_WaitAck();I2C_SendByte(wordAdr1); I2C_WaitAck();for(i=0;inum1;i+)I2C_SendByte(*(pWrDat+i); I2C_WaitAck(); I2C_Stop();/*-函数名称:读N字节数据-*/void I2C_RcvBytes(uchar wrDAdr2,uchar wordAdr2,uchar rdDAdr2,uchar *pRdDat,uchar num2) uchar i;I2C_Start();I2C_SendByte(wrDAdr2); I2C_WaitAck();I2C_SendByte(wordAdr2); I2C_WaitAck(); I2C_Stop();I2C_Start();I2C_SendByte(rdDAdr2); I2C_WaitAck();for(i=0;inum2-1;i+)I2C_RcvByte(pRdDat+i); I2C_SendAck(); I2C_RcvByte(pRdDat+i); I2C_SendNoAck();I2C_Stop();/*-函数名称:SI5338A写入register map-*/void si5338_wrregmap(uchar addr,uchar reg,uint Reg_Num) uchar curr_val1; uchar clear_curr_val,clear_new_val,combined; / ignore registers with masks of 0x00 if(Reg_StoreReg_Num.Reg_Mask != 0x00) if(Reg_StoreReg_Num.Reg_Mask = 0xFF) / do a regular I2C write to the register / at addr with the desired data value I2C_Start(); /*Disable Outputs,set reg2304=1*/ I2C_SendByte(addr|0); I2C_WaitAck(); I2C_SendByte(reg); I2C_WaitAck(); I2C_SendByte(Reg_StoreReg_Num.Reg_Val); I2C_WaitAck(); I2C_Stop(); else / do a read-modify-write using I2C and / bit-wise operations / get the current value from the device at the / register located at addr I2C_RcvBytes(addr|0,reg,addr|1,curr_val,1); / clear the bits that are allowed to be / accessed in the current value of the register clear_curr_val = curr_val0 & ( Reg_Storereg.Reg_Mask); / clear the bits in the desired data that / are not allowed to be accessed clear_new_val = Reg_StoreReg_Num.Reg_Val & Reg_Storereg.Reg_Mask; / combine the cleared values to get the new / value to write to the desired register combined = clear_curr_val | clear_new_val; I2C_Start(); /*Disable Outputs,set reg2304=1*/ I2C_SendByte(addr|0); I2C_WaitAck(); I2C_SendByte(reg); I2C_WaitAck(); I2C_SendByte(combined); I2C_WaitAck(); I2C_Stop(); /*-函数名称:SI5338A写单寄存器-*/void si5338_wrmask(uchar addr,uchar reg,uchar mask,uchar dat) uchar curr_val1; uchar clear_curr_val; uchar clear_new_val; uchar combined; / ignore registers with masks of 0x00 if(mask != 0x00) if(mask = 0xFF) / do a regular I2C write to the register / at addr with the desired data value I2C_Start(); /*Disable Outputs,set reg2304=1*/ I2C_SendByte(addr|0); I2C_WaitAck(); I2C_SendByte(reg); I2C_WaitAck(); I2C_SendByte(dat); I2C_WaitAck(); I2C_Stop(); else / do a read-modify-write using I2C and / bit-wise operations / get the current value from the device at the / register located at addr I2C_RcvBytes(addr|0,reg,addr|1,curr_val,1); / clear the bits that are allowed to be / accessed in the current value of the register clear_curr_val = curr_val0 & ( mask); / clear the bits in the desired data that / are not allowed to be accessed clear_new_val = dat & mask; / combine the cleared values to get the new / value to write to the desired register combined = clear_curr_val | clear_new_val; I2C_Start(); /*Disable Outputs,set reg2304=1*/ I2C_SendByte(addr|0); I2C_WaitAck(); I2C_SendByte(reg); I2C_WaitAck(); I2C_SendByte(combined); I2C_WaitAck(); I2C_Stop(); /*-函数名称:配置第一片SI5338A-*/void si5338_config1() uint i; uchar reg11,reg21,reg31; uchar time1=255; _nop_(); /*Disable Outputs,set reg2304=1*/ I2C_Start(); I2C_SendByte(si5338_addr1|0); I2C_WaitAck(); I2C_SendByte(0xE6); I2C_WaitAck(); I2C_SendByte(0x1F); I2C_WaitAck(); I2C_Stop(); _nop_(); /*Pause LOL,set reg2417=1*/ I2C_Start(); I2C_SendByte(si5338_addr1|0); I2C_WaitAck(); I2C_SendByte(0xF1); I2C_WaitAck(); I2C_SendByte(0x85); I2C_WaitAck(); I2C_Stop();for(i=0;i349;i+) /*配置Register Map*/ si5338_wrregmap(si5338_addr1|0,Reg_Storei.Reg_Addr,i); _nop_(); /*Configure PLL for locking,set reg497=0*/ I2C_Start(); I2C_SendByte(si5338_addr1|0); I2C_WaitAck(); I2C_SendByte(0x31); I2C_WaitAck(); I2C_SendByte(0x10); I2C_WaitAck(); I2C_Stop(); _nop_(); /*Initiate Locking of PLL,set reg2461=1*/ I2C_Start(); I2C_SendByte(si5338_addr1|0); I2C_WaitAck(); I2C_SendByte(0xF6); I2C_WaitAck(); I2C_SendByte(0x02); I2C_WaitAck(); I2C_Stop(); _nop_(); /*Restart LOL,set reg2417=0*/ I2C_Start(); I2C_SendByte(si5338_addr1|0); I2C_WaitAck(); I2C_SendByte(0xF1); I2C_WaitAck(); I2C_SendByte(0x05); I2C_WaitAck(); I2C_Stop(); DELAY_10(2500);/*wait 25ms*/ I2C_Start(); /is PLL lockedI2C_SendByte(si5338_addr1|0); I2C_WaitAck();I2C_SendByte(0xDA); I2C_WaitAck(); I2C_Stop();I2C_Start();I2C_SendByte(si5338_addr1|1); I2C_WaitAck(); I2C_RcvByte(reg1); I2C_SendNoAck();I2C_Stop(); if(reg10&0x11) != 0x00) time1-;if(!time1)/超时值为255LOCK_PLL=FALSE; DELAY(); else LOCK_PLL=TRUE; _nop_(); /*copy 2371:0 to 471:0*/ I2C_RcvBytes(si5338_addr1|0,0xED,si5338_addr1|1,reg1,1); I2C_RcvBytes(si5338_addr1|0,0x2F,si5338_addr1|1,reg2,1); reg30=(reg10&0x03)|(reg20&0xFC); I2C_Start(); I2C_SendByte(si5338_addr1|0); I2C_WaitAck(); I2C_SendByte(0x2F); I2C_WaitAck(); I2C_SendByte(reg30); I2C_WaitAck(); I2C_Stop(); _nop_(); /*copy 2367:0 to 467:0*/ I2C_RcvBytes(si5338_addr1|0,0xEC,si5338_addr1|1,reg1,1); I2C_Start(); I2C_SendByte(si5338_addr1|0); I2C_WaitAck(); I2C_SendByte(0x2E); I2C_WaitAck(); I2C_SendByte(reg10); I2C_WaitAck(); I2C_Stop(); _nop_(); /*copy 2357:0 to 457:0*/ I2C_RcvBytes(si5338_addr1|0,0xEB,si5338_addr1|1,reg1,1); I2C_Start(); I2C_SendByte(si5338_addr1|0); I2C_WaitAck(); I2C_SendByte(0x2D); I2C_WaitAck(); I2C_SendByte(reg10); I2C_WaitAck(); I2C_Stop(); _nop_(); /*set 477:2 = 000101b*/ I2C_RcvBytes(si5338_addr1|0,0x2F,si5338_addr1|1,reg1,1); reg20=(reg10&0x03)|0x14; I2C_Start(); I2C_SendByte(si5338_addr1|0); I2C_WaitAck(); I2C_SendByte(0x2F); I2C_WaitAck(); I2C_SendByte(reg20); I2C_WaitAck(); I2C_Stop(); _nop_();
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年中考语文一轮复习:统编教材文言文背诵27篇 常考必背知识点汇编
- 医学生基础医学 骨质疏松护理课件
- Unit 3 Weather(单元测试提升卷)-2025人教精通版四年级英语上册
- Unit 2 重点词汇、词性转换、词义辨析及重难点句型梳理-人教版八年级英语上册
- 医学脑机接口诊疗环境场景切换案例教学课件
- 2026年深圳中考数学专项复习 解答中档题型:圆的计算与证明(原卷+解析版)
- 2026年人教版九年级数学上册复习:垂径定理的四类综合题型(压轴题专项训练)原卷版+解析
- TXJBX0097-2025农产品质量安全区块链追溯技术规范
- 第三方支付发展对我国商业银行业务的挑战分析-以支付宝对例
- 《JBT 6367-1992 保护拖链 型式尺寸》(2026年)实施指南
- 苏晋能源控股有限公司招聘笔试真题2024
- 2025安徽省转化医学科技有限公司社会招聘4人笔试考试参考题库附答案解析
- 高一英语语法综合复习资料包
- 2025年学前教育专升本真题汇编(含答案)
- 科研项目基础条件与保障材料撰写
- 水下混凝土浇筑导管水密试验方案
- 2025年法院遴选面试题及答案
- 化工防静电安全知识培训
- 物业保安服务承包合同协议范本
- 展会舞台搭建展览服务方案投标文件(技术标)
- 2025江苏徐州市泉山国有资产投资经营有限公司招聘笔试题库及答案详解
评论
0/150
提交评论