diff options
-rw-r--r-- | adc.cpp (renamed from adc.c) | 0 | ||||
-rw-r--r-- | avr.cpp (renamed from avr.c) | 18 | ||||
-rw-r--r-- | avr.h | 3 | ||||
-rw-r--r-- | hardware.c | 6 | ||||
-rw-r--r-- | hardware.cpp | 28 | ||||
-rw-r--r-- | i2c.cpp | 148 | ||||
-rw-r--r-- | i2c.h | 13 | ||||
-rw-r--r-- | main.cpp | 5 |
8 files changed, 215 insertions, 6 deletions
@@ -1,4 +1,5 @@ #include "avr.h" +#include "adc.h" #include "util.h" void setMotorSpeed(MOTOR *motor, int speed) { @@ -14,3 +15,20 @@ void setMotorSpeed(MOTOR *motor, int speed) { *motor->pwmPort = CLAMP(0, ABS(speed), 255); } + +int getButton() { + uint16_t val = getADCValue(7); + + if(val < 144) return 5; + if(val < 228) return 4; + if(val < 304) return 3; + if(val < 376) return 2; + if(val < 600) return 1; + + return 0; +} + +void waitForButton(int i) { + while(getButton() != CLAMP(0, i, 5)); + while(getButton() != 0); +} @@ -13,4 +13,7 @@ typedef struct { void setMotorSpeed(MOTOR *motor, int speed); +int getButton(); +void waitForButton(int i); + #endif diff --git a/hardware.c b/hardware.c deleted file mode 100644 index 6bc329e..0000000 --- a/hardware.c +++ /dev/null @@ -1,6 +0,0 @@ -#include "hardware.h" - - -void initHardware() { - -} diff --git a/hardware.cpp b/hardware.cpp new file mode 100644 index 0000000..41c7ea2 --- /dev/null +++ b/hardware.cpp @@ -0,0 +1,28 @@ +#include "hardware.h" +#include "adc.h" +#include "i2c.h" + + +void initHardware() { + DDRA = 0x00; + PORTA = 0xFF; + + DDRB = 0xFF; + PORTB = 0x00; + + DDRC = 0x7C; + PORTC = 0x83; + + DDRD = 0xFF; + PORTC = 0x00; + + TCCR0 = 0x62;
+ TCCR1A = 0xA1;
+ TCCR1B = 0x82; + OCR0 = 0; + OCR1A = 0; + OCR1B = 0; + + initADC(); + initI2C(); +} @@ -0,0 +1,148 @@ +#include "i2c.h" + +#include <util/twi.h> + + +static bool I2CStartSend(uint8_t addr); +static bool I2CStartRecv(uint8_t addr); +static void I2CStop(); + + +static bool I2CStartSend(uint8_t addr) { + TWCR = (1<<TWEN)|(1<<TWINT)|(1<<TWSTA); + + while(!(TWCR & (1<<TWINT))); + + if (TW_STATUS != TW_START) return false; + + TWDR = (addr&0xFE); + TWCR = (1<<TWEN)|(1<<TWINT); + + while(!(TWCR & (1<<TWINT))); + + if(TW_STATUS == TW_MT_SLA_ACK) + return true; + + I2CStop(); + return false; +} + +static bool I2CStartRecv(uint8_t addr) { + TWCR = (1<<TWEN)|(1<<TWINT)|(1<<TWSTA); + + while(!(TWCR & (1<<TWINT))); + + if (TW_STATUS != TW_START) return false; + + TWDR = (addr|1); + TWCR = (1<<TWEN)|(1<<TWINT); + + while(!(TWCR & (1<<TWINT))); + + if(TW_STATUS == TW_MR_SLA_ACK) + return true; + + I2CStop(); + return false; +} + +static void I2CStop() { + TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO); +} + + +void initI2C() { + TWBR = 10; + TWCR = (1<<TWEN); + TWSR = 0; +} + +bool I2CSendByte(uint8_t addr, uint8_t data) { + if(!I2CStartSend(addr)) return false; + + TWDR=data; + + TWCR = (1<<TWEN)|(1<<TWINT); + while(!(TWCR & (1<<TWINT))); + + if(TW_STATUS != TW_MT_DATA_ACK) { + I2CStop(); + return false; + } + + I2CStop(); + return true; +} + +bool I2CSend(uint8_t addr, uint8_t *data, int length) { + if(!I2CStartSend(addr)) return false; + + for(int i = 0; i < length; i++) { + TWDR=data[i]; + + TWCR = (1<<TWEN)|(1<<TWINT); + while(!(TWCR & (1<<TWINT))); + + if(TW_STATUS != TW_MT_DATA_ACK) { + I2CStop(); + return false; + } + } + + I2CStop(); + return true; +} + +bool I2CRecvByte(uint8_t addr, uint8_t *data) { + if(!I2CStartRecv(addr)) return false; + + while(!(TWCR & (1<<TWINT))); + + if(TW_STATUS != TW_MR_DATA_ACK && TW_STATUS != TW_MR_DATA_NACK) { + I2CStop(); + return false; + } + + *data = TWDR; + TWCR = (1<<TWEN)|(1<<TWINT); + + I2CStop(); + return true; +} + +int I2CRecv(uint8_t addr, uint8_t *data, int length) { + if(!I2CStartRecv(addr)) return -1; + + for(int i = 0; i < length-1; i++) { + while(!(TWCR & (1<<TWINT))); + + switch(TW_STATUS) { + case TW_MR_DATA_ACK: + data[i] = TWDR; + TWCR = (1<<TWEN)|(1<<TWINT)|(1<<TWEA); + break; + case TW_MR_DATA_NACK: + data[i] = TWDR; + TWCR = (1<<TWEN)|(1<<TWINT); + I2CStop(); + return i+1; + default: + I2CStop(); + return -1; + } + } + + while(!(TWCR & (1<<TWINT))); + + switch(TW_STATUS) { + case TW_MR_DATA_ACK: + case TW_MR_DATA_NACK: + data[length-1] = TWDR; + TWCR = (1<<TWEN)|(1<<TWINT); + I2CStop(); + return length; + } + + I2CStop(); + return -1; +} @@ -0,0 +1,13 @@ +#ifndef _ROBOCUP_I2C_H_ +#define _ROBOCUP_I2C_H_ + +#include <stdint.h> + + +void initI2C(); +bool I2CSendByte(uint8_t addr, uint8_t data); +bool I2CSend(uint8_t addr, uint8_t *data, int length); +bool I2CRecvByte(uint8_t addr, uint8_t *data); +int I2CRecv(uint8_t addr, uint8_t *data, int length); + +#endif diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..2a3f761 --- /dev/null +++ b/main.cpp @@ -0,0 +1,5 @@ +int main() { + + + return 0; +} |