AnsweredAssumed Answered

ADuC841 serial communication

Question asked by hbermudez50 on Oct 15, 2011
Latest reply on Oct 17, 2011 by PatrickN

Hello, I am going to be straight forward with my problem. I am currently sending and receiving data through the uart port, using interruption 4 and 6....I read from the adcs and send those values through the serial, and I am receiving characters from the serial that represent voltage values for my dacs, both codes works perfect independently but, when combined the data is mess up....I am suspecting that my problem could be lying on the interrupts priorities, but I am not sure, my code is as follows...

 

 

 

 

#include <ADuC841.h>
#include <stdio.h>                /* prototype declarations for I/O functions */
//#include <ADUC812.H>                                         /* is enabled                        */
#include <float.h>
#include <math.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
void delay(int length);
void showhex(short x, int dacNumber);
sbit LED = 0x0B4;
//the problem could be the printing
void serial_int (void) interrupt 4
{
static unsigned char  inputCode;       
static int       fieldCounter = 0;
static int            dataCounter = 0;
static short int readingVoltages = 0;
static unsigned short temp1 = 0;
static unsigned short temp2 = 0;
static int i;
static unsigned char voltageValues[sizeof(char)*4];
static float temp = 0.0;
static unsigned short convertedValues;
  if (RI == 1)  
       {
 
  inputCode = SBUF;
                        
  if((readingVoltages == 0) && (inputCode == '<')     )      //strcmp(header, foo) == 0))
  {
            readingVoltages = 1;
fieldCounter = 0;
  }
  else if ((readingVoltages == 1) && (fieldCounter > 3) && (inputCode == '>')) //strcmp(tail, foo) == 0))
  {
        readingVoltages = 0;
  }
  else if (readingVoltages == 1)
  {                                           
             switch (fieldCounter)
{
case 0 :
//voltageValues[0] = '0';
if(inputCode == '+')
{
LED = 1;//positiveNegative = 1;
}
if(inputCode == '-')
{
LED = 0;//positiveNegative = 1;
}
fieldCounter++;                         
break;
case 1 :
voltageValues[0] = inputCode;                 
fieldCounter++;
break;
case 2 :
fieldCounter++;
voltageValues[1] = inputCode;
break;
case 3 :
fieldCounter++;
voltageValues[2] = inputCode;
break;
case 4 :
voltageValues[3] = inputCode;                 
fieldCounter++;
if(dataCounter == 0)
{
temp = atof(voltageValues);
//convertedValues = (temp / (1.22 * pow(10.0,-3.0)));
//convertedValues = (temp / (1.09 * pow(10.0,-3.0)));
convertedValues = (temp / 4.7f) * 4096;
temp1 = convertedValues & 0xFF00;
temp1 = temp1 >> 8;
temp2 = convertedValues & 0x00FF;
DAC0H = temp1;
DAC0L = temp2;
dataCounter++;
}
else
{
//     temp = atof(voltageValues);
//convertedValues = (temp / (1.22 * pow(10.0,-3.0)));
//     convertedValues = (temp / (1.09 * pow(10.0,-3.0)));
//     temp1 = convertedValues & 0xFF00;
//     temp1 = temp1 >> 8;
//     DAC1H = temp1;
//     temp2 = convertedValues & 0x00FF;
//     DAC1L = temp2;
dataCounter = 0;
}
break;
default:
break;
   }
  }
             RI = 0;
  return;
}
}
// ADC Interrupt Service Routine */
void adc_int() interrupt 6           
{
        static char channel = 0;
//variables segment
static int heartBeat = 0;
static int joystickStatus = 0;
static int throttleStatus = 0;
    static float throttlePosition = 0;
     int joystickPositionX = 0;
//     static float joystickPositionY = 0.0;
//      float servo1Position = 0;
//channel = (channel + 1) % 2;                                   // looping from chn 0 to chn 2
ADCCON2 = (ADCCON2 & 0xF0) | channel;
delay(1000000000);
SCONV = 1;
//DAC0H = ADCDATAH & 0x0F;
//DAC0L = ADCDATAL;
channel++;
if(channel > 2){channel= 0;}
printf("<");
//printf("%d", heartBeat);
       printf("%02BX%02BX",ADCDATAH,ADCDATAL);
       printf(">");
return;
}     
/*------------------------------------------------
The main C function.  Program execution starts
here after stack initialization.
------------------------------------------------*/
void main (void) {
/*------------------------------------------------
Setup the serial port for 9600 bauds.
------------------------------------------------*/
  
  SCON  = 0x50;                  /* SCON: mode 1, 8-bit UART, enable rcvr      */
  TMOD |= 0x20;               /* TMOD: timer 1, mode 2, 8-bit reload        */
  TH1   = 221;                /* TH1:  reload value for 9600 baud */
  TR1   = 1;                  /* TR1:  timer 1 run                          */
  TI    = 1;                  /* TI:   set TI to send first char of UART    */                          
/* PRECONFIGURE... */
//ADCCON1 = 0xCE;
//ADCCON2 = 0xA0 ;      // select channel to convert ch - 0
ADCCON1 = 0xEC;      // power up ADC
ADCCON2 = 0x20;
DACCON = 0x0F;
EA = 1;
ES = 1;
EADC = 1;
//SCON  = 0x50;
//TMOD |= 0x20;
//TH1   = 0xFA;            //9600 baud rate
//TR1 = 1;
//TI = 1;
while (1) {
}
   
}
void delay(int length)
{
while (length >=0)
    length--;
}

 

thanks in advance....

 

hector

Outcomes