Dueck R.Digital design with CPLD applications and VHDL.2000
.pdf770 A P P E N D I X E • EPROM Data for a Digital Function Generator
E.2 C Program
This program is also on the accompanying CD in the file
\Student_Files\EPROM\EPROM.C.
/* |
Hex File Generator |
*Written by: Ronan Capina and Robert Dueck
*This program is to create a hex file in a specific EPROM record format.
*The EPROM is addressed in blocks of 256 bytes (8 address lines) by an 8 bit
*counter to create a digital image of one of several waveform outputs.
*Two additional address bits select one of four output functions.
*When the EPROM data are run through a D/A Converter, they create an
*analog waveform running at the frequency of the counter divided by 256.
*The waveforms are sine, square, triangle, and sawtooth.
*The record format is as follows. (Spaces are inserted only for clarity.
*The actual record must have NO spaces.)
*
*: 10 0080 00 AF5F67F0602703E0322CFA92007780C3 61
*(:Record Length = 10hex = 16dec)
*(Address = 0080hex; location in EPROM of first data byte in record)
*(Record type = 00 = data)
*(16 data bytes = 32 hex digits)
*(Checksum; Record Length + Address High byte + Address Low byte
*+ Record type + data bytes + Checksum = 00, after discarding carry)
*An END record is also required, having a similar format, except that
*Record Type = 01 = END and there are no data bytes. Checksum is still
*required.
*eg. :00000001FF
*/
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> #include <conio.h>
void Sawtooth(int); void triangle(int); void Square(int);
int AddrByte(int Addr); int Chksum(int sum); void sine(int);
char *HexString(int value);
char *Hex = HULL; int Ampl;
const double Pi = 3.141592654;
int main(void)
{
FILE *fp;
int Fcn =1, Linenum, sum, Byte, Addr = 0; char Record[256];
clrscr();
|
A P P E N D I X E • EPROM Data for a Digitial Function Generator |
771 |
/* |
Hex file is written to c:\eprom\eprom.hex |
|
*To change the file name, modify the following line to:
*if (!(fp = fopen(“c:\\YourDirectoryName\\YourFile.hex”, “w+t”)))
{
*/
if (!(fp = fopen(“c:\\eprom\\eprom.hex”, “w+t”))) { printf(“Error opening output file.\r\n”); exit(1);
}else
printf(“File opened successfully. \r\n”);
while (Fcn != 5) { /* Create records for 4 functions */
/* Each function has 16 lines of data */ for (Linenum = 1; Linenum <= 16; Linenum++) {
/* Create record and address information */ if (Addr < 16)
sprintf(Record, “:10000%x”, Addr); else
if (Addr < 256)
sprintf(Record, “:1000%x”, Addr);
else
sprintf(Record, “:100%x”, Addr); strcat(Record, “00”);
/* Accumulate sum for calculating checksum */ sum = 16 + AddrByte(Addr);
/* Calculate byte values for selected function */ for (Byte = 1; Byte <= 16; Byte++) {
if (Fcn == 1) sine(Addr % 256);
else if (Fcn == 2) Square(Addr % 256);
else if (Fcn == 3) triangle(Addr % 256);
else
Sawtooth(Addr % 256);
/* Append calculated byte value (amplitude) to the record and update checksum accumulator */
strcat (Record, HexString(Ampl)); sum = sum + Ampl;
Addr++;
}
strcat (Record, HexStrIng(Chksum(sum))); fprintf(fp, “%s\r\n”, Record);
}
Fcn++;
}
fprintf(fp, “:00000001FF\r\n”); fclose(fp);
return(0);
}
772 A P P E N D I X E • EPROM Data for a Digital Function Generator
int AddrByte(int Addr)
{
if (Addr < 256) return Addr;
else
return (((int)(Addr / 256)) + (Addr % 256));
}
int Chksum(int sum)
{
int IntRem = sum % 256; if (IntRem == 0)
return 0;
else
return (256 - IntRem);
}
char *HexString(int value)
{
if (value < 16) {
sprintf(Hex, “0%x”, value); return Hex;
} else if (value > 255) return “FF”;
else {
sprintf(Hex, “%x”, value); return Hex;
}
}
void Sawtooth(int Addr)
{
Ampl = Addr; return;
}
void sine(int Addr)
{
double angle = ((((float)Addr) * 2 * Pi) / 256); Ampl = ((int)((sin(angle) * 128) + 128));
if (Ampl > 255) Ampl = 255; return;
}
void Square(int Addr)
{
if (Addr < 128) Ampl = 255; else
Ampl = 0; return;
}
|
A P P E N D I X E • EPROM Data for a Digitial Function Generator |
773 |
void triangle(int Addr) |
|
|
{ |
|
|
if (Addr < 64) |
|
|
Ampl = |
128 + (2 * Addr); |
|
else if |
((Addr >= 64) && (Addr < 192)) |
|
Ampl = |
256 - 2 * (Addr - 63); |
|
else |
|
|
Ampl = |
2 * (Addr - 191); |
|
return; |
|
|
} |
|
|
E.3 Resultant Record File
:10000000808386898C8F9295989C9FA2A5A8ABAE81
:10001000B0B3B6B9BCBFC1C4C7C9CCCED1D3D5D893
:10002000DADCDEE0E2E4E6E8EAECEDEFF0F2F3F54C
:10003000F6F7F8F9FAFBFCFCFDFEFEFFFFFFFFFF01
:10004000FFFFFFFFFFFFFEFEFDFCFCFBFAF9F8F7E8
:10005000F6F5F3F2F0EFEDECEAE8E6E4E2E0DEDC00
:10006000DAD8D5D3D1CECCC9C7C4C1BFBCB9B6B319
:10007000B0AEABA8A5A29F9C9895928F8C898683E1
:100080007F7C797673706D6A6763605D5A575451EF
:100090004F4C494643403E3B383633312E2C2A27BD
:1000A0002523211F1D1B1917151312100F0D0C0AE4
:1000B000090807060504030302010100000000000F
:1000C0000000000000000101020303040506070808
:1000D000090A0C0D0F1012131517191B1D1F2123D0
:1000E00025272A2C2E313336383B3E404346494C97
:1000F0004F5154575A5D6063676A6D707376797CAF
:10010000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
:10011000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEF
:10012000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDF
:10013000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFCF
:10014000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFBF
:10015000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFAF
:10016000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF9F
:10017000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8F
:10018000000000000000000000000000000000006F
:10019000000000000000000000000000000000005F
:1001A000000000000000000000000000000000004F
:1001B000000000000000000000000000000000003F
:1001C000000000000000000000000000000000002F
:1001D000000000000000000000000000000000001F
:1001E000000000000000000000000000000000000F
:1001F00000000000000000000000000000000000FF
:1002000080828486888A8C8E90929496989A9C9EFE
:10021000A0A2A4A6A8AAACAEB0B2B4B6B8BABCBEEE
:10022000C0C2C4C6C8CACCCED0D2D4D6D8DADCDEDE
:10023000E0E2E4E6E8EAECEEF0F2F4F6F8FAFCFECE
:10024000FEFCFAF8F6F4F2F0EEECEAE8E6E4E2E0BE
:10025000DEDCDAD8D6D4D2D0CECCCAC8C6C4C2C0AE
:10026000BEBCBAB8B6B4B2B0AEACAAA8A6A4A2A09E
:100270009E9C9A98969492908E8C8A88868482808E
:100280007E7C7A78767472706E6C6A68666462607E
774 A P P E N D I X E • EPROM Data for a Digital Function Generator
:100290005E5C5A58565452504E4C4A48464442406E
:1002A0003E3C3A38363432302E2C2A28262422205E
:1002B0001E1C1A18161412100E0C0A08060402004E
:1002C000020406080A0C0E10121416181A1C1E201E
:1002D000222426282A2C2E30323436383A3C3E400E
:1002E000424446484A4C4E50525456585A5C5E60FE
:1002F000626466686A6C6E70727476787A7C7E80EE
:10030000000102030405060708090A0B0C0D0E0F75
:10031000101112131415161718191A1B1C1D1E1F65
:10032000202122232425262728292A2B2C2D2E2F55
:10033000303132333435363738393A3B3C3D3E3F45
:10034000404142434445464748494A4B4C4D4E4F35
:10035000505152535455565758595A5B5C5D5E5F25
:10036000606162636465666768696A6B6C6D6E6F15
:10037000707172737475767778797A7B7C7D7E7F05
:10038000808182838485868788898A8B8C8D8E8FF5
:10039000909192939495969798999A9B9C9D9E9FE5
:1003A000A0A1A2A3A4A5A6A7A8A9AAABACADAEAFD5
:1003B000B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC5
:1003C000C0C1C2C3C4C5C6C7C8C9CACBCCCDCECFB5
:1003D000D0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFA5
:1003E000E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF95
:1003F000F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF85
:00000001FF


Y A B C D
1