Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1256253
  • 博文数量: 404
  • 博客积分: 10011
  • 博客等级: 上将
  • 技术积分: 5382
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-03 16:29
文章存档

2010年(40)

2009年(140)

2008年(224)

我的朋友

分类: LINUX

2009-05-13 10:08:47

AVR atmega128    ICCAVR     AVR Studio
 
串口收到数据并发送回来
 
test.c
 
//串口通讯 ,PE0-Rxd0,PE1-Txd0,返回的数据是发送的数据
#include
#include
#define xtal  3686400//晶振16MHZ
#define baud0 19200//波特率
#define baud1 19200//波特率

#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define NULL 0
#define FALSE 0
#define TRUE  1
#define false FALSE
#define true TRUE
#define nop NOP() 
/*
SFR_B(UBRR0H, 0x90)// USART0 Baud Rate Register High
SFR_B(UBRR0L, 0x09)// USART0 Baud Rate Register Low
SFR_B(UCSR0B, 0x0A)// USART0 Control and Status Register B
SFR_B(UCSR0A, 0x0B)// USART0 Control and Status Register A
SFR_B(UDR0,0x0C)// USART0 I/O Data Register
SFR_B(UCSR0C, 0x95)// USART0 Control and Status Register C
*/
/*****************************
串口初始化
// USART0 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART0 Receiver: On
// USART0 Transmitter: On
// USART0 Mode: Asynchronous
******************************/
struct guard_stru
 {
   uchar guard_type;  //门磁,防区类型
   uchar initial;    //正常状态
   uchar exc_open;   //0:normal,1:except
   uchar warn_open_time; //unit second
 };
 struct guard_stru guard[8];
 
void init_uart0(void)
{
 UCSR0B = 0x00; //disable while setting baud rate
 UCSR0A = 0x00;  //U2X=0,不加倍速率
 UCSR0C = 0x06;  //P174 8位,是否需要校验? 
 UBRR0L = 0x33; //set baud rate lo,波特率为9600   8M
 UBRR0H = 0x00; //set baud rate hi
 UCSR0B = 0x98; //接收中断允许,接收缓冲自动清空,接收允许
}
void eeprom_init(void)
{
 //PORTA=EEPROMread(0x442);
 PORTB=EEPROMread(0x444);
 PORTC=EEPROMread(0x446);
 PORTD=EEPROMread(0x448);//0,1
 PORTE=EEPROMread(0x44a);

/*
void init_uart0(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C =(1<
UBRR0L=(xtal/16/(baud0+1))%256;
UBRR0H=(xtal/16/(baud0+1))/256;
UCSR0B =(1<
}
*/
/*****************************
发送数据函数
******************************/
void trans_byte(unsigned char d)
{
while ( !( UCSR0A & (1<
;// Wait for empty transmit buffer
UDR0 = d;
}
/******************************
接收数据函数
*******************************/
unsigned char rece_byte( void )
{
while ( !(UCSR0A & (1<
;// Wait for data to be received
return UDR0;
}
/***************************
主函数
****************************/
void main(void)
{
init_uart0();
eeprom_init();
while(1)
{
eeprom_init();
trans_byte(rece_byte());
}
}
 
 
 
变量和数据定义
 
eep_stru.c
 
uchar  cc[3];//clock
uchar  gate_flag[4];  //门开超时标志
uchar  delay_count_gate[4]={0};  //开门超时计数器
const  uchar model[8]={1,2,4,8,16,32,64,128};
#define BIT7 0x80
#define BIT6 0x40
#define BIT5 0x20
#define BIT4 0x10
#define BIT3 0x08
#define BIT2 0x04
#define BIT1 0x02
#define BIT0 0x01
#define CHECKBIT(x,y) (x&y)
//-1 dictionary zone
#define start_eep 0x100
#define serial_section start_eep 
#define relay_section 0x120 
#define card_section 0x140 
#define guard_section 0x440
#define ADC_section  0x4a0 
#define rec_card_section  0x500
#define rec_alarm_section 0xa00
 
//serial_section 32 bytes      0x100
//relay_section 32 bytes       0x120
//card_section 256 bytes       0x140
//guard_section 48 bytes       0x440
//ADC_section 96 bytes         0x4a0
//rec_card_section 1152 bytes  0x500  100*(1+4+6)=1100  
//rec_alarm_section            0xa00  120*(1+2+6)=1080

//0.distribute para zone //0x100,serial0 baud //0x102,serial1 baud
//uart 0,VKM
#define rev0buff_max 710
uint    rev0buff_cnt;
uint    rev0buff_cnt1;
uchar   rev0_buff[rev0buff_max];
//串口0计时接收结束
#define rev_delay_max  30/5
uchar   flag0_achar;
uchar   rev0_delaycnt;
uchar   rev0_over;

//uart 1,self
#define rev1buff_max 710
uint    rev1buff_cnt;
uchar   rev1_buff[rev1buff_max];

//串口1计时接收结束
uchar   flag1_achar;
uchar   rev1_delaycnt;
uchar   rev1_over;
//回应命令
//VKM
uchar write_card[]={3,12,0xaa,0,2,0x10,0,0xcc,0xcc};
uchar query[]={3,12,0xaa,0,0xbb,1,0,0xa1,0xa2,0xa3,0xa4,0,0,0,0,
               0,0,0,0,0,0,0,0,
               0xdd,0xc0,0xc1,0xc2,0xc3,1,3,3,25,15,52,   //刷卡记录2003,6,6
      0xdd,0xc0,0xc1,0xc2,0xc3,1,3,3,25,15,52,
      0xdd,0xc0,0xc1,0xc2,0xc3,1,3,3,25,15,52,
      0xdd,0xc0,0xc1,0xc2,0xc3,1,3,3,25,15,52,
      0xdd,0xc0,0xc1,0xc2,0xc3,1,3,3,25,15,52,0xcc,0xcc};
uchar read[]={3,5,0xaa,0,1,1,2,0,0x0f,0,0x42,0xdd,0xcc,0xd0,0xd0,0xd0,0xd0,
              0xd1,0xd1,0xd1,0xd1,0xd2,0xd2,0xd2,0xd2,0xd3,0xd3,0xd3,0xd3,
     0xd4,0xd4,0xd4,0xd4,0xd5,0xd5,0xd5,0xd5,0xd6,0xd6,0xd6,0xd6,
     0xd7,0xd7,0xd7,0xd7,0x70,0x70,0x70,0x70,0x80,0x80,0x80,0x80,
     0x90,0x90,0x90,0x90,0x50,0x50,0x50,0x50,0x60,0x60,0x60,0x60,
              0x40,0x40,0x40,0x40,0x30,0x30,0x30,0x30,0x00,0x00,0x00,0x00,
     0xcc,0xcc};  //79个
    
//self
uchar query_card[]={8,12,0,0x8a,11,0,2,2,2,2,1,3,3,0x18,0x15,0x58,0xcc,0xcc};
uchar answer_time[]={8,12,0xaa,0x89,8,0x10,0,0xff,0xfe,0xfd,0xfc,0xfb,0xfa,0xcc,0xcc};
uchar setup_guard[]={8,12,0xaa,0x86,0xdd,0xee,0xcc,0xcc};
uchar modify_answer[]={8,12,0xaa,0x83,0xbb,0xcc,0xdd,0xee,0xef,0xcc,0xcc};
uchar guard_rep[]={8,12,0xaa,0x8b,8,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0xcc,0xcc};
uchar read_sample[]={8,12,0xaa,0x8c,18,0x77,0x78,0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,
                     0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,0xcc,0xcc};
//uchar query_gate[]={3,12,0xaa,0,18,1,0,8,9,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0,0,0,0,0,0,0,0,0xff,0xff,'\n'};

#define serial_eep_addr serial_section 
//0x100
#define serial_baud_eep_addr serial_eep_addr
uchar  serial_baud[2]={3,3};
#define send_count_eep_addr serial_baud_eep_addr+4
//uchar send_count;
struct serial_stru
{
   uchar baudl;
   //uchar baudh;
};
const struct serial_stru serial[10]=
{
    0xcf,
 0x67,
 0x33,
 0x22,
 0x19,
 0x10,
 0x0c,
 0x08,
 0x06,
 0x03
};
     
 
//1.time    no eep |no init  //
#define   year_base_eep_addr send_count_eep_addr+4
//uchar   year_base; 
struct time_stru
{
  uchar year;
  uchar month;
  uchar day;
  //uchar weekday;
  uchar hour;
  uchar minute;
  uchar second;
};
struct time_stru clock;
//2.relay  eep|init //0x120
#define relay_amount 6
#define relay_eep_addr relay_section    
#define wiegand_method 1
#define button_method  2
#define serial_method  3
#define gate_normal 0
#define gate_illegal 1
#define gate_over 4
#define gate_button 2
struct  relay_stru
{
  uchar close_mode;   //开门方式 wiegand1,button 2,seiarl 3
  uchar close_time;
};
struct relay_stru relay[relay_amount];   
struct comein_stru
{
  uchar flag;
  uchar tme;
};
struct comein_stru comein[4];
  
//3.button  no eep|no init
#define button_port PING
#define button1 BIT1
#define button2 BIT0
#define button3 BIT3
#define button4 BIT4
  
struct button_stru
{
  unsigned status:1;
  unsigned press_over:1;
  uint     press_count;  //按下时间,corrct
};
struct button_stru button[4];
//4.card eep|init //0x140  valid  36*7=252~~0x100
#define card_amount 100
#define card_fact_eep_addr card_section
#define card_eep_addr card_fact_eep_addr+2
struct  card_valid_stru
{
  uchar card_num[4];
  uchar card_head;
  uchar up_hour;
  uchar down_hour;
};
struct card_valid_stru card[card_amount];
//5.wiegand  no eep|no init
#define wiegand_amount 20/5
uchar wiegand_delay_cnt[4]; //wiegand 延时
#define wiegand_delay_max 4
struct  wiegand_stru
{
  unsigned wiegand_begin:1;
  unsigned wiegand_count:5;  //wiegand 位数
  unsigned wiegand_over:1;
  ulong wiegand_no;
};
 
struct  wiegand_stru wiegand[wiegand_amount];
   
//6.guard   eep|init //0x440,96 bytes
#define guard_amount 8
#define guard_button_eep_addr guard_section
//uchar guard_button;//是否按钮布防有效,0x440
#define guard_used_eep_addr  guard_button_eep_addr+2
uchar   guard_used;  //启用,禁用标志,0x442
#define guard_setup_eep_addr guard_used_eep_addr+2
uchar   guard_setup;  //撤防,布防,0x444
#define guard_time_eep_addr guard_setup_eep_addr+2
uchar   guard_time;//unit 3~5 second,0x446
#define gate_used_eep_addr guard_time_eep_addr+2
uchar   gate_used; //0x448,  门禁的启用禁用 0:启用1:禁用
#define gate_open_time_addr gate_used_eep_addr+2
uchar   gate_open_time;  //unit minute,0x44a 门常开告警时间
#define abolish_instance     0b00000000
#define abolish_delay        0b00000001
#define no_abolish_instance  0b00000010
#define no_abolish_delay     0b00000011  
#define comein_lmt_eep_addr  gate_open_time_addr+1
uchar   comein_lmt;         //0x44b 门开未进时间
#define guard_eep_addr gate_open_time_addr+2
//0x4ac
struct guard_stru
{
   uchar guard_type;  //门磁,防区类型
   uchar initial;    //正常状态
   uchar exc_open;   //0:normal,1:except
   uchar warn_open_time; //unit second
};
struct guard_stru guard[guard_amount];
uchar gd_sample[guard_amount];    
//04-6-17 10:48改动
uchar  gd_sample_old[guard_amount];    
struct update_stru
{
    unsigned  flag:1;//允许更新
    unsigned  cnt:6; //多长时间允许更新
};
struct update_stru update[guard_amount]=
                                        {
                                          {1,0},
                                          {1,0},
                                          {1,0},
                                          {1,0},
                                          {1,0},
                                          {1,0},
                                          {1,0},
                                          {1,0},
                                        };
#define update_timeout 25
uchar startup;

//8.ADC   eep|init //0x4a0
#define cycle_eep_addr  ADC_section 
uchar   sam_cycle=1; //采样周期
#define start_eep_addr cycle_eep_addr+2
uchar   start_flag;  //启用标志,0x4a2
#define method_eep_addr start_eep_addr+2
uchar   method_flag; //V/I,0x4a4
#define ADC_verify_eep_addr method_eep_addr+1
//uchar   ADC_verify;
#define ADC_amount 8
//#define ADC_eep_addr method_eep_addr+2
#define ADC_eep_addr ADC_verify_eep_addr+1
struct  ADC_stru   //0x4a6
{
    uint  measure_5v;       //voltage,
 uint  measure_4ma;      //current 4ma
 uint  measure_20ma;     //current 20ma
 
};
struct ADC_stru ADC_form[ADC_amount];
uint   ADC_sample[ADC_amount][ADC_amount];
//9.card record  eep| a little init //0x500,100*11=1100~~44c
#define rec_card_amount 100 
#define rec_card_count_addr rec_card_section 
//uchar   rec_card_count;  //计数循环
#define rec_card_eep_addr rec_card_count_addr+2
struct  rec_card_stru
{
   uchar card_head;
   uchar card_num[4];
   struct time_stru  clock;
};
struct rec_card_stru rec_card;
//10.alarm record   eep| a little  init //0xa00
#define rec_alarm_amount 120
#define rec_alarm_count_addr rec_alarm_section
//uchar rec_alarm_count;
#define rec_alarm_eep_addr rec_alarm_count_addr+2
struct rec_alarm_stru
{
   uchar alarm_type;
   uchar para[2];
   struct time_stru clock;
};
struct rec_alarm_stru rec_alarm;
 

//11. twi
#define pcf8583_write_addr 0xA0
#define pcf8583_read_addr 0xA1
#define consta_i2c_addr 0
#define sec1_i2c_addr 1
#define second_i2c_addr 2
#define minute_i2c_addr 3
#define hour_i2c_addr 4
#define yd_i2c_addr 5
#define wm_i2c_addr 6
#define ram_i2c_addr 0x10
#define rec_alarm_count_i2c ram_i2c_addr
//0x10
uchar   rec_alarm_count;
#define rec_card_count_i2c rec_alarm_count_i2c+1
//0x11
uchar   rec_card_count;  //计数循环
#define send_count_i2c rec_card_count_i2c+1
//0x12
uchar send_count;
//#define rec_card_100_i2c send_count_i2c+1
//0x13
//uchar rec_card_100;
 
 
#define start_cond 0x08
#define restart_cond 0x10
#define mt_sla_ack 0x18
#define mt_data_ack 0x28
#define mr_sla_ack 0x40
#define mr_data_ack 0x50
#define mr_data_nack 0x58
uchar twi_write(uchar begin_addr,uchar *input_data,uchar data_length)
{
    uchar i;
 TWCR=(1< while(!(TWCR&(1< ;
 if((TWSR&0xf8)!=start_cond)
    return FALSE;
   
 TWDR=pcf8583_write_addr;   //8583 write addr
 TWCR=(1< while(!(TWCR&(1< ;
 if((TWSR&0xf8)!=mt_sla_ack)
    return FALSE;
   
 TWDR=begin_addr;         //eeprom start addr
 TWCR=(1< while(!(TWCR&(1< ;
 if((TWSR&0xf8)!=mt_data_ack)
    return FALSE;
 
 for(i=0;i {
    TWDR=*input_data++;
    TWCR=(1<    while(!(TWCR&(1<    ;
    if((TWSR&0xf8)!=mt_data_ack)
       return FALSE;
 }
 TWCR=(1< return TRUE;
uchar twi_read(uchar begin_addr,uchar *writeto_addr,uchar data_length)
{
    uchar i;
 TWCR=(1< while(!(TWCR&(1< ;
 if((TWSR&0xf8)!=start_cond)
    return FALSE;
 
 TWDR=pcf8583_write_addr;   //8583 write  addr
 TWCR=(1< while(!(TWCR&(1< ;
 if((TWSR&0xf8)!=mt_sla_ack)
    return FALSE;
   
 TWDR=begin_addr;          //eeprom begin addr
 TWCR=(1< while(!(TWCR&(1< ;
 if((TWSR&0xf8)!=mt_data_ack)
    return FALSE;
 
 TWCR=(1< while(!(TWCR&(1< ;
 if((TWSR&0xf8)!=restart_cond)
    return FALSE;
   
 TWDR=pcf8583_read_addr;   //8583 read addr A!
 TWCR=(1< while(!(TWCR&(1< ;
 if((TWSR&0xf8)!=mr_sla_ack)
    return FALSE;
   
 for(i=0;i {
    //*writeto_addr++=TWDR;
    if(i!=data_length-1)
    {
        TWCR=(1<        while(!(TWCR&(1<        ;
        if((TWSR&0xf8)!=mr_data_ack)
         return FALSE;
     *writeto_addr++=TWDR;
  }
  }
   
     TWCR=(1<  while(!(TWCR&(1<  ;
  if((TWSR&0xf8)!=mr_data_nack)
   return FALSE;
  *writeto_addr=TWDR;
 
   TWCR=(1<  return TRUE;
}
void write_time(void)
{
  uchar i;
  uchar temp;
  uchar se[7]={0};
  se[2]=clock.second;//bcd
  se[3]=clock.minute;
  temp=clock.hour;
  if(temp>=0x12)
    temp|=BIT(6);
  else
    temp&=~BIT(6);
  temp&=~BIT(7);  //24 hour format
  se[4]=temp;    //hour
  i=clock.year;  //bcd
  i>>=4;         //bcd
  temp=clock.year&0x0f; //bcd
  i=i*10+temp;   //bcd
  temp=i/4;
  temp*=4;
  EEPROMwrite(year_base_eep_addr,temp);
  temp=i%4;
  temp<<=6;
  temp|=clock.day&0x3f;
  se[5]=temp;    //year/date
  //twi_read(wm_i2c_addr,&temp,sizeof(&temp));
  //temp&=0xe0;
  temp=clock.month&0x1f;
  se[6]=temp;
  temp=0;
  for(i=0;i<50;i++)  //many write
  {
    temp=twi_write(consta_i2c_addr,se,sizeof(se));
    if(temp)
   break;
  }  
}
void read_time(void)
{
   uchar i;
   uchar flag=0;
   uchar timer[7];
   uchar xx[1];
   for(i=0;i<50;i++)
   {
     flag=twi_read(consta_i2c_addr,timer,sizeof(timer));
  if(flag)
    break;
   }
  
   clock.second=timer[2];
   xx[0]=clock.second;
   clock.minute=timer[3];
   xx[0]=clock.minute;
   clock.hour=timer[4]&0x3f;
   xx[0]=clock.hour;
   clock.day=timer[5]&0x3f;
   xx[0]=clock.day;  
   clock.month=timer[6]&0x1f;
   xx[0]=clock.month;  
  
   //i=EEPROMread(year_base_eep_addr);  //yearbcd
   //clock.year=i+(timer[5]>>6);bcd
   //xx[0]=clock.year;  bcd
  
   i=timer[5]>>6;   //bcd
   i>>=4;
   xx[0]=timer[5]>>6;
   xx[0]&=0x0f;
   i=i*10+xx[0];
   i+=EEPROMread(year_base_eep_addr);
   xx[0]=i%10;
   i/=10;
   clock.year=(i<<4)+xx[0];  
  
  
   /*uchar temp;
   //uchar out;
   uchar te[6];
   uchar array[5];
   uchar *p;
   twi_read(second_i2c_addr,array,5);
   p=array;
   clock.second=*p++;
   te[0]=clock.second;
   clock.minute=*p++;
   te[1]=clock.minute;
   clock.hour=(*p++)&0x3f;
   te[2]=clock.hour;
   temp=*p++;
   clock.day=temp&0x3f;
   te[3]=clock.day;
   clock.year=(temp>>6)&0x03;
   te[5]=clock.year;
   temp=*p;
   clock.month=temp&0x1f;
   te[4]=clock.month;
   //clock.weekday=(temp>>5)&7;
   //te[0]=clock.weekday;*/
uchar write_time_flag=1;
uchar write_time_cnt;
void yearbase_turn()
{
   uchar i;
   uchar flag=0;
   uchar timer[7];
   uchar xx[2];
   for(i=0;i<50;i++)
   {
     flag=twi_read(consta_i2c_addr,timer,sizeof(timer));
  if(flag)
    break;
   }
  
   clock.second=timer[2];
   xx[0]=clock.second;
   clock.minute=timer[3];
   xx[0]=clock.minute;
   clock.hour=timer[4]&0x3f;
   xx[0]=clock.hour;
   clock.day=timer[5]&0x3f;
   xx[0]=clock.day;  
   clock.month=timer[6]&0x1f;
   xx[0]=clock.month;  
  
   i=EEPROMread(year_base_eep_addr);  //year
   //clock.year=i+(timer[5]>>6);
   //xx[0]=clock.year;  
  
   clock.year=timer[5]>>6;
   //xx[0]=clock.year;
   xx[0]=timer[5]>>6;
   xx[0]>>=4;
   xx[1]=timer[5]>>6;
   xx[1]&=0x0f;
   xx[0]=xx[0]*10+xx[1];
  
   //if(xx[0]%4==0)
   if(clock.year==0)
   {
     if(clock.month==1)
  {
    if(clock.day==1)
    {
     if(clock.hour==0)
  {
    if(clock.minute==0)
    {
      if(clock.second==0)
            {
              //i=EEPROMread(year_base_eep_addr);  //year
              i+=4;
           i%=100;
           EEPROMwrite(year_base_eep_addr,i);
            }
    }
  }
   }
 }
   }      
   //clock.year=i+clock.year; 
   xx[0]+=i;
   i=xx[0]%10;
   xx[0]/=10;
   clock.year=(xx[0]<<4)+i;
  
}  
  
void write_achar(uchar address,uchar data)
{
   uchar i;
   uchar flag;
   uchar addr_in;
   uchar data_in;
   //flag=0;
   addr_in=address;
   data_in=data;
  
   for(i=0;i<50;i++)
   {
     flag=twi_write(addr_in,&data_in,1);
  if(flag)
     break;
 }
}
//读一个字节
uchar read_achar(uchar address)
{
  uchar i;
  uchar addr_in;
  uchar flag;
  uchar temp;
  //flag=0;
  addr_in=address;
  for(i=0;i<50;i++)
  {
    flag=twi_read(addr_in,&temp,1);
 if(flag)
    break;
  }
  return temp; 
  /*uchar i;
  uchar flag=0;
  uchar *p;
  for(i=0;i<50;i++)
  {
    flag=twi_read(address,p,1);
 if(flag)
    break;
  }       
  return *p;*/
 
}    
void i2c_init(void)
{
  write_achar(rec_alarm_count_i2c,NULL);
  write_achar(send_count_i2c,NULL);
  write_achar(rec_card_count_i2c,NULL);

 
   
#define setbscl  PORTD|=BIT(0)
#define clrscl   PORTD&=~BIT(0)
#define setbsda  PORTD|=BIT(1)
#define clrsda   PORTD&=~BIT(1)
#define SDA      PORTD&BIT(1)
#define SomeNOP()  NOP();NOP();NOP();NOP();NOP();NOP()
void I2CStart(void)
{
   CLI();
   setbsda;
   setbscl;
   SomeNOP();
   setbsda;
   SomeNOP();
   clrscl;
}    
void I2CStop(void)
{
   clrscl;
   clrsda;
   SomeNOP();
   setbsda;
   SomeNOP();
   setbsda;
   SEI();
}    

uchar WaitAck(void)
{
   uchar errtime=255;
   setbsda;
   SomeNOP();
   setbscl;
   SomeNOP();
   while(SDA)
   {
     errtime--;
  if(!errtime)
  {
    I2CStop();
    return  false;
  }
 }
 return true;

   
  
void SendAck(void)
{
   setbsda;
   SomeNOP();
   setbscl;
   SomeNOP();
   clrscl;
}  
void SendNotAck(void)
{
   setbsda;
   SomeNOP();
   setbscl;
   SomeNOP();
   clrscl;
}
  
void I2CSendByte(uchar ch)
{
  uchar i=8;
  while(i--)
  {
    clrscl;
    nop;
    if(ch&0x80)
      setbsda;
    else
      clrsda;
    ch<<=1;
    SomeNOP();
    setbscl;
    SomeNOP();
  }
  clrscl;
}   

uchar I2CReceiveByte(void)
{
  uchar i=8;
  uchar ddata=0;
  setbsda;
  while(i--)
  {
     ddata<<=1;
  clrscl;
  SomeNOP();
  setbscl;
  SomeNOP();
  }
  clrscl;
  return ddata;
}
void GetPCF8583(uchar firsttype,uchar count,uchar *buff)
{
    uchar i;
 I2CStart();
 I2CSendByte(0xA0);
 WaitAck();
 I2CSendByte(firsttype);
 WaitAck();
 
 I2CStart();
 I2CSendByte(0xA1);
 WaitAck();
 
 for(i=0;i {
   buff[i]=I2CReceiveByte();
   if(i!=count-1) SendAck();
 }
 
 SendNotAck();
 I2CStop();
}
  
void SetPCF8583(uchar timetype,uchar value)
{
   I2CStart();
   I2CSendByte(0xA0);
   WaitAck();
   I2CSendByte(timetype);
   WaitAck();
   I2CSendByte(value);
   I2CStop();
}  
void delay(uint j)
{
    uint i,k;
 for(i=0;i  for(k=0;k<5;k++)
  {
      NOP();
   //WDT_off();
   } 
  
}  
 /* send0 */
void send0(uchar *data,uchar data_len)
{
  uchar i;
  uchar *p;
  i=0;
  p=data;
  PORTB|=BIT(3); //!
  //delay(400);
  delay(400); 
  do
  {
  while(!(UCSR0A&(1<     ;
     UDR0=*p++;
  i++;
  }
  while(i  //delay(400);
  delay(400);
  PORTB&=~BIT(3);  //!
  //delay(400);
}
 /* send1 */
void send1(uchar *data,uchar data_len)
{
  uchar i;
  uchar *p;
  i=0;
  p=data;
  do
  {
     while(!(UCSR1A&(1<     ;
     UDR1=*p++;
  i++;
  }
  while(i}
void revbuff_clear(uchar serial_no)
{
     uint i;
  uchar *p;
  i=0;
  if(serial_no)  //serial 1
  {
    rev1buff_cnt=0;
    p=rev1_buff;
    while(*p!=0xff&&i    {
       i++;
       p++;
    }
    while(i--)
      *--p=0xff;
  }
  else  //serial 0
  {
    rev0buff_cnt=0;
    p=rev0_buff;
    while(*p!=0xff&&i    {
       i++;
       p++;
    }
    while(i--)
      *--p=0xff;
  }

 
 
//update_proc
void update_proc(void)
{
    register uchar i;
    for(i=0;i    {
       if(update[i].flag)
          continue;
       else
       {
          update[i].cnt++;
          if(update[i].cnt>=update_timeout)
          {
             update[i].cnt=0;
             update[i].flag=1;
          }
       }
    }          
}           
           
                    
  
/*  rev0buff_cnt=0;
    i=0;
  p=rev0_buff;
  while(*p!=0xff&&i  {
    i++;
    p++;
   }
  while(i--)
   *--p=0xff;
}*/
 /* send short message */
/*void send1(uchar *message)
{
  uchar *p;
  p=message; 
 while(*p!='\0')
 {
  //PORTC&=~BIT(5);    //flow control
  //while(PORTC&(1<  //PORTC|=BIT(5);
  while(!(UCSR1A&(1<  ;
  UDR1=*p++;
 }
} */
/*for(i=0;i<4;i++)
 {               
    if(buttons[i].press_count==100)
 {
    buttons[i].press_flag=1;
    buttons[i].press_count=0;
 }//if
 }//for*/
 //button sample
 /*if(gates_open>0)
 {
    if((PORTG&button_code[0])==NULL)
   buttons[0].press_count++;
 if(gates_open>1)
 {
   if((PORTG&button_code[1])==NULL)
      buttons[1].press_count++;
   if(gates_open>2)
   {
      if((PORTG&button_code[2])==NULL)
      buttons[2].press_count++;
      if(gates_open>3)
   {
     if((PORTG&button_code[3])==NULL)
        buttons[3].press_count++;
      }
   }
 }         
  }*/
   /* ADC_sample[port_count][samp_count]=ADC;
 ADMUX|=port_count+1;
 if(start_reserve&0x01)
 {
   ADCSRA|=(1<   start_reserve>>=1;
 } 
 if(port_count++==ADC_amount)
 {
    port_count=0;
    start_reserve=start_flag;
    if(samp_count++==ADC_amount)
       samp_count=0;
 }*/    
/*  for(i=0;i<4;i++)
  {
     if(button[i].press_over=1)
  {
     button[i].press_over=0;
  //count process
  relay_close(i);
     /*switch(i)
  {
   case 0:
          PORTG|=0x40;
    break;
   case 1:
          PORTA|=0x80;
    break;
   case 2:
          PORTA|=0x40;
    break;
   case 3:
          PORTA|=0x20;
    break;
   }//switch
   relay[i].close_flag=1;
 }//if
  }//for
*/
//   uchar start; //start flag启用,禁用标志 
/*void gate_proc(void)
{
    uchar i;
 if(gate_open)  //
 {
    for(i=0;i    {
      if(guard[i].
}  
*/
  /*for(i=0;i  {
     if(gd_sample[i]!=guard[i].initial)
  {
     guard[i].except=1;
     BEEP=1;
   } 
  }*/
//uchar button_down_flag[4];
//uchar get_time(void)  //8583
//{
//  return 1;
//}
    //for(i=0;i<5;i++)
 //  putchar(sed[i]);
    //puts(dd);
  //puts(write_card);
     //for(size_addr=0;size_addr     //   send0(write_card[size_addr],9);
//#define serial_eep_addr serial0_eep_addr+2
/*struct button_stru
{
  uchar press_flag;
  uchar press_count;
};*/
/*//7.gate  no eep|no init
#define gate_amount 4
#define gate_open_eep_addr ADC_scetion-1
uchar gate_open;
struct gate_stru
{
   uchar status;   //1.open,0.close
   uchar open_time;  //unit  minute
   uchar normal_flag; //1.正常开门(刷卡,按钮),0.非正常开门
};
struct gate_stru gate[gate_amount];
uchar gate_sample[gate_amount];*/
   /*if(!(guard[piece].guard_type&0x02)) //
     {
       if(!(guard_setup&model[piece]))//未布防
       {
          if(last[0]&model[piece])  //命令要求布防
          guard_setup|=model[piece];
       }
      
       if(guard_setup&model[piece])  //已布防
       {
          if(!(last[0]&model[piece]))  //命令要求撤防
          guard_setup&=~model[piece];
       }*/
          //if(!(last[0]&model[piece]))  //命令要求撤防
       //   guard_setup&=~model[piece];
      //last=(uint)(rev0_buff[rev0buff_cnt-1]);
   //last<<=8;
   //last|=(uint)(rev0_buff[rev0buff_cnt-2]);
     /* if(rev1_buff[0]==2&&(rev1_buff[1]==4||rev1_buff[1]==5))
   {
     myname=rev1_buff[2]-1;
     if(myname==addr_no)   //is me?
     {
     }
    }*/
//if(card_result[0]==card[k].card_num[0]&&card_result[1]==card[k].card_num[1]&&card_result[2]==card[k].card_num[2])
 
阅读(1454) | 评论(0) | 转发(0) |
0

上一篇:ARM中文

下一篇:AVR定时器工作原理

给主人留下些什么吧!~~