c-programma voor het omgaan met het communicatie protocol.
/*
* Madeleine_V7.c
*
* Created: 15-2-2016 16:08:02
* Author : Ben
Dit is een testprogramma voor de communicatie tussen VIP
en een programma lopend op Madeleine.
De PC is de master en Madeleine is slave.
Communicatieparameters: 115200 BAUD 1 startbit 8 databits 1 stopbit noparity
Madeleine meldt zich met twee zinnen.
Wacht dan op een ASCII-string eindigend op '\n'.
Na ontvangst wordt de zin direct teruggezonden met een '\0' teken.
Gebruikte interrupts
ISR(TIMER2_OVF_vect) // Real Time Clock
ISR(INT0_vect) //Button
ISR(USART_RXC_vect) //Usart interrupt routine
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <stdlib.h>
#include "Madeleine.c"
#include "bool.h"
#include "pwm.h"
#include "pwm.c"
#define F_CPU 14745600L
//#define F_CPU 8000000L
//#define USART_BAUDRATE 9600
//#define F_CPU 3686400L
#define USART_BAUDRATE 115200
//#define USART_BAUDRATE 19200
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
#include "util/delay.h"
//#define FORWARD 46
int uart_putc(unsigned char c);
void uart_puts (char *s);
void uart_putheader();
void mot2registers();
void timer1_registers();
int counter =0;
int bufcounter = 0;
int flag = 0;
int textflag = 0;
int packetintro = 5;
int soh = 0x01; //start of header
int seqnr = 0;// sequencenumber
int messize = 0; //messagesize
int cmd = 0; // no commands
int stx = 0x02; // start of text
int etx = 0x03; //end_of_text character
int eot = 0x04; //end_of_transmission
int crcbyte = 0;
char buffer[20];
//char charbuffer[20];
char charbuffer[20];//external definition. external declaration in stk500.h
int timer2sec = 0;
int main (void)
{
UCSRB |= (1 << RXEN) | (1 << TXEN); // Turn on the transmission and reception circuitry
UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes
UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register
UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register
UCSRB |= (1 << RXCIE); // Enable the USART Receive Complete interrupt (USART_RXC)
sei(); // Enable the Global Interrupt Enable flag so that interrupts can be processed
_delay_ms(4000); // Gives bluetooth time to make connection
uart_puts("Madeleine"); // Robot introduces itself
led_on(); // Meaning I am almost ready
MotorInit();
// setMotorPower(3,3);
// mot1_init();
// mot2_init(); //Let op gebruikt timer2
int0_init(); //Voor button
// timer2_init(); //inschakelen real time clock. Zie interruptvector functie
for (;;) // Loop forever
{
// Do nothing - echoing is handled by the ISR instead of in the main loop
// led(flag);
} //end for
} //end main
int procedure(int cmd) // Ontvangt commando's van RS232-Messages en stuurt antwoorden terug
{
switch (cmd)
{
case 1 : //version
{
uart_putheader();
uart_puts(version());
uart_putc(etx);
}
break;
/*
sscanf(buffer,"%s%d",t,&i);
uart_puts(t);
itoa(i,tekst,10);
uart_puts(tekst);
*/
case 2 : //leds opdracht: led Arg (Arg = 0...255)
{
int leds = 0;
led((uint8_t) leds); // casting en naar functie
led(leds);
uart_putc(etx);
}
break;
/*
case 3 : //adc Arg (Arg = 0...7)
{
char *p;
uint16_t i = 0;
char adcbuffer[20];
int adc = 0;
sscanf(buffer,"%d",&adc);
i = readADC( adc); // casting en naar functie
p = itoa(i,adcbuffer,10);
uart_putc(soh);
uart_putc(seqnr);
uart_putc(messize);
uart_putc(cmd);
uart_putc(stx);
uart_puts(p);
uart_putc(etx);
uart_putc(crcbyte);
}
break;
*/
case 4 : //mot1 opdracht: mot1 Arg (Arg = R | F| S | B)
{
uart_putheader();
char blank;
char motcmd;
uart_puts
(itoa(DDRB
,charbuffer
,10));
uart_puts
(itoa(PORTB
,charbuffer
,10));
sscanf(buffer
,"%c%c",&blank
,&motcmd
);
mot1(motcmd);
uart_putc(motcmd);
uart_putc('\0');
uart_puts
(itoa(PORTB
,charbuffer
,10));
uart_putc(etx);
}
break;
case 5 : //mot2 opdracht: mot2 Arg (Arg = R | F| S | B)
{
uart_putheader();
char blank;
char motcmd;
// uart_puts(itoa(DDRB,charbuffer,10));
// uart_puts(itoa(PORTB,charbuffer,10));
sscanf(buffer
,"%c%c",&blank
,&motcmd
);
mot2(motcmd);
uart_puts("MOT2 param");
uart_putc(motcmd);
uart_putc('\0');
// uart_puts(itoa(PORTB,charbuffer,10));
uart_putc(etx);
}
break;
case 6 : //led_on
{
uart_putheader();
led_on();
uart_putc(etx);
}
break;
case 7 : //led_off
{
uart_putheader();
led_off();
uart_putc(etx);
}
break;
case 8 : //timer2_init
{
timer2sec = 0;
uart_putheader();
// timer2_init();
uart_puts
(itoa(TIMSK
,charbuffer
,10));
uart_putc(etx);
}
break;
case 9 : //timer2_off
{
uart_putheader();
// timer2_off();
uart_puts
(itoa(TIMSK
,charbuffer
,10));
uart_putc(etx);
}
break;
case 10 : //adc
{
unsigned int result = 0;
uart_putheader();
result = adc();
uart_puts("adc lezen intern"); // herhaling van laatste. Gemiddelde beter??
uart_puts
(itoa(result
,charbuffer
,10));
uart_putc(etx);
}
break;
case 11 : //mot2registers()
{
uart_putheader();
mot2registers();
uart_putc('\0');
uart_putc(etx);
}
break;
case 12 : //motoren opdracht: mleft (Arg = -9... 9) mright idem
{
int mleft = 0;
int mright = 0;
sscanf(buffer
,"%d%d",&mleft
,&mright
);
// setMotorPower((short) mleft,(short) mright);
setMotorPower((short) mright,(short) mleft); //uitzoeken waarom dat nodig is.
uart_putheader();
uart_putc(etx);
}
break;
case 13 : //registers timer1 opvragen
{
uart_putheader();
timer1_registers();
uart_putc(etx);
}
break;
case 14 : //adcr
{
unsigned int result = 0;
uart_putheader();
result = adcr();
uart_puts("adc rechts"); // herhaling van laatste. Gemiddelde beter??
uart_puts
(itoa(result
,charbuffer
,10));
uart_putc(etx);
}
break;
case 15 : //adcl
{
unsigned int result = 0;
uart_putheader();
result = adcl();
uart_puts("adc links"); // herhaling van laatste. Gemiddelde beter??
uart_puts
(itoa(result
,charbuffer
,10));
uart_putc(etx);
}
break;
case 16 : //adcra
{
unsigned int result = 0;
uart_putheader();
result = adcra();
uart_puts("adc rechtsachter"); // herhaling van laatste. Gemiddelde beter??
uart_puts
(itoa(result
,charbuffer
,10));
uart_putc(etx);
}
break;
case 255 : //respond
{
uart_putheader();
// mot2registers();
uart_putc('\0');
uart_putc(etx);
}
break;
} //end switch
cmd = 0;
counter = 0;
// flag = 0;
textflag = 0;
bufcounter = 0;
return 0;
}// end procedure
/* Werkt.
ISR(TIMER2_OVF_vect) // Real Time Clock
{
if (PINB & (1<<PINB0))//If set ( = off)
{led_on();
}
else
{led_off();
}
}
*/
/*
ISR(TIMER2_OVF_vect) // Real Time Clock
{
if (timer2sec > 8)
{
// mot1('S');
// mot2('S');
setMotorPower(0,0);
timer2_off();
}
if (timer2sec == 4)
{
// mot1('R');
// mot2('R');
setMotorPower(-3,-3);
}
if (timer2sec == 0)
{
// mot1('F');
// mot2('F');
setMotorPower(3,3);
}
timer2sec++;
}
*/
/*
timer2 bijbehorende VIP commands:
adc
motoren 4 4
timer2_init
*/
/* Wordt in Madeleine niet gebruikt tot nog toe.
Timer2 nader bestuderen in Atmel documentatie
Het is gecompliceerd.
ISR(TIMER2_OVF_vect) // Real Time Clock
{
uart_putheader();
unsigned int result = 0;
result = adc();
uart_puts("sensor middenvoor"); // herhaling van laatste. Gemiddelde beter??
uart_puts(itoa(result,charbuffer,10));
if (result >= 350)
{
setMotorPower(0,0);
timer2_off();
// uart_puts("gestopt");
// uart_putc(etx);
// uart_putc(eot);
}
uart_putc(etx);
}
*/
/*
ISR(INT0_vect) //Button
{
if (PINB & (1<<PINB0))//If set ( = off)
{led_on();
}
else
{led_off();}
// led_off();
}
*/
ISR(INT0_vect) //Button
{
timer2sec = 0;
// timer2_init();
}
ISR(USART_RXC_vect) //Usart interrupt routine
{
unsigned char ReceivedByte;
ReceivedByte = UDR; // Fetch the received byte value into the variable "ByteReceived"
// UDR = ReceivedByte; // Echo back the received byte back to the computer
// uart_putc(ReceivedByte);
if (textflag == 0)
{
switch (counter )
{
case 0 :
{
if (ReceivedByte == soh)
{
}
}
break;
case 1 :
{
seqnr = ReceivedByte;
}
break;
case 2 :
{
messize = ReceivedByte;
}
break;
case 3 :
{
cmd = ReceivedByte;
// led(cmd);
}
break;
case 4 :
{
if (ReceivedByte == stx)
{
textflag = 1;
}
}
break;
}
++counter;
}
else
{
if (ReceivedByte != etx)
{
buffer[bufcounter] = ReceivedByte;
++bufcounter;
// UDR = ReceivedByte;
}
else
{
buffer[bufcounter] = '\0';
// uart_puts(buffer);
procedure(cmd);
counter = 0;
textflag = 0;
flag =1;
bufcounter = 0;
}
}//end if
} //end isr
int uart_putc(unsigned char c)
{
while (!(UCSRA & (1<<UDRE))) /* wachten tot zenden mogelijk is */
{
}
UDR = c; /* zend een character */
return 0;
}
/* puts is onafhankelijk van het controllertype */
void uart_puts(char *s)
{
while (*s)
{ /* so lange *s != '\0' dus ongelijk aan een "string-eindteken" */
uart_putc(*s);
s++;
}
uart_putc('\0');
}
void uart_putheader()
{
uart_putc(soh);
uart_putc(seqnr);
uart_putc(messize);
uart_putc(cmd);
uart_putc(stx);
}
void timer1_registers()
{
uart_puts("TCCR1A");
uart_puts
(itoa(TCCR1A
,charbuffer
,10));
uart_puts("TCCR1B");
uart_puts
(itoa(TCCR1B
,charbuffer
,10));
}
void mot2registers()
//hulproutine voor registers mot2
{ uart_puts("DDRB");
uart_puts
(itoa(DDRB
,charbuffer
,10));
uart_puts("PORTB");
uart_puts
(itoa(PORTB
,charbuffer
,10));
uart_puts("PORTD");
uart_puts
(itoa(PORTD
,charbuffer
,10));
uart_puts("DDRD");
uart_puts
(itoa(DDRD
,charbuffer
,10));
uart_puts("TCCR2");
uart_puts
(itoa(TCCR2
,charbuffer
,10));
}