O.K. Here is what I’ve done:
orangutan code:
/*Serial Control Test Program for Orangutan or Baby Orangutan
V 1.1
Adam Borrell
5/29/08*/
#define F_CPU 20000000//CPU clock for Baby Orangutan
//#define F_CPU 8000000//CPU clock for Orangutan
//***Uncomment ONE of the above lines for the specific device***
#define BAUD 9600//baud rate for UART
#define MYUBRR (F_CPU/16/BAUD-1)//baud rate variable for UART hardware
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
unsigned volatile char dataIn,newSerCmd=0,wasPressedAlready=0;
void USART_Init(unsigned int ubrr){//Initialize USART hardware & settings for Serial Radio
UBRR0H=(unsigned char)(ubrr>>8);//set buad rate
UBRR0L=(unsigned char) ubrr;
UCSR0B=(1<<TXEN0)|(1<<RXEN0)|(1<<RXCIE0);//enable transmitter & receiver, receive complete interrupt
UCSR0C=(3<<UCSZ00);//Set frame format for 8bit with 1 stop
}
void USART_Trans(unsigned char data){//Transmit a byte of data over USART
while(!(UCSR0A&(1<<UDRE0)));//wait for transmition to complete
UDR0=data;
}
ISR(USART_RX_vect){//USART Byte reieved
dataIn=UDR0;//grab copy of serial byte
newSerCmd=1;//indicate new byte received
}
int main(){
DDRB|=(1<<PB0);//setup output pins
DDRB &=~ (1<<PB3);
PORTB |=(1<<PB3);
USART_Init(MYUBRR);//Initialize USART
sei();//enable global interrupts
while(1){
if(newSerCmd){//if new byte received
switch(dataIn){
case 'f'://forward
PORTB|=(1<<PB0);
//PORTD&=~(1<<PD5);
break;
case 's'://reverse
PORTB&=~(1<<PB0);
//PORTD|=(1<<PD5);
break;
case 130://stop
PORTB&=~(1<<PB1);
PORTD&=~(1<<PD5);
break;
default:
break;
}
newSerCmd=0;
}
if((PINB&((1<<PB3)|(1<<PB4)|(1<<PB5)))){
if(!wasPressedAlready){
USART_Trans('a');//transmit byte
wasPressedAlready=1;
}
}else if(wasPressedAlready){
wasPressedAlready=0;
_delay_ms(10);//debounce delay
}
}
return 0;
}
And the linux code:
/* functions for communicating with the pololu sadapter over a serial port */
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <sys/select.h>
/* write a message to the SSC */
/*void sendCommand(Sadapter, char id, char cmd, char servo, char data) {
char buf[5];
snprintf(buf,5,"%c%c%c%c%c",0x80,id,cmd,servo,data);
write(fd,buf,5);
}
void sendCommand6(Sadapter, char controller, char cmd, char servo, char data1, char data2) {
char buf[6];
servo += controller*16;
snprintf(buf,6,"%c%c%c%c%c%c",0x80,0x01,cmd,servo,data1,data2);
write(fd,buf,6);
}
int enable(Sadapter, int controller, int servo, int enabled) {
if(!connected) { return 0; }
if(enabled) {
sendCommand((char)controller,0,(char)servo,(char)0x40);
printf("enabled %i\n",servo); fflush(stdout);
} else {
sendCommand((char)controller,0,(char)servo,0x00);
printf("disabled %i\n",servo); fflush(stdout);
}
}
int stepTo(Sadapter, int controller, int servo, int pos) {
if(!connected) { return 0; }
sendCommand6((char)controller,4,(char)servo,(char)(pos/128),(char)(pos%128));
return 1;
}
*/
int connected, fd, done=0;
char idstring[26];
void endSession() {
if(!connected) return;
close(fd);
}
int connectSadapter() {
struct termios options;
int count,ret;
connected=0; /* reset in case we fail to connect */
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY);
if (fd == -1) return 0;
else fcntl(fd, F_SETFL, 0);
tcgetattr(fd, &options);
/* go to 9600 baud */
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
options.c_cflag |= (CLOCAL | CREAD); /* enable */
options.c_cflag &= ~PARENB; /* 8N1 */
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
/* set all of the options */
tcsetattr(fd, TCSANOW, &options);
connected=1;
return 1;
}
int main()
{//I added the devInput variable
char input[32];
char devInput[32];
if(done){
printf("Program Terminated!\n");
system("PAUSE");
return -1;
}
printf("\n\nPololu Serial Adapter program\n\n");
printf("Commands:\n");
printf("Forward - w\n");
printf("Reverse - s\n");
printf("Stop - x\n");
printf("Exit - q\n");
//connect to serial adapter
connectSadapter();
while (!done)
{
*input=getchar();
switch(*input)
{
case 'q'://exit the program
done = 1;
printf("Ending ssesion and exitting program\n");
endSession();
break;
case 'w'://forward
printf("Forward\n");
write(fd,"f",1);
break;
case 's'://reverse
printf("Reverse\n");
write(fd,"back",4);
break;
case 'x'://stop
printf("Stop\n");
write(fd,"s",1);
break;
default:
break;
}
}
//I added this read function
*input = read(fd, devInput, 1);
if (*input == 'a')
{
printf("I got A!!! It's WORKING!!!\n");
}
}
const char *getId() {
return idstring;
}
what do you think?