Question

In: Computer Science

Write a code to generate a 5 kHz signal using timer. Vary it's duty cycle. Don't...

Write a code to generate a 5 kHz signal using timer. Vary it's duty cycle. Don't use delay function.

using any language

Solutions

Expert Solution

#include <P32xxxx.h>

#include <math.h> // required to use the sine function

#define NUMPTS 64

int sine[NUMPTS], triangle[NUMPTS];

void initio(int freq) { // freq can be 5-6050 Hz

 TRISD = 0xFF00; // make the bottom 8 bits of PORT D outputs

 SPI2CONbits.ON = 0; // disable SPI to reset any previous state

 SPI2BRG = 9; // 1 MHz SPI clock

 SPI2CONbits.MSTEN = 1; // enable master mode

 SPI2CONbits.CKE = 1; // set clock-to-data timing

 SPI2CONbits.MODE16 = 1; // activate 16-bit mode

 SPI2CONbits.ON = 1; // turn SPI on

 TRISF = 0xFFFE; // make RF0 an output to control load and ce

 PORTFbits.RF0 = 1; // set RF0 = 1

 PR1 = (20e6/NUMPTS)/freq - 1; // set period register for desired wave

 // frequency

 T1CONbits.ON = 1; // turn Timer1 on

}

void initwavetables(void) {

 int i;

 for (i=0; i<NUMPTS; i++) {

 sine[i] = 2047*(sin(2*3.14159*i/NUMPTS) + 1); // 12-bit scale

 if (i<NUMPTS/2) triangle[i] = i*511/NUMPTS; // 8-bit scale

 else triangle[i] = 510-i*511/NUMPTS;

 }

}

void genwaves(void) {

 int i;

 while (1) {

 for (i=0; i<NUMPTS; i++) {

 IFS0bits.T1IF = 0; // clear timer overflow flag

 PORTFbits.RF0 = 1; // disable load while inputs are changing

 SPI2BUF = sine[i]; // send current points to the DACs

 PORTD = triangle[i];

 while (SPI2STATbits.SPIBUSY); // wait until transfer completes

 PORTFbits.RF0 = 0; // load new points into DACs

 while (!IFS0bits.T1IF); // wait until time to send next point

 }

 }

}

int main(void) {

 initio(5000);

 initwavetables();

 genwaves();

}

please upvote comment incase of any doubt


Related Solutions

7. Write a C program to generate a 16kHz PWM signal that has 40% duty cycle...
7. Write a C program to generate a 16kHz PWM signal that has 40% duty cycle using Timer2 of an ATMega8 microcontroller.   8. Consider a push button switch SW0 connected to pin INT0 external interrupts of an ATMega8 AVR. 8 LEDS are connected to port PB0-PB7 on the same AVR. These LEDS are connected such a lit LED displays that the output port is on logic level 1. On start-up, TASK1 is executed and continue indefinitely. If the push-button switch...
Using Matlab, write code to generate Three signal components We will use a sum of sinusoids,...
Using Matlab, write code to generate Three signal components We will use a sum of sinusoids, all of which are sampled at 10 kHz. Signal 1 should contain a sum of sinusoids with frequencies at 25Hz, 40Hz and 75Hz. Signal 2 should contain a sum of sinusoids with frequencies at 600Hz, 730Hz, and 800Hz. Signal 3 should contain a sum of sinusoids with frequencies at 3500Hz, 4000Hz, and 4200Hz. Choose a variety of amplitudes and phase shifts for the sinusoids....
a – DSB. Write the code for an m-file (script) to generate a DSB signal. The...
a – DSB. Write the code for an m-file (script) to generate a DSB signal. The modulating (message) signal is a single tone signal with frequency 1kHz and the carrier frequency is 30kHz. Time Vector: 3001 points over range from 0 to 3ms (3 cycles of the modulating signal). Plot your original message signal both in time and its spectrum. (Note: the Matlab examples 6.1 and 6.2 will help, but use the cosine functions for your signals instead of sine...
DSB: Write the code for an m-file (script) to generate a DSB signal. The modulating (message)...
DSB: Write the code for an m-file (script) to generate a DSB signal. The modulating (message) signal is a single tone signal with frequency 1kHz and the carrier frequency is 30kHz. Time Vector: 3001 points over range from 0 to 3ms (3 cycles of the modulating signal). Plot your original message signal both in time and its spectrum. (Note: use the cosine functions for your signals). In your report you should include the plots for the modulated signal in time...
arduino c code only write a code that counts down a timer on serial monitor and...
arduino c code only write a code that counts down a timer on serial monitor and if A1 is typed into serial monitor prints the timer counting down and writes at 1 second hello and at 5 secs goodbye and repeats every 5 secs A2 is typed as above at 2 seconds writes hello and 3 seconds writes goodbye A3 same as above but at 3 seconds says hello and 2 seconds goodbye This continues until c is pressed to...
5. What are two methods to generate single-side band AM signal. If the modulating signal (message)...
5. What are two methods to generate single-side band AM signal. If the modulating signal (message) has DC omponent, which method you will choose? Why? could you please answer this in a complete brief explanation?
write a program for the msp430fr6989 Using the general purpose timer, specifically the ACLK, create a...
write a program for the msp430fr6989 Using the general purpose timer, specifically the ACLK, create a program which makes LED1 stay on for 4 seconds and off for 2 seconds and repeat indefinitely. Modify the above program to extend the timing to 20 and 10 seconds, respectively.
Write the R code to generate five independent uniform random numbers and use that to generate...
Write the R code to generate five independent uniform random numbers and use that to generate 5 independent Bernoulli random variables with probability of success p = 0.4. please use RStudio. please do not use lab nor rbern for calculations.
Write a verilog code for 5 to 8 multiplier using fourbit adder
Write a verilog code for 5 to 8 multiplier using fourbit adder
Write this code in java and don't forget to comment every step. Write a method which...
Write this code in java and don't forget to comment every step. Write a method which asks a baker how hot their water is, and prints out whether it is OK to make bread with the water. If the water is at or above 110F, your method should print "Too Warm." If the water is below 90.5F, print "Too Cold." If it is in between, print "Just right to bake!" For example, if the user inputs the number 100.5, the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT