#include<msp430x14x.h>
#define INT8U unsigned char
#define INT16U unsigned int
#define WRITE_BURST 0x40 //连续写入
#define BYTES_IN_RXFIFO 0x7F //接收缓冲区的有效字节数
#define CRC_OK 0x80 //CRC校验通过位标志
//无线模块引脚定义
#define GDO0 P3DIR &= ~BIT1 //GDO0
#define MISO P3DIR &= ~BIT3 //MISO
#define SCK1 P3OUT |= BIT4; //SCK=1
#define SCK0 P3OUT &= ~BIT4; //SCK=0
#define MOSI1 P3OUT |= BIT5; //MOSI=1
#define MOSI0 P3OUT &= ~BIT5; //MOSI=0
//18B20引脚定义
#define DQ1 P1OUT |= BIT6
#define DQ0 P1OUT &= ~BIT6
#define DQ_in P1DIR &= ~BIT6
#define DQ_out P1DIR |= BIT6
#define DQ_val (P1IN & BIT6)
//1602液晶引脚定义
#define DataDir P4DIR
#define DataPort P4OUT
#define Busy 0x80
#define CtrlDir P5DIR
#define CLR_RS P5OUT&=~BIT0; //RS = P5.0
#define SET_RS P5OUT|=BIT0;
#define CLR_RW P5OUT&=~BIT1; //RW = P5.1
#define SET_RW P5OUT|=BIT1;
#define CLR_EN P5OUT&=~BIT2; //EN = P5.2
#define SET_EN P5OUT|=BIT2;
#define dl P3OUT &=~ BIT7; //灯亮
#define dm P3OUT |= BIT7; //灯灭
#define Num_of_Results 32
INT8U RxBuf[8]={0};
INT8U TxBuf[8]={1,2,3,4,5,6,7,8}; // 8字节, 如果需要更长的数据包,请正确设置
INT8U leng =0;
INT8U a,b,ptr[4];
INT8U dN[10]={0,1,2,3,4,5,6,7,8,9};
INT8U shuzi[] = {"0123456789."};
INT8U scandata[10] = {0xc0,0xcf,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
//*****************************************************************************************
//INT8U PaTabel[8] = {0x60 ,0x60 ,0x60 ,0x60 ,0x60 ,0x60 ,0x60 ,0x60};
INT8U PaTabel[8] = {0xc0 ,0xc0 ,0xc0 ,0xc0 ,0xc0 ,0xc0 ,0xc0 ,0xc0};//修改发射功率
//*****************************************************************************************
void SpiInit(void);
void CpuInit(void);
void RESET_CC1100(void);
void POWER_UP_RESET_CC1100(void);
void halSpiWriteReg(INT8U addr, INT8U value);
void halSpiWriteBurstReg(INT8U addr, INT8U *buffer, INT8U count);
void halSpiStrobe(INT8U strobe);
INT8U halSpiReadReg(INT8U addr);
void halSpiReadBurstReg(INT8U addr, INT8U *buffer, INT8U count);
INT8U halSpiReadStatus(INT8U addr);
void halRfWriteRfSettings(void);
void halRfSendPacket(INT8U *txBuffer, INT8U size);
INT8U halRfReceivePacket(INT8U *rxBuffer, INT8U *length);
//*****************************************************************************************
// CC1100 STROBE, CONTROL AND STATUS REGSITER
#define CCxxx0_IOCFG2 0x00 // GDO2 output pin configuration
#define CCxxx0_IOCFG1 0x01 // GDO1 output pin configuration
#define CCxxx0_IOCFG0 0x02 // GDO0 output pin configuration
#define CCxxx0_FIFOTHR 0x03 // RX FIFO and TX FIFO thresholds
#define CCxxx0_SYNC1 0x04 // Sync word, high INT8U
#define CCxxx0_SYNC0 0x05 // Sync word, low INT8U
#define CCxxx0_PKTLEN 0x06 // Packet length
#define CCxxx0_PKTCTRL1 0x07 // Packet automation control
#define CCxxx0_PKTCTRL0 0x08 // Packet automation control
#define CCxxx0_ADDR 0x09 // Device address
#define CCxxx0_CHANNR 0x0A // Channel number
#define CCxxx0_FSCTRL1 0x0B // Frequency synthesizer control
#define CCxxx0_FSCTRL0 0x0C // Frequency synthesizer control
#define CCxxx0_FREQ2 0x0D // Frequency control word, high INT8U
#define CCxxx0_FREQ1 0x0E // Frequency control word, middle INT8U
#define CCxxx0_FREQ0 0x0F // Frequency control word, low INT8U
#define CCxxx0_MDMCFG4 0x10 // Modem configuration
#define CCxxx0_MDMCFG3 0x11 // Modem configuration
#define CCxxx0_MDMCFG2 0x12 // Modem configuration
#define CCxxx0_RCCTRL1 0x27 // RC oscillator configuration
#define CCxxx0_RCCTRL0 0x28 // RC oscillator configuration
#define CCxxx0_FSTEST 0x29 // Frequency synthesizer calibration control
#define CCxxx0_PTEST 0x2A // Production test
#define CCxxx0_AGCTEST 0x2B // AGC test
#define CCxxx0_TEST2 0x2C // Various test settings
#define CCxxx0_TEST1 0x2D // Various test settings
#define CCxxx0_TEST0 0x2E // Various test settings
// Strobe commands
#define CCxxx0_SRES 0x30 // Reset chip.
#define CCxxx0_SFSTXON 0x31 // Enable and calibrate frequency synthesizer (if MCSM0.FS_AUTOCAL=1).
// If in RX/TX: Go to a wait state where only the synthesizer is
// running (for quick RX / TX turnaround).
#define CCxxx0_SXOFF 0x32 // Turn off crystal oscillator.
#define CCxxx0_SCAL 0x33 // Calibrate frequency synthesizer and turn it off
// (enables quick start).
#define CCxxx0_SRX 0x34 // Enable RX. Perform calibration first if coming from IDLE and
// MCSM0.FS_AUTOCAL=1.
#define CCxxx0_STX 0x35 // In IDLE state: Enable TX. Perform calibration first if
// MCSM0.FS_AUTOCAL=1. If in RX state and CCA is enabled:
// Only go to TX if channel is clear.
#define CCxxx0_SIDLE 0x36 // Exit RX / TX, turn off frequency synthesizer and exit
// Wake-On-Radio mode if applicable.
#define CCxxx0_SAFC 0x37 // Perform AFC adjustment of the frequency synthesizer
#define CCxxx0_SWOR 0x38 // Start automatic RX polling sequence (Wake-on-Radio)
#define CCxxx0_SPWD 0x39 // Enter power down mode when CSn goes high.
#define CCxxx0_SFRX 0x3A // Flush the RX FIFO buffer.
#define CCxxx0_SFTX 0x3B // Flush the TX FIFO buffer.
#define CCxxx0_SWORRST 0x3C // Reset real time clock.
#define CCxxx0_SNOP 0x3D // No operation. May be used to pad strobe commands to two
// INT8Us for simpler software.
#define CCxxx0_PARTNUM 0x30
#define CCxxx0_VERSION 0x31
#define CCxxx0_FREQEST 0x32
#define CCxxx0_LQI 0x33
#define CCxxx0_RSSI 0x34
#define CCxxx0_MARCSTATE 0x35
#define CCxxx0_WORTIME1 0x36
#define CCxxx0_WORTIME0 0x37
#define CCxxx0_PKTSTATUS 0x38
#define CCxxx0_VCO_VC_DAC 0x39
#define CCxxx0_TXBYTES 0x3A
#define CCxxx0_RXBYTES 0x3B
#define CCxxx0_PATABLE 0x3E
#define CCxxx0_TXFIFO 0x3F
#define CCxxx0_RXFIFO 0x3F
// RF_SETTINGS is a data structure which contains all relevant CCxxx0 registers
typedef struct S_RF_SETTINGS
{
INT8U FSCTRL2; //自已加的
INT8U FSCTRL1; // Frequency synthesizer control.
INT8U FSCTRL0; // Frequency synthesizer control.
INT8U FREQ2; // Frequency control word, high INT8U.
INT8U FREQ1; // Frequency control word, middle INT8U.
INT8U FREQ0; // Frequency control word, low INT8U.
INT8U MDMCFG4; // Modem configuration.
INT8U MDMCFG3; // Modem configuration.
INT8U MDMCFG2; // Modem configuration.
INT8U MDMCFG1; // Modem configuration.
INT8U MDMCFG0; // Modem configuration.
INT8U CHANNR; // Channel number.
INT8U DEVIATN; // Modem deviation setting (when FSK modulation is enabled).
INT8U FREND1; // Front end RX configuration.
INT8U FREND0; // Front end RX configuration.
INT8U MCSM0; // Main Radio Control State Machine configuration.
INT8U FOCCFG; // Frequency Offset Compensation Configuration.
INT8U BSCFG; // Bit synchronization Configuration.
INT8U AGCCTRL2; // AGC control.
INT8U AGCCTRL1; // AGC control.
INT8U AGCCTRL0; // AGC control.
INT8U FSCAL3; // Frequency synthesizer calibration.
INT8U FSCAL2; // Frequency synthesizer calibration.
INT8U FSCAL1; // Frequency synthesizer calibration.
INT8U FSCAL0; // Frequency synthesizer calibration.
INT8U FSTEST; // Frequency synthesizer calibration control
INT8U TEST2; // Various test settings.
INT8U TEST1; // Various test settings.
INT8U TEST0; // Various test settings.
INT8U IOCFG2; // GDO2 output pin configuration
INT8U IOCFG0; // GDO0 output pin configuration
INT8U PKTCTRL1; // Packet automation control.
INT8U PKTCTRL0; // Packet automation control.
INT8U ADDR; // Device address.
INT8U PKTLEN; // Packet length.
} RF_SETTINGS;
/////////////////////////////////////////////////////////////////
const RF_SETTINGS rfSettings =
{
0x00,
0x08, // FSCTRL1 Frequency synthesizer control.
0x00, // FSCTRL0 Frequency synthesizer control.
0x10, // FREQ2 Frequency control word, high byte.
0xA7, // FREQ1 Frequency control word, middle byte.
0x62, // FREQ0 Frequency control word, low byte.
0x5B, // MDMCFG4 Modem configuration.
//0xf6, // MDMCFG4 chang by allen
0xF8, // MDMCFG3 Modem configuration.
//0x83, // MDMCFG3 chang by allen data rate = 2.398K
0x03, // MDMCFG2 Modem configuration.
0x22, // MDMCFG1 Modem configuration.
0xF8, // MDMCFG0 Modem configuration.
0x00, // CHANNR Channel number.
0x47, // DEVIATN Modem deviation setting (when FSK modulation is enabled).
0xB6, // FREND1 Front end RX configuration.
0x10, // FREND0 Front end RX configuration.
0x18, // MCSM0 Main Radio Control State Machine configuration.
0x1D, // FOCCFG Frequency Offset Compensation Configuration.
0x1C, // BSCFG Bit synchronization Configuration.
0xC7, // AGCCTRL2 AGC control.
0x00, // AGCCTRL1 AGC control.
0xB2, // AGCCTRL0 AGC control.
0xEA, // FSCAL3 Frequency synthesizer calibration.
0x2A, // FSCAL2 Frequency synthesizer calibration.
0x00, // FSCAL1 Frequency synthesizer calibration.
0x11, // FSCAL0 Frequency synthesizer calibration.
0x59, // FSTEST Frequency synthesizer calibration.
0x81, // TEST2 Various test settings.
0x35, // TEST1 Various test settings.
0x09, // TEST0 Various test settings.
0x0B, // IOCFG2 GDO2 output pin configuration.
0x06, // IOCFG0D GDO0 output pin configuration. Refer to SmartRF?Studio User Manual for detailed pseudo register explanation.
0x04, // PKTCTRL1 Packet automation control.
//0x05, // PKTCTRL0 Packet automation control.
0x01, //PKTCTRL0 crc disable chang by allen at 09.12.24
0x00, // ADDR Device address.
0x0c // PKTLEN Packet length.
};
void Disp_Numb();
//操作18B20温度传感器
/*******************************************
函数名称:DelayNus
功 能:实现N个微秒的延时
参 数:n--延时长度
返回值 :无
说明 :定时器A的计数时钟是1MHz,CPU主频8MHz
所以通过定时器延时能够得到极为精确的
us级延时
********************************************/
void DelayNus(INT16U n)
{
CCR0 = n;
TACTL |= MC_1; //增计数到CCR0
while(!(TACTL & BIT0)); //等待
TACTL &= ~MC_1; //停止计数
TACTL &= ~BIT0; //清除中断标志
}
/*******************************************
函数名称:Init_18B20
功 能:对DS18B20进行复位操作
参 数:无
返回值 :初始化状态标志:1--失败,0--成功
********************************************/
INT8U Init_18B20(void)
{
INT8U Error;
DQ_out;
_DINT();
DQ0;
DelayNus(500);
DQ1;
DelayNus(55);
DQ_in;
_NOP();
if(DQ_val)
{
Error = 1; //初始化失败
}
else
{
Error = 0; //初始化成功
}
DQ_out;
DQ1;
_EINT();
DelayNus(400);
return Error;
}
/*******************************************
函数名称:Write_18B20
功 能:向DS18B20写入一个字节的数据
参 数:wdata--写入的数据
返回值 :无
********************************************/
void Write_18B20(INT8U wdata)
{
INT8U i;
_DINT();
for(i = 0; i < 8;i++)
{
DQ0;
DelayNus(6); //延时6us
if(wdata & 0X01) DQ1;
else DQ0;
wdata >>= 1;
DelayNus(50); //延时50us
DQ1;
DelayNus(10); //延时10us
}
_EINT();
}
/*******************************************
函数名称:Read_18B20
功 能:从DS18B20读取一个字节的数据
参 数:无
返回值 :读出的一个字节数据
********************************************/
INT8U Read_18B20(void)
{
INT8U i;
INT8U temp = 0;
_DINT();
for(i = 0;i < 8;i++)
{
temp >>= 1;
DQ0;
DelayNus(6); //延时6us
DQ1;
DelayNus(8); //延时9us
DQ_in;
_NOP();
if(DQ_val) temp |= 0x80;
DelayNus(45); //延时45us
DQ_out;
DQ1;
DelayNus(10); //延时10us
}
_EINT();
return temp;
}
/*******************************************
函数名称:Skip
功 能:发送跳过读取产品ID号命令
参 数:无
返回值 :无
********************************************/
void Skip(void)
{
Write_18B20(0xcc);
}
/*******************************************
函数名称:Convert
功 能:发送温度转换命令
参 数:无
返回值 :无
********************************************/
void Convert(void)
{
Write_18B20(0x44);
}
/*******************************************
函数名称:Read_SP
功 能:发送读ScratchPad命令
参 数:无
返回值 :无
********************************************/
void Read_SP(void)
{
Write_18B20(0xbe);
}
/*******************************************
函数名称:ReadTemp
功 能:从DS18B20的ScratchPad读取温度转换结果
参 数:无
返回值 :读取的温度数值
********************************************/
INT16U ReadTemp(void)
{
INT8U temp_low;
INT16U temp;
temp_low = Read_18B20(); //读低位
temp = Read_18B20(); //读高位
temp = (temp<<8) | temp_low;
return temp;
}
/*******************************************
函数名称:ReadTemp
功 能:控制DS18B20完成一次温度转换
参 数:无
返回值 :测量的温度数值
********************************************/
INT16U Do1Convert(void)
{
INT8U i;
do
{
i = Init_18B20();
}
while(i);
Skip();
Convert();
for(i = 20;i > 0;i--)
DelayNus(60000); //延时800ms以上
do
{
i = Init_18B20();
}
while(i);
Skip();
Read_SP();
return ReadTemp();
}
//写1602液晶
/***********************************************
函数名称:DispStr
功 能:让液晶从某个位置起连续显示一个字符串
参 数:x--位置的列坐标
y--位置的行坐标
ptr--指向字符串存放位置的指针
返回值 :无
***********************************************/
void Disp1Char(INT8U x,INT8U y,INT8U data);
void DispStr(INT8U x,INT8U y,INT8U *ptr)
{
INT8U *temp;
INT8U i,n = 0;
temp = ptr;
while(*ptr++ != '\0') n++; //计算字符串有效字符的个数
for (i=0;i<n;i++)
{
Disp1Char(x++,y,temp[i]);
if (x == 0x0f)
{
x = 0;
y ^= 1;
}
}
}
/*******************************************
函数名称:DispNchar
功 能:让液晶从某个位置起连续显示N个字符
参 数:x--位置的列坐标
y--位置的行坐标
n--字符个数
ptr--指向字符存放位置的指针
返回值 :无
********************************************/
void Disp1Char(INT8U x,INT8U y,INT8U data);
void DispNChar(INT8U x,INT8U y, INT8U n,INT8U *ptr)
{
INT8U i;
for (i=0;i<n;i++)
{
Disp1Char(x++,y,ptr[i]);
if (x == 0x0f)
{
x = 0;
y ^= 1;
}
}
}
/*******************************************
函数名称:LocateXY
功 能:向液晶输入显示字符位置的坐标信息
参 数:x--位置的列坐标
y--位置的行坐标
返回值 :无
********************************************/
void LcdWriteCommand(INT8U cmd,INT8U chk);
void LocateXY(INT8U x,INT8U y)
{
INT8U temp;
temp = x&0x0f;
y &= 0x01;
if(y) temp |= 0x40; //如果在第2行
temp |= 0x80;
LcdWriteCommand(temp,1);
}
/*******************************************
函数名称:Disp1Char
功 能:在某个位置显示一个字符
参 数:x--位置的列坐标
y--位置的行坐标
data--显示的字符数据
返回值 :无
********************************************/
void LcdWriteData( INT8U data );
void Disp1Char(INT8U x,INT8U y,INT8U data)
{
LocateXY( x, y );
LcdWriteData( data );
}
/*******************************************
函数名称:LcdReset
功 能:对1602液晶模块进行复位操作
参 数:无
返回值 :无
********************************************/
void delay_5ms(void);
void LcdWriteCommand(INT8U cmd,INT8U chk);
void LcdReset(void)
{
CtrlDir |= 0x07; //控制线端口设为输出状态
DataDir = 0xFF; //数据端口设为输出状态
LcdWriteCommand(0x38, 0); //规定的复位操作
delay_5ms();
LcdWriteCommand(0x38, 0);
delay_5ms();
LcdWriteCommand(0x38, 0);
delay_5ms();
LcdWriteCommand(0x38, 1); //显示模式设置
LcdWriteCommand(0x08, 1); //显示关闭
LcdWriteCommand(0x01, 1); //显示清屏
LcdWriteCommand(0x06, 1); //写字符时整体不移动
LcdWriteCommand(0x0c, 1); //显示开,不开游标,不闪烁
}
/*******************************************
函数名称:LcdWriteCommand
功 能:向液晶模块写入命令
参 数:cmd--命令,
chk--是否判忙的标志,1:判忙,0:不判
返回值 :无
********************************************/
void WaitForEnable(void);
void LcdWriteCommand(INT8U cmd,INT8U chk)
{
if (chk) WaitForEnable(); // 检测忙信号?
CLR_RS;
CLR_RW;
_NOP();
DataPort = cmd; //将命令字写入数据端口
_NOP();
SET_EN; //产生使能脉冲信号
_NOP();
_NOP();
CLR_EN;
}
/*******************************************
函数名称:LcdWriteData
功 能:向液晶显示的当前地址写入显示数据
参 数:data--显示字符数据
返回值 :无
********************************************/
void WaitForEnable(void);
void LcdWriteData( INT8U data )
{
WaitForEnable(); //等待液晶不忙
SET_RS;
CLR_RW;
_NOP();
DataPort = data; //将显示数据写入数据端口
_NOP();
SET_EN; //产生使能脉冲信号
_NOP();
_NOP();
CLR_EN;
}
/*******************************************
函数名称:WaitForEnable
功 能:等待1602液晶完成内部操作
参 数:无
返回值 :无
********************************************/
void WaitForEnable(void)
{
P4DIR &= 0x00; //将P4口切换为输入状态
CLR_RS;
SET_RW;
_NOP();
SET_EN;
_NOP();
_NOP();
while((P4IN & Busy)!=0); //检测忙标志
CLR_EN;
P4DIR |= 0xFF; //将P4口切换为输出状态
}
/*******************************************
函数名称:Delay5ms
功 能:延时约5ms
参 数:无
返回值 :无
********************************************/
void delay_5ms(void)
{
INT16U i=40000;
while (i != 0)
{
i--;
}
}
//操作无线模块
//*****************************************************************************************
//函数名:delay(unsigned int s)
//输入:时间
//输出:无
//功能描述:普通廷时,内部用
//*****************************************************************************************
static void delay(unsigned int s)
{
unsigned int i;
for(i=0; i<s; i++);
for(i=0; i<s; i++);
}
void yanshi(INT16U z)//延时10us
{
INT16U x,y;
for(x=z;x>0;x--)
for(y=1;y>0;y--);
}
void yanshi1(INT16U z)//延时13us
{
INT16U x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void halWait(INT16U timeout)
{
do {
yanshi(1);
} while (--timeout);
}
void SpiInit(void)
{
CSN0;//CSN=0
SCK0;//SCK=0
CSN1;//CSN=1
}
/*****************************************************************************************
//函数名:CpuInit()
//输入:无
//输出:无
//功能描述:SPI初始化程序
****************************************************************************************/
void CpuInit(void)
{
SpiInit();
delay(1000);
}
//*****************************************************************************************
//函数名:SpisendByte(INT8U dat)
//输入:发送的数据
//输出:无
//功能描述:SPI发送一个字节
//*****************************************************************************************
INT8U SpiTxRxByte(INT8U dat)
{
INT8U i,temp;
temp = 0;
SCK0;//SCK = 0
for(i=0; i<8; i++)
{
if(dat & 0x80)
{
MOSI1;//MOSI = 1
}
else MOSI0;//MOSI = 0
dat <<= 1;
SCK1; //SCK = 1
yanshi(1);
temp <<= 1;
MISO;
if((P3IN & BIT3)==BIT3/*MISO*/)temp++;
SCK0;//SCK = 0
yanshi(1);
}
return temp;
}
//*****************************************************************************************
//函数名:void RESET_CC1100(void)
//输入:无
//输出:无
//功能描述:复位CC1100
//*****************************************************************************************
void RESET_CC1100(void)
{
CSN0; //CSN = 0
MISO;
while ((P3IN & BIT3)==BIT3/*MISO*/);
SpiTxRxByte(CCxxx0_SRES); //写入复位命令
MISO;
while ((P3IN & BIT3)==BIT3/*MISO*/);
CSN1;//CSN = 1
}
//*****************************************************************************************
//函数名:void POWER_UP_RESET_CC1100(void)
//输入:无
//输出:无
//功能描述:上电复位CC1100
//*****************************************************************************************
void POWER_UP_RESET_CC1100(void)
{
CSN1; //CSN = 1
halWait(1);
CSN0; //CSN = 0
halWait(1);
CSN1; //CSN = 1
halWait(10);
RESET_CC1100(); //复位CC1100
}
//*****************************************************************************************
//函数名:void halSpiWriteReg(INT8U addr, INT8U value)
//输入:地址和配置字
//输出:无
//功能描述:SPI写寄存器
//*****************************************************************************************
void halSpiWriteReg(INT8U addr, INT8U value)
{
CSN0; //CSN = 0
MISO;
while ((P3IN & BIT3)==BIT3/*MISO*/);
SpiTxRxByte(addr); //写地址
SpiTxRxByte(value); //写入配置
CSN1; //CSN = 1
}
//*****************************************************************************************
//函数名:void halSpiWriteBurstReg(INT8U addr, INT8U *buffer, INT8U count)
//输入:地址,写入缓冲区,写入个数
//输出:无
//功能描述:SPI连续写配置寄存器
//*****************************************************************************************
void halSpiWriteBurstReg(INT8U addr, INT8U *buffer, INT8U count)
{
INT8U i, temp;
temp = addr | WRITE_BURST;
CSN0; //CSN = 0
MISO;
while ((P3IN & BIT3)==BIT3/*MISO*/);
SpiTxRxByte(temp);
for (i = 0; i < count; i++)
{
SpiTxRxByte(buffer[i]);
}
CSN1; //CSN = 1
}
//*****************************************************************************************
//函数名:void halSpiStrobe(INT8U strobe)
//输入:命令
//输出:无
//功能描述:SPI写命令
//*****************************************************************************************
void halSpiStrobe(INT8U strobe)
{
CSN0; //CSN = 0
MISO;
while ((P3IN & BIT3)==BIT3/*MISO*/);
SpiTxRxByte(strobe); //写入命令
CSN1; //CSN = 1
}
//*****************************************************************************************
//函数名:INT8U halSpiReadReg(INT8U addr)
//输入:地址
//输出:该寄存器的配置字
//功能描述:SPI读寄存器
//*****************************************************************************************
INT8U halSpiReadReg(INT8U addr)
{
INT8U temp, value;
temp = addr|READ_SINGLE;//读寄存器命令
CSN0; //CSN = 0
MISO;
while ((P3IN & BIT3)==BIT3/*MISO*/);
SpiTxRxByte(temp);
value = SpiTxRxByte(0);
CSN1; //CSN = 1
return value;
}
//*****************************************************************************************
//函数名:void halSpiReadBurstReg(INT8U addr, INT8U *buffer, INT8U count)
//输入:地址,读出数据后暂存的缓冲区,读出配置个数
//输出:无
//功能描述:SPI连续写配置寄存器
//*****************************************************************************************
void halSpiReadBurstReg(INT8U addr, INT8U *buffer, INT8U count)
{
INT8U i,temp;
temp = addr | READ_BURST; //写入要读的配置寄存器地址和读命令
CSN0; //CSN = 0
MISO;
while ((P3IN & BIT3)==BIT3/*MISO*/);
SpiTxRxByte(temp);
for (i = 0; i < count; i++)
{
buffer[i] = SpiTxRxByte(0);
}
CSN1; //CSN = 1
}
//*****************************************************************************************
//函数名:INT8U halSpiReadReg(INT8U addr)
//输入:地址
//输出:该状态寄存器当前值
//功能描述:SPI读状态寄存器
//*****************************************************************************************
INT8U halSpiReadStatus(INT8U addr)
{
INT8U value,temp;
temp = addr | READ_BURST; //写入要读的状态寄存器的地址同时写入读命令
CSN0; //CSN = 0
MISO;
while ((P3IN & BIT3)==BIT3/*MISO*/);
SpiTxRxByte(temp);
value = SpiTxRxByte(0);
CSN1; //CSN = 1
return value;
}
//*****************************************************************************************
//函数名:void halRfWriteRfSettings(RF_SETTINGS *pRfSettings)
//输入:无
//输出:无
//功能描述:配置CC1100的寄存器
//*****************************************************************************************
void halRfWriteRfSettings(void)
{
halSpiWriteReg(CCxxx0_FSCTRL0, rfSettings.FSCTRL2);//自已加的
// Write register settings
halSpiWriteReg(CCxxx0_FSCTRL1, rfSettings.FSCTRL1);
halSpiWriteReg(CCxxx0_FSCTRL0, rfSettings.FSCTRL0);
halSpiWriteReg(CCxxx0_FREQ2, rfSettings.FREQ2);
halSpiWriteReg(CCxxx0_FREQ1, rfSettings.FREQ1);
halSpiWriteReg(CCxxx0_FREQ0, rfSettings.FREQ0);
halSpiWriteReg(CCxxx0_MDMCFG4, rfSettings.MDMCFG4);
halSpiWriteReg(CCxxx0_MDMCFG3, rfSettings.MDMCFG3);
halSpiWriteReg(CCxxx0_MDMCFG2, rfSettings.MDMCFG2);
halSpiWriteReg(CCxxx0_MDMCFG1, rfSettings.MDMCFG1);
halSpiWriteReg(CCxxx0_MDMCFG0, rfSettings.MDMCFG0);
halSpiWriteReg(CCxxx0_CHANNR, rfSettings.CHANNR);
halSpiWriteReg(CCxxx0_DEVIATN, rfSettings.DEVIATN);
halSpiWriteReg(CCxxx0_FREND1, rfSettings.FREND1);
halSpiWriteReg(CCxxx0_FREND0, rfSettings.FREND0);
halSpiWriteReg(CCxxx0_MCSM0 , rfSettings.MCSM0 );
halSpiWriteReg(CCxxx0_FOCCFG, rfSettings.FOCCFG);
halSpiWriteReg(CCxxx0_BSCFG, rfSettings.BSCFG);
halSpiWriteReg(CCxxx0_AGCCTRL2, rfSettings.AGCCTRL2);
halSpiWriteReg(CCxxx0_AGCCTRL1, rfSettings.AGCCTRL1);
halSpiWriteReg(CCxxx0_AGCCTRL0, rfSettings.AGCCTRL0);
halSpiWriteReg(CCxxx0_FSCAL3, rfSettings.FSCAL3);
halSpiWriteReg(CCxxx0_FSCAL2, rfSettings.FSCAL2);
halSpiWriteReg(CCxxx0_FSCAL1, rfSettings.FSCAL1);
halSpiWriteReg(CCxxx0_FSCAL0, rfSettings.FSCAL0);
halSpiWriteReg(CCxxx0_FSTEST, rfSettings.FSTEST);
halSpiWriteReg(CCxxx0_TEST2, rfSettings.TEST2);
halSpiWriteReg(CCxxx0_TEST1, rfSettings.TEST1);
halSpiWriteReg(CCxxx0_TEST0, rfSettings.TEST0);
halSpiWriteReg(CCxxx0_IOCFG2, rfSettings.IOCFG2);
halSpiWriteReg(CCxxx0_IOCFG0, rfSettings.IOCFG0);
halSpiWriteReg(CCxxx0_PKTCTRL1, rfSettings.PKTCTRL1);
halSpiWriteReg(CCxxx0_PKTCTRL0, rfSettings.PKTCTRL0);
halSpiWriteReg(CCxxx0_ADDR, rfSettings.ADDR);
halSpiWriteReg(CCxxx0_PKTLEN, rfSettings.PKTLEN);
}
//*****************************************************************************************
//函数名:void halRfSendPacket(INT8U *txBuffer, INT8U size)
//输入:发送的缓冲区,发送数据个数
//输出:无
//功能描述:CC1100发送一组数据
//*****************************************************************************************
void halRfSendPacket(INT8U *txBuffer, INT8U size)
{
halSpiWriteReg(CCxxx0_TXFIFO, size);
halSpiWriteBurstReg(CCxxx0_TXFIFO, txBuffer, size); //写入要发送的数据
halSpiStrobe(CCxxx0_STX); //进入发送模式发送数据
GDO0;
while ((P3IN & BIT1)!=BIT1/*!GDO0*/);
while ((P3IN & BIT1)==BIT1/*GDO0*/);
halSpiStrobe(CCxxx0_SFTX);
delay(5);
}
void setRxMode(void)
{
halSpiStrobe(CCxxx0_SRX); //进入接收状态
}
INT8U halRfReceivePacket(INT8U *rxBuffer, INT8U *length)
{
INT8U packetLength;
INT8U i=(*length)*4; // 具体多少要根据datarate和length来决定
halSpiStrobe(CCxxx0_SRX); //进入接收状态
delay(1);
GDO0;
while ((P3IN & BIT1)==BIT1/*GDO0*/)
{
delay(1);
--i;
if(i<1)
return 0;
}
if ((halSpiReadStatus(CCxxx0_RXBYTES) & BYTES_IN_RXFIFO)) //如果接的字节数不为0
{
packetLength = halSpiReadReg(CCxxx0_RXFIFO);//读出第一个字节,此字节为该帧数据长度
if(packetLength == 0x08)
{
halSpiReadBurstReg(CCxxx0_RXFIFO, rxBuffer, 8); //读出所有接收到的数据
*length = packetLength; //把接收数据长度的修改为当前数据的长度
halSpiStrobe(CCxxx0_SFRX); //清洗接收缓冲区
return 1;
}
else
{
*length = packetLength;
halSpiStrobe(CCxxx0_SFRX); //清洗接收缓冲区
return 0;
}
}
return 0;
}
void main(void)
{
WDTCTL = WDTPW + WDTHOLD;
//进入程序之前先关闭所有的I/O口置0
P1DIR = 0XFF;P1OUT = 0Xff;
P2DIR = 0XFF;P2OUT = 0X00;
P3DIR = 0XFF;P3OUT = 0X00;
P4DIR = 0XFF;P4OUT = 0X00;
P5DIR = 0XFF;P5OUT = 0X00;
P6DIR = 0XFF;P6OUT = 0X00;
BCSCTL1 &= ~XT2OFF;//打开XT2高频晶体振荡器
do
{
INT8U a;
IFG1 &= ~OFIFG;//清除晶振失败标志
for (a = 0xFF; a > 0; a--);//等待外部晶体起振
}
while ((IFG1 & OFIFG));//检测外部晶振失效标志位是否清除
BCSCTL2 |=SELM_2+SELS;//MCLK选择外部高频晶振
TACTL = TASSEL_2+ID_3+TACLR;
//TBCCTL0 = CCIE;
//TBCCR0 = 50000;
//TBCTL = TBSSEL_1+MC_1+TBCLR;
//INT8U key1_flag = 0;
//unsigned int key2_flag = 0;
//unsigned int key1_scan_cnt = 400;
//unsigned int key2_scan_cnt = 300;
//INT8U i = 0;
//INT8U leng =0;
//INT8U tf =0;
CpuInit();
POWER_UP_RESET_CC1100();
halRfWriteRfSettings();
halSpiWriteBurstReg(CCxxx0_PATABLE, PaTabel, 8);
//halSpiStrobe(CCxxx0_SRX); //进入接收状态
//setRxMode();
//h=RxBuf[5];
LcdReset();
DispStr(0,0,"N0W T :");
DispStr(0,1,"NOW V :");
Disp1Char(8,0,0+0x30);
Disp1Char(7,0,0+0x30);
Disp1Char(9,0,0x2e);
Disp1Char(10,0,0+0x30);
//Disp1Char(7,0,9+0x30);
//Disp1Char(7,0,2+0x30);
//_EINT(); //打开全局中断控制位
INT8U i;
while(1)
{
//t=TxBuf[0];
//halRfSendPacket(TxBuf,8);
/*leng =8;// 预计接受8 bytes
if(halRfReceivePacket(RxBuf,&leng))
{
P1OUT = 0X00;
Disp_Numb();
Disp1Char(i,0,shuzi[ptr[0]]);
i++;
if(i==9)
i=i+1;
if(i==11)
i=7;*/
Disp_Numb(Do1Convert());
for(i=3;i<6;i++)
{
TxBuf[5]=scandata[dN[i]];
halRfSendPacket(TxBuf,8);
}
//}
}
}
/*******************************************
函数名称:Disp_Numb
功 能:将从DS18B20读取的11bit温度数据转换
成液晶显示的温度数字
参 数:temper--11bit温度数据
返回值 :无
********************************************/
/*void Disp_Numb()
{
switch (RxBuf[5])
{
case 0xc0:ptr[0]=0;break;
case 0xcf:ptr[0]=1;break;
case 0xa4:ptr[0]=2;break;
case 0xb0:ptr[0]=3;break;
case 0x99:ptr[0]=4;break;
case 0x92:ptr[0]=5;break;
case 0x82:ptr[0]=6;break;
case 0xf8:ptr[0]=7;break;
case 0x80:ptr[0]=8;break;
case 0x90:ptr[0]=9;break;
}
}*/
void Disp_Numb(INT16U temper)
{
INT8U i;
for(i = 0;i < 6;i++) dN[i] = 0; //初始化显示变量
//数值转换
if(temper & BIT0)
{
dN[0] = 5;
dN[1] = 2;
dN[2] = 6;
}
if(temper&BIT1)
{
dN[1] += 5;
dN[2] += 2;
dN[3] += 1;
}
if(temper & BIT2)
{
dN[2] += 5;
dN[3] += 2;
if(dN[2] >= 10)
{
dN[2] -= 10;
dN[3] += 1;
}
}
if(temper&BIT3)
{
dN[3] += 5;
}
if(temper & BIT4)
{
dN[4] += 1;
}
if(temper & BIT5)
{
dN[4] += 2;
}
if(temper & BIT6)
{
dN[4] += 4;
}
if(temper & BIT7)
{
dN[4] += 8;
if(dN[4] >= 10)
{
dN[4] -= 10;
dN[5] += 1;
}
}
if(temper & BIT8)
{
dN[4] += 6;
dN[5] += 1;
if(dN[4] >= 10)
{
dN[4] -= 10;
dN[5] += 1;
}
}
if(temper & BIT9)
{
dN[4] += 2;
dN[5] += 3;
if(dN[4] >= 10)
{
dN[4] -= 10;
dN[5] += 1;
}
}
if(temper & BITA)
{
dN[4] += 4;
dN[5] += 6;
if(dN[4] >= 10)
{
dN[4] -= 10;
dN[5] += 1;
}
if(dN[5] >= 10)
{
dN[5] -= 10;
}
}
}
|
|