⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 txd.c

📁 2.4G无线芯片NRF24L01的控制程序
💻 C
📖 第 1 页 / 共 2 页
字号:

#include <iom64v.h>
#include <macros.h>
#include "TXD24HH.h"
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long 

/////////////////////////////////////////////////////////////////////////////////

   //#define txddd  
	#define debug1 
	#define debug2 
	#define debug3 
//////////////////////////////////////////////////////////////////////////////
typedef unsigned char BYTE;
//#define F_OSC 8000000 
#define F_OSC 14745600 
//uchar  const  code1[97]={48,
///#define F_OSC 7372800 
#define CLOCK_COUNT 1
#define rff  0x02
#define exemm1	(*(volatile unsigned char *)0xc000)
#define rate 0x0f 
#define  SUART0_FIFO_SIZE  15
#define UART0_FIFO_SIZE 12
#define UART1_FIFO_SIZE 12
#define TIMER0_DEFAULT_MS  10

#define FLAGA  (PIND&0X02)
 #define FLAGB  (PINE&0X10)
 #define FLAGC  (PINE&0X20)
 
#define OE1  PORTF|=0X10
#define A01  PORTD|=0X10
#define A11  PORTD|=0X20
#define OE0  PORTF&=0XEF
#define A00  PORTD&=0XEF
#define A10  PORTD&=0XDF

#define PK1  PORTF|=0X20
//#define WR1  PORTD|=0X80
//#define RD1  PORTG|=0X02
#define PK0  PORTF&=0XDF
#define LED_ON  PORTF|=0X80
#define LED_OFF  PORTF&=0X7F
//#define SS1  PORTB|=0X01
//#define SS0  PORTB&=0XFE
//#define CE1  PORTE|=0X10
//#define CE0  PORTE&=0XEF
#define rxd_stop  EIMSK&=0XFE
#define rxd_start  EIMSK|=0X01
//#define rxd_stop  CE0
//#define rxd_start  CE1
//#define rxd_stop  
//#define rxd_start  
#define SS1  PORTB|=0X20
#define SS0  PORTB&=0XDF
#define CE1  PORTB|=0X40
#define CE0  PORTB&=0XBF
static uint  timer3back=0;//记忆计算的初值

static uint   delay_count=0;//延时计数器
static uchar  timer0_second=0;//秒计数器
static ulong  time0_clock=0;//系统时钟
static ulong  timer0_dog_clock=0;//软狗
uchar error=0;
uint   TCNT1back=0;
uint   TCNT1_suart0_1_3=0;//设置到BIT宽度的1/3处
uchar  SUART0_RI=0;//数据位计数器
uchar  SUART0_BUF=0;//接收寄存器
//??????????????sbit SUART0_BUF_HIGH=SUART0_BUF^7;//接收寄存器最高位-移位变量
uchar  *pdata;
uchar   suart0_fifo[SUART0_FIFO_SIZE];//接收FIFO
uint    suart0_fifo_in=0;//写指针
uint    suart0_fifo_out=0;//读指针
uchar  uart0_fifo[UART0_FIFO_SIZE];//接收FIFO
uint   uart0_fifo_in=0;//写指针
uint   uart0_fifo_out=0;//读指针
uint countx;
uchar  uart1_fifo[UART1_FIFO_SIZE];//接收FIFO
uint   uart1_fifo_in=0;//写指针
uint  uart1_fifo_out=0;//读指针
uchar UA00R ;
uchar flag_max=0;
uchar flag_rx;
uchar fifost=0;
uint txdtime=0;
uint txdtime1=0;
uint txdtime2=0;
uint txdtime3=0;
uchar rxdtemp=0;
uchar rxdtemp2=0;
uchar start_stop=1;
uchar step1=0;
uchar step2=0;
uchar cmd_or_data=0;
uchar write_usb_count=0;
uchar write_usb_count2=0; //data count
//Watchdog initialize
// prescale: 16K 
//uchar usbdata[1024];
void watchdog_init(void)
{
 WDR(); //this prevents a timout on enabling
 WDTCR = 0x08; //WATCHDOG ENABLED - dont forget to issue WDRs
}
void delayms(uint k)
{uint i ,j;
 for(i=0;i<2400;i++)
 {for(j=0;j<k;j++);
 }
}

void WRITE_EE(uint address ,uchar eedata)
{while(EECR&(1<<EEWE));//dengdai 
 EEAR=address;
 EEDR=eedata;
 SREG&=0X7F;
 EECR|=(1<<EEMWE);
 EECR|=(1<<EEWE);
 SREG|=0X80;
}

uchar READ_EE(uint address)
{while(EECR&(1<<EEWE));//dengdai 
 EEAR=address; //gei dizhi 
 EECR|=(1<<EERE);//du 
 return EEDR ;
}

void uart0_init(ulong baud )
{   uint UBRR0;//uart0 没有串口
	UCSR0B = 0x00; //disable while setting baud rate
    UCSR0A = 0x20;//UDRE 1 
	UCSR0C = 0x06;
	
  #ifdef DOUBLE_BAUDRATE
	UCSR0A|=(1<<U2X);
	baud>>=1;//波特率倍增
	#endif 
	UBRR0=((ulong)F_OSC)/16/CLOCK_COUNT/baud-1;
	UBRR0H=(uchar)(UBRR0>>8);
	UBRR0L=(uchar)UBRR0;
	
    //UCSR0B = 0x18;//开中断BIT7 是接收中断 BIT6是发送结素中断
	UCSR0B|=(1<<4);//BIT4接收使能
	UCSR0B|=(1<<3);//BIT3发送使能
	UCSR0B|=(1<<7);//BIT7 是接收中断 
	//UCSR0B|=(1<<6);//BIT6是发送结素中断
}
void uart1_init(uint baud )
{   uint UBRR0;//uart0 没有串口
    DDRD|=0X04;
    PORTD|=0X04;
	UCSR1B = 0x00; //disable while setting baud rate
    UCSR1A = 0x20;//UDRE 1 
	UCSR1C = 0x06;
	
  #ifdef DOUBLE_BAUDRATE
	UCSR1A|=(1<<U2X);
	baud>>=1;//波特率倍增
	#endif 
	UBRR0=((ulong)F_OSC)/16/CLOCK_COUNT/baud-1;
	UBRR1H=(uchar)(UBRR0>>8);
	UBRR1L=(uchar)UBRR0;
    //UCSR1B = 0x18;//开中断
	UCSR1B|=(1<<4);//BIT4接收使能
	UCSR1B|=(1<<3);//BIT3发送使能
	UCSR1B|=(1<<7);//BIT7 是接收中断 
	//UCSR1B|=(1<<6);//BIT6是发送结素中断
	
}



void uart0_txd(uchar x)
{ while((UCSR0A&0X20)==0);
  UDR0=x;

}
void uart0_str(uchar *p)
{while(*p)
  {uart0_txd(*p);
    p++;
  }
}

void uart1_txd(uchar x)
{ while((UCSR1A&0X20)==0);
  UDR1=x;
  while(!(UCSR1A&0X40));
  UCSR1A|=0X40;
}

void timer0_init(void)
{
	ETIMSK&=0XFB;//关定时器3中断
	TCCR3A=0X00; //普通模式 
	TCCR3B=0X00;//关闭timer3
	timer3back=(0x10000-((ulong)F_OSC)*TIMER0_DEFAULT_MS/CLOCK_COUNT/8000);
	TCNT3=timer3back;                                       //8分频 1MS=1/1000秒
	//TCCR3B=0X01;//开 timer3  无预分频 
	 TCCR3B = 0x02; //start Timer 8分频 
	ETIMSK|=0X04;//开定时器3中断
}

//***********************************************************************************************************************
void timer0_mdelay(uint count)
{
	if(count<TIMER0_DEFAULT_MS)count=TIMER0_DEFAULT_MS;//修正太短的延时
	count/=TIMER0_DEFAULT_MS;//把毫秒修正为timer0的中断次数
	ETIMSK&=0XFB;//关定时器3中断
	delay_count=0;//定时器里面的直清0 精度为 10MS
	ETIMSK|=0X04;//开定时器3中断
	while(delay_count<count);
	//dog();//等待时间到
}

//***********************************************************************************************************************
//函数作用:秒延时函数
//参数说明:无
//注意事项:count--秒数
//返回说明:无
//***********************************************************************************************************************
void timer0_delay(uchar count)
{
	while(count--)timer0_mdelay(1000);
	 //uart1_txd(0Xcc);
}

//***********************************************************************************************************************
//函数作用:得到timer0时钟
//参数说明:无
//注意事项:
//返回说明:无
//***********************************************************************************************************************
ulong timer0_get_clock(void)
{
	return time0_clock;
}


//***********************************************************************************************************************
//函数作用:喂软件狗
//参数说明:无
//注意事项:
//返回说明:无
//***********************************************************************************************************************
void soft_dog_feed(void)
{
	ETIMSK&=0XFB;//关定时器3中断
	timer0_dog_clock=time0_clock;
	ETIMSK|=0X04;//开定时器3中断
}
/*
void suart0_init(uint baud)
{	
    DDRE&=0XBF;//
	PORTE|=0X40;//上拉电阻势能
	TIMSK&=0XFB;//关定时器1中断
	TCCR1A=0X00; //普通模式 
	TCCR1B=0X00;//关闭timer1	
	TCNT1back=65536-((ulong)F_OSC)/baud/CLOCK_COUNT;//F_OSC=4M ,clock_count=1;
	//TCNT1back+=40;//   需要校正
	TCNT1=TCNT1back;
	TCNT1_suart0_1_3=(65536-((ulong)F_OSC)/baud/CLOCK_COUNT/3);
	TIMSK|=0X04;//开定时器1中断
	EIMSK&=0XBF;//关INT0中断
	EICRB&=0XCF;//低电平触发模式
	suart0_flush();//清缓冲区
	EIMSK|=0X40;//打INT0中断
	
	
}*/

/////////////////软串口 定时器1
#pragma interrupt_handler timer2_isr:15
static void timer2_isr(void)  // //用定时器1 
{  
	TCNT1=TCNT1back;
	if(SUART0_RI==0)//起始位
	{
		//起始位容错
		if(PINE&0X40)
		{
		
			EIMSK|=0X40;//打INT0中断
			TCCR1B=0X00;//关闭timer1	
			 uart1_txd(0XCC);
		}
		else
		{
			SUART0_RI++;
		}
	}
	else if(SUART0_RI==9)//停止位
	{	
		//停止位容错
		if(PINE&0X40)
		{
			//送FIFO缓冲区
			suart0_fifo[suart0_fifo_in++]=SUART0_BUF;
		   //	uart_txd(0X33);
			uart1_txd(SUART0_BUF);
			if(suart0_fifo_in>=SUART0_FIFO_SIZE) suart0_fifo_in=0;
		}
		SUART0_RI=0;//清数据位计数器
		
		EIMSK|=0X40;//打INT0中断
			TCCR1B=0X00;//关闭timer1	
			TIFR|=0X40;
	}
	else//数据位
	{
		SUART0_BUF>>=1;
		if(PINE&0X40)
		SUART0_BUF|=0x80;
		else SUART0_BUF&=0x7f;
		SUART0_RI++;
	}
	
	
}

#pragma interrupt_handler uart1_isr:31
////////// 串口1 接收中断
static void uart1_isr(void)
{   
		uart1_fifo[uart1_fifo_in]=UDR1;//接收串口一个字符
	    uart1_txd(uart1_fifo[uart1_fifo_in]);
		uart1_fifo_in++;
		if(uart1_fifo_in>=UART1_FIFO_SIZE)uart1_fifo_in=0;//如果接收缓冲区满	
		
}
#pragma interrupt_handler timer0_isr:30//名字用的是0  实际用的是定时器三
static void timer0_isr(void)
{   // uart1_txd(0Xee);		  	
	ETIMSK&=0XFB;//关定时器3中断
	//PORTC=~PORTC;
	TCNT3=timer3back;
	delay_count++;//延时计数器
	//秒计数器时间到
	if(++timer0_second>=(uchar)(1000/TIMER0_DEFAULT_MS))
	{
		timer0_second=0;//清秒计数器
		time0_clock++;//系统时钟增加
		if((time0_clock-timer0_dog_clock)>=254);
		//STOP;//软件看门狗 在boot中不能喂
	}
	
	//EXT_DOG;//外部狗

	ETIMSK|=0X04;//开定时器3中断
}


void spi_init(void)
{// DDRB|=(1<<0)|(1<<1)|(1<<2);//ss sck mosi
  // PORTB|=(1<<0)|(1<<1)|(1<<2);
   
  DDRB|=(1<<0)|(1<<1)|(1<<2)|(1<<5);//ss sck mosi
   PORTB|=(1<<0)|(1<<1)|(1<<2)|(1<<5);
   
  //SPCR=0X7c; //低先发
    SPCR=0X50; //164高先发
  SPSR|=(1<<SPI2X);//倍速度
}

void port_init(void)
{
  // #define CE1  PORTB|=0X40
 DDRB|=0X40;
 
   //#define PK1  PORTF|=0X20
   DDRF|=0X20;PK1;

   DDRF|=0X80;LED_ON;//LED
   //#define LED_ON PORTF|=0X80
   
   DDRD&=0XFD;
   PORTD|=0X02; //FLAG A 
   
   DDRE&=0XEF;
   PORTE|=0X10; //FLAG B
   
      DDRE&=0XDF;
   PORTE|=0X20; //FLAG C
   
    //#define OE1  PORTF|=0X10
   DDRF|=0X10;//OE
   
     // #define A01  PORTD|=0X10
  //#define A11  PORTD|=0X20
   DDRD|=0X30;
}

void exint_init(uchar intx,uchar kind )//x=0---7  //kind  0--3 
{	uchar x;                           //0 低电平 1 任意电平变化 2 下跳 3 上跳
    x=(1<<intx);                       //当X=0--3时 没有 KIND=1默认为0
  if(intx>3)
  {
    DDRE&=~x;
	PORTE|=x;
   }
   else 
   {
    DDRD&=~x;
	PORTE|=x;
   }
	
	EIMSK&=~x;
	if(intx>3)
	{if(intx==4){EICRB&=0Xfc;EICRB|=kind;}//低电平触发模式
	 else if(intx==5){EICRB&=0Xf3;EICRB|=(kind<<2);}
	  else if(intx==6){EICRB&=0Xcf;EICRB|=(kind<<4);}
	  else if(intx==7){EICRB&=0X3f;EICRB|=(kind<<6);}
	}
	else 
	{if(kind==1) kind=0;
	 if(intx==0){EICRA&=0Xfc;EICRA|=kind;}//低电平触发模式
	 else if(intx==1){EICRA&=0Xf3;EICRA|=(kind<<2);}
	  else if(intx==2){EICRA&=0Xcf;EICRA|=(kind<<4);}
	  else if(intx==3){EICRA&=0X3f;EICRA|=(kind<<6);}
	}
	
	EIMSK|=x;//打kai中断
}




//**********************************************************//
//
//  Function: SPI_RW_Reg
//
//  Description:
//  Writes value 'value' to register 'reg'
//
//
//  In/Out parameters:
//  In: 'reg' register to write value 'value' to.
//  Return status byte.
//
//  Author: RSK   Date: 28.11.05
//**********************************************************//
BYTE SPI_RW_Reg(BYTE reg, BYTE value)
{
BYTE status;

 SS0;                                     // CSN low, init SPI transaction
  status = SPI_RW(reg);                           // select register
  SPI_RW(value);                                  // ..and write value to it..
  SS1;                                     // CSN high again

  return(status);                                 // return nRF24L01 status byte
}

uchar SPI_COM(uchar com)
{uchar status;
   SS0;
  status = SPI_RW(com);
 // SPI_RW(value);                                  // ..and write value to it..
  SS1;  
   return(status);
   }
 
uchar  SPI_RW(BYTE byte)
{
      SPDR=byte;
	  while(!(SPSR&0X80));
      return SPDR;

}
//**********************************************************//
//
//  Function: SPI_Read
//
//  Description:
//  Read one byte from nRF24L01 register, 'reg'
//
//
//  In/Out parameters:
//  In: reg, register to read
//  Out: return reg_val, register value.
uchar  SPI_Read(BYTE reg)
{
  BYTE reg_val;

  SS0;                                        // CSN low, initialize SPI communication...
  
  SPI_RW(reg);                                    // Select register to read from..
  reg_val = SPI_RW(0);                            // ..then read registervalue
  SS1;                                     // CSN high, terminate SPI communication
  return(reg_val);                                // return register value
}
//**********************************************************//
//
//  Function: SPI_Write_Buf
//
//  Description:
//  Writes contents of buffer '*pBuf' to nRF24L01
//  Typically used to write TX payload, Rx/Tx address
//
//
//  In/Out parameters:
//  In: register 'reg' to write, buffer '*pBuf*' contains
//  data to be written and buffer size 'buf_size' is #of
//  bytes to be written
//  Out: return nRF24L01 status byte.
//
//  Author: RSK   Date: 28.11.05
//**********************************************************//
BYTE SPI_Write_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)
{
BYTE status,byte_ctr;

  SS0;                                     // Set CSN low, init SPI tranaction
  status = SPI_RW(reg);                           // Select register to write to and read status byte

  for(byte_ctr=0; byte_ctr<bytes; byte_ctr++)     // then write all byte in buffer(*pBuf)
    SPI_RW(*pBuf++);

  SS1;                                     // Set CSN high again

  return(status);                                 // return nRF24L01 status byte
}

//**********************************************************//
//
//  Function: SPI_Read_Buf
//
//  Description:
//  Reads 'bytes' #of bytes from register 'reg'
//  Typically used to read RX payload, Rx/Tx address
//
//
//  In/Out parameters:
//  In: 'reg', register to read from, '*pBuf' are buffer
//  the read bytes are stored to and 'bytes' are #of bytes
//  to read.
//  Out: return nRF24L01 status byte.
//
//  Author: RSK   Date: 28.11.05
//**********************************************************//
BYTE SPI_Read_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)
{
BYTE status,byte_ctr;

  SS0;                                     // Set CSN low, init SPI tranaction
  status = SPI_RW(reg);                           // Select register to write to and read status byte

  for(byte_ctr=0;byte_ctr<bytes;byte_ctr++)
    //pBuf[byte_ctr] = SPI_RW(0);                   // Perform SPI_RW to read byte from nRF24L01
   *pBuf++=SPI_RW(0);
   // *pdata++=SPI_RW(0);
  SS1;                                     // Set CSN high again

  return(status);                                 // return nRF24L01 status byte
}


//**********************************************************
//
//  Function: TX_Mode
//
//  Description:
//  This function initializes one nRF24L01 device to
//  TX mode, set TX address, set RX address for auto.ack,
//  fill TX payload, select RF channel, datarate & TX pwr.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -