原创 【原创】用DA(TLC5615)芯片产生正弦波!

2009-4-27 10:24 5164 9 10 分类: MCU/ 嵌入式

附件里是一个好用的工具,《正弦波数据生成器》,精度可调!


/*
***********************************************
CPU:           MEGA64
晶振:         7.3728M
功能:         实现频率为25Hz的正弦波
***********************************************
*/
#include<avr/io.h>
#include<util/delay.h>
#include<avr/eeprom.h>
#include<avr/pgmspace.h>
#include<avr/interrupt.h>


#define uint8  unsigned char
#define  int8    signed char
#define uint16 unsigned int
#define  int16   signed int
#define uint32 unsigned long
#define  int32   signed long
#define uchar unsigned char
#define uint unsigned int
/*----------------SPI端口定义-----------------*/
#define MMC_SD_CS_DDR  DDRB
#define MMC_SD_CS_PORT PORTB
#define MMC_SD_CS_BIT  0


#define MMC_SD_SCK_DDR  DDRB
#define MMC_SD_SCK_PORT PORTB
#define MMC_SD_SCK_BIT  1


#define MMC_SD_MOSI_DDR  DDRB
#define MMC_SD_MOSI_PORT PORTB
#define MMC_SD_MOSI_BIT  2


#define MMC_SD_MISO_DDR  DDRB
#define MMC_SD_MISO_PORT PORTB
#define MMC_SD_MISO_BIT  3


#define MMC_SD_PORT_INI  {\
       MMC_SD_CS_PORT |= 1<<MMC_SD_CS_BIT;\
       MMC_SD_SCK_PORT |= 1<<MMC_SD_SCK_BIT;\
       MMC_SD_MOSI_PORT |= 1<<MMC_SD_MOSI_BIT;\
       MMC_SD_MISO_PORT |= 1<<MMC_SD_MISO_BIT;\
       \
       MMC_SD_CS_DDR |= 1<<MMC_SD_CS_BIT;\
       MMC_SD_SCK_DDR |= 1<<MMC_SD_SCK_BIT;\
       MMC_SD_MOSI_DDR |= 1<<MMC_SD_MOSI_BIT;\
       MMC_SD_MISO_DDR &= ~(1<<MMC_SD_MISO_BIT);\
      }
#define SPI_CS_Assert   MMC_SD_CS_PORT &= ~(1<<MMC_SD_CS_BIT) 
#define SPI_CS_Deassert MMC_SD_CS_PORT |=  1<<MMC_SD_CS_BIT
uint16 i="0";
uint16 t="0";
uint8 t1=0,t2=0;
/*---------------------正弦波数据表---------------------------*/
uint16 wave[1024]={
 0x1FF,0x202,0x205,0x208,0x20C,0x20F,0x212,0x215,0x218,0x21B,0x21E,0x221,0x225,0x228,0x22B,0x22E
,0x231,0x234,0x237,0x23A,0x23E,0x241,0x244,0x247,0x24A,0x24D,0x250,0x253,0x256,0x25A,0x25D,0x260
,0x263,0x266,0x269,0x26C,0x26F,0x272,0x275,0x278,0x27B,0x27E,0x281,0x284,0x287,0x28A,0x28D,0x290
,0x293,0x296,0x299,0x29C,0x29F,0x2A2,0x2A5,0x2A8,0x2AB,0x2AE,0x2B1,0x2B4,0x2B7,0x2BA,0x2BD,0x2C0
,0x2C3,0x2C6,0x2C9,0x2CB,0x2CE,0x2D1,0x2D4,0x2D7,0x2DA,0x2DD,0x2DF,0x2E2,0x2E5,0x2E8,0x2EB,0x2ED
,0x2F0,0x2F3,0x2F6,0x2F8,0x2FB,0x2FE,0x301,0x303,0x306,0x309,0x30B,0x30E,0x311,0x313,0x316,0x319
,0x31B,0x31E,0x320,0x323,0x326,0x328,0x32B,0x32D,0x330,0x332,0x335,0x337,0x33A,0x33C,0x33F,0x341
,0x343,0x346,0x348,0x34B,0x34D,0x34F,0x352,0x354,0x357,0x359,0x35B,0x35D,0x360,0x362,0x364,0x366
,0x369,0x36B,0x36D,0x36F,0x371,0x374,0x376,0x378,0x37A,0x37C,0x37E,0x380,0x382,0x384,0x386,0x388
,0x38A,0x38C,0x38E,0x390,0x392,0x394,0x396,0x398,0x39A,0x39C,0x39E,0x39F,0x3A1,0x3A3,0x3A5,0x3A7
,0x3A8,0x3AA,0x3AC,0x3AD,0x3AF,0x3B1,0x3B2,0x3B4,0x3B6,0x3B7,0x3B9,0x3BA,0x3BC,0x3BE,0x3BF,0x3C1
,0x3C2,0x3C4,0x3C5,0x3C6,0x3C8,0x3C9,0x3CB,0x3CC,0x3CD,0x3CF,0x3D0,0x3D1,0x3D3,0x3D4,0x3D5,0x3D6
,0x3D8,0x3D9,0x3DA,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38E,0x38C
,0x38A,0x388,0x386,0x384,0x382,0x380,0x37E,0x37C,0x37A,0x378,0x376,0x374,0x371,0x36F,0x36D,0x36B
,0x369,0x366,0x364,0x362,0x360,0x35D,0x35B,0x359,0x357,0x354,0x352,0x34F,0x34D,0x34B,0x348,0x346
,0x343,0x341,0x33F,0x33C,0x33A,0x337,0x335,0x332,0x330,0x32D,0x32B,0x328,0x326,0x323,0x320,0x31E
,0x31B,0x319,0x316,0x313,0x311,0x30E,0x30B,0x309,0x306,0x303,0x301,0x2FE,0x2FB,0x2F8,0x2F6,0x2F3
,0x2F0,0x2ED,0x2EB,0x2E8,0x2E5,0x2E2,0x2DF,0x2DD,0x2DA,0x2D7,0x2D4,0x2D1,0x2CE,0x2CB,0x2C9,0x2C6
,0x2C3,0x2C0,0x2BD,0x2BA,0x2B7,0x2B4,0x2B1,0x2AE,0x2AB,0x2A8,0x2A5,0x2A2,0x29F,0x29C,0x299,0x296
,0x293,0x290,0x28D,0x28A,0x287,0x284,0x281,0x27E,0x27B,0x278,0x275,0x272,0x26F,0x26C,0x269,0x266
,0x263,0x260,0x25D,0x25A,0x256,0x253,0x250,0x24D,0x24A,0x247,0x244,0x241,0x23E,0x23A,0x237,0x234
,0x231,0x22E,0x22B,0x228,0x225,0x221,0x21E,0x21B,0x218,0x215,0x212,0x20F,0x20C,0x208,0x205,0x202
,0x1FF,0x1FC,0x1F9,0x1F6,0x1F2,0x1EF,0x1EC,0x1E9,0x1E6,0x1E3,0x1E0,0x1DC,0x1D9,0x1D6,0x1D3,0x1D0
,0x1CD,0x1CA,0x1C7,0x1C3,0x1C0,0x1BD,0x1BA,0x1B7,0x1B4,0x1B1,0x1AE,0x1AB,0x1A8,0x1A4,0x1A1,0x19E
,0x19B,0x198,0x195,0x192,0x18F,0x18C,0x189,0x186,0x183,0x180,0x17D,0x17A,0x177,0x174,0x171,0x16E
,0x16B,0x168,0x165,0x162,0x15F,0x15C,0x159,0x156,0x153,0x150,0x14D,0x14A,0x147,0x144,0x141,0x13E
,0x13B,0x138,0x135,0x133,0x130,0x12D,0x12A,0x127,0x124,0x121,0x11F,0x11C,0x119,0x116,0x113,0x111
,0x10E,0x10B,0x108,0x106,0x103,0x100,0x0FD,0x0FB,0x0F8,0x0F5,0x0F3,0x0F0,0x0ED,0x0EB,0x0E8,0x0E5
,0x0E3,0x0E0,0x0DE,0x0DB,0x0D8,0x0D6,0x0D3,0x0D1,0x0CE,0x0CC,0x0C9,0x0C7,0x0C4,0x0C2,0x0BF,0x0BD
,0x0BB,0x0B8,0x0B6,0x0B3,0x0B1,0x0AF,0x0AC,0x0AA,0x0A7,0x0A5,0x0A3,0x0A1,0x09E,0x09C,0x09A,0x098
,0x095,0x093,0x091,0x08F,0x08D,0x08A,0x088,0x086,0x084,0x082,0x080,0x07E,0x07C,0x07A,0x078,0x076
,0x074,0x072,0x070,0x06E,0x06C,0x06A,0x068,0x066,0x064,0x062,0x060,0x05F,0x05D,0x05B,0x059,0x057
,0x056,0x054,0x052,0x051,0x04F,0x04D,0x04C,0x04A,0x048,0x047,0x045,0x044,0x042,0x040,0x03F,0x03D
,0x03C,0x03A,0x039,0x038,0x036,0x035,0x033,0x032,0x031,0x02F,0x02E,0x02D,0x02B,0x02A,0x029,0x028
,0x026,0x025,0x024,0x023,0x022,0x021,0x020,0x01E,0x01D,0x01C,0x01B,0x01A,0x019,0x018,0x017,0x016
,0x016,0x015,0x014,0x013,0x012,0x011,0x010,0x010,0x00F,0x00E,0x00D,0x00D,0x00C,0x00B,0x00B,0x00A
,0x009,0x009,0x008,0x008,0x007,0x007,0x006,0x006,0x005,0x005,0x004,0x004,0x003,0x003,0x003,0x002
,0x002,0x002,0x001,0x001,0x001,0x001,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000
,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x001,0x001,0x001,0x001,0x002
,0x002,0x002,0x003,0x003,0x003,0x004,0x004,0x005,0x005,0x006,0x006,0x007,0x007,0x008,0x008,0x009
,0x009,0x00A,0x00B,0x00B,0x00C,0x00D,0x00D,0x00E,0x00F,0x010,0x010,0x011,0x012,0x013,0x014,0x015
,0x016,0x016,0x017,0x018,0x019,0x01A,0x01B,0x01C,0x01D,0x01E,0x020,0x021,0x022,0x023,0x024,0x025
,0x026,0x028,0x029,0x02A,0x02B,0x02D,0x02E,0x02F,0x031,0x032,0x033,0x035,0x036,0x038,0x039,0x03A
,0x03C,0x03D,0x03F,0x040,0x042,0x044,0x045,0x047,0x048,0x04A,0x04C,0x04D,0x04F,0x051,0x052,0x054
,0x056,0x057,0x059,0x05B,0x05D,0x05F,0x060,0x062,0x064,0x066,0x068,0x06A,0x06C,0x06E,0x070,0x072
,0x074,0x076,0x078,0x07A,0x07C,0x07E,0x080,0x082,0x084,0x086,0x088,0x08A,0x08D,0x08F,0x091,0x093
,0x095,0x098,0x09A,0x09C,0x09E,0x0A1,0x0A3,0x0A5,0x0A8,0x0AA,0x0AC,0x0AF,0x0B1,0x0B3,0x0B6,0x0B8
,0x0BB,0x0BD,0x0BF,0x0C2,0x0C4,0x0C7,0x0C9,0x0CC,0x0CE,0x0D1,0x0D3,0x0D6,0x0D8,0x0DB,0x0DE,0x0E0
,0x0E3,0x0E5,0x0E8,0x0EB,0x0ED,0x0F0,0x0F3,0x0F5,0x0F8,0x0FB,0x0FD,0x100,0x103,0x106,0x108,0x10B
,0x10E,0x111,0x113,0x116,0x119,0x11C,0x11F,0x121,0x124,0x127,0x12A,0x12D,0x130,0x133,0x135,0x138
,0x13B,0x13E,0x141,0x144,0x147,0x14A,0x14D,0x150,0x153,0x156,0x159,0x15C,0x15F,0x162,0x165,0x168
,0x16B,0x16E,0x171,0x174,0x177,0x17A,0x17D,0x180,0x183,0x186,0x189,0x18C,0x18F,0x192,0x195,0x198
,0x19B,0x19E,0x1A1,0x1A4,0x1A8,0x1AB,0x1AE,0x1B1,0x1B4,0x1B7,0x1BA,0x1BD,0x1C0,0x1C4,0x1C7,0x1CA
,0x1CD,0x1D0,0x1D3,0x1D6,0x1D9,0x1DD,0x1E0,0x1E3,0x1E6,0x1E9,0x1EC,0x1EF,0x1F2,0x1F6,0x1F9,0x1FC
};
/* spi low speed, below 400KHz */
void SPI_Low(void)
{
    SPCR &= ~_BV(DORD);
 SPCR =   _BV(SPE)|_BV(MSTR)|_BV(SPR0);
 SPSR &= ~_BV(SPI2X);
}
/* spi high speed, not exceed 25MHz */
void SPI_High(void)
{
    SPCR &= ~_BV(DORD);//MSB在前
 SPCR =  _BV(SPE)|_BV(MSTR);
 SPSR |= _BV(SPI2X);
}
/* read and write one byte , full duplex */
uint8 SPI_WriteByte(uint8 val)
{  
 SPDR = val;
 while(!(SPSR & _BV(SPIF)));
 return SPDR; 
}
void SPI_Init(void)
{
 MMC_SD_PORT_INI; /* Port Initialize */
 SPI_High();
}
void timer0_init(void)
{
 cli();
 TCCR0 = 0x00; //stop
 ASSR  = 0x00; //set async mode
 TCNT0 = 0xDC; //set count
 OCR0  = 0x24;
 TCCR0 = 0x02; //start timer
 TIMSK = 0x01; //timer interrupt sources
 sei();
}
//T/C0中断例程
SIGNAL(SIG_OVERFLOW0)
{
   TCNT0 = 0xE1;//40us
   t="wave";
   t="t"<<2; //此处是重点,为何先左移2位,要搞懂mega64硬件spi模式,同时要搞懂tlc5615的12位数据传输模式。
   t1=(uint8)((t&0xff00)>>8);
   t2=(uint8)(t&0x00ff);
   SPI_CS_Assert;   
   SPI_WriteByte(t1); //高8位
   SPI_WriteByte(t2); //低8位
   SPI_CS_Deassert;
   i++;
   if(i>=1023){i=0;}
}
void init_devices(void)
{
 //stop errant interrupts until set up
 cli(); //disable all interrupts
 XDIV  = 0x00; //xtal divider
 XMCRA = 0x00; //external memory
 timer0_init();
 DDRF=0XFF;
 PORTF=0XFF;
 MCUCR = 0x00;
 EICRA = 0x00; //extended ext ints
 EICRB = 0x00; //extended ext ints
 EIMSK = 0x00;
 TIMSK = 0x01; //timer interrupt sources
 ETIMSK = 0x00; //extended timer interrupt sources
 sei(); //re-enable interrupts
 //all peripherals are now initialized
}
int main()
{
 SPI_Init();
 init_devices();
 while(1)
  {
   ;
  }


}


正弦波数据生成器:


rar


AVR studio工程文件:


rar

PARTNER CONTENT

文章评论1条评论)

登录后参与讨论

用户1414082 2009-4-29 20:36

GOOD! 下载了~
相关推荐阅读
用户540356 2011-06-01 10:51
[转]关注电子工程师的身体健康与职业规划
关注电子工程师的身体健康与职业规划发表于 2010-04-10 23:35:55 图片中右边这位是著名的it企业华为的员工胡新宇,这位年轻的电子科技大学的硕士生命永远定格在了25岁。过劳死这个...
用户540356 2010-03-25 09:32
NIOS II开发的整个流程---硬件开发
FPGA黑金开发板【转载】【FPGA黑金开发板】NIOS II开发的整个流程---硬件开发 版权归金沙滩工作室(http://www.kingst.org)所有前言    从今天开始,NIOS的学习征...
用户540356 2009-10-19 13:57
ADUM1201在隔离RS232中的应用
双通道数字式隔离器ADUM1201在RS232总线通信系统中的应用 关键字:ADUM1201   ADUM5241   ADM3251E<?xml:namespace prefix = o ns...
用户540356 2009-05-15 17:56
AVR硬件SPI从机模式调试记录!
由于第一次用MEGA8的SPI从模式,程序使用中断模式接收,刚开始spi不能进入中断,网上查资料,  有网友把MOSI方向设为输出能解决问题,按照此方法果然进入了中断,程序顺利完成并打开看门狗。  试...
用户540356 2009-05-14 20:40
一路上由你
你知道吗 爱你并不容易 爱需要很多勇气是天意吧 让我爱上你 但你却离我而去晨曦细雨重临在这大地 人孤孤单单躲避转身刹那在这熟识的路旁 察觉身后路人是你也许轮回里早已注定 今生就该我还给你一颗心在风雨里...
用户540356 2009-04-27 15:25
RS485数据总线雷击过压防护
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> 1. RS485总线的应用领域工业...
EE直播间
更多
我要评论
1
9
关闭 站长推荐上一条 /3 下一条