Hi,
okay, also hier die ganze Initialisierungsroutinen, Variablendefinitionen, etc. pp.
Code:
#define SND_PORT PORTD
#define SND_PIN PD5
#define SND_DDR DDRD
#define SND_PRESCALER 256
#define SND_OK 1
#define SND_ABORT 2
#define SND_PCCONNECTED 3
#define SND_ERROR 4
#define KEYPAD_ESC_PORT PORTD
#define KEYPAD_ESC_PIN PD6
#define KEYPAD_ESC_PINS PIND
#define KEYPAD_ESC_DDR DDRD
#define KEYPAD_OK_PORT PORTB
#define KEYPAD_OK_PIN PB1
#define KEYPAD_OK_PINS PINB
#define KEYPAD_OK_DDR DDRB
#define KEYPAD_DEC_PORT PORTD
#define KEYPAD_DEC_PIN PD7
#define KEYPAD_DEC_PINS PIND
#define KEYPAD_DEC_DDR DDRD
#define KEYPAD_INC_PORT PORTB
#define KEYPAD_INC_PIN PB0
#define KEYPAD_INC_PINS PINB
#define KEYPAD_INC_DDR DDRB
#define KEYPAD_SCANNING_FREQUENCY 1000 //0..65535
#define KEYPAD_DEBOUNCE_SLOW 200 //0..255
#define KEYPAD_DEBOUNCE_FAST 80
#define LCD_SCROLLING_ROW 1
#define LCD_SCANNING_FREQUENCY 75
#define LCD_DELAY_FACTOR 10
#define LCD_ROW_LENGTH 15
#define MOTOR_A_PORT PORTC
#define MOTOR_A_DDR DDRC
#define MOTOR_A_ENABLE_PIN PC5
#define MOTOR_A_CWCCW_PIN PC6
#define MOTOR_A_CLOCK_PIN PC7
#define MOTOR_B_PORT PORTC
#define MOTOR_B_DDR DDRC
#define MOTOR_B_ENABLE_PIN PC2
#define MOTOR_B_CWCCW_PIN PC3
#define MOTOR_B_CLOCK_PIN PC4
#define MOTOR_A_TOGGLE (MOTOR_A_PORT ^= (1 << MOTOR_A_CLOCK_PIN))
#define MOTOR_B_TOGGLE (MOTOR_B_PORT ^= (1 << MOTOR_B_CLOCK_PIN))
#define O2O 0 //MotA läuft rückwärts
#define l2O 1 //MotA läuft vorwärts
#define O2l 2 //MotB läuft vorwärts
#define l2l 3 //MotA und MotB 1 Schritt
#define l25 4 //MotA 1 Schritt, MotB 4 Schritte
#define l2lO 5 //MotA 1 Schritt, MotB 9 Schritte
#define l2lOO 6 //MotA 1 Schritt, MotB 99 Schritte
#define MOTOR_FAST 720
#define MOTOR_MEDIUM 480
#define MOTOR_SLOW 240
#define PHOT_LED_PORT PORTA
#define PHOT_LED_DDR DDRA
#define PHOT_LED_PIN PA6
#define phot_led_toggle() (PHOT_LED_PORT ^= (1<<PHOT_LED_PIN))
#define PHOT_CHANNEL 0b0111 //PORTA7 (ADC7)
#define PHOT_UREF 2.560
#define LED_BUSY_PORT PORTB
#define LED_BUSY_DDR DDRB
#define LED_BUSY_PIN PB4
#define LED_ERROR_PORT PORTB
#define LED_ERROR_DDR DDRB
#define LED_ERROR_PIN PB3
#define led_busy_on() (LED_BUSY_PORT |= (1<<LED_BUSY_PIN))
#define led_busy_off() (LED_BUSY_PORT &= ~(1<<LED_BUSY_PIN))
#define led_error_on() (LED_ERROR_PORT |= (1<<LED_ERROR_PIN))
#define led_error_off() (LED_ERROR_PORT &= ~(1<<LED_ERROR_PIN))
#define RS232_RETRY_THRESHOLD 10 //maximum = 32
#define SCREENSAVER 60 //maximum = 64
#define TIMEOUT 3 //maximum = 8
//--do not edit --
#define F_CPU 12000000UL
#define MODE_PC 0
#define MODE_STANDALONE 1
#define KEYPAD_KEYOK 1
#define KEYPAD_KEYESC 2
#define KEYPAD_KEYINC 3
#define KEYPAD_KEYDEC 4
#define KEYPAD_PRESCALER 256
#define LCD_PRESCALER 1024
#define PROG_INTERVALLMESSUNG 1
#define PROG_WACHSTUMSKURVE 2
#define PROG_LOESUNG_INDUZ 3
#define PROG_EXP_HALT 4
#define PROG_DATENTRANSFER 6
#define UART_BAUD_RATE 115200
#define UART_STR_LENGTH 40
#define EEPROM_CHIP_T 0b10100000
#define EEPROM_CHIP_V 0b10100010
//#define EEPROM_CHIP_V 0xA4
#define LOWBYTE(x) ((uint8_t)(x))
#define HIGHBYTE(x) ((uint8_t)((x) >> 8 ))
#define TOUINT16_T(x,y) (((x) << 8) | y)
#define stop_T1() (TCCR1B &= ~(1<<CS12))
//Fixdaten:
//-- globals --
volatile struct {
unsigned LCD_hold:1;
unsigned KEYPAD_close:1;
unsigned KEYPAD_keypressed:3;
unsigned Mode:1;
unsigned Screensaver:6;
unsigned Screen_Off:1;
} Status;
volatile struct {
unsigned Of:1;
uint8_t Counter;
uint8_t Counter_backup;
uint16_t Preloader;
} T0,T1,T2;
volatile struct {
unsigned Program:4;
unsigned Selection:3;
unsigned Hold:2;
unsigned Execute_Program_Nr:4;
float OD;
uint16_t Vol;
} Menue;
volatile struct {
uint8_t Interval; //in Minuten
float Df;
uint16_t Induz_Volume;
float Dilute_at_OD;
float Alarm_at_OD;
float Set_at_OD;
unsigned SIGA:1;
unsigned SIGB:1;
} Settings;
volatile struct {
unsigned Key:3;
float Blank;
float Abs;
uint16_t Eep_addr_counter;
uint16_t Eep_sc;
char Eep_str[40];
uint16_t Time_left; //in Sek bis zur nächsten Messg
uint8_t TRX_Indexer;
} Data;
volatile struct {
unsigned Open:1;
unsigned Cmd_Start:1;
unsigned Cmd_Complete:1;
unsigned Cmd_Received:1;
uint8_t i;
char Cmd[UART_STR_LENGTH];
unsigned rtys:5;
unsigned Pong:1;
unsigned Timeout:3;
} RS232;
volatile struct {
char Cmd[40];
char Par[40];
char Out[UART_STR_LENGTH];
uint8_t Par_l;
uint16_t crc;
} Command;
volatile char LCD_lauftext[100];
volatile char LCD_temp;
//-- forwards --
int main(void);
uint8_t strlength(const char* str);
void snd_init(void);
void motor_init(void);
void avrboard_init(void);
void delay_1s(void);
void start_T1(void);
void lcd_clr_scrollbar(void);
void lcd_scroll_text(char* text, uint16_t Hz, uint8_t delay_factor);
void keypad_init(void);
void keypad_open(uint16_t Hz, uint8_t debounce_factor);
void keypad_close(void);
uint8_t keypad_check(void);
void keypad_wait(uint8_t key);
void rs232_gets(void);
void rs232_puts(char* str);
void rs232_open(void);
void rs232_decode(void);
unsigned rs232_checkcrc(void);
void rs232_execute(void);
void rs232_close(void);
void snd_beep(uint16_t Hz, uint8_t time_index);
void snd_play(uint8_t Melody);
void led_init(void);
void menue_show(void);
void menue_select(void);
void phot_ReadAbs(void);
void phot_ReadBlank(void);
uint16_t phot_ReadADC(void);
void motor_stop(void);
unsigned motor_open(uint32_t Ticks, uint8_t Prog, uint16_t frequency);
uint16_t eeprom_readuint16_t(uint16_t eeprom_adress);
void eeprom_writeuint16_t(uint16_t eeprom_adress, uint16_t var16);
float eeprom_readfloat(uint16_t eeprom_adress);
void eeprom_writefloat(uint16_t eeprom_adress, float var32);
unsigned eeprom_add_data(uint16_t min, float value);
void eeprom_read_data(uint16_t eeprom_adress);
uint16_t eeprom_get_stopcodon(void);
void eeprom_close(void);
void eeprom_check(char err);
/*
uint8_t strlength(const char * str) {
uint8_t length=0;
while(str[length]!='\0'){
length++;
}
return length;
}*/
ISR(TIMER0_OVF_vect)
{
//Keypad-Abfrage
if (!(KEYPAD_OK_PINS & (1 << KEYPAD_OK_PIN))){
Status.KEYPAD_keypressed=KEYPAD_KEYOK;
} else
if (!(KEYPAD_ESC_PINS & (1 << KEYPAD_ESC_PIN))){
Status.KEYPAD_keypressed=KEYPAD_KEYESC;
} else
if (!(KEYPAD_INC_PINS & (1 << KEYPAD_INC_PIN))){
Status.KEYPAD_keypressed=KEYPAD_KEYINC;
} else
if (!(KEYPAD_DEC_PINS & (1 << KEYPAD_DEC_PIN))){
Status.KEYPAD_keypressed=KEYPAD_KEYDEC;
} else Status.KEYPAD_keypressed = 0;
T0.Of=1;
TCNT0=T0.Preloader;
}
ISR(TIMER1_OVF_vect) {
if (T1.Counter<255) T1.Counter++;
else T1.Counter=0;
if (Status.Screensaver < SCREENSAVER) Status.Screensaver++; //zählt bis 10 und stopp da.
if (RS232.Timeout < TIMEOUT) RS232.Timeout++;
T1.Of=1;
TCNT1=T1.Preloader;
}
ISR(TIMER2_OVF_vect)
{
if (Status.LCD_hold==0){
if (T2.Counter == 0) {
lcd_gotoxy(0,LCD_SCROLLING_ROW);
for (uint8_t i=0; i<=LCD_ROW_LENGTH; i++) {
lcd_putc(LCD_lauftext[i]);
}
LCD_temp=LCD_lauftext[0];
for (uint8_t i=0; i<=strlen((const char *)LCD_lauftext); i++) {
LCD_lauftext[i]=LCD_lauftext[i+1];
}
LCD_lauftext[strlen((const char *)LCD_lauftext)]=LCD_temp;
T2.Counter = T2.Counter_backup;
} else T2.Counter--;
}
T2.Of=1;
TCNT2=T2.Preloader;
}
void start_T1(){
T1.Preloader=18660; //für 1s, Prescaler ist 256
TCCR1B |= (1<<CS12); //256 als Vorteiler
TCNT1 = T1.Preloader;
TIMSK |= (1<<TOIE1);
T1.Of=0;
}
/*
void delay_1s(){
_delay_ms(20);_delay_ms(20);_delay_ms(20);_delay_ms(20);_delay_ms(20);
_delay_ms(20);_delay_ms(20);_delay_ms(20);_delay_ms(20);_delay_ms(20);
_delay_ms(20);_delay_ms(20);_delay_ms(20);_delay_ms(20);_delay_ms(20);
_delay_ms(20);_delay_ms(20);_delay_ms(20);_delay_ms(20);_delay_ms(20);
_delay_ms(20);_delay_ms(20);_delay_ms(20);_delay_ms(20);_delay_ms(20);
_delay_ms(20);_delay_ms(20);_delay_ms(20);_delay_ms(20);_delay_ms(20);
_delay_ms(20);_delay_ms(20);_delay_ms(20);_delay_ms(20);_delay_ms(20);
_delay_ms(20);_delay_ms(20);_delay_ms(20);_delay_ms(20);_delay_ms(20);
_delay_ms(20);_delay_ms(20);_delay_ms(20);_delay_ms(20);_delay_ms(20);
_delay_ms(20);_delay_ms(20);_delay_ms(20);_delay_ms(20);_delay_ms(20);
}
*/
void snd_init() {
SND_DDR |= (1<<SND_PIN); //PB0 als Ausgang
SND_PORT &= ~(1<<SND_PIN); //PB0 auf Low
}
void keypad_init() {
KEYPAD_OK_DDR &= ~(1<<KEYPAD_OK_PIN);
KEYPAD_OK_PORT |= (1<<KEYPAD_OK_PIN);
KEYPAD_ESC_DDR &= ~(1<<KEYPAD_ESC_PIN);
KEYPAD_ESC_PORT |= (1<<KEYPAD_ESC_PIN);
KEYPAD_INC_DDR &= ~(1<<KEYPAD_INC_PIN);
KEYPAD_INC_PORT |= (1<<KEYPAD_INC_PIN);
KEYPAD_DEC_DDR &= ~(1<<KEYPAD_DEC_PIN);
KEYPAD_DEC_PORT |= (1<<KEYPAD_DEC_PIN);
}
void motor_init() {
MOTOR_A_DDR |= (1<<MOTOR_A_ENABLE_PIN); //Ausgang
MOTOR_A_PORT &= ~(1<<MOTOR_A_ENABLE_PIN); //LOW
MOTOR_A_DDR |= (1<<MOTOR_A_CWCCW_PIN); //Ausgang
MOTOR_A_PORT &= ~(1<<MOTOR_A_CWCCW_PIN); //LOW
MOTOR_A_DDR |= (1<<MOTOR_A_CLOCK_PIN); //Ausgang
MOTOR_A_PORT &= ~(1<<MOTOR_A_CLOCK_PIN); //LOW
MOTOR_B_DDR |= (1<<MOTOR_B_ENABLE_PIN); //Ausgang
MOTOR_B_PORT &= ~(1<<MOTOR_B_ENABLE_PIN); //LOW
MOTOR_B_DDR |= (1<<MOTOR_B_CWCCW_PIN); //Ausgang
MOTOR_B_PORT &= ~(1<<MOTOR_B_CWCCW_PIN); //LOW
MOTOR_B_DDR |= (1<<MOTOR_B_CLOCK_PIN); //Ausgang
MOTOR_B_PORT &= ~(1<<MOTOR_B_CLOCK_PIN); //LOW
}
void led_init() {
LED_BUSY_DDR |= (1<<LED_BUSY_PIN); //Ausgang
LED_BUSY_PORT &= ~(1<<LED_BUSY_PIN); //LOW
LED_ERROR_DDR |= (1<<LED_ERROR_PIN); //Ausgang
LED_ERROR_PORT &= ~(1<<LED_ERROR_PIN); //LOW
}
void avrboard_init(){
motor_init(); //zuerst, saugt zu viel strom...
led_init();
led_busy_on();
//FIXDATEN
T0.Of=0;
T1.Of=0;
T1.Counter=0;
T2.Of=0;
RS232.Pong=0;
Status.LCD_hold=0;
Status.KEYPAD_close=0;
Status.Screensaver=0;
Status.Screen_Off = 0;
Status.Mode = MODE_PC;
RS232.Timeout=0;
Menue.Program=0;
Menue.Selection=0;
Menue.Hold=0;
RS232.i=0;
RS232.Open=0;
Data.Eep_addr_counter=0;
Data.Eep_sc = 0;
Data.TRX_Indexer = 0;
Data.Key = 0;
sei();
//Rest
keypad_init();
lcd_init(LCD_DISP_ON);
lcd_clrscr();
lcd_puts("Beginne Init.");
lcd_gotoxy(0,1);
lcd_puts("[OK] druecken!");
//keypad_wait(KEYPAD_KEYOK);
start_T1();
snd_init();
i2c_init();
PHOT_LED_DDR |= (1<<PHOT_LED_PIN); //Ausgang
PHOT_LED_PORT &= ~(1<<PHOT_LED_PIN); //LOW
#ifndef nors232
uart_init( UART_BAUD_SELECT_DOUBLE_SPEED(UART_BAUD_RATE,F_CPU) );
#endif
led_busy_off();
keypad_open(KEYPAD_SCANNING_FREQUENCY,KEYPAD_DEBOUNCE_SLOW);
}
twimaster.c
Code:
/*************************************************************************
* Title: I2C master library using hardware TWI interface
* Author: Peter Fleury <pfleury@gmx.ch> http://jump.to/fleury
* File: $Id: twimaster.c,v 1.3 2005/07/02 11:14:21 Peter Exp $
* Software: AVR-GCC 3.4.3 / avr-libc 1.2.3
* Target: any AVR device with hardware TWI
* Usage: API compatible with I2C Software Library i2cmaster.h
**************************************************************************/
#include <inttypes.h>
#include <compat/twi.h>
#include <i2cmaster.h>
/* define CPU frequency in Mhz here if not defined in Makefile */
#ifndef F_CPU
#define F_CPU 12000000UL
#endif
/* I2C clock in Hz */
#define SCL_CLOCK 100000L
/*************************************************************************
Initialization of the I2C bus interface. Need to be called only once
*************************************************************************/
void i2c_init(void)
{
/* initialize TWI clock: 100 kHz clock, TWPS = 0 => prescaler = 1 */
TWSR = 0; /* no prescaler */
TWBR = ((F_CPU/SCL_CLOCK)-16)/2; /* must be > 10 for stable operation */
}/* i2c_init */
...
Brauchst sonst nochwas?
Viele Grüße,
Simon
Lesezeichen