SI5338A - I2C - 配置程序
更新时间:2023-11-13 09:10:01 阅读量: 教育文库 文档下载
#include
#include \ //clockbuilder产生寄存器值头文件
#define uchar unsigned char #define uint unsigned int
#define TRUE 1 #define FALSE 0
sbit SCL=P2^6; sbit SDA=P2^1; 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; else SDA=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;i /*-------------------------------------------------------------------- 函数名称:读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;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_val[1]; uchar clear_curr_val,clear_new_val,combined; // ignore registers with masks of 0x00 if(Reg_Store[Reg_Num].Reg_Mask != 0x00) { if(Reg_Store[Reg_Num].Reg_Mask == 0xFF) { // do a regular I2C write to the register // at addr with the desired data value I2C_Start(); Outputs,set reg230[4]=1*/ I2C_SendByte(addr|0); I2C_WaitAck(); I2C_SendByte(reg); I2C_WaitAck(); I2C_SendByte(Reg_Store[Reg_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); /*Disable // clear the bits that are allowed to be // accessed in the current value of the register clear_curr_val = curr_val[0] & (~ Reg_Store[reg].Reg_Mask); // clear the bits in the desired data that // are not allowed to be accessed clear_new_val = Reg_Store[Reg_Num].Reg_Val & Reg_Store[reg].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 reg230[4]=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_val[1]; 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 reg230[4]=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_val[0] & (~ 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(); reg230[4]=1*/ I2C_SendByte(addr|0); /*Disable Outputs,set I2C_WaitAck(); I2C_SendByte(reg); I2C_WaitAck(); I2C_SendByte(combined); I2C_WaitAck(); I2C_Stop(); } } } /*-------------------------------------------------------------------- 函数名称:配置第一片SI5338A --------------------------------------------------------------------*/ void si5338_config1() { uint i; uchar reg1[1],reg2[1],reg3[1]; uchar time1=255; _nop_(); /*Disable Outputs,set reg230[4]=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 reg241[7]=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;i<349;i++) /*配置Register Map*/ { si5338_wrregmap(si5338_addr1|0,Reg_Store[i].Reg_Addr,i); } _nop_(); /*Configure PLL for locking,set reg49[7]=0*/ I2C_Start(); I2C_SendByte(si5338_addr1|0); I2C_WaitAck(); I2C_SendByte(0x31); I2C_WaitAck(); I2C_SendByte(0x10); I2C_WaitAck(); I2C_Stop(); _nop_(); PLL,set reg246[1]=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 reg241[7]=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); /*Initiate Locking of /*wait 25ms*/ I2C_Start(); //is PLL locked I2C_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((reg1[0]&0x11) != 0x00) { time1--; if(!time1) //超时值为255 { LOCK_PLL=FALSE; } DELAY(); } else { LOCK_PLL=TRUE; } _nop_(); /*copy 237[1:0] to 47[1:0]*/ I2C_RcvBytes(si5338_addr1|0,0xED,si5338_addr1|1,reg1,1); I2C_RcvBytes(si5338_addr1|0,0x2F,si5338_addr1|1,reg2,1); reg3[0]=(reg1[0]&0x03)|(reg2[0]&0xFC); I2C_Start(); I2C_SendByte(si5338_addr1|0); I2C_WaitAck(); I2C_SendByte(0x2F); I2C_WaitAck(); I2C_SendByte(reg3[0]); I2C_WaitAck(); I2C_Stop(); _nop_(); /*copy 236[7:0] to 46[7: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(reg1[0]); I2C_WaitAck(); I2C_Stop(); _nop_(); /*copy 235[7:0] to 45[7: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(reg1[0]); I2C_WaitAck(); I2C_Stop(); _nop_(); /*set 47[7:2] = 000101b*/ I2C_RcvBytes(si5338_addr1|0,0x2F,si5338_addr1|1,reg1,1); reg2[0]=(reg1[0]&0x03)|0x14; I2C_Start(); I2C_SendByte(si5338_addr1|0); I2C_WaitAck(); I2C_SendByte(0x2F); I2C_WaitAck(); I2C_SendByte(reg2[0]); I2C_WaitAck(); I2C_Stop(); _nop_(); /*Set PLL to use FCAL values,set reg49[7]=1*/ I2C_Start(); I2C_SendByte(si5338_addr1|0); I2C_WaitAck(); I2C_SendByte(0x31); I2C_WaitAck(); I2C_SendByte(0x90); I2C_WaitAck(); I2C_Stop(); _nop_(); /*Enable Outputs,set reg230[4]=0*/ I2C_Start(); I2C_SendByte(si5338_addr1|0); I2C_WaitAck(); I2C_SendByte(0xE6); I2C_WaitAck(); I2C_SendByte(0x00); I2C_WaitAck(); I2C_Stop(); } void main(void) { uchar data1[50]; DELAY_10(100); si5338_config1(); I2C_RcvBytes(si5338_addr1|0,0x00,si5338_addr1|1,data1,50); I2C_RcvBytes(si5338_addr1|0,0x32,si5338_addr1|1,data1,50); I2C_RcvBytes(si5338_addr1|0,0x64,si5338_addr1|1,data1,50); I2C_RcvBytes(si5338_addr1|0,0x96,si5338_addr1|1,data1,50); I2C_RcvBytes(si5338_addr1|0,0xC8,si5338_addr1|1,data1,50); DELAY(); DELAY(); DELAY(); }
正在阅读:
SI5338A - I2C - 配置程序11-13
“碧水一号”水面垃圾收集船设计 - 图文03-03
2.7(2)分数与小数的互化05-12
情感经典美文11-21
河北省石家庄市第一中学 高中生物必修一测试题附答案05-01
2021年免疫接种冷链管理停电应急预案05-16
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- 配置
- SI5338A
- 程序
- I2C
- 2019-2020年三年级数学下册 回顾整理教案 青岛版五年制
- 2010届高考复习5年高考3年联考精品物理题库:第五章 万有引力、天体运动
- 可能性2
- 粤港版吕祖灵签一到五十签
- 15秋天津大学工程造价计价与控制
- 北京市一零一中学2018届高三3月月考历史试题 Word版含答案 - 图文
- 直扩
- 2018国家公务员考试面试热点:“科学家不如明星”
- 蛋白质、多肽等大分子的质谱分析
- ARCGIS 空间分析 实习五说明
- 大学物理D复习题
- 飞翔(150字)作文
- GPS原理与应用 选择题
- 中空玻璃塑钢窗专项施工方案
- 应对风险和机遇所采取措施的有效性分析报告
- 优质稻谷标准
- 关于学习宣传黄福昌精神的材料
- 大学生校选课课堂问题行为研究综述1
- DIN EN 1559-1铸件-技术交付状态(中文版)
- 数据库实验3报告