Tải bản đầy đủ (.pdf) (27 trang)

Convolution Squarewave Generation Using TMS320C5515

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.04 MB, 27 trang )

VIETNAM NATIONAL UNIVERSITY – HO CHI MINH CITY
HO CHI MINH UNIVERSITY OF TECHNOLOGY
ELECTRICAL & ELECTRONICS FACULTY
---------------o0o---------------

Digital Signal Processing Lab
Report of Lab 3
Convolution & Squarewave Generation
TMS320C5515 eZDSPTM USB Stick

GROUP 5
Student: Tran Nguyen Khang
ID: 1651047
In-class ID: 30
Student: Tran Minh Khoa
ID: 1651056
In-class ID: 31


Page 2

Abstract
This report introduces 2 experiments on the TMS320C5515 eZDSPTM USB Stick Development
Tool for performing a convolution of two vectors and generate a given signal. We calculated y[n]
from given x[n] and h[n] for problem 1. The report also includes a hardwave-based programming
and Matlab-based demonstration.
Problem 1:
Write a program to calculate y(n) = x(n) * h(n) (A filter)
Given x(n) = {-3, 2, 1, 0, 2, 3, -3,2}
h(n) = {-1, -2, 3, 1, 2}
Determine the filter’s order and realize the block diagram of the filter.


Problem 2:
Generate a square wave input signal from an external function generator (f=200Hz) and internal
generator (C5515/35) then calculate/sketch the output.

Introduction
Convolution is a mathematical operation used to express the relation between input and output
of an LTI system. It relates input, output and impulse response of an LTI system as

y(t)=x(t)∗h(t)

Where y (t) = output of LTI
x (t) = input of LTI
h (t) = impulse response of LTI

TRAN MINH KHOA 1651056

TRAN NGUYEN KHANG 1651047


Page 3
We use Matlab and CCS to compute the output y[n]. With the result sequence y[n], we will
display it on a LCD 96×16 monochrome OLED display screen and check this result with the
performance on MATLAB.
The sequence y[n] is equal to the convolution of sequences x[n] and h[n]:

x[n] is input signal and h[n] is impulse response, in this project we have:
x[n] = [-3, 2, 1, 0, 2, 3, -3, 2]
h[n] = [-1, -2, 3, 1, 2]

Figure. Block Diagram


Matlab can be used as an External Function Generator and TMS320C5515 can be used as
Internal Generator.

TRAN MINH KHOA 1651056

TRAN NGUYEN KHANG 1651047


Page 4

Q1: Matlab code
%library;
x = [-3 2 1 0 2 3 -3 2];
nx = [0 1 2 3 4 5 6 7];
h = [-1 -2 3 1 2];
nh = [0 1 2 3 4];
%y = conv(x,h) %Matlab provides a convolution function already
[x11,n11] = sigshift(x,nx,1); %x(n-1)
[x12,n12] = sigshift(x,nx,2); %x(n-2)
[x13,n13] = sigshift(x,nx,3); %x(n-3)
[x14,n14] = sigshift(x,nx,4); %x(n-4)
%[x12,n12] = sigshift(x,n,-2);
[y1,n1] = sigadd(-2*x11,n11,3*x12,n12);
%-2x(n-1) + 3x(n-2)
[y2,n2] = sigadd(x13,n13,2*x14,n14);
%x(n-3) + 2x(n-4)
[y2,n2] = sigadd(y2,n2,-x,nx);
%-x(n) + x(n-3) + 2x(n-4)
[y1,n1] = sigadd(y2,n2,y1,n1)

%-x(n) -2x(n-1) + 3x(n-2) + x(n-3) + 2x(n-4)
Firter_order = length(h)
stem(n1,y1)
%convolution;

function [x,n] = imp(n0,n1,n2)
n = [n1:n2];
x = [(n - n0) == 0];
end
function [x,n] = stepseq(n0,n1,n2)
n=[n1:n2];
x = [(n-n0) >= 0];
end
function [y,n] = sigfold(x,n)
y=fliplr(x); n = -fliplr(n);
end
function [y,n] = sigmult(x1,n1,x2,n2)
n=min(min(n1),min(n2)):max(max(n1),max(n2));
y1 = zeros(1,length(n)); y2 = y1;
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1;
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2;
y=y1.*y2;
end
function [y,n] = sigadd(x1,n1,x2,n2)
n=min(min(n1),min(n2)):max(max(n1),max(n2));
y1 = zeros(1,length(n)); y2 = y1;
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1;
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2;
y=y1+y2;
end

function [y,n] = sigshift(x,m,k)
n=m+k; y = x;
end
function [y,ny] = conv_m(x,nx,h,nh)
nyb = nx(1)+nh(1);
nye = nx(length(x)) + nh(length(h));
ny = [nyb:nye];
y = conv(x,h);
end
TRAN MINH KHOA 1651056

TRAN NGUYEN KHANG 1651047


Page 5

Figure. Results on MATLAB

SOURCE CODES FOR Q1
MAIN.C
#include "stdio.h"
#include "usbstk5515.h"
int main( )
{
int x[50],h[50],y[50];
int i,j,m,n,length_out, length_line, length_of_y;
char output[50] ={0};
USBSTK5515_I2C_init( );
// Input lengths of x[n] and h[n]
printf("\n Length of x(n): ");

scanf("%d", &m);

TRAN MINH KHOA 1651056

TRAN NGUYEN KHANG 1651047


Page 6
// Input elements of x[n] and h[n]
printf ("\n Input elements of x(n):\n");
for (i=0;iscanf("%d",&x[i]);
printf("\n Length of h(n): ");
scanf("%d", &n);

printf ("\n Input elements of h(n):\n");
for (i=0;iscanf("%d",&h[i]);

for(i=m;i<=m+n-1;i++) x[i]=0;// length of h[n]: m + n - 1
for(i=n;i<=m+n-1;i++) h[i]=0;
// Convolution
for(i=0;i{
y[i]=0;
for(j=0;j<=i;j++)
{
y[i]=y[i]+(x[j]*h[i-j]); }
}
//Show result

printf("x(n) (*) h(n) = y[n]");
length_out=0;
length_line=0;
printf(" = \n");
for (i=0;iif (length_out >=14 && length_out <=17) length_line = length_out;
if (y[i]<10 && y[i] >=0){
output[length_out]= y[i]+48;
output[length_out+1]= 44;
length_out= length_out+2;}
if (y[i]>9) {
output[length_out]= y[i]/10+48;
//length_out++;
output[length_out+1]= y[i] - (y[i]/10)*10 +48;
output[length_out+2]= 44;
length_out=length_out+3;
}

if (y[i]<0) {
output[length_out]= 45;
if (y[i]<-9) {
output[length_out+1]= -((y[i])/10)+48;
//length_out++;
output[length_out+2]= ((-y[i])%10)+48;
output[length_out+3]= 44;
length_out=length_out+4;

TRAN MINH KHOA 1651056

TRAN NGUYEN KHANG 1651047



Page 7

}
else {
output[length_out+1]= -y[i] + (y[i]/10)*10+48;
output[length_out+2]= 44;
length_out=length_out+3;
}
}
printf(" %d",y[i]);
}
test(output,length_out, length_line);
return 0;
}
}

OLED_TEST.C
#include"usbstk5515.h"
#include"usbstk5515_i2c.h"
#include"usbstk5515_gpio.h"
#include"lcd.h"
#define OSD9616_I2C_ADDR 0x3C // OSD9616 I2C address
/* ------------------------------------------------------------------------ *
*
*
* Int16 OSD9616_send( Uint16 comdat, Uint16 data )
*
*

*
*
Sends 2 bytes of data to the OSD9616
*
*
*
* ------------------------------------------------------------------------ */
Int16 OSD9616_send( Uint16 comdat, Uint16 data )
{
Uint8 cmd[2];
cmd[0] = comdat & 0x00FF; // Specifies whether data is Command or Data
cmd[1] = data;
// Command / Data
return USBSTK5515_I2C_write( OSD9616_I2C_ADDR, cmd, 2 );
}
Int16 OSD9616_multiSend( Uint8* data, Uint16 len )
{

TRAN MINH KHOA 1651056

TRAN NGUYEN KHANG 1651047


Page 8
Uint16 x;
Uint8 cmd[10];
for(x=0;x// Command / Data
{
cmd[x] = data[x];

}
return USBSTK5515_I2C_write( OSD9616_I2C_ADDR, cmd, len );
}
/* ------------------------------------------------------------------------ *
*
*
* Int16 printLetter(Uint16 l1,Uint16 l2,Uint16 l3,Uint16 l4)
*
*
*
Send 4 bytes representing a Character
*
*
*
* ------------------------------------------------------------------------ */
void delay(long time)
{
while(time--);
}

*

Int16 printLetter(Uint16 l1,Uint16 l2,Uint16 l3,Uint16 l4, Uint16 l5)
{
OSD9616_send(0x40,l5);
OSD9616_send(0x40,l4);
OSD9616_send(0x40,l3);
OSD9616_send(0x40,l2);
OSD9616_send(0x40,l1);
// OSD9616_send(0x40,0x00);

return 0;
}
static Uint8 font[] ={
0x00, 0x00, 0x00, 0x00, 0x00, // 32d 0x20h SPACE
0x00, 0x00, 0x5f, 0x00, 0x00, // 33d 0x21h !
0x00, 0x07, 0x00, 0x07, 0x00, // 34d 0x22h "
0x14, 0x7f, 0x14, 0x7f, 0x14, // 35d 0x23 #
0x24, 0x2a, 0x7f, 0x2a, 0x12, // 36d 0x24 $
0x23, 0x13, 0x08, 0x64, 0x62, // 37d 0x25 %
0x36, 0x49, 0x55, 0x22, 0x50, // 38d 0x26 &
0x00, 0x05, 0x03, 0x00, 0x00, // 39d 0x27 '
0x00, 0x1c, 0x22, 0x41, 0x00, // 40d 0x28 (
0x00, 0x41, 0x22, 0x1c, 0x00, // 41d 0x29 )
0x14, 0x08, 0x3e, 0x08, 0x14, // 42d 0x2a *
0x08, 0x08, 0x3e, 0x08, 0x08, // 43d 0x2b +
0x00, 0x50, 0x30, 0x00, 0x00, // 44d 0x2c ,
0x08, 0x08, 0x08, 0x08, 0x08, // 45d 0x2d 0x00, 0x60, 0x60, 0x00, 0x00, // 46d 0x2e .
0x20, 0x10, 0x08, 0x04, 0x02, // 47d 0x2f /
0x3e, 0x51, 0x49, 0x45, 0x3e, // 48d 0x30 0
0x00, 0x42, 0x7f, 0x40, 0x00, // 49d 0x31 1
0x42, 0x61, 0x51, 0x49, 0x46, // 50d 0x32 2
0x21, 0x41, 0x45, 0x4b, 0x31, // 51d 0x33 3
0x18, 0x14, 0x12, 0x7f, 0x10, // 52d 0x34 4

TRAN MINH KHOA 1651056

TRAN NGUYEN KHANG 1651047


Page 9

0x27, 0x45, 0x45, 0x45, 0x39, // 53d 0x35 5
0x3c, 0x4a, 0x49, 0x49, 0x30, // 54d 0x36 6
0x01, 0x71, 0x09, 0x05, 0x03, // 55d 0x37 7
0x36, 0x49, 0x49, 0x49, 0x36, // 56d 0x38 8
0x06, 0x49, 0x49, 0x29, 0x1e, // 57d 0x39 9
0x00, 0x36, 0x36, 0x00, 0x00, // 58d 0x3a :
0x00, 0x56, 0x36, 0x00, 0x00, // 59d 0x3b ;
0x08, 0x14, 0x22, 0x41, 0x00, // 60d 0x3c <
0x14, 0x14, 0x14, 0x14, 0x14, // 61d 0x3d =
0x00, 0x41, 0x22, 0x14, 0x08, // 62d 0x3e >
0x02, 0x01, 0x51, 0x09, 0x06, // 63d 0x3f ?
0x32, 0x49, 0x79, 0x41, 0x3e, // 64d 0x40 @
0x7e, 0x11, 0x11, 0x11, 0x7e, // 65d 0x41 A
0x7f, 0x49, 0x49, 0x49, 0x36, // 66d 0x42 B
0x3e, 0x41, 0x41, 0x41, 0x22, // 67d 0x43 C
0x7f, 0x41, 0x41, 0x22, 0x1c, // 68d 0x44 D
0x7f, 0x49, 0x49, 0x49, 0x41, // 69d 0x45 E
0x7f, 0x09, 0x09, 0x09, 0x01, // 70d 0x46 F
0x3e, 0x41, 0x49, 0x49, 0x7a, // 71d 0x47 G
0x7f, 0x08, 0x08, 0x08, 0x7f, // 72d 0x48 H
0x00, 0x41, 0x7f, 0x41, 0x00, // 73d 0x49 I
0x20, 0x40, 0x41, 0x3f, 0x01, // 74d 0x4a J
0x7f, 0x08, 0x14, 0x22, 0x41, // 75d 0x4b K
0x7f, 0x40, 0x40, 0x40, 0x40, // 76d 0x4c L
0x7f, 0x02, 0x0c, 0x02, 0x7f, // 77d 0x4d M
0x7f, 0x04, 0x08, 0x10, 0x7f, // 78d 0x4e N
0x3e, 0x41, 0x41, 0x41, 0x3e, // 79d 0x4f O
0x7f, 0x09, 0x09, 0x09, 0x06, // 80d 0x50 P
0x3e, 0x41, 0x51, 0x21, 0x5e, // 81d 0x51 Q
0x7f, 0x09, 0x19, 0x29, 0x46, // 82d 0x52 R

0x46, 0x49, 0x49, 0x49, 0x31, // 83d 0x53 S
0x01, 0x01, 0x7f, 0x01, 0x01, // 84d 0x54 T
0x3f, 0x40, 0x40, 0x40, 0x3f, // 85d 0x55 U
0x1f, 0x20, 0x40, 0x20, 0x1f, // 86d 0x56 V
0x3f, 0x40, 0x38, 0x40, 0x3f, // 87d 0x57 W
0x63, 0x14, 0x08, 0x14, 0x63, // 88d 0x58 X
0x07, 0x08, 0x70, 0x08, 0x07, // 89d 0x59 Y
0x61, 0x51, 0x49, 0x45, 0x43, // 90d 0x5a Z
0x00, 0x7f, 0x41, 0x41, 0x00, // 91d 0x5b [
0x02, 0x04, 0x08, 0x10, 0x20, // 92d 0x5c //
0x00, 0x41, 0x41, 0x7f, 0x00, // 93d 0x5d ]
0x04, 0x02, 0x01, 0x02, 0x04, // 94d 0x5e ^
0x40, 0x40, 0x40, 0x40, 0x40, // 95d 0x5f _
0x00, 0x01, 0x02, 0x04, 0x00, // 96d 0x60 `
0x20, 0x54, 0x54, 0x54, 0x78, // 97d 0x61 a
0x7f, 0x48, 0x44, 0x44, 0x38, // 98d 0x62 b
0x38, 0x44, 0x44, 0x44, 0x20, // 99d 0x63 c
0x38, 0x44, 0x44, 0x48, 0x7f, // 100d 0x64 d
0x38, 0x54, 0x54, 0x54, 0x18, // 101d 0x65 e
0x08, 0x7e, 0x09, 0x01, 0x02, // 102d 0x66 f
0x0c, 0x52, 0x52, 0x52, 0x3e, // 103d 0x67 g
0x7f, 0x08, 0x04, 0x04, 0x78, // 104d 0x68 h
0x00, 0x44, 0x7d, 0x40, 0x00, // 105d 0x69 i

TRAN MINH KHOA 1651056

TRAN NGUYEN KHANG 1651047


Page 10

0x20, 0x40, 0x44, 0x3d, 0x00, // 106d 0x6a j
0x7f, 0x10, 0x28, 0x44, 0x00, // 107d 0x6b k
0x00, 0x41, 0x7f, 0x40, 0x00, // 108d 0x6c l
0x7c, 0x04, 0x18, 0x04, 0x78, // 109d 0x6d m
0x7c, 0x08, 0x04, 0x04, 0x78, // 110d 0x6e n
0x38, 0x44, 0x44, 0x44, 0x38, // 111d 0x6f o
0x7c, 0x14, 0x14, 0x14, 0x08, // 112d 0x70 p
0x08, 0x14, 0x14, 0x18, 0x7c, // 113d 0x71 q
0x7c, 0x08, 0x04, 0x04, 0x08, // 114d 0x72 r
0x48, 0x54, 0x54, 0x54, 0x20, // 115d 0x73 s
0x04, 0x3f, 0x44, 0x40, 0x20, // 116d 0x74 t
0x3c, 0x40, 0x40, 0x20, 0x7c, // 117d 0x75 u
0x1c, 0x20, 0x40, 0x20, 0x1c, // 118d 0x76 v
0x3c, 0x40, 0x30, 0x40, 0x3c, // 129d 0x77 w
0x44, 0x28, 0x10, 0x28, 0x44, // 120d 0x78 x
0x0c, 0x50, 0x50, 0x50, 0x3c, // 121d 0x79 y
0x44, 0x64, 0x54, 0x4c, 0x44, // 122d 0x7a z
0x00, 0x08, 0x36, 0x41, 0x00, // 123d 0x7b {
0x00, 0x00, 0x7f, 0x00, 0x00, // 124d 0x7c |
0x00, 0x41, 0x36, 0x08, 0x00, // 125d 0x7d }
0x10, 0x08, 0x08, 0x10, 0x08, // 126d 0x7e ~
0x00, 0x7E, 0x42, 0x42, 0x7E, // 127d 0x7f °
};

void display_char(char character){
Uint8 i;
Uint8 index;
index=((int)character - 32)*5;
if ((int)character >=32 && (int)character <=127){
for (i=0;i< 5; i++){

OSD9616_send(0x40,font[index+i]);
}
OSD9616_send(0x40,0x00); // space between characters
}
}

void text(char *words){
Uint8 i;
Uint16 lenght;
lenght = strlen(words);
for (i=0; i<= lenght ; i++){
display_char(words[i]);
}
}
void test(char* output,int out,int line)
/* Fill page 0 */
{ Int16 i;
Uint8 cmd[10]; // For multibyte commands
/* Initialize I2C */
// USBSTK5515_I2C_init( );

TRAN MINH KHOA 1651056

TRAN NGUYEN KHANG 1651047


Page 11

/* Initialize LCD power */
USBSTK5515_GPIO_setDirection( 12, 1 ); // Output

USBSTK5515_GPIO_setOutput( 12, 1 ); // Enable 13V
/* Initialize OSD9616 display */
OSD9616_send(0x00,0x00); // Set low column address
OSD9616_send(0x00,0x10); // Set high column address
OSD9616_send(0x00,0x40); // Set start line address
cmd[0] = 0x00 & 0x00FF; // Set contrast control register
cmd[1] = 0x81;
cmd[2] = 0x7f;
USBSTK5515_I2C_write( OSD9616_I2C_ADDR, cmd, 3 );
OSD9616_send(0x00,0xa0); // Set segment re-map 95 to 0
OSD9616_send(0x00,0xa6); // Set normal display
cmd[0] = 0x00 & 0x00FF; // Set multiplex ratio(1 to 16)
cmd[1] = 0xa8;
cmd[2] = 0x0f;
USBSTK5515_I2C_write( OSD9616_I2C_ADDR, cmd, 3 );
OSD9616_send(0x00,0xd3); // Set display offset
OSD9616_send(0x00,0x00); // Not offset
OSD9616_send(0x00,0xd5); // Set display clock divide ratio/oscillator frequency
OSD9616_send(0x00,0xf0); // Set divide ratio
cmd[0] = 0x00 & 0x00FF; // Set pre-charge period
cmd[1] = 0xd9;
cmd[2] = 0x22;
USBSTK5515_I2C_write( OSD9616_I2C_ADDR, cmd, 3 );
cmd[0] = 0x00 & 0x00FF; // Set com pins hardware configuration
cmd[1] = 0xda;
cmd[2] = 0x02;
USBSTK5515_I2C_write( OSD9616_I2C_ADDR, cmd, 3 );
OSD9616_send(0x00,0xdb); // Set vcomh
OSD9616_send(0x00,0x49); // 0.83*vref
cmd[0] = 0x00 & 0x00FF; //--set DC-DC enable

cmd[1] = 0x8d;
cmd[2] = 0x14;
USBSTK5515_I2C_write( OSD9616_I2C_ADDR, cmd, 3 );
OSD9616_send(0x00,0xaf); // Turn on oled panel
OSD9616_send(0x00,0x00); // Set low column address
OSD9616_send(0x00,0x10); // Set high column address
OSD9616_send(0x00,0xb0+0); // Set page for page 0 to page 5
/* Write to page 0 */
OSD9616_send(0x00,0x00); // Set low column address
OSD9616_send(0x00,0x10); // Set high column address
OSD9616_send(0x00,0xb0+0); // Set page for page 0 to page 5
TRAN MINH KHOA 1651056

TRAN NGUYEN KHANG 1651047


Page 12
for(i=0;i<160;i++)
{
OSD9616_send(0x40,0x00); // Spaces
}

for(i=0;i{
display_char(output[i]);
OSD9616_send(0x40,0x00);
}
/* Fill page 1*/
OSD9616_send(0x00,0x00); // Set low column address
OSD9616_send(0x00,0x10); // Set high column address

OSD9616_send(0x00,0xb0+1);//et page for page 0 to page 5
/* Write to page 1*/
OSD9616_send(0x00,0x00); // Set low column address
OSD9616_send(0x00,0x10); // Set high column address
OSD9616_send(0x00,0xb0+1); // Set page for page 0 to page 5
for(i=0;i<160;i++)
{
OSD9616_send(0x40,0x00);
}
for(i=line;i{
display_char(output[i]);
OSD9616_send(0x40,0x00);
}
cmd[0] = 0x00;
cmd[1] = 0x29; // Vertical and Right Horizontal Scroll
cmd[2] = 0x00; // Dummy byte
cmd[3] = 0x00; // Define start page address
cmd[4] = 0x02; // Set time interval between each scroll step
cmd[5] = 0x01; // Define end page address
cmd[6] = 0x00; // Vertical scrolling offset
OSD9616_multiSend( cmd, 7 );
OSD9616_send(0x00,0x2f);
}

TRAN MINH KHOA 1651056

TRAN NGUYEN KHANG 1651047



Page 13

Figure. Result displayed on the TMS320C5515 screen

Figure. Result displayed on the Console

TRAN MINH KHOA 1651056

TRAN NGUYEN KHANG 1651047


Page 14

Q2:
Matlab Simulation of signal generator: We want to use Matlab to check to corresponding
output of a square wave generator. At the end we created a “.wav” file then ran it and looked
back in the Soundcard Scope to see the result.
To create a square wave signal that can be used in convolution, FFT, DFT, modulation, we chose
to generate it by taking Fourier transformation of a square wave:

Which gave us the function:

%t=0:(1/f)/1000:1;
syms n
x = 3
Fin = 4000*i;
Fc = 2000;
y = zeros(1000);
y_hilbert = zeros(1000);
y_lsb = zeros(1000);

y_usb = zeros(1000);
F =10000
f=1000;
x=0:(1/f)/1000:4/f- 1/(f*1000);
sq =
(sin(2*pi*f*x)+sin(3*2*pi*f*x)/3+sin(5*2*pi*f*x)/5+sin(7*2*pi*f*x)/7+sin(9*2*p
i*f*x)/9+sin(11*2*pi*f*x)/11+sin(13*2*pi*f*x)/13+sin(15*2*pi*f*x)/15+sin(17*2*
pi*f*x)/17+sin(19*2*pi*f*x)/19+sin(21*2*pi*f*x)/21+sin(23*2*pi*f*x)/23+sin(25*
2*pi*f*x)/25)*1.33;
plot(x,sq)
axis([0 0.004 -2 2])
length(x)
length(sq)

TRAN MINH KHOA 1651056

TRAN NGUYEN KHANG 1651047


Page 15
X=fft(sq,1000);
X=fftshift(X);
length(X)
ff=-500:1:499
figure()
stem(ff,abs(X)/1000) ;xlabel('Frequency axis');ylabel('Amplitude');
axis([-60 60 -1 1])
y=sq.*cos(2*pi*x*Fc/F);
y = double(y);
y_lsb = double(y_lsb);


Input_Data_dsb=y;
minC = min(Input_Data_dsb);
maxC = max(Input_Data_dsb);
ScaledData = 2*(Input_Data_dsb-minC)./(maxC-minC) - 1;
audiowrite('C:\Users\User\Desktop\TF Instagram
Post\123.wav',ScaledData,10000);

‘.wav’ output showed on Soundcard Scope:

TRAN MINH KHOA 1651056

TRAN NGUYEN KHANG 1651047


Page 16
FFT of the output on Soundcard Scope:

After seeing noise in the Soundcard Scope, although it was exactly square wave, we looked on
the Matlab to check for the expected output

TRAN MINH KHOA 1651056

TRAN NGUYEN KHANG 1651047


Page 17
CCS source code: In this lab we will create a Square wave signal 𝑥(𝑡), which has the
frequency is 200Hz. in Matlab and send this signal to C5515. The C5515 will collect signal 𝑥(𝑡)
in time domain. After collecting the signal, the C5515 will send back exactly the signal in same

domain to the computer. Sound Scope Oscilloscope program modulate this Square wave signal.
In the same time, the MATLAB also plot both signal 𝑥(𝑡) and 𝑥′(𝑡) to compare the result.
We implemented the aic3204 sample source code and modify the aic3204_tone_headphone
source code and main.c as follow:
#include "stdio.h"
#include "usbstk5515.h"
extern Int16 AIC3204_rset( Uint16 regnum, Uint16 regval);
#define XmitL 0x10
#define XmitR 0x20
/* -----------------------------------------------------------------------*
* AIC3204 Tone
*
Output a 1 kHz tone through the HEADPHONE jack
*
* -----------------------------------------------------------------------Int16 aic3204_tone_headphone( )
{
//additional
extern float Y[96];
Int16 sinetable[96]={0} ;
Int16 j, i = 0;
Int16 sample;
FILE * fp;
long int k=0;
for(k=0; k<96;k++)
{
sinetable[k] = 1000*Y[k];
}
fp=fopen("E:/dsp/Y12.txt","wt");
for (k=0; k<96;k++ )
{

fprintf(fp,"%d \n",sinetable[k]);
}
fclose(fp);
//end addition

*
*
*
*
*
*/

/* Pre-generated sine wave data, 16-bit signed samples
Int16 j, i = 0;
Int16 sample;*/
/* Configure AIC3204 */
AIC3204_rset( 0, 0 );
// Select page 0
AIC3204_rset( 1, 1 );
// Reset codec
AIC3204_rset( 0, 1 );
// Select page 1
AIC3204_rset( 1, 8 );
// Disable crude AVDD generation from DVDD
AIC3204_rset( 2, 1 );
// Enable Analog Blocks, use LDO power
AIC3204_rset( 0, 0 );
/* PLL and Clocks config and Power Up */
AIC3204_rset( 27, 0x0d );
// BCLK and WCLK are set as o/p; AIC3204(Master)

AIC3204_rset( 28, 0x00 ); // Data ofset = 0
AIC3204_rset( 4, 3 );

TRAN MINH KHOA 1651056

TRAN NGUYEN KHANG 1651047


// PLL setting: PLLCLK <- MCLK, CODEC_CLKIN <-PLL CLK
Page 18
AIC3204_rset( 6, 7 );
// PLL setting: J=7
AIC3204_rset( 7, 0x06 );
// PLL setting: HI_BYTE(D=1680)
AIC3204_rset( 8, 0x90 );
// PLL setting: LO_BYTE(D=1680)
AIC3204_rset( 30, 0x88 );
// For 32 bit clocks per frame in Master mode ONLY
// BCLK=DAC_CLK/N =(12288000/8) = 1.536MHz = 32*fs
AIC3204_rset( 5, 0x91 );
// PLL setting: Power up PLL, P=1 and R=1
AIC3204_rset( 13, 0 );
// Hi_Byte(DOSR) for DOSR = 128 decimal or 0x0080 DAC oversamppling
AIC3204_rset( 14, 0x80 );
// Lo_Byte(DOSR) for DOSR = 128 decimal or 0x0080
AIC3204_rset( 20, 0x80 );
// AOSR for AOSR = 128 decimal or 0x0080 for decimation filters 1 to 6
AIC3204_rset( 11, 0x82 ); // Power up NDAC and set NDAC value to 2
AIC3204_rset( 12, 0x87 ); // Power up MDAC and set MDAC value to 7
AIC3204_rset( 18, 0x87 ); // Power up NADC and set NADC value to 7

AIC3204_rset( 19, 0x82 ); // Power up MADC and set MADC value to 2
/* DAC ROUTING and Power Up */
AIC3204_rset( 0, 1 );
// Select page 1
AIC3204_rset( 0x0c, 8 );
// LDAC AFIR routed to HPL
AIC3204_rset( 0x0d, 8 );
// RDAC AFIR routed to HPR
AIC3204_rset( 0, 0 );
// Select page 0
AIC3204_rset( 64, 2 );
// Left vol=right vol
AIC3204_rset( 65, 0);
// Left DAC gain to 0dB VOL; Right tracks Left
AIC3204_rset( 63, 0xd4 );
// Power up left,right data paths and set channel
AIC3204_rset( 0, 1 );
// Select page 1
AIC3204_rset( 0x10, 0x00 );// Unmute HPL , 0dB gain
AIC3204_rset( 0x11, 0x00 );// Unmute HPR , 0dB gain
AIC3204_rset( 9, 0x30 );
// Power up HPL,HPR
AIC3204_rset( 0, 0 );
// Select page 0
USBSTK5515_wait( 100 );
// wait
/* ADC ROUTING and Power Up */
AIC3204_rset( 0, 1 );
// Select page 1
AIC3204_rset( 0x34, 0x30 );// STEREO 1 Jack

// IN2_L to LADC_P through 40 kohm
AIC3204_rset( 0x37, 0x30 );// IN2_R to RADC_P through 40 kohmm
AIC3204_rset( 0x36, 3 );
// CM_1 (common mode) to LADC_M through 40 kohm
AIC3204_rset( 0x39, 0xc0 );
// CM_1 (common mode) to RADC_M through 40 kohm
AIC3204_rset( 0x3b, 0 );
// MIC_PGA_L unmute
AIC3204_rset( 0x3c, 0 );
// MIC_PGA_R unmute
AIC3204_rset( 0, 0 );
// Select page 0
AIC3204_rset( 0x51, 0xc0 );// Powerup Left and Right ADC
AIC3204_rset( 0x52, 0 );
// Unmute Left and Right ADC
AIC3204_rset( 0, 0 );
USBSTK5515_wait( 200 );
/* I2S settings */
I2S0_SRGR = 0x0;
I2S0_CR = 0x8010;
I2S0_ICMR = 0x3f;

// Wait

// 16-bit word, slave, enable I2C
// Enable interrupts

/* Play Tone */
for ( i = 0 ; i < 5 ; i++ )
{

for ( j = 0 ; j < 10000 ; j++ )
{
for ( sample = 0 ; sample < 48 ; sample++ )
{
TRAN MINH KHOA 1651056

TRAN NGUYEN KHANG 1651047


while((XmitR & I2S0_IR) == 0);
// Wait for transmit interrupt to be pending
I2S0_W0_MSW_W = (sinetable[sample]) ;
// 16 bit left channel transmit audio data
I2S0_W1_MSW_W = (sinetable[sample]) ;
// 16 bit right channel transmit audio data
}
}
}
/* Disble I2S */
I2S0_CR = 0x00;

Page 19

return 0;
}

And in main.c, it would be:
#include "stdio.h"
#include "usbstk5515.h"
#include <stdio.h>

#include <stdlib.h>
#include <math.h>
#include <stdint.h>
#define PI 3.14159265
/* -----------------------------------------------------------------------*
* Testing Function
*
* -----------------------------------------------------------------------int en = 0;
float Xc[4800]={0},Xm[4800]={0},Y[4800]={0};
int fs =4800;
double t=0;
long int i1=0,ta=0;
FILE *fp;

*
*
*
*
*/

void TEST_execute( Int16 ( *funchandle )( ), char *testname, Int16 testid )
{
Int16 status;
/* Display test ID */
printf( "%02d Testing %s...\n", testid, testname );
/* Call test function */
status = funchandle( );
/* Check for test fail */
if ( status != 0 )
{

/* Print error message */
printf( "
FAIL... error code %d... quitting\n", status );
/* Software Breakpoint to Code Composer */
SW_BREAKPOINT;
}
else
{

TRAN MINH KHOA 1651056

TRAN NGUYEN KHANG 1651047


/* Print error message */
printf( "
PASS\n" );
}
}

Page 20

extern Int16 aic3204_test( );
/* ------------------------------------------------------------------------ *
*
*
* main( )
*
*
*

* ------------------------------------------------------------------------ */
void main( void )
{
/* Initialize BSL */
USBSTK5515_init( );
///////////////////////////////////////////////////////////////////////////////////////
//////////////
while(en==0)
{
fp=fopen("E:/dsp/enable.txt","rt");
fscanf(fp,"%d",&en);
fclose(fp);
}
for (i1=0; i1<4800;i1++ )
{
Xc[i1]=0.5*sin(2000*PI*t);
t= t + (1/(double)fs);
}
fp=fopen("E:/dsp/Xm.txt","rt");
for(ta=1;ta<4801;ta++)
fscanf(fp,"%f",&Xm[ta-1]);
fclose(fp);
printf("\nLoading\n");
fp=fopen("E:/dsp/Y.txt","wt");
for (i1=0; i1<4800;i1++ )
{
Y[i1]=Xm[i1];
fprintf(fp,"%f \n",Y[i1]);
}
fclose(fp);

printf("\nProcessing\n");
fp=fopen("E:/dsp/Y1.txt","wt");
for (i1=0; i1<4800;i1++ )
{
fprintf(fp,"%f \n",1000*Y[i1]);
}
fclose(fp);
en = 0;
fp=fopen("E:/dsp/enable.txt","wt");
fprintf(fp,"%d \n",en);
fclose(fp);
printf("\nResuft./.\n");
///////////////////////////////////////////////////////////////////////////////////////
///////////////
TRAN MINH KHOA 1651056

TRAN NGUYEN KHANG 1651047


Page 21
printf("EXBUSSEL = %02x\n", SYS_EXBUSSEL);
TEST_execute( aic3204_test, "AIC3204", 1 );
printf( "\n***ALL Tests Passed***\n" );
SW_BREAKPOINT;
}

Therefore, the flow chart of TMS320C5515 would be:

The corresponding Matlab code is:


TRAN MINH KHOA 1651056

TRAN NGUYEN KHANG 1651047


Page 22
The corresponding Matlab code is:
clc;
clear;
close all;
fs=4800;
f=200;
x=0:1/fs:1-(1/fs);
length(t);
fileID = fopen( 'E:\dsp\enable.txt', 'w' );
formatSpec = '%d \n';
en = 0;
fprintf(fileID,formatSpec,en);
fclose(fileID);
%X = square(2*pi*f*t);
X =
(sin(2*pi*f*x)+sin(3*2*pi*f*x)/3+sin(5*2*pi*f*x)/5+sin(7*2*pi*f*x)/7+sin(9*2*pi*f*x)/
9+sin(11*2*pi*f*x)/11+sin(13*2*pi*f*x)/13+sin(15*2*pi*f*x)/15+sin(17*2*pi*f*x)/17+sin
(19*2*pi*f*x)/19+sin(21*2*pi*f*x)/21+sin(23*2*pi*f*x)/23+sin(25*2*pi*f*x)/25)*1.33;
Y = X;
fileID = fopen( 'E:\dsp\Xm.txt', 'w' );
formatSpec = '%f \n';
fprintf(fileID,formatSpec,X);
fclose(fileID);


plot(x,Y);
axis([0,0.02,-1.1,1.1]);
title('MATLAB modulation');
xlabel('Time');
ylabel('Amplitude');
fileID = fopen( 'E:\dsp\enable.txt', 'w' );
formatSpec = '%d \n';
en = 1;
fprintf(fileID,formatSpec,en);
fclose(fileID);
while en == 1
fileID = fopen('E:\dsp\enable.txt','r');
formatSpec = '%d';
en = fscanf(fileID,formatSpec);
fclose(fileID);
end
fileID = fopen('E:\dsp\Y.txt','r');
formatSpec = '%f';
Yr = fscanf(fileID,formatSpec);
fclose(fileID);
Yr=Yr';

figure,plot(x,Yr);
axis([0,0.02,-1.1,1.1]);
TRAN MINH KHOA 1651056

TRAN NGUYEN KHANG 1651047


title('TMS320C5515 modulation');

xlabel('Time');
ylabel('Amplitude');

Page 23

Therefore, the flow chart of Matlab would be:

Looked back the Soundcard Scope and Matlab output, we got:

TRAN MINH KHOA 1651056

TRAN NGUYEN KHANG 1651047


Page 24

TRAN MINH KHOA 1651056

TRAN NGUYEN KHANG 1651047


Page 25
To sketch the convolution of 2 signals, we added some commands in matlab:

title('TMS320C5515 modulation');
xlabel('Time');
ylabel('Amplitude');
figure
plot(conv(X,Yr))
title('Output - Convolution of 2 signals');


Add main.c:
for(i=4800;i<=4800+4800-1;i++)
{
convolution[i]=0;
for(j=0;j<=i;j++)
{
convolution[i]=convolution[i]+(Y[i]*Y[i-j]); }
}

Therefore, the audio out function at aic3204_tone_headphone.c would be:
for ( i = 0 ; i < 5 ; i++ )
{
for ( j = 0 ; j < 10000 ; j++ )
{
for ( sample = 0 ; sample < 4800+4799 ; sample++ )
{
while((XmitR & I2S0_IR) == 0);
// Wait for transmit interrupt to be pending
I2S0_W0_MSW_W = (10000*convolution[sample]) ;
// 16 bit left channel transmit audio data
I2S0_W1_MSW_W = (10000*convolution[sample]) ;
// 16 bit right channel transmit audio data
}
}
}

TRAN MINH KHOA 1651056

TRAN NGUYEN KHANG 1651047



×