Sawtooth wave of 1 khz

cancel
Showing results for 
Search instead for 
Did you mean: 

Sawtooth wave of 1 khz

235 Views
Dom051
Contributor I

Hi, I have this code showing a sawtooth wave less than 1hz and I am trying to make it 1khz


Someone suggest the use of a timer but i dont know how to do it, im new in this language

/* p7_4.c: Use DAC to generate sawtooth waveform
* The DAC is initialized with no buffer and use software trigger,
* so every write to the DAC data registers will change the
analog output.
* The loop count i is incremented by 0x0010 every loop. The
12-bit DAC
* has the range of 0-0x0FFF. Divide 0x1000 by 0x0010 yields
0x0100 or 256.
* The sawtooth has 256 steps and each step takes 1 ms. The
period of the
* waveform is 256 ms and the frequency is about 3.9 Hz.
*/


void DAC0_init(void);
void delayMs(int n);

int main (void ){
	int i;
	DAC0_init(); /*configure DAC0*/
	while(1){
		for(i=0;i<0x1000;i+=0x0010){
			DAC0->DAT[0].DATL=i&0xff;  /*write low bye*/
			DAC0->DAT[0].DATH=(i>>8)&0x0f; /*write high bye*/
			delayMs (1); /*delay 1ms*/

		}
	}
}

void DAC0_init(void){
			SIM->SCGC6 |=0x80000000; /*clock to DAC module*/
			DAC0->C1=0; /*disble the user of buffer*/
			DAC0->C0=0X80|0X20; /*enable DAC and use software trigger*/

}
/*Delay n miliseconds
*The CPU core clock is set to MCGFLILCLH at 41.94 MHZ in SystemInit().
*/
void delayMs (int n){
	int i;
	int j;
	for(i=0; i<n;i++)
		for(j=0;j<7000;j++){}

}

 

 

 

 

0 Kudos
1 Reply

196 Views
CarlosGarabito
NXP Employee
NXP Employee

Hi, this looks like a Homework, and the code is taken from the book "Freescale ARM Cortex-M Embedded Programming, from Muhammad Ali" chapter "Generating a staircase Ramp", page 308, so, to understand this new language for you, you need to read the first 7 chapter from the book.

 

So, in the first comments, it says "frequency is about 3.9 Hz", so, the sawtooth is at 1 Hz? Or at 3.9 Hz? Did you check it on the oscilloscope?

0 Kudos