Below are my code for part 1 1.smbus.cpp #include \"smbus.h\" #include \"Arduino
ID: 2082889 • Letter: B
Question
Below are my code for part 1
1.smbus.cpp
#include "smbus.h"
#include "Arduino.h"
#include "Wire.h"
/**
* Enables the TWI module, sets the correct clock rate, and performs
* any other initial housekeeping you may need done in this module.
*
* This function should be called by the user (you) before any other SMBUS_ functions.
*/
void SMBUS_init()
{
TWSR = 0x00;
TWBR = 0x0C;
TWCR = 1<<TWEN;
}
/**
* PRIVATE METHOD PROTOTYPES
*
* Declare any private methods here that you don't want the outside world to
* be able to call. By convention, private (internal) methods aren't prefaced with
* the module name.
*
*/
void start();
void stop();
void write(uint8_t data);
uint8_t read(uint8_t shouldAck);
/**
* MODULE METHOD IMPLEMENTATIONS
* Put your implementations for this module's methods here
*
* HINTS:
* SMBus specifies 7-bit addresses (0-127). The read/write bit
* that's part of the address byte sent on the bus is *not* considered
* to be part of the device's address.
*
* For example, to write to a device with address 0x20, you place 0x40
* on the bus; to read from 0x20, you place 0x41 on the bus.
*
* Make sure to take this into consideration when implementing
* these functions, as you may have to shift the address around a bit.
*
* Standard SMBus terminology:
* S: Start
* P: Stop
* Wr: Write enabled (R/W bit = 0)
* A: Ack
* NA: Nack
* []: Anything sent by the device
*/
/**
* Read from a register from the SMBus device
*
* devAddr: the 7-bit address specifying the device to read from.
* regAddr: the address of the device's register to read.
*
* Hint: the format of this transaction is:
* S devAddr Wr [A] regAddr [A] S devAddr Rd [A] [Data] NA P
*/
uint8_t SMBUS_readRegister(uint8_t devAddr, uint8_t regAddr)
{
start(); // S
write(devAddr << 1); // devAddr Wr [A]
write(regAddr); // regAddr [A}
start(); // S
write(devAddr << 1 | 0x01); // devAddr Rd [A]
uint8_t val = read(0); // [Data] NA
stop(); // P
return val;
}
/**
* Read a 16-bit word from the SMBus device
*
* devAddr: the 7-bit address specifying the device to read from.
* regAddr: the address of the device's register to read.
*
* Hint: the format of this transaction is:
* S devAddr Wr [A] regAddr [A] S devAddr Rd [A] [HiByte] A [LoByte] NA P
*/
//uint16_t SMBUS_readRegisterWord(uint8_t devAddr, uint8_t regAddr)
//{
// implement this function if you want to use it!
//}
/**
* Read a block of data from the SMBus device
*
* devAddr: the 7-bit address specifying the device to read from.
* regAddr: the address of the device's register to start reading from
* buffer: the buffer to read the data into
* Hint: the format of this transaction is:
* S devAddr Wr [A] regAddr [A] S devAddr Rd [A] ([Data] A, ...) [Data] NA P
*/
//void SMBUS_readRegister(uint8_t devAddr, uint8_t regAddr, uint8_t* buffer, size_t numBytesToRead)
//{
// implement this function if you want to use it!
//}
/**
* Write to an SMBus device's register.
*
* devAddr: the 7-bit address specifying the device to write to.
* regAddr: the address of the device's register to write.
* regVal: the value to write to the register.
*
* Hint: the format of this transaction is:
* format: S devAddr Wr [A] regAddr [A] regVal [A] P
*/
uint8_t SMBUS_writeRegister(uint8_t devAddr, uint8_t regAddr, uint8_t regVal)
{
start(); // S
write(devAddr << 1); // devAddr Wr [A]
write(regAddr); // regAddr [A]
write(regVal); // regVal [A]
stop(); // P
}
/**
* Low-level I2C functions.
*
* Implement your TWI register-level code in these functions
*/
/**
* Start an i2C transaction.
*/
void start()
{
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
while ((TWCR & (1<<TWINT)) == 0);
}
/**
* Stop an i2C transaction.
*/
void stop()
{
TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
}
/**
* Write a byte to the bus.
*
* data: the byte to write.
*/
void write(uint8_t data)
{
TWDR = data;
TWCR = (1<<TWINT)|(1<<TWEN);
while ((TWCR & (1<<TWINT)) == 0);
}
/**
* Read a byte from the bus.
*
* shouldAck: whether to send an ACK or a NACK.
* returns: the byte read from the bus.
*/
uint8_t read(uint8_t shouldAck)
{
TWCR = (1<<TWINT)|(1<<TWEN)|(shouldAck ? (1<<TWEA) : 0);
while ((TWCR & (1<<TWINT)) == 0);
return TWDR;
}
2. smbus.h
/** SMBus Interface Driver
*
* This .h file provides the function declarations that let you interact with SMBus-compliant devices
* using the i2C master mode of the AVR's TWI module.
*
* Do not modify these function declarations. You'll implement these functions in the corresponding
* smbus.cpp source file.
**/
/** Tell the compiler to only include this file once per compilation,
* just in case it's included in multiple source files part of the same compilation.
* The old way of doing this was with "include guards" (see https://en.wikipedia.org/wiki/Include_guard)
**/
#pragma once
#include <stdint.h>
void SMBUS_init();
uint8_t SMBUS_readRegister(uint8_t devAddr, uint8_t regAddr);
uint8_t SMBUS_writeRegister(uint8_t devAddr, uint8_t regAddr, uint8_t regVal);
// optional functions you can implement if you want to
//uint16_t SMBUS_readRegisterWord(uint8_t devAddr, uint8_t regAddr);
//void SMBUS_readRegister(uint8_t devAddr, uint8_t regAddr, uint8_t* buffer, size_t numBytesToRead);
3. main function
#include "smbus.h"
#include "Arduino.h"
#include <math.h>
#define DEVADDRESS 0b1101000
#define POW_MAG_REGADDR 0x6b
#define ACCEL_X_HIGH_REGADDR 0x3b
#define ACCEL_X_LOW_REGADDR 0x3c
#define ACCEL_Y_HIGH_REGADDR 0x3d
#define ACCEL_Y_LOW_REGADDR 0x3e
#define ACCEL_Z_HIGH_REGADDR 0x3f
#define ACCEL_Z_LOW_REGADDR 0x40
void setup () {
Serial.begin(9600);
SMBUS_init();
SMBUS_writeRegister(DEVADDRESS, POW_MAG_REGADDR, 0x00);
}
void loop(){
int16_t x1 = 0x00;
int16_t y1 = 0x00;
int16_t z1 = 0x00;
double a;
double b;
//double v;
//double w;
SMBUS_readRegister(DEVADDRESS, ACCEL_X_HIGH_REGADDR);
SMBUS_readRegister(DEVADDRESS, ACCEL_X_LOW_REGADDR);
SMBUS_readRegister(DEVADDRESS, ACCEL_Y_HIGH_REGADDR);
SMBUS_readRegister(DEVADDRESS, ACCEL_Y_LOW_REGADDR);
SMBUS_readRegister(DEVADDRESS, ACCEL_Z_HIGH_REGADDR);
SMBUS_readRegister(DEVADDRESS, ACCEL_Z_LOW_REGADDR);
x1 = (SMBUS_readRegister(DEVADDRESS, ACCEL_X_HIGH_REGADDR)<< 8)|SMBUS_readRegister(DEVADDRESS, ACCEL_X_LOW_REGADDR);
y1 = (SMBUS_readRegister(DEVADDRESS, ACCEL_Y_HIGH_REGADDR)<< 8)|SMBUS_readRegister(DEVADDRESS, ACCEL_Y_LOW_REGADDR);
z1 = (SMBUS_readRegister(DEVADDRESS, ACCEL_Z_HIGH_REGADDR)<< 8)|SMBUS_readRegister(DEVADDRESS, ACCEL_Z_LOW_REGADDR);
a = atan2((double) y1,(double) x1);
//v = asin((double) x1);
//w = acos((double) y1);
b = (a/(2*3.142))*360;
Serial.println (b);
//Serial.println (v);
//Serial.println (w);
delay(100);
}
Please use arduino to compile.
Now that you can accurately capture the angle of the digital level, you need to display it to the user. The serial monitor is fine for debugging, but it's not exactly practical for displaying the angle in a consumer product. You can choose one of following two methods for displaying the angle 1. Seven-segment angle read-out: Add two seven-segment displays (you can purchase these at the EE Shop) to print the current angle. You will need an additional LED to use as a "minus" symbol You will need to use two shift registers to drive the two seven-segment displays. 2. LCD Screen for 2D Leveling: Interface a Nokia 5110 graphical LCD module with your ATMega328p using SPI. Draw a circle or box on the LCD that moves around to indicate the level's orientation in both horizontal and vertical directions. Provide some sort of indication when the device is level -maybe enlarge the circle on the screen, flash it on or off, or flash the backlight. The Nokia 5110 LCD is a simple black-and-white graphical display with a resolution of 84x48 pixels. This is a great option for adventurous students interested in basic computer graphics programming, and we have a few of these LCDs I can hand out to students who come and see me in the lab (until supplies run out) They're also available on Amazon with fast Prime shipping, or on eBay, if you want to buy one to keep after the class is done. Just when you thought you had everything working, the project manager emails you with a few new feature requests for the product. You tell her that you don't have enough time to get all of them implemented before the next prototype is spun off, but you'll hammer outExplanation / Answer
Step1- What is a seven segment Display?
Step2-Parts and Tools
Step3-Testing the Display
Step4-Built the circuit
Step5-Project1-one digit count down timer
Step6-Multiplexing
Step7-Project2-two digit count down timer
Step8-Final Thoughts
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.