|
发表于 2013-6-28 21:46:31
|
显示全部楼层
shawn_bu 发表于 2013-6-28 11:39 ![](static/image/common/back.gif)
Atmega8用arduino的IDE有点蛋疼啊,貌似写不了多少程序。哈哈。。。 - SPI读操作代码
- uint8 SPI_Read(void)
- {
- uint8 i,rxdata;
- rxdata = 0x00;
- for (i = 0;i < 8;i++)
- {
- rxdata = rxdata<<1;
- SCLK_ON();
- if (MISO_IN)
- {
- rxdata |= 0x01;
- }
- else
- {
- rxdata &= ~0x01;
- }
- SCLK_OFF();
- }
- return rxdata;
- }
- SPI写操作代码
- void SPI_Write(uint8 txdata)
- {
- uint8 i;
- for (i = 0;i < 8;i++)
- {
- if (txdata&0x80)
- {
- MOSI_ON();
- }
- else
- {
- MOSI_OFF();
- }
- SCLK_ON();
- txdata = txdata<<1;
- SCLK_OFF();
- }
- }
- CC2420配置寄存器读操作
- uint16 CC2420_ReadReg(uint8 addr)
- {
- uint16 value;
- CSN_OFF();
- SPI_Write(addr|REG_READ);
- value = SPI_Word_Read();
- CSN_ON();
- return value;
- }
- CC2420配置寄存器写操作
- void CC2420_WriteReg(uint8 addr, uint16 value)
- {
- CSN_OFF();
- SPI_Write(addr|REG_WRITE);
- SPI_Word_Write(value);
- CSN_ON();
- }
- CC2420 RAM读操作
- uint8 CC2420_RAM_Read(uint8 addr,uint8 block)
- {
- uint8 value;
- CSN_OFF();
- SPI_Write(addr|RAM);
- SPI_Write((block<<6)|RAM_READ);
- value = SPI_Read();
- CSN_ON();
- return value;
- }
- CC2420 RAM写操作
- void CC2420_RAM_Write(uint8 addr,uint8 block, uint8 value)
- {
- CSN_OFF();
- SPI_Write(addr|RAM);
- SPI_Write((block<<6)|RAM_WRITE);
- SPI_Write(value);
- CSN_ON();
- }
- CC2420初始化
- void CC2420_Init(void)
- {
- RESET_OFF();
- delay_ms(10);
- RESET_ON();
- delay_ms(10);
- CC2420_Command(CMD_SXOSCON);
- delay_ms(10);
- CC2420_PSDU[ 1 ] =
- (PAN_ID_COMPRESSION<<6)|(ACKNOWLEDGMENT_REQUEST<<5)|
- (FRAME_PENDING<<4)|(SECURITY_ENABLE<<3)|(FRAME_TYPE_DATA<<0);
- CC2420_PSDU[ 2 ] =
- (SOURCE_ADDRESSING_MODE<<6)|(FRAME_VERSION<<4)|
- (DEST_ADDRESSING_MODE<<2);
- CC2420_PSDU[ 3 ] = SEQUENCE_NUMBER;
- CC2420_PSDU[ 4 ] = CC2420_Destination_PANID[0];
- CC2420_PSDU[ 5 ] = CC2420_Destination_PANID[1];
- CC2420_PSDU[ 6 ] = CC2420_Destination_IEEEAddr[0];
- CC2420_PSDU[ 7 ] = CC2420_Destination_IEEEAddr[1];
- CC2420_PSDU[ 8 ] = CC2420_Destination_IEEEAddr[2];
- CC2420_PSDU[ 9 ] = CC2420_Destination_IEEEAddr[3];
- CC2420_PSDU[10] = CC2420_Destination_IEEEAddr[4];
- CC2420_PSDU[11] = CC2420_Destination_IEEEAddr[5];
- CC2420_PSDU[12] = CC2420_Destination_IEEEAddr[6];
- CC2420_PSDU[13] = CC2420_Destination_IEEEAddr[7];
- CC2420_PSDU[14] = CC2420_Source_PANID[0];
- CC2420_PSDU[15] = CC2420_Source_PANID[1];
- CC2420_RAM_Write(RAM_PANID, 2, CC2420_Source_PANID[0]);
- CC2420_RAM_Write(RAM_PANID+1, 2, CC2420_Source_PANID[1]);
- CC2420_PSDU[16] = CC2420_Source_IEEEAddr[0];
- CC2420_PSDU[17] = CC2420_Source_IEEEAddr[1];
- CC2420_PSDU[18] = CC2420_Source_IEEEAddr[2];
- CC2420_PSDU[19] = CC2420_Source_IEEEAddr[3];
- CC2420_PSDU[20 ] = CC2420_Source_IEEEAddr[4];
- CC2420_PSDU[21] = CC2420_Source_IEEEAddr[5];
- CC2420_PSDU[22] = CC2420_Source_IEEEAddr[6];
- CC2420_PSDU[23] = CC2420_Source_IEEEAddr[7];
- CC2420_RAM_Write(RAM_IEEEADR, 2, CC2420_Source_IEEEAddr[0]);
- CC2420_RAM_Write(RAM_IEEEADR+1, 2, CC2420_Source_IEEEAddr[1]);
- CC2420_RAM_Write(RAM_IEEEADR+2, 2, CC2420_Source_IEEEAddr[2]);
- CC2420_RAM_Write(RAM_IEEEADR+3, 2, CC2420_Source_IEEEAddr[3]);
- CC2420_RAM_Write(RAM_IEEEADR+4, 2, CC2420_Source_IEEEAddr[4]);
- CC2420_RAM_Write(RAM_IEEEADR+5, 2, CC2420_Source_IEEEAddr[5]);
- CC2420_RAM_Write(RAM_IEEEADR+6, 2, CC2420_Source_IEEEAddr[6]);
- CC2420_RAM_Write(RAM_IEEEADR+7, 2, CC2420_Source_IEEEAddr[7]);
- CC2420_WriteReg(REG_MDMCTRL0, CCA_HYST|CCA_MODE|PREAMBLE_LENGTH|AUTOCRC|ADR_DECODE);
- CC2420_WriteReg(REG_SYNCWORD,SYNCWORD);
- CC2420_WriteReg(REG_SECCTRL0,0);
- CSN_OFF();
- SPI_Write(REG_RXFIFO|REG_READ);
- SPI_Read();
- CSN_ON();
- CC2420_Command(CMD_SFLUSHRX);
- CC2420_Command(CMD_SFLUSHTX);
- delay_ms(10);
- }
- CC2420 FIFO发送流程
- FIFO写数据操作
- void CC2420_WriteTXFIFO(void)
- {
- uint8 i;
- CC2420_Command(CMD_SFLUSHTX);
- CSN_OFF();
- SPI_Write(REG_TXFIFO|REG_WRITE);
- SPI_Write(CC2420_PSDU[0]);
- for(i=0;i
- {
- SPI_Write(CC2420_PSDU[1+i]);
- }
- CSN_ON();
- }
- FIFO数据发送操作
- void CC2420_TxPacket(void)
- {
- CC2420_Command(CMD_SRFOFF);
- CC2420_Command(CMD_STXON);
- while(!SFD_IN);
- while(SFD_IN);
- }
- CC2420 FIFO接收流程
- 接收模式设置
- void CC2420_SetRxMode(void)
- {
- CC2420_Command(CMD_SRFOFF);
- CC2420_Command(CMD_SRXON);
- }
- FIFO接收数据
- uint8 CC2420_RxPacket(void)
- {
- if((!SFD_IN)&&(FIFO_IN))
- {
- return TRUE;
- }
- return FALSE;
- }
- 收到数据后读取FIFO数据
- void CC2420_ReadRXFIFO(void)
- {
- uint8 i;
- CSN_OFF();
- SPI_Write(REG_RXFIFO|REG_READ);
- CC2420_PSDU[0] = SPI_Read();
- for(i=0;i
- {
- CC2420_PSDU[1+i] = SPI_Read();
- }
- CSN_ON();
- CC2420_Command(CMD_SFLUSHRX);}
复制代码 cc2420的资料 |
|