英锐恩单片机论坛,Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流

 找回密码
 立即注册
搜索
电子烟方案单片机单片机开发深圳单片机开发
单片机方案国产单片机8位单片机电子烟方案开发
查看: 3545|回复: 0
打印 上一主题 下一主题

CC1020寄存器读写函数

[复制链接]
跳转到指定楼层
1#
发表于 2009-4-8 10:32:20 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
根据CHIPCON公司AN_025_source_code_1_2源码改编(模拟SPI部分)。
改进之处:1:把写函数的合并字节部分去掉。
2:优化代码。
3:读写操作完后把IO口及时设成输入,提高可靠性。
代码经过反复测试,运行良好,测试PASS!!
#define PDO         RA0
#define PDI         RA0
#define PCLK        RA7
#define PSEL        RA2
#define DIO         RA1
#define DCLK        RB0
#define SETDIO      TRISA1
#define SETPIO      TRISA0  

#define INPUT  1
#define OUTPUT 0
#define FALSE 0
#define TRUE  1
//本人用F628的接口部分。
/****************************************************************************/
/*  This routine writes to a single CC1020 register                         */
/****************************************************************************/
void WriteCC1020(char val){
char BitCounter;
for (BitCounter=8;BitCounter!=0;BitCounter--){
  PCLK=0;
  PDI=0;
  if(val&0x80)
   PDI=1;
  val<<=1;
  PCLK=1;
}
PCLK=0;
}

void WriteToCC1020Register(char Address, char data)
{
SETPIO=OUTPUT;
PSEL=0;
WriteCC1020((Address<<1)|0x01);       //写数最低位是1,Address最高位无用
WriteCC1020(data);
SETPIO=INPUT;
PSEL=1;
}
/****************************************************************************/
/*  This routine reads from a single CC1020 register                        */
/****************************************************************************/
char ReadFromCC1020Register(char Address)
{
char BitCounter;
char Byte;
SETPIO=OUTPUT;
PSEL=0;
// Send address bits
WriteCC1020(Address<<1);                //读数最低位是0,位移后最低位一定是0。
// Set up PDATA as an input
SETPIO=INPUT;
for (BitCounter=8;BitCounter!=0;BitCounter--){
  PCLK=1;
  Byte<<=1;
  if(PDO)
   Byte|=1;
  PCLK=0;
}
PSEL=1;
return Byte;
}
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|公司首页|Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流 ( 粤ICP备09008620号 )

GMT+8, 2024-5-13 17:27 , Processed in 0.050556 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表