Commit ea909cd0 authored by Snorre Nilssen Vestli's avatar Snorre Nilssen Vestli

basic functionality added. no test. no review. beware of coil ignition

parent 699b58ab
......@@ -12,7 +12,7 @@
#include <avr/io.h>
#warning dummy values!
#define GPIO_SIGNAL_INPUT_PORT PORTC
#define GPIO_SIGNAL_INPUT_PIN PINC
......@@ -20,19 +20,33 @@
#define GPIO_ONTIME_HEX_PORT (PORTB)
#define GPIO_ONTIME_HEX_PIN (PINB)
#define GPIO_ONTIME_HEX_0_PORT (PORTD)
#define GPIO_ONTIME_HEX_0_PIN (PIND)
#define GPIO_ONTIME_HEX_1_PORT (PORTD)
#define GPIO_ONTIME_HEX_1_PIN (PIND)
#define GPIO_ONTIME_HEX_2_PORT (PORTD)
#define GPIO_ONTIME_HEX_2_PIN (PIND)
#define GPIO_ONTIME_HEX_3_PORT (PORTD)
#define GPIO_ONTIME_HEX_3_PIN (PIND)
#define GPIO_ONTIME_HEX_PIN0 0
#define GPIO_ONTIME_HEX_PIN1 1
#define GPIO_ONTIME_HEX_PIN2 2
#define GPIO_ONTIME_HEX_PIN3 3
#define GPIO_OFFTIME_HEX_PORT (PORTB)
#define GPIO_OFFTIME_HEX_PIN (PINB)
#define GPIO_OFFTIME_HEX_PIN0 4
#define GPIO_OFFTIME_HEX_PIN1 5
#define GPIO_OFFTIME_HEX_PIN2 6
#define GPIO_OFFTIME_HEX_0_PORT (PORTC)
#define GPIO_OFFTIME_HEX_0_PIN (PINC)
#define GPIO_OFFTIME_HEX_1_PORT (PORTC)
#define GPIO_OFFTIME_HEX_1_PIN (PINC)
#define GPIO_OFFTIME_HEX_2_PORT (PORTD)
#define GPIO_OFFTIME_HEX_2_PIN (PIND)
#define GPIO_OFFTIME_HEX_3_PORT (PORTD)
#define GPIO_OFFTIME_HEX_3_PIN (PIND)
#define GPIO_OFFTIME_HEX_PIN0 2
#define GPIO_OFFTIME_HEX_PIN1 3
#define GPIO_OFFTIME_HEX_PIN2 4
#define GPIO_OFFTIME_HEX_PIN3 7
......@@ -47,6 +61,9 @@
#define GPIO_OFFTIME_HEX_PIN2_bm (1 << GPIO_OFFTIME_HEX_PIN2)
#define GPIO_OFFTIME_HEX_PIN3_bm (1 << GPIO_OFFTIME_HEX_PIN3)
#define ADC_MUX_bm ((1 << MUX0)|(1 << MUX1)|(1 << MUX2)|(1 << MUX3))
#define ADC_ONTIME_ADC_MUX_gc 4
#define ADC_OFFTIME_ADC_MUX_gc 5
......
......@@ -9,16 +9,44 @@
#ifndef TESLA_CONFIG_H_
#define TESLA_CONFIG_H_
#include <inttypes.h>
#warning dummy values!
uint8_t ontime_table[16] = {
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0
uint16_t ontime_table[16] = { // 256/255 of max value. (in 1/2 microseconds) max value ~2^14 ~=16k
2, // almost nothing
100, // 50us
200, // 100us
300, // 150us
400, // 200us
500, // 250us
600, // 300us
700, // 350us
800, // 400us
900, // 450us
1000, // 500us
1200, // 600us
1400, // 700us
1600, // 800us
1800, // 900us
2000 // 1000us
};
uint8_t offtime_table[16] = {
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0
uint16_t offtime_table[16] = {
100, // 50us
200, // ...
400,
600,
800,
1000,
1200,
1400,
1600,
1800,
2000,
2200,
2400,
2600,
2800,
3000 //1.5 ms
};
......
......@@ -5,68 +5,81 @@
* Author: Snorre
*/
#include <avr/io.h>
#include "../config/gpio_config.h"
#include "adc.h"
#define SMOOOTH 4
#define ADC_ONTIME_SELECT 0
#define ADC_OFFTIME_SELECT 1
uint16_t adc_val_ontime = 0,adc_val_offtime = 0, adc_buf_ontime[SMOOOTH], adc_buf_offtime[SMOOOTH];
uint16_t adc_buf_ontime[SMOOOTH], adc_buf_offtime[SMOOOTH];
uint8_t adc_val_ontime = 0, adc_val_offtime = 255;
uint8_t adc_ontime_iterator, adc_offtime_iterator, adc_channel_select;
void adc_init()
{
//TODO:
void adc_init( void )
{
//enable
//set divisor
//set mode
ADCSRA = (1 << ADEN)|(7 << ADPS0); // enable and set clock to 16MHz / 128 = 125kHz
//set mux
ADMUX = (ADMUX & ~ADC_MUX_bm) | ADC_ONTIME_ADC_MUX_gc;
adc_channel_select = ADC_ONTIME_SELECT;
//start
ADCSRA |= (1 << ADSC);
}
void adc_update()
void adc_update( void )
{
//TODO:if done {
if (adc_channel_select = ADC_OFFTIME_SELECT) {
static uint16_t accumulator;
if (ADCSRA & (1 << ADIF)) {
if (adc_channel_select == ADC_OFFTIME_SELECT) {
adc_buf_offtime[adc_offtime_iterator] = ADC;
ADMUX = (ADMUX & ~ADC_MUX_bm) | ADC_ONTIME_ADC_MUX_gc;
adc_channel_select = ADC_ONTIME_SELECT;
adc_offtime_iterator++;
adc_offtime_iterator %= SMOOTH;
adc_offtime_iterator %= SMOOOTH;
//TODO:change channel
adc_channel_select = ADC_ONTIME_SELECT;
adc_val_offtime = 0;
accumulator = 0;
for (uint8_t i = 0; i < SMOOOTH; i++){
adc_val_offtime += adc_buf_offtime[i];
accumulator += adc_buf_offtime[i];
}
adc_val_offtime = accumulator/(SMOOOTH*4);
} else {
adc_buf_ontime[adc_ontime_iterator] = ADC;
ADMUX = (ADMUX & ~ADC_MUX_bm) | ADC_OFFTIME_ADC_MUX_gc;
adc_channel_select = ADC_OFFTIME_SELECT;
adc_ontime_iterator++;
adc_ontime_iterator %= SMOOTH;
adc_ontime_iterator %= SMOOOTH;
//TODO:change channel
adc_channel_select = ADC_OFFTIME_SELECT;
adc_val_offtime = 0;
accumulator = 0;
for (uint8_t i = 0; i < SMOOOTH; i++){
adc_val_ontime += adc_buf_ontime[i];
accumulator += adc_buf_ontime[i];
}
adc_val_ontime = accumulator/(SMOOOTH*4);
}
//TODO:start sampler
//}
ADCSRA |= (1 << ADSC)| (1 << ADIF); //Start conversion and clear flag
}
}
uint16_t adc_ontime()
uint8_t adc_ontime( void )
{
return adc_val_ontime;
}
uint16_t adc_offtime()
uint8_t adc_offtime( void )
{
return adc_val_offtime;
}
\ No newline at end of file
......@@ -11,11 +11,11 @@
#include <inttypes.h>
void adc_init();
void adc_init( void );
void adc_update();
void adc_update( void );
uint16_t adc_ontime();
uint16_t adc_offtime();
uint8_t adc_ontime( void );
uint8_t adc_offtime( void );
#endif /* ADC_H_ */
\ No newline at end of file
......@@ -10,42 +10,41 @@
uint8_t gpio_offtime = 0, gpio_ontime = 0;
void gpio_init()
void gpio_init( void )
{
//turn on a bunch of pullups
GPIO_ONTIME_HEX_PORT |= GPIO_ONTIME_HEX_PIN0_bm| GPIO_ONTIME_HEX_PIN1_bm| GPIO_ONTIME_HEX_PIN2_bm| GPIO_ONTIME_HEX_PIN3_bm;
GPIO_OFFTIME_HEX_PORT |= GPIO_OFFTIME_HEX_PIN0_bm|GPIO_OFFTIME_HEX_PIN1_bm|GPIO_OFFTIME_HEX_PIN2_bm|GPIO_OFFTIME_HEX_PIN3_bm;
GPIO_ONTIME_HEX_0_PORT |= GPIO_ONTIME_HEX_PIN0_bm;
GPIO_ONTIME_HEX_1_PORT |= GPIO_ONTIME_HEX_PIN1_bm;
GPIO_ONTIME_HEX_2_PORT |= GPIO_ONTIME_HEX_PIN2_bm;
GPIO_ONTIME_HEX_3_PORT |= GPIO_ONTIME_HEX_PIN3_bm;
GPIO_OFFTIME_HEX_0_PORT |= GPIO_OFFTIME_HEX_PIN0_bm;
GPIO_OFFTIME_HEX_1_PORT |= GPIO_OFFTIME_HEX_PIN1_bm;
GPIO_OFFTIME_HEX_2_PORT |= GPIO_OFFTIME_HEX_PIN2_bm;
GPIO_OFFTIME_HEX_3_PORT |= GPIO_OFFTIME_HEX_PIN3_bm;
}
void gpio_update()
uint8_t gpio_get_offtime( void )
{
//Donthing?
}
uint8_t gpio_get_offtime()
{
uint8_t pinvalue = GPIO_OFFTIME_HEX_PIN;
uint8_t retval;
retval = (((pinvalue & GPIO_OFFTIME_HEX_PIN0_bm) >> GPIO_OFFTIME_HEX_PIN0) << 0);
retval += (((pinvalue & GPIO_OFFTIME_HEX_PIN1_bm) >> GPIO_OFFTIME_HEX_PIN1) << 1);
retval += (((pinvalue & GPIO_OFFTIME_HEX_PIN2_bm) >> GPIO_OFFTIME_HEX_PIN2) << 2);
retval += (((pinvalue & GPIO_OFFTIME_HEX_PIN3_bm) >> GPIO_OFFTIME_HEX_PIN3) << 3);
retval = (((GPIO_OFFTIME_HEX_0_PIN & GPIO_OFFTIME_HEX_PIN0_bm) >> GPIO_OFFTIME_HEX_PIN0) << 0);
retval += (((GPIO_OFFTIME_HEX_1_PIN & GPIO_OFFTIME_HEX_PIN1_bm) >> GPIO_OFFTIME_HEX_PIN1) << 1);
retval += (((GPIO_OFFTIME_HEX_2_PIN & GPIO_OFFTIME_HEX_PIN2_bm) >> GPIO_OFFTIME_HEX_PIN2) << 2);
retval += (((GPIO_OFFTIME_HEX_3_PIN & GPIO_OFFTIME_HEX_PIN3_bm) >> GPIO_OFFTIME_HEX_PIN3) << 3);
return retval;
}
uint8_t gpio_get_ontime()
uint8_t gpio_get_ontime( void )
{
uint8_t pinvalue = GPIO_ONTIME_HEX_PIN;
uint8_t retval;
retval = (((pinvalue & GPIO_ONTIME_HEX_PIN0_bm) >> GPIO_ONTIME_HEX_PIN0) << 0);
retval += (((pinvalue & GPIO_ONTIME_HEX_PIN1_bm) >> GPIO_ONTIME_HEX_PIN1) << 1);
retval += (((pinvalue & GPIO_ONTIME_HEX_PIN2_bm) >> GPIO_ONTIME_HEX_PIN2) << 2);
retval += (((pinvalue & GPIO_ONTIME_HEX_PIN3_bm) >> GPIO_ONTIME_HEX_PIN3) << 3);
retval = (((GPIO_ONTIME_HEX_0_PIN & GPIO_ONTIME_HEX_PIN0_bm) >> GPIO_ONTIME_HEX_PIN0) << 0);
retval += (((GPIO_ONTIME_HEX_1_PIN & GPIO_ONTIME_HEX_PIN1_bm) >> GPIO_ONTIME_HEX_PIN1) << 1);
retval += (((GPIO_ONTIME_HEX_2_PIN & GPIO_ONTIME_HEX_PIN2_bm) >> GPIO_ONTIME_HEX_PIN2) << 2);
retval += (((GPIO_ONTIME_HEX_3_PIN & GPIO_ONTIME_HEX_PIN3_bm) >> GPIO_ONTIME_HEX_PIN3) << 3);
return retval;
}
uint8_t gpio_get_input()
uint8_t gpio_get_input( void )
{
return !!(GPIO_SIGNAL_INPUT_PIN&GPIO_SIGNAL_INPUT_PIN0_bm);
return !!(GPIO_SIGNAL_INPUT_PIN & GPIO_SIGNAL_INPUT_PIN0_bm);
}
\ No newline at end of file
......@@ -10,13 +10,12 @@
#define GPIO_H_
#include <inttypes.h>
void gpio_init();
void gpio_update();
void gpio_init( void );
uint8_t gpio_get_offtime();
uint8_t gpio_get_ontime();
uint8_t gpio_get_offtime( void );
uint8_t gpio_get_ontime( void );
uint8_t gpio_get_input();
uint8_t gpio_get_input( void );
#endif /* GPIO_H_ */
\ No newline at end of file
......@@ -6,9 +6,67 @@
*/
#include "timer.h"
#include "../config/gpio_config.h"
void timer_init()
static int16_t timeout = 0;
void timer_init( void )
{
//one timer for 1/2 us ticks, one for optic pwm
//16bit timer set to /8
DDRB |= (1 << PORTD5); // optic PWM out
OCR0A = 8; //16MHz/8 = 2MHz
TCCR0A = (1 << COM0B1)|(1 << WGM01)|(1 << WGM00); // Non-inverting mode, CTC fast pwm.
TCCR0B = (1 << WGM02);
TCCR1B = (1 << CS11); // Set div8 for demimicrosecond systemtick.
}
void timeout_start(uint16_t time) // in 1/2 us
{
timeout = TCNT1 + time;
}
uint8_t timeout_done( void )
{
if ((int16_t)TCNT1 - timeout > 0){
return 1;
} else {
return 0;
}
}
void timer_pwm_start( void )
{
OCR0B = 2; //25%
TCCR0B |= (1 < CS00); // Full speed!
}
void timer_pwm_25( void )
{
OCR0B = 2;
}
void timer_pwm_75( void )
{
OCR0B = 6;
}
void timer_pwm_stop()
{
TCCR0B &= ~((1 << CS02)|(1 << CS01)|(1 << CS00));
PORTB &= ~(1 << PORTD5);
}
\ No newline at end of file
......@@ -9,15 +9,17 @@
#ifndef TIMER_H_
#define TIMER_H_
void timer_init();
#include <inttypes.h>
void timeout_us(uint8_t time);
void timeout_done();
void timer_init( void );
void timer_pwm_start();
void timer_pwm_set(uint8_t pw);
void timer_pwm_stop();
void timeout_start(uint16_t time);
uint8_t timeout_done( void );
void timer_pwm_start( void );
void timer_pwm_stop( void );
void timer_pwm_25( void );
void timer_pwm_75( void );
#endif /* TIMER_H_ */
\ No newline at end of file
......@@ -6,31 +6,47 @@
*/
#include "optic.h"
#include "../config/optic_config.h"
#include "../drivers/timer.h"
#include "../drivers/adc.h"
#include "../drivers/gpio.h"
uint16_t pulse_limit, deadtime;
uint16_t ontime_base, offtime_base;
void optic_init()
void optic_init( void )
{
ontime_base = ontime_table[gpio_get_ontime()];
offtime_base = offtime_table[gpio_get_offtime()];
}
void optic_update()
void optic_update( void )
{
//calculate pulse length (from 0 to almost 1x base value)
pulse_limit = (uint16_t) (
((uint32_t)(ontime_base)*(uint32_t)adc_ontime())
>> 8);
//calculate deadtime length (from 1 - almost 2x base value)
deadtime = (uint16_t) ((
((uint32_t)(offtime_base)*(uint32_t)adc_offtime())
>> 8)
+ offtime_base);
}
void optic_pulse()
void optic_pulse( void )
{
//calculate pulse length
//start timer
//loop until
//pulse timer end
//or gpio down.
timer_pwm_75();
timeout_start(pulse_limit);
while (gpio_get_input() && !timeout_done()){
};
timer_pwm_25();
}
void optic_deadtime_wait()
void optic_deadtime_wait( void )
{
//calculate deadtime length
//start timer
//loop until deadtime timer end
timeout_start(deadtime);
while(!timeout_done()){
};
}
\ No newline at end of file
......@@ -9,11 +9,11 @@
#ifndef OPTIC_H_
#define OPTIC_H_
void optic_init();
void optic_update();
void optic_init( void );
void optic_update( void );
void optic_pulse();
void optic_deadtime_wait();
void optic_pulse( void );
void optic_deadtime_wait( void );
#endif /* OPTIC_H_ */
\ No newline at end of file
......@@ -14,16 +14,14 @@
int main(void)
{
optic_init();
gpio_init();
timer_init();
adc_init();
optic_init();
while (1)
{
adc_update();
gpio_update();
optic_update();
if (gpio_get_input()){
optic_pulse();
......
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