




已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025自考专业(工商企业管理)考前冲刺练习题及参考答案详解【轻巧夺冠】
- 2025粮油食品检验人员考前冲刺练习试题及参考答案详解(突破训练)
- 2023年度计算机二级全真模拟模拟题(综合卷)附答案详解
- 2025机械设备制造修理人员高频难、易错点题(轻巧夺冠)附答案详解
- 2025年广东省化州市中考数学练习题(B卷)附答案详解
- 2025康复医学治疗技术副高级职称经典例题附完整答案详解【有一套】
- 2024-2025学年医师定期考核真题及参考答案详解【基础题】
- 2024年事业单位招聘每日一练试卷带答案详解(能力提升)
- 苏州大学科研助理岗位招聘笔试备考题库参考答案详解
- 业务流程优化方案设计模板工作效率提升版
- 宝钢工程RH精炼炉设备与工艺技术介绍
- 采购报告范文
- 某县某年度高标准基本农田建设项目复核报告
- 医学教材 围术期过敏反应
- 【MOOC】管理会计学-西南财经大学 中国大学慕课MOOC答案
- 现代辅助生殖技术护理伦理
- 体育设施建设造价评估方案
- 风力发电运维值班员(高级工)理论考试题库(浓缩400题)
- 人美版美术七年级上册第一单元《第2课 品篆刻之美》课件
- 宪法培训课件教学课件
- 华为全球培训中心
评论
0/150
提交评论