?? txd.c
字號:
#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 + -