Simultaneous frequency generation and serial reading

07 Nov 2010 . Edited: 08 Nov 2010

Hello Everyone,

I am trying to do a basic project where I read bytes from the serial interface and transmit the bytes using FSK ( Frequency Shift Keying) over the DigitalOut. The basic idea is that for every 1 encountered in the serial data, I send out a square wave of  frequency MARK and for every 0 I send out a sqaure wave for frequency SPACE, ( just like the Bell 202 system).

The problem I am facing is how do I run the 2 tasks( Read the serial data & produce square wave) simultaneously since I am using a wait statement in the second task.

The code to generate the square wave is :

 

void op_square_wave(int frequency, long duration_us)
{

	long i;
	long looptimes;
	long delayAmount_us;
	delayAmount_us = (long)(1000000/frequency/2);
	looptimes = (long)(frequency * duration_us)/1000000; 


	for ( i = 0; i<looptimes;i++)
	{
		AudioOut = 1;
		wait_us(delayAmount_us);
		AudioOut = 0;
		wait_us(delayAmount_us);
	}
}

And the rest of the code  is :

 

 

void read_serial(void)
{

if(pc.readable())
{ 
  output[output_index] = pc.getc();
}


main()
{
  int bit_index = 0;
  pc.attach(&read_serial)
 
  while(1)
{ 
  while(bit_index < 8 )
{
frequency = (output[0]>> bit_index) & 0x01? MARK:SPACE; 

op_square_wave(frequency,BAUD_PERIOD);


}

code to shift output array by 1 is put here>>
}

The problem I face is that while the mbed is doing the wait in the "op_square_wave" I want it to read the serial port.

08 Nov 2010

Use the hardware PWM.