encoder, adc, averaging
This commit is contained in:
76
adc.c
Normal file
76
adc.c
Normal file
@@ -0,0 +1,76 @@
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
|
||||
#include "adc.h"
|
||||
|
||||
void adc_init()
|
||||
{
|
||||
ADMUX = 1 << REFS0 | 0 << REFS1; //Select external Vref
|
||||
|
||||
//ADC Status Register A
|
||||
ADCSRA = 1 << ADEN //Enable ADC
|
||||
| 1 << ADIE //Enable ISR after conversion complete
|
||||
//| 1<<ADATE //Freerunning-Mode
|
||||
//| 1<<ADLAR //2 results bits are left aligned
|
||||
| 1 << ADPS2 //Set clock-prescaler to 128
|
||||
| 1 << ADPS1 | 1 << ADPS0;
|
||||
ADCSRA |= 1 << ADSC; //Start first Conversion for "warmup"
|
||||
}
|
||||
|
||||
void set_ADC_Channel(uint8_t adr)
|
||||
{
|
||||
if (adr < 11)
|
||||
{
|
||||
ADMUX &= (0b11110000); //Clear MUX-Address
|
||||
ADMUX |= adr; //Set new MUX-Address
|
||||
}
|
||||
}
|
||||
|
||||
ISR(ADC_vect)
|
||||
{
|
||||
//static uint8_t init[4] = {0,0,0,0};
|
||||
static uint8_t current_channel = 0;
|
||||
|
||||
static uint16_t read_buffer[128];
|
||||
static uint8_t buffer_pos = 0;
|
||||
|
||||
|
||||
//Reading 10bit conversion result
|
||||
uint16_t ADC_reading = ADCL; //copy the first LSB bits
|
||||
ADC_reading |= ADCH << 8; //copy remaing byte
|
||||
|
||||
read_buffer[buffer_pos] = ADC_reading;
|
||||
buffer_pos++;
|
||||
|
||||
if(buffer_pos == (sizeof(read_buffer) / sizeof(read_buffer[0]))){
|
||||
uint32_t sum = 0;
|
||||
for(uint8_t i = 0; i < buffer_pos; i++){
|
||||
sum += read_buffer[i];
|
||||
}
|
||||
temp_values[current_channel] = sum/buffer_pos;
|
||||
|
||||
buffer_pos = 0;
|
||||
|
||||
current_channel++;
|
||||
if(current_channel == 4)
|
||||
current_channel = 0;
|
||||
set_ADC_Channel(current_channel);
|
||||
}
|
||||
|
||||
ADCSRA |= (1 << ADSC); //Start next conversion
|
||||
|
||||
|
||||
//TODO write non broken adc code
|
||||
//if(adc_buf[current_channel].position == BUFFER_SIZE-1){
|
||||
// //if(init[current_channel]){
|
||||
// //float tmp = (99*adc_avrg[current_channel]) + get_buffer_mean(&adc_buf[current_channel]);
|
||||
// //tmp /= 100;
|
||||
// adc_avrg[current_channel] = get_buffer_mean(&adc_buf[current_channel]);
|
||||
// //}
|
||||
// //else{
|
||||
// // adc_avrg[current_channel] = get_buffer_mean(&adc_buf[current_channel]);
|
||||
// // init[current_channel]=0;
|
||||
// //}
|
||||
//}
|
||||
|
||||
}
|
||||
11
adc.h
Normal file
11
adc.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef _ADC_H_
|
||||
#define _ADC_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
extern volatile uint16_t temp_values[4];
|
||||
|
||||
void adc_init(void);
|
||||
|
||||
#endif//_ADC_H_
|
||||
|
||||
76
main.c
76
main.c
@@ -13,22 +13,23 @@
|
||||
#include "i2c.h"
|
||||
#include "modbus.h"
|
||||
#include "menu.h"
|
||||
#include "adc.h"
|
||||
|
||||
volatile uint16_t holdingRegisters[10];
|
||||
|
||||
volatile uint16_t temp_values[4];
|
||||
volatile uint16_t temp_setpoints[4];
|
||||
|
||||
void modbusGet(void) {
|
||||
if (modbusGetBusState() & (1<<ReceiveCompleted))
|
||||
{
|
||||
switch(rxbuffer[1]) {
|
||||
case fcReadHoldingRegisters:
|
||||
i2c_start(0x71);
|
||||
holdingRegisters[0] = i2c_readNak();
|
||||
i2c_stop();
|
||||
holdingRegisters[0] = menu_state;
|
||||
case fcPresetSingleRegister:
|
||||
case fcPresetMultipleRegisters:
|
||||
modbusExchangeRegisters(holdingRegisters,0,4);
|
||||
lcd_set_position(holdingRegisters[1], holdingRegisters[2]);
|
||||
lcd_write("test");
|
||||
menu_state = holdingRegisters[0];
|
||||
break;
|
||||
default:
|
||||
modbusSendException(ecIllegalFunction);
|
||||
@@ -38,29 +39,30 @@ void modbusGet(void) {
|
||||
}
|
||||
|
||||
void do_buttons(){
|
||||
//i2c_start(0x71);
|
||||
//uint8_t buttons = i2c_readNak();
|
||||
//i2c_stop();
|
||||
////TODO error handling
|
||||
i2c_start(0x71);
|
||||
uint8_t buttons = i2c_readNak();
|
||||
i2c_stop();
|
||||
//TODO error handling
|
||||
|
||||
//if(buttons & (1 << 0))
|
||||
// set_item(1);
|
||||
//else if(buttons & (1 << 1))
|
||||
// set_item(2);
|
||||
//else if(buttons & (1 << 2))
|
||||
// set_item(2);
|
||||
//else if(buttons & (1 << 2))
|
||||
// set_item(3);
|
||||
//else if(buttons & (1 << 3))
|
||||
// set_item(4);
|
||||
//else if(buttons & (1 << 4))
|
||||
// set_item(5);
|
||||
if(buttons & (1 << 0))
|
||||
set_item(1);
|
||||
else if(buttons & (1 << 1))
|
||||
set_item(2);
|
||||
else if(buttons & (1 << 2))
|
||||
set_item(2);
|
||||
else if(buttons & (1 << 2))
|
||||
set_item(3);
|
||||
else if(buttons & (1 << 3))
|
||||
set_item(4);
|
||||
else if(buttons & (1 << 4))
|
||||
set_item(5);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
i2c_init();
|
||||
lcd_init();
|
||||
adc_init();
|
||||
|
||||
lcd_clear();
|
||||
|
||||
@@ -71,14 +73,46 @@ int main(void)
|
||||
TCCR0B|=(1<<CS01); //prescaler 8
|
||||
TIMSK0|=(1<<TOIE0);
|
||||
|
||||
PORTD |= (1<<6) | (1 << 7);
|
||||
PCICR |= (1<<PCIE2);
|
||||
PCMSK2 |= (1<<PCINT23);
|
||||
|
||||
draw_menu();
|
||||
|
||||
uint16_t x = 0;
|
||||
while(1){
|
||||
modbusGet();
|
||||
do_buttons();
|
||||
if(x==0){
|
||||
write_temps();
|
||||
write_setpoints();
|
||||
update_cursor();
|
||||
}
|
||||
x++;
|
||||
if(x>=2024)
|
||||
x=0;
|
||||
}
|
||||
}
|
||||
|
||||
ISR(TIMER0_OVF_vect) { //this ISR is called 9765.625 times per second
|
||||
modbusTickTimer();
|
||||
}
|
||||
|
||||
ISR(PCINT2_vect){
|
||||
if((PIND & (1<<7)) && (menu_state >= 1) && (menu_state <= 3)){
|
||||
if(PIND & (1<<6))
|
||||
temp_setpoints[menu_state-1]++;
|
||||
else
|
||||
temp_setpoints[menu_state-1]--;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//if(PIND & (1<<7))
|
||||
// if(PIND & (1<<6)){
|
||||
// if(temp_setpoints[menu_state] > 0)
|
||||
// }
|
||||
// else{
|
||||
// if(temp_setpoints[menu_state] < 300)
|
||||
// }
|
||||
}
|
||||
|
||||
2
makefile
2
makefile
@@ -1,5 +1,5 @@
|
||||
TARGET = main
|
||||
FILES = main i2c lcd modbus menu
|
||||
FILES = main i2c lcd modbus menu adc
|
||||
MCU = atmega328p
|
||||
PROGC = m328p
|
||||
CC = avr-gcc
|
||||
|
||||
70
menu.c
70
menu.c
@@ -1,4 +1,12 @@
|
||||
#include "lcd.h"
|
||||
#include "menu.h"
|
||||
#include <stdio.h>
|
||||
|
||||
// 0 = nothing selected
|
||||
// 1 = change zone 1
|
||||
// 2 = change zone 2
|
||||
// 3 = change zone 3
|
||||
volatile uint8_t menu_state = 0;
|
||||
|
||||
void write_heater_real_temp(uint8_t n, uint16_t temp){
|
||||
|
||||
@@ -8,6 +16,26 @@ void write_heater_set_temp(uint8_t n, uint16_t temp){
|
||||
|
||||
}
|
||||
|
||||
void update_cursor(){
|
||||
switch(menu_state){
|
||||
case 1:
|
||||
lcd_set_position(2,6);
|
||||
lcd_cursor(1);
|
||||
break;
|
||||
case 2:
|
||||
lcd_set_position(2,11);
|
||||
lcd_cursor(1);
|
||||
break;
|
||||
case 3:
|
||||
lcd_set_position(2,16);
|
||||
lcd_cursor(1);
|
||||
break;
|
||||
default:
|
||||
lcd_cursor(0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void draw_menu(){
|
||||
lcd_clear();
|
||||
lcd_write(" Heat Zone Control");
|
||||
@@ -15,11 +43,47 @@ void draw_menu(){
|
||||
lcd_write(" set:");
|
||||
lcd_set_position(3, 0);
|
||||
lcd_write("real:");
|
||||
//char str[16];
|
||||
//sprintf(str, "test %d", 1);
|
||||
//lcd_print_str(str);
|
||||
}
|
||||
|
||||
void write_temps(){
|
||||
char str[4];
|
||||
|
||||
lcd_set_position(3, 6);
|
||||
sprintf(str, "%3i", temp_values[0]);
|
||||
lcd_print_str(str);
|
||||
|
||||
lcd_set_position(3, 11);
|
||||
sprintf(str, "%3i", temp_values[1]);
|
||||
lcd_print_str(str);
|
||||
|
||||
lcd_set_position(3, 16);
|
||||
sprintf(str, "%3d", temp_values[2]);
|
||||
lcd_print_str(str);
|
||||
}
|
||||
|
||||
void write_setpoints(){
|
||||
char str[4];
|
||||
|
||||
lcd_set_position(2, 6);
|
||||
sprintf(str, "%3i", temp_setpoints[0]);
|
||||
lcd_print_str(str);
|
||||
|
||||
lcd_set_position(2, 11);
|
||||
sprintf(str, "%3i", temp_setpoints[1]);
|
||||
lcd_print_str(str);
|
||||
|
||||
lcd_set_position(2, 16);
|
||||
sprintf(str, "%3i", temp_setpoints[2]);
|
||||
lcd_print_str(str);
|
||||
}
|
||||
|
||||
void set_item(uint8_t page_num){
|
||||
lcd_clear();
|
||||
lcd_home();
|
||||
lcd_write("page %x", page_num);
|
||||
//if(menu_state=page_num)
|
||||
// menu_state=0;
|
||||
//else
|
||||
menu_state = page_num;
|
||||
}
|
||||
|
||||
|
||||
10
menu.h
10
menu.h
@@ -1,7 +1,15 @@
|
||||
#ifndef _MENU_H_
|
||||
#include <stdint.h>
|
||||
|
||||
void set_page(uint8_t page_num);
|
||||
extern volatile uint8_t menu_state;
|
||||
|
||||
extern volatile uint16_t temp_values[4];
|
||||
extern volatile uint16_t temp_setpoints[4];
|
||||
|
||||
void write_temps(void);
|
||||
void write_setpoints(void);
|
||||
void update_cursor(void);
|
||||
void set_item(uint8_t page_num);
|
||||
void draw_menu(void);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user