Loading ...
Sorry, an error occurred while loading the content.

RE::my test code

Expand Messages
  • unpredicted24
    //the code is constructed in avr studio4 #define F_CPU 16000000UL #include #include #include #include
    Message 1 of 1 , Apr 25, 2011
    • 0 Attachment
      //the code is constructed in avr studio4
      #define F_CPU 16000000UL
      #include <avr/io.h>
      #include <util/delay.h>
      #include <stdio.h>
      #include <avr\interrupt.h>
      void init_port(void);
      void init_mouse(void);
      void send(unsigned char);
      ISR(INT0_vect) ;
      signed char packet[3];
      unsigned char dataready=0, packetcount=0, packetready=0, datareporting=0;

      void disable_receive() ;
      void init_receive();
      unsigned char data=0;
      #define INTR_ON() asm(" sei")
      int main()
      {
      init_port();
      init_mouse();
      while(1)
      {
      //display packet on LCD
      }
      }
      return 1;
      }

      void init_port(void)
      {
      DDRA=0x00;//input
      DDRB=0xFF;//output
      PORTB=0x00;
      DDRC=0xff;//output
      PORTC=0x00;
      DDRD=0x32;//D.1=output;D.2,D.3=input
      }

      void init_mouse(void)
      {
      disable_receive();
      send(0xFF);//reset command

      init_receive();
      while(data!=0xFA)//acknowledgement
      {}
      while(data!=0xAA)//Self Test
      {}
      while(data!=0x00)//Mouse ID
      {}

      _delay_ms(100);
      disable_receive();
      send(0xFF);//reset command
      init_receive();
      while(data!=0xFA)//acknowledgement
      {}
      while(data!=0xAA)//Self Test
      {}
      while(data!=0x00)//Mouse ID
      {}

      _delay_ms(100);
      send(0xFF);//reset command
      while(data!=0xFA)//acknowledgement
      {}
      while(data!=0xAA)//Self Test
      {}
      while(data!=0x00)//Mouse ID
      {}

      _delay_ms(100);

      send(0xf3);//set sampling rate
      while(data!=0xfa)//ack
      {}
      send(0x50);//sampling rate=80
      while(data!=0xfa)//ack
      {}

      _delay_ms(100);

      send(0xE8);//set Resolution
      while(data!=0xFA)//ack
      {}
      send(03);//8count/mm
      while(data!=0xfa);

      }

      void send(unsigned char command)
      {
      char parity =0;
      char mask=0x01;
      int i=0;
      //calculating odd parity
      parity=!((command & 0x01)^((command & 0x02)>>1)^
      ((command & 0x04)>>2)^((command & 0x08)>>3)^
      ((command & 0x10)>>4)^((command & 0x20)>>5)^
      ((command & 0x40)>>6)^((command & 0x80)>>7));
      PORTD|=1<<PD4;//PULL CLOCK LOW
      _delay_ms(120);

      PORTD|=1<<PD5;//pull data low
      _delay_ms(30);

      PORTD&=0<<PD4;//Release clock
      _delay_ms(30);
      while(!(PIND & 0x08));//clk high wait
      for (i=0;i<8;i++)
      {
      while(PIND & 0x08);//clk low wait
      PORTD=!(command & mask)<<PD5;//send inverted bits on data
      while(!(PIND&0x08));//clk high wait
      mask=mask<<1;
      }

      while(PIND &0x08);//clk low wait
      PORTD=!(parity)<<PD5;//send parity
      while(!(PIND & 0x08));//clk high wait
      while(PIND & 0x08);//clk low wait
      PORTD=0<<PD5;//stop bit inverted
      while(PIND & 0x04);//data low wait
      while(!(PIND & 0x04));//data high wait

      }

      void init_receive()
      {

      MCUCR = 0b00000000; // INT0 interrupt enable
      GICR = 0b01000000;
      INTR_ON();
      }

      void disable_receive()
      {

      GICR = 0; //Disable interrupts

      }

      ISR(INT0_vect)

      {
      unsigned char count=0,parity=0;
      data=0;
      while(PIND & 0x08);//wait for clock low
      for(count=0;count<8;count++)
      {
      while(!(PIND & 0x08));//wait for clock high
      while(PIND & 0x08);//wait for clock low
      data=data | (((PIND & 0x04)>>2) << count);
      }
      while(!(PIND & 0x08));//wait for clock high
      while(PIND & 0x08);//wait for clock low
      parity=((PIND & 0x04)>>2);
      while(!(PIND & 0x08));//wait for clock high
      while(PIND & 0x08);//wait for clock low
      while(!(PIND & 0x08)); // wait for clock high
      if(datareporting) packet[packetcount++]=data;
      if(packetcount > 2) {
      packetcount = 0;
      packetready = 1;
      }

      }
    Your message has been successfully submitted and would be delivered to recipients shortly.