|
|
View previous topic :: View next topic |
Author |
Message |
Gerhard
Joined: 30 Aug 2007 Posts: 144 Location: South Africa
|
Advice regarding code optimisation |
Posted: Sat Sep 13, 2008 8:34 am |
|
|
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
|
|
Posted: Sat Sep 13, 2008 11:34 am |
|
|
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
|
|
Posted: Sat Sep 13, 2008 12:58 pm |
|
|
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? |
|
|
|
|
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
|