Code-Work @ lowlevel

This commit is contained in:
sicarius 2007-02-13 19:55:03 +00:00
parent 0dbbdb4db3
commit b090fa7757
3 changed files with 327 additions and 0 deletions

View file

@ -0,0 +1,39 @@
maussensor.o: ../hal/maussensor.c ../hal/maussensor.h ../hal/../global.h \
c:/winavr/bin/../avr/include/stdlib.h \
c:\winavr\bin\../lib/gcc/avr/4.1.1/include/stddef.h \
../hal/../hal/board.h c:/winavr/bin/../avr/include/avr/io.h \
c:/winavr/bin/../avr/include/avr/sfr_defs.h \
c:/winavr/bin/../avr/include/inttypes.h \
c:/winavr/bin/../avr/include/stdint.h \
c:/winavr/bin/../avr/include/avr/iom128.h \
c:/winavr/bin/../avr/include/avr/portpins.h \
c:/winavr/bin/../avr/include/avr/version.h \
c:/winavr/bin/../avr/include/avr/interrupt.h ../hal/../hal/uart.h
../hal/maussensor.h:
../hal/../global.h:
c:/winavr/bin/../avr/include/stdlib.h:
c:\winavr\bin\../lib/gcc/avr/4.1.1/include/stddef.h:
../hal/../hal/board.h:
c:/winavr/bin/../avr/include/avr/io.h:
c:/winavr/bin/../avr/include/avr/sfr_defs.h:
c:/winavr/bin/../avr/include/inttypes.h:
c:/winavr/bin/../avr/include/stdint.h:
c:/winavr/bin/../avr/include/avr/iom128.h:
c:/winavr/bin/../avr/include/avr/portpins.h:
c:/winavr/bin/../avr/include/avr/version.h:
c:/winavr/bin/../avr/include/avr/interrupt.h:
../hal/../hal/uart.h:

View file

@ -0,0 +1,163 @@
#include "maussensor.h"
Maussensor::Maussensor(int index) {
if(index == MAUSSENSOR0) {
sda_pin = MAUS0_SDA;
sck_pin = MAUS0_SCK;
}
else {
sda_pin = MAUS1_SDA;
sck_pin = MAUS1_SCK;
}
}
Maussensor::~Maussensor() {
}
// Gibt die X-Koordinate Zurück
uint8 Maussensor::GetX() {
return maus_sens_read(MOUSE_DELTA_X_REG);
}
// Gibt die Y-Koordinate Zurück
uint8 Maussensor::GetY() {
return maus_sens_read(MOUSE_DELTA_Y_REG);
}
/*!
* Gibt den SQUAL-Wert zurueck. Dieser gibt an, wieviele Merkmale der Sensor
* im aktuell aufgenommenen Bild des Untergrunds wahrnimmt
*/
uint8 Maussensor::GetSqual() {
return maus_sens_read(MOUSE_SQUAL_REG);
}
/*!
* Uebertraegt ein Byte an den Sensor
* @param data das Byte
*/
void Maussensor::maus_sens_writeByte(uint8 data){
int8 i;
MAUS_DDR |= (1<<sda_pin); // SDA auf Output
for (i=7; i>=0; i--){
MAUS_PORT &= ~(1<<sck_pin); // SCK auf Low, vorbereiten
//Daten rausschreiben
MAUS_PORT = (MAUS_PORT & (~MAUS_PIN)) | ((data >> (7 - sda_pin)) & (1<<sda_pin));
data = data <<1; // naechstes Bit vorbereiten
asm volatile("nop"); // Etwas warten
MAUS_PORT |= (1<<sck_pin); // SCK =1 Sensor uebernimmt auf steigender Flanke
}
}
/*!
* Liest ein Byte vom Sensor
* @return das Byte
*/
uint8 Maussensor::maus_sens_readByte(void){
int i;
char data=0;
MAUS_DDR &= ~(1<<sda_pin); // SDA auf Input
for (i=7; i>-1; i--){
MAUS_PORT &= ~(1<<sck_pin); // SCK =0 Sensor bereitet Daten auf fallender Flanke vor !
data=data<<1; // Platz schaffen
asm volatile("nop"); // Etwas warten
MAUS_PORT |= (1<<sck_pin); // SCK =1 Daten lesen auf steigender Flanke
data |= (MAUS_PIN >> sda_pin) & 0x01; //Daten lesen
}
return data;
}
/*!
* Uebertraegt ein write-Kommando an den Sensor
* @param adr Adresse
* @param data Datum
*/
void Maussensor::maus_sens_write(int8 adr, uint8 data){
int16 i;
//MOUSE_Enable();
maus_sens_writeByte(adr|=0x80); //rl MSB muss 1 sein Datenblatt S.12 Write Operation
maus_sens_writeByte(data);
for (i=0; i<300; i++){ asm volatile("nop"); } // mindestens 100 Mikrosekunden Pause!!!
}
/*!
* Schickt ein Lesekommando an den Sensor
* und liest ein Byte zurueck
* @param adr die Adresse
* @return das Datum
*/
uint8 Maussensor::maus_sens_read(uint8 adr){
//MOUSE_Enable();
int16 i;
maus_sens_writeByte(adr);
for (i=0; i<300; i++){asm volatile("nop");} // mindestens 100 Mikrosekunden Pause!!!
return maus_sens_readByte();
}
/*!
* Initialisiere Maussensor
*/
void Maussensor::maus_sens_init(void){
msleep(100);
MAUS_DDR |= (1<<sck_pin); // SCK auf Output
MAUS_PORT &= ~(1<<sck_pin); // SCK auf 0
msleep(10);
maus_sens_write(MOUSE_CONFIG_REG,MOUSE_CFG_RESET); //Reset sensor
maus_sens_write(MOUSE_CONFIG_REG,MOUSE_CFG_FORCEAWAKE); //Always on
}
/*! muessen wir nach dem ersten Pixel suchen?*/
static uint8 firstRead;
/*!
* Bereitet das auslesen eines ganzen Bildes vor
*/
void Maussensor::maus_image_prepare(void){
maus_sens_write(MOUSE_CONFIG_REG,MOUSE_CFG_FORCEAWAKE); //Always on
maus_sens_write(MOUSE_PIXEL_DATA_REG,0x00); // Frame grabben anstossen
firstRead=1; //suche erstes Pixel
}
/*!
* Liefert bei jedem Aufruf das naechste Pixel des Bildes
* Insgesamt gibt es 324 Pixel
* <pre>
* 18 36 ... 324
* .. .. ... ..
* 2 20 ... ..
* 1 19 ... 307
* </pre>
* Bevor diese Funktion aufgerufen wird, muss maus_image_prepare() aufgerufen werden!
* @return Die Pixeldaten (Bit 0 bis Bit5), Pruefbit, ob Daten gueltig (Bit6), Markierung fuer den Anfang eines Frames (Bit7)
*/
int8 Maussensor::maus_image_read(void){
int8 pixel=maus_sens_read(MOUSE_PIXEL_DATA_REG);
if ( firstRead ==1){
while ( (pixel & 0x80) != 0x80){
pixel=maus_sens_read(MOUSE_PIXEL_DATA_REG);
// if ((pixel & 0x70) != 0x70)
// return 0;
}
firstRead=0;
}
return pixel;
}

View file

@ -0,0 +1,125 @@
#ifndef _MAUSSENSOR_H_
#define _MAUSSENSOR_H_
#include "../global.h"
// Wir klauen die Mausdefinitionen beim CT-Bot ;)
#define ADNS2610 /*!< Welcher Sensortyp? ADNS2610 oder 2620 */
#ifdef ADNS2610
/* ADNS2610 */
#define MOUSE_CONFIG_REG 0x00 /*!< Reset, Power Down, Forced Awake, etc */
#define MOUSE_STATUS_REG 0x01 /*!< Product ID, Mouse state of Asleep or Awake */
#define MOUSE_DELTA_Y_REG 0x02 /*!< Y Movement */
#define MOUSE_DELTA_X_REG 0x03 /*!< X Movement */
#define MOUSE_SQUAL_REG 0x04 /*!< Measure of the number of features visible by the sensor */
#define MOUSE_MAX_PIXEL_REG 0x05 /*!< Maximum Pixel value in current frame.*/
#define MOUSE_MIN_PIXEL_REG 0x06 /*!< Minimum Pixel value in current frame.*/
#define MOUSE_PIXEL_SUM_REG 0x07 /*!< This register is used to find the average pixel value.*/
#define MOUSE_PIXEL_DATA_REG 0x08 /*!< Actual picture of surface */
#define MOUSE_SHUTTER_UPPER_REG 0x09 /*!< The sensor adjusts the shutter to keep the average and maximum pixel values within normal operating ranges.*/
#define MOUSE_SHUTTER_LOWER_REG 0x0A /*!< The sensor adjusts the shutter to keep the average and maximum pixel values within normal operating ranges.*/
#define MOUSE_INVERSE_PRODUCT_ID_REG 0x11 /*!< Inverse Product ID */
#define MOUSE_CFG_RESET 0x80 /*!< Reset Circuit */
#define MOUSE_CFG_POWERDOWN 0x40 /*!< Power Down analog Circuit */
#define MOUSE_CFG_FORCEAWAKE 0x01 /*!< Keep Sensor awake */
#else
/* ADNS2620 */
#define MOUSE_CONFIG_REG 0x40 /*!< Reset, Power Down, Forced Awake, etc */
#define MOUSE_STATUS_REG 0x41 /*!< Product ID, Mouse state of Asleep or Awake */
#define MOUSE_DELTA_Y_REG 0x42 /*!< Y Movement */
#define MOUSE_DELTA_X_REG 0x43 /*!< X Movement */
#define MOUSE_SQUAL_REG 0x44 /*!< Measure of the number of features visible by the sensor */
#define MOUSE_MAX_PIXEL_REG 0x45 /*!< Maximum Pixel value in current frame.*/
#define MOUSE_MIN_PIXEL_REG 0x46 /*!< Minimum Pixel value in current frame.*/
#define MOUSE_PIXEL_SUM_REG 0x47 /*!< This register is used to find the average pixel value.*/
#define MOUSE_PIXEL_DATA_REG 0x48 /*!< Actual picture of surface */
#define MOUSE_SHUTTER_UPPER_REG 0x49 /*!< The sensor adjusts the shutter to keep the average and maximum pixel values within normal operating ranges.*/
#define MOUSE_SHUTTER_LOWER_REG 0x4A /*!< The sensor adjusts the shutter to keep the average and maximum pixel values within normal operating ranges.*/
#define MOUSE_FRAME_PERIOD_REG 0x4B /*!< The frame period counter counts up until it overflows. */
#endif
// Unsere Pins vom Maussensor
#define MAUS_DDR DDRC // Die Mäuse hängen am DDRC
#define MAUS_PORT PORTC
#define MAUS_PIN PINC
#define MAUS0_SDA 4
#define MAUS0_SCK 6
#define MAUS1_SDA 5
#define MAUS1_SCK 7
// Konstanten fürs intialisieren
#define MAUSSENSOR0 0
#define MAUSSENSOR1 1
class Maussensor
{
private:
// Pins, an denen der Maussensor angeschlossen ist
int sda_pin;
int sck_pin;
/*!
* Initialisiere Maussensor
*/
void maus_sens_init(void);
/*!
* Schickt ein Lesekommando an den Sensor
* und liest ein Byte zurueck
* @param adr die Adresse
* @return das Datum
*/
uint8 maus_sens_read(uint8 adr);
uint8 maus_sens_readByte();
/*! Stellt sicher, dass der Maussensor nicht mehr die serielle Datenleitung treibt */
#define maus_sens_highZ() maus_sens_write(0x77,0x00)
/*!
* Uebertraegt ein write-Kommando an den Sensor
* @param adr Adresse
* @param data Datum
*/
void maus_sens_write(int8 adr, uint8 data);
void maus_sens_writeByte(uint8 data);
/*!
* Liefert bei jedem Aufruf das naechste Pixel des Bildes
* Insgesamt gibt es 324 Pixel
* <pre>
* 18 36 ... 324
* .. .. ... ..
* 2 20 ... ..
* 1 19 ... 307
* </pre>
* Bevor diese Funktion aufgerufen wird, muss maus_image_prepare() aufgerufen werden!
* @return Die Pixeldaten (Bit 0 bis Bit5), Pruefbit, ob Daten gueltig (Bit6), Markierung fuer den Anfang eines Frames (Bit7)
*/
int8 maus_image_read(void);
/*!
* Bereitet das auslesen eines ganzen Bildes vor
*/
void maus_image_prepare(void);
/*!
* Gibt den SQUAL-Wert zurueck. Dieser gibt an, wieviele Merkmale der Sensor
* im aktuell aufgenommenen Bild des Untergrunds wahrnimmt
*/
public:
Maussensor(int index);
~Maussensor();
uint8 GetX();
uint8 GetY();
uint8 GetSqual();
};
#endif