Generating pwm based sine wave on mc68hc908jl16

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Generating pwm based sine wave on mc68hc908jl16

1,421 Views
champion_7891
Contributor I

Hello!

 

I am new to using the HC08 series of microcontrollers. I have tried the code warrior IDE to write a code that should be able to generate a sine wave using pwm, but two things limit my testing. No simulator, and no in- circuit programming circuit. However, I wondered if some of you would be kind enough to read the code and correct it. I have worked before on ATMEGA16 and have been able to generate a sine using the code as follows:

 

#include <avr/io.h>
#include <avr/interrupt.h>
#include <math.h>
#include <stdio.h>


static unsigned char sample_number=0; //initializing sample number, a complete sine wave has 256 samples
static unsigned char sine_table[256]; //sine look up table; 0-360 degrees scales for 1-255
int main(void)
{

//M_pi is the pi value defined in mathlib, sin is also function of the same library
double value;//stores the computed sine value
int temp;//to temporarily convert into int

for (int i=0; i<=255; i++)

 value=128+(127*sin(2.0*M_PI*i/256));
 temp=(int)value;
 sine_table[i]=temp; //updating the sine table
}

TCCR0|=(1<<WGM00)|(1<<WGM01)|(1<<COM01)|(1<<CS00);// initializing timer 0 with fast pwm, clear OC0 on compare match, clk/1
DDRB=0x08;//setting pwm pin as output 
TCNT0=0x00;//counter reg init to 0
TIMSK=TIMSK|0x01;// counter overflow enabled.

sei();
while(1);
}

SIGNAL(SIG_OVERFLOW0) 
   
{
 OCR0=sine_table[sample_number]; //contains the value to which the comparison of the counter value is made
 sample_number++;
 if (sample_number>=255) //=>implies completion of 1 sine wave
 { 
  sample_number = 0;
 }
 
 
}

 

////////

 

The most closest equivalent translation to mc68hc908jl16 that I have been able to come up with is:

 

#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#include <math.h>
#include <stdio.h>
#define M_PI 3.142
static unsigned char sample_number=0;
static unsigned char sine_table[256];

void main(void)
{

  EnableInterrupts; /* enable interrupts */
  /* include your code here */
  double value;
  int temp;
  for (int i=0; i<=255; i++)
  { 
   value=128+(127*sin(2.0*M_PI*i/256));
   temp=(int)value;
   sine_table[i]=temp;
   DDRA=0xFF;
   PTA=sine_table[i];
 }
 
  T1SC=0x70;
  T1SC=0x40;
 
 
 
}


interrupt void TimerTOF (void)
{

  T1MODL=sine_table[sample_number];
 sample_number++;
 if (sample_number>=255) // wait for 2 counts to make state last for twice as long
 { 
  sample_number = 0;
 }
}

 

I tried going over the data sheet, but I was not able to make any sense all the timer registers, specifically the channel status and control register. So, please explain to me the usage of these registers, and please help me correct my code.

 

Thanks all!

Labels (1)
0 Kudos
3 Replies

702 Views
bigmac
Specialist III

Hello,

 

The previous thread https://community.freescale.com/message/69394#69394 may be the sort of thing that you require to do - to generate PWM based sinusoid over the frequency range 67 to 250 Hz.  With the device you are using, the upper frequency limit will be quite modest.  However, the code is presented as assembly code , rather than C.  It is a long thread, and you might need to read most of it.

 

Within the thread, most of the emphasis is on the frequency control aspect, to achieve a frequency resolution of 0.1 Hz.  A sine lookup table is used to generate the waveform, much as you intend.  The ISR processing is time critical, and determines the maximum frequency that may be achieved.

 

A fundamental criticism of your code snippet is that you are generating the sine lookup table as part of your initialisation code (at run time), and this requires the use of floating point operations.  It is unlikely that the MCU you are using will have sufficient resources for this process.  You will need to pre-calulate the sine table data, and program it as a constant array.

 

The TIM module will need to be configured for edge-aligned PWM operation.  The TMOD value controls the PWM period, and the TIM channel register controls the pulse width.  It would seem that this is not properly understood.  Any updates of both the period and the pulse width should occur within the channel ISR, rather than the TIM overflow ISR.

 

Regards,

Mac

 

0 Kudos

702 Views
champion_7891
Contributor I

guys i could really use some help here as the datsheet is ambigous on how to actually generate pwm...

0 Kudos

702 Views
JimDon
Senior Contributor III

Well, I'd love to help you, but that chip is obsolete I am not not familiar with it.

If you start using something in S08 family you will have a simulator and in circuit debugging.

In fact you can get a USBDM for under 20.00...

0 Kudos