I would like a PWM sinewave Therefore I changed the CAPCOM-program on the HITEX-site into this:
#define _MAIN_ /* External Function Prototypes */ /* We geven de nodige submappen mee */ #include <reg167.h> #include <stdio.h> #include <intmac.h> //volatile int i;//test /*** Main Program Loop ***/ sbit PWM0 = P2^0; // //prototypes // void main(void); void init_controller(void); void CAPCOM0_init_166(void); void timer0_1_init_166(void); void timer0_int(void); int k; // //program // void main(void) { init_controller() ; /* Set up controller and start drive */ /* We springen naar de hoofdmap Control.c, meer bepaald de functie init_controller()*/ IEN = 1 ; /* Global interrupt enable */ while(1) { } } void init_controller(void) { PWM0 = 1 ; /* Make sure PWM0 is off */ CAPCOM0_init_166() ; /* Set up CAPCOM0 in double register compare mode */ timer0_1_init_166() ; /* Set up timer 0 & 1 for timebases */ } void CAPCOM0_init_166(void) { /* Set up P2.0 & P2.4 as output compares */ DP2 = 1 ; // Set P2.0 to output P2 = 0 ; CCM0 |= 0x0005 ; /* Compare mode 1 for PWM generation */ /* CC0 uses timer 0 */ CCM2 |= 0x0004 ; /* Compare mode 0 for double register mode */ /* CC8 uses timer 0 */ CC0 = 0xFF00 ; /* Set initial duty ratio to 100% off */ CC8 = 0xFF00 ; } /*** Overflow Interrupt On Timer 0 To Define PWM Period ***/ void timer0_1_init_166(void) { /* timer 0 = 200us PWM Period */ T0IC = 0 ; T0REL = 0xFE00 ; /* Set reload value for 200us at .4us per count */ T0 = T0REL ; /* Make sure first synth interrupt is after */ /* first controller interrupt */ /* The setting of CCx registers to zero */ /* ensures that no unwanted outputs will occur */ Set_Priority_11(T0IC) ; T0IR = 0 ; /* Clear any spurious interrupts */ T0IE = 1 ; /* Enable timer overflow interrupt */ T0R = 1 ; /* start timer 0 */ } /* End init_timer_0*/ void timer0_int(void) interrupt 0x20 { IEN =0; k = 0x00BB; CC8 = 0xFF00+k; CC0 = 0xFE00; IEN =1; }
void timer0_int(void) interrupt 0x20 { IEN =0; k = 0x00BB; CC8 = 0xFF00+k; CC0 = 0xFE00;
Problem: in the interrupt of the timer, I would like to change automatically the values of CC0 and CC8, by referring to a sine-table. But the µC doesn't seems to calculate another variable? You'll have to provide more detail. Do you mean something like the following doesn't work?
extern unsigned int table_cc0[]; extern unsigned int table_cc8[]; void timer0_int(void) interrupt 0x20 { static int i = 0; CC8 = table_cc8[i]; CC0 = table_cc0[i]; if ( ++i > TABLE_SIZE ) i = 0; }
Instead of a table you might use an alorithm. See: http://www.keil.com/appnotes/docs/apnt_178.asp
After installing the debuggerand using your suggestion, the registers CC0 en CC8 indeed changed values from the table. There must be a problem with the output of the capcom-unit...?