SPARTAN Superway Personal Blog (Spring Week 4)
The team was looking into the Atting 841 and the needed communication protocol for the capacitors bank. The team also started to create a simple flowchart and a program draft for the slave attiny 841.
Attiny 841 running code
/* assume parallel Vcc to an analog pin;
Rx, Tx are binary numbers
PIN_A1, output binary as voltage #
PIN_A2, input as address */
#include <avr/sleep.h>
#include <EEPROM.h>
int addr = 0;
byte val;
byte target = 0;
int read_state = 0;
int voltage = 0;
const byte interruptPin = PIN_B0; // pin used for waking up
const byte receivingPin = PIN_A2; // Rx in 841
const byte transmittingPin = PIN_A1; // Tx in 841
const byte voltagePin = PIN_B1; // pin used to read voltage
void setup() {
pinMode(receivingPin, INPUT);
pinMode(interruptPin, INPUT);
pinMode(transmittingPin, OUTPUT);
pinMode(voltagePin, INPUT);
}
int wakeUpNow () {
delayMicroseconds(200);
}
void sleepNow () {
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
attachInterrupt(digitalPinToInterrupt(interruptPin), wakeUpNow, RISING);
sleep_mode();
sleep_disable();
detachInterrupt(digitalPinToInterrupt(interruptPin)); //turn off the interrupt
}
void loop() {
val = EEPROM.read(addr); //obtain address value
target = Serial.read(receivingPin); //obtain target value from the master
if (addr == target) {
voltage = Serial.read(voltagePin); //read voltage
analogWrite(transmittingPin, voltage); //output voltage value to Tx
}
sleepNow(); // sleep function called here
}
Comments
Post a Comment