Commit fbdb302a authored by Grace Copplestone's avatar Grace Copplestone

change file structure

parent 7c1d2a87
Pipeline #639 canceled with stage
File added
//
// xmega twi for as5510 linear magnetic encoder
// sec, 2016
#include "serial.h"
#include <avr/io.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#define SLAVE_ADDRESS 0x56
#define CPU_SPEED 32000000
USART_data_t USART_data;
uint32_t value; //magnetic encoder reading
unsigned char Data_LSB, Data_MSB; //high byte and low byte of reading
int main(void) {
// set up clock
OSC.CTRL = OSC_RC32MEN_bm; // enable 32MHz clock
while (!(OSC.STATUS & OSC_RC32MRDY_bm)); // wait for clock to be ready
CCP = CCP_IOREG_gc; // enable protected register change
CLK.CTRL = CLK_SCLKSEL_RC32M_gc; // switch to 32MHz clock
//set up usart
PORTD.DIRSET = PIN3_bm; //TXD0 where DIRSET sets a pin as an output
PORTD.DIRCLR = PIN2_bm; //RXD0 where DIRCLR sets a pin as in input
USART_InterruptDriver_Initialize(&USART_data, &USARTD0, USART_DREINTLVL_LO_gc);
USART_Format_Set(USART_data.usart, USART_CHSIZE_8BIT_gc,
USART_PMODE_DISABLED_gc, 0);
USART_RxdInterruptLevel_Set(USART_data.usart, USART_RXCINTLVL_LO_gc);
USART_Baudrate_Set(&USARTD0, 123 , -4); //230400 baud with .08% error
USART_Rx_Enable(USART_data.usart);
USART_Tx_Enable(USART_data.usart);
//enable interrupts
PMIC.CTRL |= PMIC_LOLVLEX_bm;
//set up LED as output
PORTC.DIRSET = PIN0_bm;
//setting an internal pull up resistor on pin6 for button
PORTC.PIN4CTRL = PORT_OPC_PULLUP_gc;
sei(); //set global interrupt enable
while(1){
PORTC.OUTSET = PIN0_bm;
_delay_ms(100);
PORTC.OUTCLR = PIN0_bm;
_delay_ms(100);
if(PORTC.IN & PIN4_bm){
usart_send_byte(&USART_data,121);
usart_send_byte(&USART_data,101);
usart_send_byte(&USART_data,115);
usart_send_byte(&USART_data,10);
usart_send_byte(&USART_data,10); // translate byte using an ASCII table
}
else{
usart_send_byte(&USART_data,110);
usart_send_byte(&USART_data,111);
usart_send_byte(&USART_data,10);
usart_send_byte(&USART_data,10); // translate byte using an ASCII table
}
}
}
ISR(USARTD0_RXC_vect){USART_RXComplete(&USART_data);}
ISR(USARTD0_DRE_vect){USART_DataRegEmpty(&USART_data);}
PROJECT=8e5_usart
SOURCES=$(PROJECT).c
MMCU=atxmega8e5
F_CPU = 32000000
TARGET = x8e5
PROGRAMMER= atmelice_pdi
CFLAGS=-mmcu=$(MMCU) -Wall -Os -DF_CPU=$(F_CPU)
$(PROJECT).hex: $(PROJECT).out
avr-objcopy -O ihex $(PROJECT).out $(PROJECT).c.hex;\
avr-size --mcu=$(MMCU) --format=avr $(PROJECT).out
$(PROJECT).out: $(SOURCES)
avr-gcc $(CFLAGS) -I./ -o $(PROJECT).out $(SOURCES)
program: $(PROJECT).hex
avrdude -p $(TARGET) -c $(PROGRAMMER) -U flash:w:$(PROJECT).c.hex
......@@ -6,7 +6,7 @@
[Manual](http://www.atmel.com/images/Atmel-42005-8-and-16-bit-AVR-Microcontrollers-XMEGA-E_Manual.pdf)
[Data sheet](documentation/8e5_datasheet.pdf)
[Data sheet](8e5_datasheet.pdf)
Not sure whether this image should be here or in a separate folder.
#include <avr/io.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#define output(directions,pin) (directions |= pin) // set port direction for output
#define set(port,pin) (port |= pin) // set port pin
#define clear(port,pin) (port &= (~pin)) // clear port pin
#define pin_test(pins,pin) (pins & pin) // test for port pin
#define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set
/* USART buffer defines. */
/* \brief Receive buffer size: 2,4,8,16,32,64,128 or 256 bytes. */
#define USART_RX_BUFFER_SIZE 128
/* \brief Transmit buffer size: 2,4,8,16,32,64,128 or 256 bytes */
#define USART_TX_BUFFER_SIZE 128
/* \brief Receive buffer mask. */
#define USART_RX_BUFFER_MASK ( USART_RX_BUFFER_SIZE - 1 )
/* \brief Transmit buffer mask. */
#define USART_TX_BUFFER_MASK ( USART_TX_BUFFER_SIZE - 1 )
#if ( USART_RX_BUFFER_SIZE & USART_RX_BUFFER_MASK )
#error RX buffer size is not a power of 2
#endif
#if ( USART_TX_BUFFER_SIZE & USART_TX_BUFFER_MASK )
#error TX buffer size is not a power of 2
#endif
/* \brief USART transmit and receive ring buffer. */
typedef struct USART_Buffer
{
/* \brief Receive buffer. */
volatile uint8_t RX[USART_RX_BUFFER_SIZE];
/* \brief Transmit buffer. */
volatile uint8_t TX[USART_TX_BUFFER_SIZE];
/* \brief Receive buffer head. */
volatile uint8_t RX_Head;
/* \brief Receive buffer tail. */
volatile uint8_t RX_Tail;
/* \brief Transmit buffer head. */
volatile uint8_t TX_Head;
/* \brief Transmit buffer tail. */
volatile uint8_t TX_Tail;
} USART_Buffer_t;
/*! \brief Struct used when interrupt driven driver is used.
*
* Struct containing pointer to a usart, a buffer and a location to store Data
* register interrupt level temporary.
*/
typedef struct Usart_and_buffer
{
/* \brief Pointer to USART module to use. */
USART_t * usart;
/* \brief Data register empty interrupt level. */
USART_DREINTLVL_t dreIntLevel;
/* \brief Data buffer. */
USART_Buffer_t buffer;
} USART_data_t;
#define USART_RxdInterruptLevel_Set(_usart, _rxdIntLevel) \
((_usart)->CTRLA = ((_usart)->CTRLA & ~USART_RXCINTLVL_gm) | _rxdIntLevel)
#define USART_Format_Set(_usart, _charSize, _parityMode, _twoStopBits) \
(_usart)->CTRLC = (uint8_t) _charSize | _parityMode | \
(_twoStopBits ? USART_SBMODE_bm : 0)
#define USART_Baudrate_Set(_usart, _bselValue, _bScaleFactor) \
(_usart)->BAUDCTRLA =(uint8_t)_bselValue; \
(_usart)->BAUDCTRLB =(_bScaleFactor << USART_BSCALE0_bp)|(_bselValue >> 8)
#define USART_Rx_Enable(_usart) ((_usart)->CTRLB |= USART_RXEN_bm)
#define USART_Rx_Disable(_usart) ((_usart)->CTRLB &= ~USART_RXEN_bm)
#define USART_Tx_Enable(_usart) ((_usart)->CTRLB |= USART_TXEN_bm)
#define USART_Tx_Disable(_usart) ((_usart)->CTRLB &= ~USART_TXEN_bm)
_Bool USART_TXBuffer_FreeSpace(USART_data_t * usart_data)
{
/* Make copies to make sure that volatile access is specified. */
uint8_t tempHead = (usart_data->buffer.TX_Head + 1) & USART_TX_BUFFER_MASK;
uint8_t tempTail = usart_data->buffer.TX_Tail;
/* There are data left in the buffer unless Head and Tail are equal. */
return (tempHead != tempTail);
}
_Bool USART_TXBuffer_PutByte(USART_data_t * usart_data, uint8_t data)
{
uint8_t tempCTRLA;
uint8_t tempTX_Head;
_Bool TXBuffer_FreeSpace;
USART_Buffer_t * TXbufPtr;
TXbufPtr = &usart_data->buffer;
TXBuffer_FreeSpace = USART_TXBuffer_FreeSpace(usart_data);
if(TXBuffer_FreeSpace)
{
tempTX_Head = TXbufPtr->TX_Head;
TXbufPtr->TX[tempTX_Head]= data;
/* Advance buffer head. */
TXbufPtr->TX_Head = (tempTX_Head + 1) & USART_TX_BUFFER_MASK;
/* Enable DRE interrupt. */
tempCTRLA = usart_data->usart->CTRLA;
tempCTRLA = (tempCTRLA & ~USART_DREINTLVL_gm) | usart_data->dreIntLevel;
usart_data->usart->CTRLA = tempCTRLA;
}
return TXBuffer_FreeSpace;
}
_Bool USART_RXBufferData_Available(USART_data_t * usart_data)
{
/* Make copies to make sure that volatile access is specified. */
uint8_t tempHead = usart_data->buffer.RX_Head;
uint8_t tempTail = usart_data->buffer.RX_Tail;
/* There are data left in the buffer unless Head and Tail are equal. */
return (tempHead != tempTail);
}
uint8_t USART_RXBuffer_GetByte(USART_data_t * usart_data)
{
USART_Buffer_t * bufPtr;
uint8_t ans;
bufPtr = &usart_data->buffer;
ans = (bufPtr->RX[bufPtr->RX_Tail]);
/* Advance buffer tail. */
bufPtr->RX_Tail = (bufPtr->RX_Tail + 1) & USART_RX_BUFFER_MASK;
return ans;
}
_Bool USART_RXComplete(USART_data_t * usart_data)
{
USART_Buffer_t * bufPtr;
_Bool ans;
bufPtr = &usart_data->buffer;
/* Advance buffer head. */
uint8_t tempRX_Head = (bufPtr->RX_Head + 1) & USART_RX_BUFFER_MASK;
/* Check for overflow. */
uint8_t tempRX_Tail = bufPtr->RX_Tail;
uint8_t data = usart_data->usart->DATA;
if (tempRX_Head == tempRX_Tail) {
ans = 0;
}else{
ans = 1;
usart_data->buffer.RX[usart_data->buffer.RX_Head] = data;
usart_data->buffer.RX_Head = tempRX_Head;
}
return ans;
}
void USART_DataRegEmpty(USART_data_t * usart_data)
{
USART_Buffer_t * bufPtr;
bufPtr = &usart_data->buffer;
/* Check if all data is transmitted. */
uint8_t tempTX_Tail = usart_data->buffer.TX_Tail;
if (bufPtr->TX_Head == tempTX_Tail){
/* Disable DRE interrupts. */
uint8_t tempCTRLA = usart_data->usart->CTRLA;
tempCTRLA = (tempCTRLA & ~USART_DREINTLVL_gm) | USART_DREINTLVL_OFF_gc;
usart_data->usart->CTRLA = tempCTRLA;
}else{
/* Start transmitting. */
uint8_t data = bufPtr->TX[usart_data->buffer.TX_Tail];
usart_data->usart->DATA = data;
/* Advance buffer tail. */
bufPtr->TX_Tail = (bufPtr->TX_Tail + 1) & USART_TX_BUFFER_MASK;
}
}
void USART_InterruptDriver_Initialize(USART_data_t * usart_data,
USART_t * usart,
USART_DREINTLVL_t dreIntLevel)
{
usart_data->usart = usart;
usart_data->dreIntLevel = dreIntLevel;
usart_data->buffer.RX_Tail = 0;
usart_data->buffer.RX_Head = 0;
usart_data->buffer.TX_Tail = 0;
usart_data->buffer.TX_Head = 0;
}
void USART_InterruptDriver_DreInterruptLevel_Set(USART_data_t * usart_data,
USART_DREINTLVL_t dreIntLevel)
{
usart_data->dreIntLevel = dreIntLevel;
}
void usart_send_byte(USART_data_t * usart_data, char msg){
while(!USART_TXBuffer_FreeSpace(usart_data)){}
USART_TXBuffer_PutByte(usart_data,msg);
}
void usart_send_uint32(USART_data_t * usart_data, uint32_t n){
unsigned char * nn = (unsigned char *)&n;
usart_send_byte(usart_data, nn[0] );
usart_send_byte(usart_data, nn[1] );
usart_send_byte(usart_data, nn[2] );
usart_send_byte(usart_data, nn[3] );
}
void usart_send_int32(USART_data_t * usart_data, int32_t n){
unsigned char * nn = ( unsigned char *)&n;
usart_send_byte(usart_data, nn[0] );
usart_send_byte(usart_data, nn[1] );
usart_send_byte(usart_data, nn[2] );
usart_send_byte(usart_data, nn[3] );
}
uint32_t parse_uint32(char* b){return *(uint32_t *) b;}
int32_t parse_int32(char* b){return *(int32_t *) b;}
uint16_t parse_uint16(char* b){return *(uint16_t *) b;}
/*
void usart_write_buffer(char* buffer){
//write \n terminated buffer over usart
int j=0;
do{
usart_send_byte( buffer[j] );
j++;
} while( j < PACKET_SIZE);
//} while( buffer[j-1] != 10);
}
*/
<html>
<head></head>
<head>
<link rel="stylesheet" href="https://copplestone.pages.cba.mit.edu/styling/stylesheet.css">
</head>
<body>
<h1>XMega Learning</h1>
......@@ -30,77 +32,77 @@
<h3>DAC</h3>
<ul>
<li><a href='dac/sine.png'>Sine Wave</a></li>
<li><a href='dac/hello.8E5.dac.c'>hello.8E5.dac.c</a></li>
<li><a href='dac/hello.8E5.dac.make'>hello.8E5.dac.make</a></li>
<li><a href='dac/make_lookup.py'>make_lookup.py</a></li>
<li><a href='dac/xmega-8e5-dac-layout.png'>Layout</a></li>
<li><a href='dac/xmega-8e5-dac-traces.png'>Traces</a></li>
<li><a href='dac/xmega-8e5-dac-outline.png'>Outline</a></li>
<li><a href='dac/xmega-8e5-dac-board.jpg'>Board</a></li>
<li><a href='functionality/dac/sine.png'>Sine Wave</a></li>
<li><a href='functionality/dac/hello.8E5.dac.c'>hello.8E5.dac.c</a></li>
<li><a href='functionality/dac/hello.8E5.dac.make'>hello.8E5.dac.make</a></li>
<li><a href='functionality/dac/make_lookup.py'>make_lookup.py</a></li>
<li><a href='functionality/dac/xmega-8e5-dac-layout.png'>Layout</a></li>
<li><a href='functionality/dac/xmega-8e5-dac-traces.png'>Traces</a></li>
<li><a href='functionality/dac/xmega-8e5-dac-outline.png'>Outline</a></li>
<li><a href='functionality/dac/xmega-8e5-dac-board.jpg'>Board</a></li>
</ul>
<h3>Quadrature Decoding with Timer/Counters</h3>
<ul>
<li><a href='qdec/qdec.mp4'>Operation</a></li>
<li><a href='qdec/hello.16a4u.qdec.c'>hello.16a4u.qdec.c</a></li>
<li><a href='qdec/hello.16a4u.qdec.make'>hello.16a4u.qdec.make</a></li>
<li><a href='qdec/serial.h'>serial.h</a></li>
<li><a href='qdec/xmega-16a4u-qdec-layout.png'>Layout</a></li>
<li><a href='qdec/xmega-16a4u-qdec-traces.png'>Traces</a></li>
<li><a href='qdec/xmega-16a4u-qdec-interior.png'>Outline</a></li>
<li><a href='qdec/xmega-16a4u-qdec-board.jpg'>Board</a></li>
<li><a href='functionality/qdec/qdec.mp4'>Operation</a></li>
<li><a href='functionality/qdec/hello.16a4u.qdec.c'>hello.16a4u.qdec.c</a></li>
<li><a href='functionality/qdec/hello.16a4u.qdec.make'>hello.16a4u.qdec.make</a></li>
<li><a href='functionality/qdec/serial.h'>serial.h</a></li>
<li><a href='functionality/qdec/xmega-16a4u-qdec-layout.png'>Layout</a></li>
<li><a href='functionality/qdec/xmega-16a4u-qdec-traces.png'>Traces</a></li>
<li><a href='functionality/qdec/xmega-16a4u-qdec-interior.png'>Outline</a></li>
<li><a href='functionality/qdec/xmega-16a4u-qdec-board.jpg'>Board</a></li>
</ul>
<h3>ADC</h3>
<ul>
<li><a href='adc/adc.8E5.c'>adc.8E5.c</a></li>
<li><a href='adc/adc.8E5.make'>adc.8E5.make</a></li>
<li><a href='adc/serial.h'>serial.h</a></li>
<li><a href='adc/adc.h'>adc.h</a></li>
<li><a href='adc/adc.8E5.layout.png'>Layout</a></li>
<li><a href='adc/adc.8E5.traces.png'>Traces</a></li>
<li><a href='adc/adc.8E5.interior.png'>Outline</a></li>
<li><a href='adc/adc.8E5.board.jpg'>Board</a></li>
<li><a href='functionality/adc/adc.8E5.c'>adc.8E5.c</a></li>
<li><a href='functionality/adc/adc.8E5.make'>adc.8E5.make</a></li>
<li><a href='functionality/adc/serial.h'>serial.h</a></li>
<li><a href='functionality/adc/adc.h'>adc.h</a></li>
<li><a href='functionality/adc/adc.8E5.layout.png'>Layout</a></li>
<li><a href='functionality/adc/adc.8E5.traces.png'>Traces</a></li>
<li><a href='functionality/adc/adc.8E5.interior.png'>Outline</a></li>
<li><a href='functionality/adc/adc.8E5.board.jpg'>Board</a></li>
<li><a href='http://www.tag-connect.com/'>Tag Connect Programming connector</a></li>
<li><a href='adc/load-cell-small.mp4'>Force measurement using onboard 64x gain.</a></li>
<li><a href='functionality/adc/load-cell-small.mp4'>Force measurement using onboard 64x gain.</a></li>
<li><a href='http://www.phidgets.com/products.php?category=34&product_id=3133_0'>$7 load cell</a></li>
</ul>
<h3>USART</h3>
<ul>
<li><a href='usart/usart.32a4u.c'>usart.32a4u.c</a></li>
<li><a href='usart/usart.32a4u.make'>usart.32a4u.make</a></li>
<li><a href='usart/serial.h'>serial.h</a></li>
<li><a href='usart/usart.32a4u.layout.png'>Layout</a></li>
<li><a href='usart/usart.32a4u.traces.png'>Traces</a></li>
<li><a href='usart/usart.32a4u.interior.png'>Outline</a></li>
<li><a href='functionality/usart/usart.32a4u.c'>usart.32a4u.c</a></li>
<li><a href='functionality/usart/usart.32a4u.make'>usart.32a4u.make</a></li>
<li><a href='functionality/usart/serial.h'>serial.h</a></li>
<li><a href='functionality/usart/usart.32a4u.layout.png'>Layout</a></li>
<li><a href='functionality/usart/usart.32a4u.traces.png'>Traces</a></li>
<li><a href='functionality/usart/usart.32a4u.interior.png'>Outline</a></li>
<li><a href='http://www.tag-connect.com/'>Tag Connect Programming connector</a></li>
</ul>
<h3>XCL</h3>
<ul>
<li><a href='xcl/xcl.8e5.c'>xcl.8e5.c</a></li>
<li><a href='xcl/xcl.8e5.make'>xcl.8e5.make</a></li>
<li><a href='xcl/xcl.8e5.layout.png'>Layout</a></li>
<li><a href='xcl/xcl.8e5.traces.png'>Traces</a></li>
<li><a href='xcl/xcl.8e5.interior.png'>Outline</a></li>
<li><a href='xcl/xcl.8e5.scope.png'>ANDing square waves (without power supplied to VCC!)</a></li>
<li><a href='functionality/xcl/xcl.8e5.c'>xcl.8e5.c</a></li>
<li><a href='functionality/xcl/xcl.8e5.make'>xcl.8e5.make</a></li>
<li><a href='functionality/xcl/xcl.8e5.layout.png'>Layout</a></li>
<li><a href='functionality/xcl/xcl.8e5.traces.png'>Traces</a></li>
<li><a href='functionality/xcl/xcl.8e5.interior.png'>Outline</a></li>
<li><a href='functionality/xcl/xcl.8e5.scope.png'>ANDing square waves (without power supplied to VCC!)</a></li>
</ul>
<h3>I2C</h3>
<ul>
<li><a href='i2c/hall-encoder.c'>hall-encoder.c</a></li>
<li><a href='i2c/serial.h'>serial.h</a></li>
<li><a href='i2c/hall-encoder.make'>hall-encoder.make</a></li>
<li><a href='i2c/hall-encoder-layout.png'>hall-encoder-layout.png</a></li>
<li><a href='i2c/hall-encoder-traces.png'>hall-encoder-traces.png</a></li>
<li><a href='i2c/hall-encoder-interior.png'>hall-encoder-interior.png</a></li>
<li><a href='i2c/load.py'>load.py</a></li>
<li><a href='i2c/testing.jpg'>Testing</a></li>
<li><a href='i2c/loadcell-reading-v-force.png'>Results</a></li>
<li><a href='functionality/i2c/hall-encoder.c'>hall-encoder.c</a></li>
<li><a href='functionality/i2c/serial.h'>serial.h</a></li>
<li><a href='functionality/i2c/hall-encoder.make'>hall-encoder.make</a></li>
<li><a href='functionality/i2c/hall-encoder-layout.png'>hall-encoder-layout.png</a></li>
<li><a href='functionality/i2c/hall-encoder-traces.png'>hall-encoder-traces.png</a></li>
<li><a href='functionality/i2c/hall-encoder-interior.png'>hall-encoder-interior.png</a></li>
<li><a href='functionality/i2c/load.py'>load.py</a></li>
<li><a href='functionality/i2c/testing.jpg'>Testing</a></li>
<li><a href='functionality/i2c/loadcell-reading-v-force.png'>Results</a></li>
</ul>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment