working with leds now
This commit is contained in:
parent
8ea404c3fa
commit
b9a402d64b
@ -7,5 +7,5 @@ project(slider_firmware C)
|
||||
|
||||
add_subdirectory(SliderCommunication)
|
||||
|
||||
add_avr_executable(slider_firmware led.c main.c adc.c button.c)
|
||||
add_avr_executable(slider_firmware led.c main.c adc.c button.c comm.c)
|
||||
target_link_libraries(slider_firmware DSPLAB_SliderCommunication)
|
||||
|
@ -16,21 +16,9 @@ adc_t adc;
|
||||
|
||||
/* adc driver */
|
||||
void adc_init(uint8_t * channels) {
|
||||
adc.current_channel = 0;
|
||||
adc.channel_map = channels;
|
||||
// adc.current_channel = 0;
|
||||
// adc.channel_map = channels;
|
||||
|
||||
ADMUX = channels[0];
|
||||
ADCSRA = (1<<ADATE) | (1<<ADEN) | 7; // enable adc, maximum prescaler value
|
||||
ADCSRA = /*(1<<ADATE) | */ (1<<ADEN); // enable adc, maximum prescaler value
|
||||
}
|
||||
|
||||
ISR(ADC_vect) {
|
||||
adc.buffer[adc.current_channel] = ADC;
|
||||
adc.current_channel++;
|
||||
|
||||
if (adc.current_channel == ADC_BUFFER_SIZE) {
|
||||
adc.current_channel = 0;
|
||||
}
|
||||
|
||||
ADMUX = adc.channel_map[adc.current_channel];
|
||||
}
|
||||
|
||||
|
@ -19,10 +19,11 @@ typedef struct {
|
||||
} adc_buffer_t;
|
||||
|
||||
typedef struct adc {
|
||||
adc_sample_t buffer[ADC_BUFFER_SIZE];
|
||||
//adc_sample_t buffer[ADC_BUFFER_SIZE];
|
||||
|
||||
volatile uint8_t current_channel;
|
||||
/*volatile uint8_t current_channel;
|
||||
uint8_t * channel_map;
|
||||
*/
|
||||
} adc_t;
|
||||
|
||||
// the adc instance
|
||||
@ -30,7 +31,9 @@ extern adc_t adc;
|
||||
|
||||
void adc_init(uint8_t * channels);
|
||||
|
||||
#define adc_start() ADCSRA |= (1<<ADIE) | (1<<ADSC)
|
||||
#define adc_stop() ADCSRA &= ~(1<<ADIE)
|
||||
#define adc_start() do { ADCSRA |= (1<<ADSC); } while(0)
|
||||
#define adc_finished() (ADCSRA & (1<<ADIF))
|
||||
#define adc_set_channel(channel) do { ADMUX = channel; } while(0)
|
||||
#define adc_value() ADC
|
||||
|
||||
#endif /* ADC_H_ */
|
||||
|
@ -25,7 +25,7 @@ button_state_t button_is_pressed()
|
||||
|
||||
ISR(PCINT1_vect) {
|
||||
if(!(BUTTON_REG&(1<<BUTTON_PIN))) {
|
||||
app.state.state.button = !app.state.state.button;
|
||||
app.button = !app.button;
|
||||
led_toggle();
|
||||
}
|
||||
}
|
||||
|
14
firmware/slider/comm.c
Normal file
14
firmware/slider/comm.c
Normal file
@ -0,0 +1,14 @@
|
||||
/*
|
||||
* comm.c
|
||||
*
|
||||
* Created on: 13.02.2019
|
||||
* Author: julian
|
||||
*/
|
||||
|
||||
#include <avr/io.h>
|
||||
|
||||
void comm_init()
|
||||
{
|
||||
DDRA |= (1<<PA5); // setup MOSI as output
|
||||
USICR = (1<<USIWM0)|(1<<USICS1);
|
||||
}
|
13
firmware/slider/comm.h
Normal file
13
firmware/slider/comm.h
Normal file
@ -0,0 +1,13 @@
|
||||
/*
|
||||
* comm.h
|
||||
*
|
||||
* Created on: 13.02.2019
|
||||
* Author: julian
|
||||
*/
|
||||
|
||||
#ifndef COMM_H_
|
||||
#define COMM_H_
|
||||
|
||||
void comm_init();
|
||||
|
||||
#endif /* COMM_H_ */
|
@ -4,6 +4,7 @@
|
||||
#include "led.h"
|
||||
#include "button.h"
|
||||
#include "main.h"
|
||||
#include "comm.h"
|
||||
|
||||
/* Communication driver */
|
||||
#define SLAVE_ENABLE_DDR DDRA
|
||||
@ -12,16 +13,12 @@
|
||||
|
||||
#include <interface.h>
|
||||
|
||||
#include "main.h"
|
||||
|
||||
void comm_init();
|
||||
|
||||
// the public instance of the app buffer
|
||||
struct app app = {};
|
||||
|
||||
uint8_t channel_map[ADC_BUFFER_SIZE] = {
|
||||
0,
|
||||
1,
|
||||
0,
|
||||
2,
|
||||
3,
|
||||
7
|
||||
@ -49,12 +46,8 @@ void init()
|
||||
sei();
|
||||
}
|
||||
|
||||
void comm_init()
|
||||
{
|
||||
DDRA |= (1<<PA5); // setup MOSI as output
|
||||
USICR = (1<<USIWM0)|(1<<USICS1);
|
||||
}
|
||||
|
||||
// All states of the transaction state machine
|
||||
volatile enum {
|
||||
COMM_WAIT = 0,
|
||||
COMM_SEND_START ,
|
||||
@ -69,25 +62,33 @@ static inline void comm_service()
|
||||
|
||||
if (USISR & (1<<USIOIF))
|
||||
{
|
||||
led_on();
|
||||
cli();
|
||||
|
||||
switch(comm_state)
|
||||
{
|
||||
case COMM_WAIT:
|
||||
// only trigger on start of transmission
|
||||
if (IS_EOM(USIDR)) {
|
||||
// pass package through
|
||||
USIDR &= ~0x80;
|
||||
comm_state = COMM_SEND_START;
|
||||
adc_stop();
|
||||
|
||||
// sample first channel
|
||||
adc_start();
|
||||
}
|
||||
break;
|
||||
case COMM_SEND_START:
|
||||
temp = adc.buffer[count];
|
||||
// transmit LSByte of value
|
||||
temp = adc_value();
|
||||
|
||||
// sample next channel
|
||||
adc_set_channel(channel_map[count+1]);
|
||||
adc_start();
|
||||
|
||||
USIDR = temp & 0x7F;
|
||||
comm_state = COMM_SEND_WAIT;
|
||||
break;
|
||||
case COMM_SEND_WAIT:
|
||||
// transmit MSByte
|
||||
USIDR = (temp>>7)&0x7F;
|
||||
|
||||
if (++count == ADC_BUFFER_SIZE) {
|
||||
@ -97,20 +98,23 @@ static inline void comm_service()
|
||||
}
|
||||
break;
|
||||
case COMM_SEND_END:
|
||||
// cleanup the transaction
|
||||
count = 0;
|
||||
USIDR = app.state.state.button | 0x80;
|
||||
// transmit the button state and end of transaction
|
||||
USIDR = app.button | 0x80;
|
||||
default:
|
||||
comm_state = COMM_WAIT;
|
||||
adc_start();
|
||||
// reset channel of adc back to 0
|
||||
adc_set_channel(channel_map[0]);
|
||||
break;
|
||||
}
|
||||
|
||||
USISR |= (1<<USIOIF);
|
||||
|
||||
sei();
|
||||
led_off();
|
||||
}
|
||||
}
|
||||
|
||||
#include <util/delay.h>
|
||||
|
||||
// little debug helper function
|
||||
|
@ -7,7 +7,7 @@
|
||||
#include <slave/slave.h>
|
||||
|
||||
struct app {
|
||||
slave_t state;
|
||||
volatile uint8_t button;
|
||||
};
|
||||
|
||||
extern struct app app;
|
||||
|
Loading…
Reference in New Issue
Block a user