Created
June 14, 2021 23:49
-
-
Save HadiElnemr/7d09c03197599bead50e0ff3aa840940 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Hadi Elnemr 46-6804 T-54 Mechatronics Lab Evaluation 8 | |
#define F_CPU 16000000 | |
#define __AVR_ATmega328P__ | |
#include <avr/io.h> | |
#include <util/delay.h> | |
void init_adc(void) { // AREF 0, channel 0. | |
ADMUX &= ~(1 << REFS0 | 1 << REFS1 | 1 << ADLAR | 1 << MUX0 | 1 << MUX1 | |
| 1 << MUX2 | 1 << MUX3); | |
ADCSRA |= 1 << ADEN; | |
DIDR0 |= 0xFF; | |
} | |
long adc_conv(void) { // Get pot. value | |
ADCSRA |= 1 << ADSC; | |
while (ADCSRA & (1 << ADSC)) | |
; | |
return ADC; | |
} | |
void enable_pwm(void) { // phase correct PWM | |
TCCR0A |= ((1 << WGM00) | (1 << COM0A1)); | |
TCCR0A &= ~(1 << WGM01) & ~(1 << COM0A0); | |
TCCR0B &= (~(1 << CS02)) & (~(1 << CS00)) & ~(1 << WGM02); | |
TCCR0B |= 1 << CS01; | |
} | |
void i2c_init() { | |
TWSR &= ~(1 << TWPS1) & ~(1 << TWPS0); | |
TWBR = 0x0C; | |
TWCR |= 1 << TWEN | 1 << TWINT | 1 << TWSTA; | |
while (TWCR & 1 << TWINT) | |
; | |
TWDR = 0x68 << 1; // send slave (mpu) address with write bit = 0 | |
TWCR |= 1 << TWINT; | |
while (TWCR & 1 << TWINT) | |
; | |
} | |
void mpu_init() { | |
TWDR = 0x6B; // Point to PWR_MGMT register | |
TWCR |= 1 << TWINT; | |
while (TWCR & 1 << TWINT) | |
; | |
TWCR |= 1 << TWINT; | |
TWDR = 0; // Write 0 to PWR_MGMT register | |
while (TWCR & 1 << TWINT) | |
; | |
} | |
signed long read_mpu() { | |
TWCR |= 1 << TWINT | 1 << TWSTA; | |
while (TWCR & 1 << TWINT) | |
; | |
TWDR = 0x68 << 1; // send slave (mpu) address with write bit = 0 | |
TWCR |= 1 << TWINT; | |
while (TWCR & 1 << TWINT) | |
; | |
TWDR = 0x43; // send gyro register address | |
TWCR |= 1 << TWINT | 1 << TWSTA; // start again | |
while (TWCR & 1 << TWINT) | |
; | |
TWDR = 0x68 << 1 | 1; // send slave (mpu) address with read bit = 1 | |
TWCR |= 1 << TWINT; | |
while (TWCR & 1 << TWINT) | |
; | |
TWCR |= 1 << TWINT; // wait for data to be read | |
while (TWCR & 1 << TWINT) | |
; | |
signed long ret = TWDR << 8; // store gyro_1 | |
TWCR |= 1 << TWINT; | |
while (TWCR & 1 << TWINT) | |
; // wait for data (Part 2) to be read | |
return ret | TWDR; | |
} | |
void lab8_eval(void) { | |
DDRD |= 1 << DDD2 | 1 << DDD3 | 1 << DDD6; // D2, D3, D6 output | |
DDRC &= ~(1 << DDC0) & ~(1 << DDC1) & ~(1 << DDC4) & ~(1 << DDC5); // Pin C4,5 in | |
init_adc(); | |
enable_pwm(); | |
i2c_init(); | |
mpu_init(); | |
enable_pwm(); | |
signed long gyrodata; | |
while (1) { | |
gyrodata = read_mpu(); | |
if(gyrodata > 0) | |
PORTD |= 1<<PORTD3 & ~(1<<PORTD2); | |
else | |
PORTD |= 1<<PORTD2 & ~(1<<PORTD3); | |
OCR0A = gyrodata; | |
} | |
} | |
int main(void) { | |
lab8_eval(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment