Move master branch from from mios git repo
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
#include <spi.h>
|
||||
#include <ds1305.h>
|
||||
|
||||
#define DS1305_PIN 4
|
||||
#define LED_PIN 3
|
||||
|
||||
// following two methods required by DS1305 library to select/deselect device
|
||||
void select_ds1305(void)
|
||||
{
|
||||
digitalWrite(DS1305_PIN, HIGH);
|
||||
}
|
||||
|
||||
void deselect_ds1305(void)
|
||||
{
|
||||
digitalWrite(DS1305_PIN, LOW);
|
||||
}
|
||||
|
||||
// flash led that's connected to pin 3
|
||||
void flash_led(void)
|
||||
{
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
for (int i=0; i<10; i++) {
|
||||
digitalWrite(LED_PIN, i%2==0);
|
||||
delay(50);
|
||||
}
|
||||
}
|
||||
|
||||
// called when pin INT0 goes from 1 to 0
|
||||
ISR(INT0_vect)
|
||||
{
|
||||
// get alarm value to clear alarm interrupt flag on DS1305
|
||||
DS1305_DATETIME alarm;
|
||||
get_alarm0(&alarm);
|
||||
flash_led();
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
// flash LED at start to indicate were about to start
|
||||
flash_led();
|
||||
flash_led();
|
||||
|
||||
// pin used to enable DS1305
|
||||
pinMode(DS1305_PIN, OUTPUT);
|
||||
|
||||
// Make sure ADC is unselected and setup spi
|
||||
deselect_ds1305();
|
||||
setup_spi(SPI_MODE_1, SPI_MSB, SPI_NO_INTERRUPT, SPI_MSTR_CLK16);
|
||||
set_control(0);
|
||||
|
||||
// set current date/time to Thursday 06/05/09 20:32:30
|
||||
DS1305_DATETIME current = {0x30, 0x32, 0x20, 0x05, 0x06, 0x05, 0x09};
|
||||
set_time(¤t);
|
||||
|
||||
// change the value of alarm to the required time/granularity:
|
||||
// raise alarm every day (at 20:32:00):
|
||||
// DS1305_DATETIME alarm = {0x00, 0x32, 0x20, 0x00 | DS1305_ALARM_SET};
|
||||
// raise alarm every minute (when seconds = 00):
|
||||
// DS1305_DATETIME alarm = {0x00, 0x32 | DS1305_ALARM_SET,
|
||||
// 0x20 | DS1305_ALARM_SET, 0x05 | DS1305_ALARM_SET};
|
||||
// raise alarm every second:
|
||||
DS1305_DATETIME alarm = {0x55 | DS1305_ALARM_SET, 0x32 | DS1305_ALARM_SET,
|
||||
0x20 | DS1305_ALARM_SET, 0x05 | DS1305_ALARM_SET};
|
||||
set_alarm0(&alarm);
|
||||
// turn on timer and make alarm0 lower INT0 pin when alarm is triggered
|
||||
set_control(DS1305_EOSC | DS1305_INTCN | DS1305_AIE0);
|
||||
|
||||
// raise interrupt when INT0 pin falls (pin PD0 at AT90usbXXX, pin PD2 on ATmegaXXX (arduino pin 2))
|
||||
// the code in ISR(INT0_vect) above will be called
|
||||
EICRA = (1<<ISC01) | (0<<ISC00);
|
||||
// enable interrupts
|
||||
EIMSK = (1<<INT0);
|
||||
sei();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// do nothing - all action happens in interrupt
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#include <spi.h>
|
||||
|
||||
// MAX6675 connected to pin D9
|
||||
#define MAX6675_SELECT_PIN 8
|
||||
#define DESELECT_MAX6675 digitalWrite(MAX6675_SELECT_PIN, HIGH)
|
||||
#define SELECT_MAX6675 digitalWrite(MAX6675_SELECT_PIN, LOW)
|
||||
|
||||
void setup()
|
||||
{
|
||||
pinMode(MAX6675_SELECT_PIN, OUTPUT);
|
||||
DESELECT_MAX6675;
|
||||
setup_spi(SPI_MODE_1, SPI_MSB, SPI_NO_INTERRUPT, SPI_MSTR_CLK8);
|
||||
Serial.begin(19200);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
delay(500);
|
||||
|
||||
// select the device, wait > 100nS, read two bytes, deselect
|
||||
SELECT_MAX6675;
|
||||
delayMicroseconds(1);
|
||||
unsigned char highByte = send_spi(0);
|
||||
unsigned char lowByte = send_spi(0);
|
||||
DESELECT_MAX6675;
|
||||
|
||||
// if bit 3 is high thermocouple is unconnected
|
||||
if (lowByte & (1<<2)) {
|
||||
Serial.print("Not connected ");
|
||||
Serial.print(highByte, HEX); Serial.print(" ");
|
||||
Serial.println(lowByte, HEX);
|
||||
} else {
|
||||
// temperature value is in bits 6-0 of highByte and 7-2 of lowByte
|
||||
short value = (highByte << 5 | lowByte>>3);
|
||||
// 1 bit is 0.25 degree 'divide' by 4 to show degrees
|
||||
Serial.print(value/4); Serial.print("."); Serial.println(value%4 *25);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#include <spi.h>
|
||||
|
||||
#define SELECT_ADC digitalWrite(8, LOW);
|
||||
#define DESELECT_ADC digitalWrite(8, HIGH);
|
||||
|
||||
unsigned short read_adc(void)
|
||||
{
|
||||
// select ADC wait 100microseconds then read two bytes
|
||||
SELECT_ADC;
|
||||
unsigned char one = send_spi(0xFF);
|
||||
unsigned char two = send_spi(0xFF);
|
||||
DESELECT_ADC;
|
||||
// 12 bits of ADC value is bottom 5 bits of first
|
||||
// byte and top 7 bits of second, move into 16 bit int
|
||||
return ((0x1F & one) << 7) | (two >> 1);
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
pinMode(8, OUTPUT);
|
||||
Serial.begin(19200);
|
||||
// make sure ADC is not selected and setup spi
|
||||
DESELECT_ADC;
|
||||
setup_spi(SPI_MODE_0, SPI_MSB, SPI_NO_INTERRUPT, SPI_MSTR_CLK16);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
unsigned short num = read_adc();
|
||||
Serial.println(num);
|
||||
delay(1000);
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
#include <spi.h>
|
||||
#include <util/delay.h>
|
||||
|
||||
#define BUTTON_PIN 2
|
||||
#define LED_PIN 3
|
||||
#define FLASH_LED_COMMAND 0x01
|
||||
|
||||
#define OTHER_SELECT_PIN 7
|
||||
#define SELECT_OTHER digitalWrite(OTHER_SELECT_PIN, LOW);
|
||||
#define DESELECT_OTHER digitalWrite(OTHER_SELECT_PIN, HIGH);
|
||||
|
||||
#define BUFSIZE 20
|
||||
volatile unsigned char incoming[BUFSIZE];
|
||||
volatile short int received=0;
|
||||
|
||||
// flash led that's connected to pin PD7
|
||||
void flash_led(int count)
|
||||
{
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
for (int i=0; i<count*2; i++) {
|
||||
digitalWrite(LED_PIN, i%2==0);
|
||||
_delay_ms(75);
|
||||
}
|
||||
}
|
||||
|
||||
// send a SPI message to the other device - 3 bytes then go back into
|
||||
// slave mode
|
||||
void send_message()
|
||||
{
|
||||
setup_spi(SPI_MODE_1, SPI_MSB, SPI_NO_INTERRUPT, SPI_MSTR_CLK8);
|
||||
if (SPCR & (1<<MSTR)) { // if we are still in master mode
|
||||
SELECT_OTHER; // tell other device to flash LED twice
|
||||
send_spi(FLASH_LED_COMMAND); send_spi(0x02); send_spi(0x00);
|
||||
DESELECT_OTHER;
|
||||
}
|
||||
setup_spi(SPI_MODE_1, SPI_MSB, SPI_INTERRUPT, SPI_SLAVE);
|
||||
}
|
||||
|
||||
|
||||
// called when button pushed and pin INT0 goes from 1 to 0
|
||||
void button_pressed()
|
||||
{
|
||||
send_message();
|
||||
_delay_ms(500); // 'debounce'
|
||||
}
|
||||
|
||||
// parse the data received from the other device
|
||||
// currently just knows about the FLASH_LED_COMMAND
|
||||
void parse_message()
|
||||
{
|
||||
switch(incoming[0]) {
|
||||
case FLASH_LED_COMMAND:
|
||||
flash_led(incoming[1]);
|
||||
break;
|
||||
default:
|
||||
flash_led(20);
|
||||
}
|
||||
}
|
||||
|
||||
// called by the SPI system when there is data ready.
|
||||
// Just store the incoming data in a buffer, when we receive a
|
||||
// terminating byte (0x00) call parse_message to process the data received
|
||||
ISR(SPI_STC_vect)
|
||||
{
|
||||
incoming[received++] = received_from_spi(0x00);
|
||||
if (received == BUFSIZE || incoming[received-1] == 0x00) {
|
||||
parse_message();
|
||||
received = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
// make sure other device is unselected (pin is HIGH) and setup spi
|
||||
DESELECT_OTHER;
|
||||
pinMode(OTHER_SELECT_PIN, OUTPUT);
|
||||
setup_spi(SPI_MODE_1, SPI_MSB, SPI_INTERRUPT, SPI_SLAVE);
|
||||
|
||||
// raise interrupt when INT0 pin falls (arduino pin 2))
|
||||
// the function button_pressed will be called
|
||||
attachInterrupt(0, button_pressed, FALLING);
|
||||
|
||||
// flash LED at start to indicate were ready
|
||||
flash_led(1);
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* First stab at a test suite for spi lib.
|
||||
*/
|
||||
#include <spi.h>
|
||||
|
||||
void reportError(int line, char *message, uint8_t expected, uint8_t actual)
|
||||
{
|
||||
Serial.print("Test "); Serial.print(message);
|
||||
Serial.print(" failed at line "); Serial.print(line-3);
|
||||
Serial.print(", expected="); Serial.print(expected, BIN);
|
||||
Serial.print(", actual="); Serial.println(actual, BIN);
|
||||
}
|
||||
|
||||
#define assertEquals(message, expected, actual) \
|
||||
if (expected != actual) { reportError(__LINE__, message, expected, actual); return; }
|
||||
|
||||
|
||||
#define test_setup_spi(message, eSPCR, eSPSR, mode, dord, interrupt, clock) \
|
||||
setup_spi(mode, dord, interrupt, clock); \
|
||||
assertEquals(message, eSPCR, SPCR); \
|
||||
assertEquals(message, eSPSR, SPSR & 0x01);
|
||||
|
||||
|
||||
void test_registers()
|
||||
{
|
||||
// top 4 bits of SPCR - enabled, interrupt, lsb/msb, master/slave
|
||||
disable_spi();
|
||||
assertEquals("0", 0x00, SPCR);
|
||||
|
||||
test_setup_spi("1", 0xE0, 0x00, SPI_MODE_0, SPI_LSB, SPI_INTERRUPT, SPI_SLAVE);
|
||||
test_setup_spi("2", 0x60, 0x00, SPI_MODE_0, SPI_LSB, SPI_NO_INTERRUPT, SPI_SLAVE);
|
||||
test_setup_spi("3", 0x40, 0x00, SPI_MODE_0, SPI_MSB, SPI_NO_INTERRUPT, SPI_SLAVE);
|
||||
|
||||
// CPOL/CPHA bits - timing mode
|
||||
test_setup_spi("4", 0x40, 0x00, SPI_MODE_0, SPI_MSB, SPI_NO_INTERRUPT, SPI_SLAVE);
|
||||
test_setup_spi("5", 0x44, 0x00, SPI_MODE_1, SPI_MSB, SPI_NO_INTERRUPT, SPI_SLAVE);
|
||||
test_setup_spi("6", 0x48, 0x00, SPI_MODE_2, SPI_MSB, SPI_NO_INTERRUPT, SPI_SLAVE);
|
||||
test_setup_spi("7", 0x4C, 0x00, SPI_MODE_3, SPI_MSB, SPI_NO_INTERRUPT, SPI_SLAVE);
|
||||
|
||||
// clock
|
||||
test_setup_spi("9", 0xE0, 1, SPI_MODE_0, SPI_LSB, SPI_INTERRUPT, SPI_MSTR_CLK2);
|
||||
test_setup_spi("10", 0xE0, 0, SPI_MODE_0, SPI_LSB, SPI_INTERRUPT, SPI_MSTR_CLK4);
|
||||
test_setup_spi("11", 0xE1, 1, SPI_MODE_0, SPI_LSB, SPI_INTERRUPT, SPI_MSTR_CLK8);
|
||||
test_setup_spi("12", 0xE1, 0, SPI_MODE_0, SPI_LSB, SPI_INTERRUPT, SPI_MSTR_CLK16);
|
||||
test_setup_spi("13", 0xE2, 1, SPI_MODE_0, SPI_LSB, SPI_INTERRUPT, SPI_MSTR_CLK32);
|
||||
test_setup_spi("14", 0xE2, 0, SPI_MODE_0, SPI_LSB, SPI_INTERRUPT, SPI_MSTR_CLK64);
|
||||
test_setup_spi("15", 0xE3, 0, SPI_MODE_0, SPI_LSB, SPI_INTERRUPT, SPI_MSTR_CLK128);
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(19200);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
delay(5000);
|
||||
Serial.println("Running tests");
|
||||
test_registers();
|
||||
Serial.println("Run tests");
|
||||
while(true);
|
||||
}
|
||||
Reference in New Issue
Block a user