CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Advice regarding code optimisation

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Gerhard



Joined: 30 Aug 2007
Posts: 144
Location: South Africa

View user's profile Send private message Send e-mail

Advice regarding code optimisation
PostPosted: Sat Sep 13, 2008 8:34 am     Reply with quote

Hi. I am busy with a project to sample data from 2*1khz analog sources, send the samples over a 485 cable, receive and split the 2 signals again and output the analog values with a MAX519 DAC. I have the code working but the output is not very accurate and i was wondering if someone might have some ideas as to improve the quality of the output signals.
Included is the transmit and receive programs.

Transmit
Code:
#include <16F886.H>
#fuses HS, NOWDT, NOPROTECT, NOBROWNOUT, PUT, NOLVP
#use delay(clock=20m)
#use rs232(baud=38400, xmit=PIN_C6, rcv=PIN_C7)

void setup()
{   
     setup_adc_ports(sAN0|sAN1);
     setup_adc( ADC_CLOCK_DIV_32  );
     enable_interrupts(global);

}
void read_analog();
void write_toRS232(int,int);
int ch0=0,ch1=0;

void main()

   
  setup();
 
while(1)
  {
    read_analog();
    write_toRS232(ch0,ch1);
 
  }

}

void write_toRS232(cho,chi)
{   
   putc(cho);
   putc(chi);
 
   
}

void read_analog()
{
  set_adc_channel(0);
  delay_us(2);
  ch0=read_adc();
  set_adc_channel(1);
  delay_us(2);
  ch1=read_adc();

}


Recieve
Code:
#include <16F886.H>
#fuses HS,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=20m)       
#use i2c(Master, sda=PIN_C4, scl=PIN_C3,FORCE_HW,Fast=400000)
#use rs232(baud=38400, xmit=PIN_C6, rcv=PIN_C7)


int chan0 = 0;
int chan1 = 0;

#define MAX519_ADDRESS           0x40     
#define MAX519_DATA_COMMAND_DAC0 0x00  //Set channel 0 on dual dac
#define MAX519_DATA_COMMAND_DAC1 0x01  //Set channel 1 on dual dac

#int_rda         //serial interupt service routine
void serial_isr() {
 
   if (kbhit()) {                  //char ready on uart to come in
      chan0 = getc();
      chan1 = getc();    
       }
       
}   
//=========================

void max519_write_dac(int8 chan0,int8 chan1)
{
disable_interrupts(global);
i2c_start();
i2c_write(MAX519_ADDRESS);
i2c_write(MAX519_DATA_COMMAND_DAC0);
i2c_write(chan0);                   
i2c_write(MAX519_DATA_COMMAND_DAC1);
i2c_write(chan1);
i2c_stop();
}
 


void main()
{
enable_interrupts(INT_RDA);         // Enable Serial Reception Interrupt
enable_interrupts(GLOBAL);


while(1)
    {
     
enable_interrupts(GLOBAL);
  chan0 = getc();
  chan1 = getc();
 
   
   delay_us(4);
    max519_write_dac(chan0,chan1);
    }
}


As can be seen the code needs to be extremely fast as to not lose any sampled data.

Any help would be appreciated.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Sep 13, 2008 11:34 am     Reply with quote

In your receiver code you are using both interrupt and non-interrupt
code to get the characters. I'm not sure how your code even works.
I suggest that you remove all interrupt code from the receiver code.


Also, in your receiver interrupt routine, you check kbhit(). That's not
necessary. When an #int_rda interrupt occurs, you automatically
have a "kbhit". The interrupt will only occur if there is a kbhit.
You don't need to check it inside the isr.

But get rid of the isr for your initial test. You don't need it.
Also remove the code that enables interrupts.
Gerhard



Joined: 30 Aug 2007
Posts: 144
Location: South Africa

View user's profile Send private message Send e-mail

PostPosted: Sat Sep 13, 2008 12:58 pm     Reply with quote

Thanks. At least I'm learning and that explains the analog that i am reading out.

In my transmitter code i have certain time delays that the pic takes to read the ADC and to transmit the data. However in my receive data i need to only output a value once that has been read by the adc otherwise my output will differ from the input. I basically need to
reconstruct the input signals with the dac as closely as possible. Is there a way to overcome this and always read the data say at 3khz and output the data in exactly the same way?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group