SRI RAAJA RAAJAN
COLLEGE OF ENGINEERING AND TECHNOLOGY
AMARAVATHIPUDUR, KARAIKUDI-630301
DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING
University
registration no:
Name
Department
Subject
Semester
Year
1
SRI RAAJA RAAJAN
COLLEGE OF ENGINEERING AND TECHNOLOGY
(APPROVED BY AICTE, NEW DELHI &AFFILIATED TO ANNA UNIVERSITY, CHENNAI.)
146/14B1, Amaravathi Village, Amaravathiputhur Post,
Karaikudi -630301, Sivagangai Dt, Tamilnadu
Website: www.sriraajaraajan.in. E - Mail: srrcet2010@gmail.com. Ph: 04565-234230
DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING
Certified that this is the bonafide record of work done by
Semester
Year in the laboratory
During the academic year 2024-2025.
Signature of Lab –in-charge Head of the department
Submitted for the practical examination held on
Internal Examiner External Examiner
EX.NO NAME OF THE EXPERIMENT PAGE NO MARKS SIGN
CONTENTS
EX.NO.1 Real-Time Sine Wave Generation Using Code Composer Studio
DATE :
AIM:
To generate a real-time sine wave using a microcontroller or Digital Signal Processor (DSP) in Code
Composer Studio (CCS). The sine wave will be generated using a look-up table and then output to a
digital-to-analog converter (DAC) or via a Pulse Width Modulation (PWM) signal to simulate the sine
wave output.
Materials Required:
1. Texas Instruments microcontroller or DSP with Code Composer Studio support (e.g., MSP430,
Tiva C, or C2000 series).
2. Code Composer Studio (CCS) IDE installed.
3. A DAC or PWM output configured on the microcontroller.
4. An oscilloscope or logic analyzer (optional for validation of output waveform).
5. Basic knowledge of embedded programming and digital signal processing.
Procedure:
1. Setting Up the Project
1. Create a New Project:
o Open Code Composer Studio (CCS).
o Create a new project by selecting File -> New -> CCS Project.
o Select the appropriate device for your project (e.g., MSP430, Tiva C, C2000).
o Choose Empty Project and select the language as C.
o Click Finish.
2. Initialize the Sine Wave Lookup Table
1. Define the Sine Wave Table: A common method to generate a sine wave in embedded
systems is to use a pre-calculated sine wave lookup table. The table can be scaled and indexed
for efficient real-time sine wave generation.
Here's a simple C code to define a sine wave lookup table:
#include <math.h>
#define TABLE_SIZE 256 // Number of samples in the sine wave table
#define PI 3.14159
// Sine wave lookup table
int sine_table[TABLE_SIZE];
// Function to initialize sine table
void init_sine_wave_table() {
for (int i = 0; i < TABLE_SIZE; i++) {
sine_table[i] = (int)(sin(2 * PI * i / TABLE_SIZE) * 2047); //
Scale sine values to 12-bit range (-2047 to 2047)
}
}
The sine values are scaled to fit a 12-bit range for better resolution when outputting.
3. Set Up the Timer for Real-Time Execution
1. Timer Setup: A timer will be used to generate periodic interrupts at a desired frequency to
output each sample of the sine wave. For instance, if you want to generate a sine wave of 1
kHz, you need to set up a timer interrupt with the appropriate period.
Below is an example of setting up a timer interrupt on an MSP430 (adapt based on your target
microcontroller):
#include <msp430.h>
void timer_setup() {
// Set Timer A to generate an interrupt every 1ms (for a 1 kHz sine
wave)
TA0CCR0 = 1000; // Timer period for 1ms at a 1 MHz clock
TA0CCTL0 = CCIE; // Enable interrupt
TA0CTL = TASSEL_2 + MC_1; // SMCLK, Up mode
}
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A(void) {
static unsigned int index = 0;
// Output the sine wave value from the lookup table
int sine_value = sine_table[index];
// Send sine_value to DAC or PWM (This part is hardware dependent)
output_sine_wave(sine_value);
// Increment the index and loop back if necessary
index = (index + 1) % TABLE_SIZE;
}
In the above example, every 1ms, a new sine wave sample is generated and passed to the
output_sine_wave() function, which is responsible for outputting the value through the DAC
or PWM.
4. Outputting the Sine Wave (DAC or PWM)
1. Using DAC (if available): If your microcontroller or DSP has an onboard DAC, configure it to
output the sine wave signal. For example:
void output_sine_wave(int value) {
// Assume we have a DAC output register (example for a hypothetical
microcontroller)
DAC_OUTPUT_REG = value;
}
2. Using PWM (if no DAC is available): If a DAC is not available, you can use PWM to
simulate the sine wave. A low-pass filter can smooth the PWM signal to approximate a sine
wave.
c
Copy
void output_sine_wave(int value) {
// Assume PWM channel is configured to accept a duty cycle from 0-2047
(for 12-bit resolution)
PWM_SET_DUTY_CYCLE(value);
}
5. Verify the Output (Optional)
1. Connect the Output to an Oscilloscope: Connect the DAC or PWM output to an oscilloscope
to verify the generated sine wave. You should see a continuous sine wave with the desired
frequency (e.g., 1 kHz) and amplitude.
2. Adjust Frequency: If necessary, adjust the timer period or sine table size to modify the
frequency of the sine wave.
PROGRAM :
#include <msp430.h>
#include <math.h>
#define TABLE_SIZE 256
#define PI 3.14159
int sine_table[TABLE_SIZE];
void init_sine_wave_table() {
for (int i = 0; i < TABLE_SIZE; i++) {
sine_table[i] = (int)(sin(2 * PI * i / TABLE_SIZE) * 2047);
}
}
void timer_setup() {
TA0CCR0 = 1000;
TA0CCTL0 = CCIE;
TA0CTL = TASSEL_2 + MC_1;
}
void output_sine_wave(int value) {
PWM_SET_DUTY_CYCLE(value);
}
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A(void) {
static unsigned int index = 0;
int sine_value = sine_table[index];
output_sine_wave(sine_value);
index = (index + 1) % TABLE_SIZE;
}
int main(void) {
WDTCTL = WDTPW + WDTHOLD; // Stop the watchdog timer
init_sine_wave_table();
timer_setup();
__bis_SR_register(GIE); // Enable global interrupt
while (1) {
// Main loop (no need to do anything in the loop)
}
}
Expected Results:
A sine wave with the desired frequency (1 kHz) and amplitude should be generated.
The oscilloscope or logic analyzer should show a continuous sine wave signal at the output.
Conclusion:
In this experiment, you successfully generated a real-time sine wave on a microcontroller using a sine
wave lookup table, a timer interrupt, and either a DAC or PWM output. This technique is efficient for
generating continuous waveforms in embedded systems with limited processing power and memory.