Naruto Uzumaki

Generating pwm based sine wave on mc68hc908jl16

Discussion created by Naruto Uzumaki on Jun 16, 2011
Latest reply on Jun 23, 2011 by bigmac

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!

Outcomes