STM32的SPI搞了2天都还没搞出来,一下程序是用STM32的SPI驱动串行DA,,TLV5608输出正弦波,问题是波形死活不出来。
- #include "spi.h"
- //spi1_NSS==>PA4
- //spi1_clk==>PA5
- //spi1_MISO==>PA6
- //spi1_MOSI==>PA7
- //LDAC=======>PA0
- //MODE=======>PA1
- //FS=======>PA3
- unsigned short sin[550]={ //??????
- 0x1FF,0x205,0x20B,0x211,0x216,0x21C,0x222,0x228,0x22E,0x233,0x239,0x23F,0x245,0x24B,0x250,0x256
- ,0x25C,0x262,0x267,0x26D,0x273,0x279,0x27E,0x284,0x289,0x28F,0x295,0x29A,0x2A0,0x2A5,0x2AB,0x2B0
- ,0x2B6,0x2BB,0x2C1,0x2C6,0x2CB,0x2D1,0x2D6,0x2DB,0x2E1,0x2E6,0x2EB,0x2F0,0x2F5,0x2FB,0x300,0x305
- ,0x30A,0x30F,0x314,0x318,0x31D,0x322,0x327,0x32C,0x330,0x335,0x33A,0x33E,0x343,0x347,0x34C,0x350
- ,0x355,0x359,0x35D,0x361,0x366,0x36A,0x36E,0x372,0x376,0x37A,0x37E,0x382,0x385,0x389,0x38D,0x390
- ,0x394,0x398,0x39B,0x39F,0x3A2,0x3A5,0x3A8,0x3AC,0x3AF,0x3B2,0x3B5,0x3B8,0x3BB,0x3BE,0x3C1,0x3C3
- ,0x3C6,0x3C9,0x3CB,0x3CE,0x3D0,0x3D3,0x3D5,0x3D7,0x3D9,0x3DC,0x3DE,0x3E0,0x3E2,0x3E4,0x3E5,0x3E7
- ,0x3E9,0x3EB,0x3EC,0x3EE,0x3EF,0x3F1,0x3F2,0x3F3,0x3F4,0x3F5,0x3F7,0x3F7,0x3F8,0x3F9,0x3FA,0x3FB
- ,0x3FB,0x3FC,0x3FD,0x3FD,0x3FD,0x3FE,0x3FE,0x3FE,0x3FE,0x3FE,0x3FE,0x3FE,0x3FE,0x3FE,0x3FE,0x3FD
- ,0x3FD,0x3FD,0x3FC,0x3FB,0x3FB,0x3FA,0x3F9,0x3F8,0x3F7,0x3F7,0x3F5,0x3F4,0x3F3,0x3F2,0x3F1,0x3EF
- ,0x3EE,0x3EC,0x3EB,0x3E9,0x3E7,0x3E5,0x3E4,0x3E2,0x3E0,0x3DE,0x3DC,0x3D9,0x3D7,0x3D5,0x3D3,0x3D0
- ,0x3CE,0x3CB,0x3C9,0x3C6,0x3C3,0x3C1,0x3BE,0x3BB,0x3B8,0x3B5,0x3B2,0x3AF,0x3AC,0x3A9,0x3A5,0x3A2
- ,0x39F,0x39B,0x398,0x394,0x390,0x38D,0x389,0x385,0x382,0x37E,0x37A,0x376,0x372,0x36E,0x36A,0x366
- ,0x361,0x35D,0x359,0x355,0x350,0x34C,0x347,0x343,0x33E,0x33A,0x335,0x330,0x32C,0x327,0x322,0x31D
- ,0x318,0x314,0x30F,0x30A,0x305,0x300,0x2FB,0x2F5,0x2F0,0x2EB,0x2E6,0x2E1,0x2DB,0x2D6,0x2D1,0x2CB
- ,0x2C6,0x2C1,0x2BB,0x2B6,0x2B0,0x2AB,0x2A5,0x2A0,0x29A,0x295,0x28F,0x289,0x284,0x27E,0x279,0x273
- ,0x26D,0x267,0x262,0x25C,0x256,0x250,0x24B,0x245,0x23F,0x239,0x233,0x22E,0x228,0x222,0x21C,0x216
- ,0x211,0x20B,0x205,0x1FF,0x1F9,0x1F3,0x1ED,0x1E8,0x1E2,0x1DC,0x1D6,0x1D0,0x1CA,0x1C5,0x1BF,0x1B9
- ,0x1B3,0x1AE,0x1A8,0x1A2,0x19C,0x197,0x191,0x18B,0x185,0x180,0x17A,0x175,0x16F,0x169,0x164,0x15E
- ,0x159,0x153,0x14E,0x148,0x143,0x13D,0x138,0x133,0x12D,0x128,0x123,0x11D,0x118,0x113,0x10E,0x109
- ,0x103,0x0FE,0x0F9,0x0F4,0x0EF,0x0EA,0x0E6,0x0E1,0x0DC,0x0D7,0x0D2,0x0CE,0x0C9,0x0C4,0x0C0,0x0BB
- ,0x0B7,0x0B2,0x0AE,0x0A9,0x0A5,0x0A1,0x09D,0x098,0x094,0x090,0x08C,0x088,0x084,0x080,0x07C,0x079
- ,0x075,0x071,0x06E,0x06A,0x066,0x063,0x05F,0x05C,0x059,0x055,0x052,0x04F,0x04C,0x049,0x046,0x043
- ,0x040,0x03D,0x03B,0x038,0x035,0x033,0x030,0x02E,0x02B,0x029,0x027,0x024,0x022,0x020,0x01E,0x01C
- ,0x01A,0x019,0x017,0x015,0x013,0x012,0x010,0x00F,0x00D,0x00C,0x00B,0x00A,0x009,0x007,0x007,0x006
- ,0x005,0x004,0x003,0x003,0x002,0x001,0x001,0x001,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000
- ,0x000,0x000,0x001,0x001,0x001,0x002,0x003,0x003,0x004,0x005,0x006,0x007,0x007,0x009,0x00A,0x00B
- ,0x00C,0x00D,0x00F,0x010,0x012,0x013,0x015,0x017,0x019,0x01A,0x01C,0x01E,0x020,0x022,0x025,0x027
- ,0x029,0x02B,0x02E,0x030,0x033,0x035,0x038,0x03B,0x03D,0x040,0x043,0x046,0x049,0x04C,0x04F,0x052
- ,0x056,0x059,0x05C,0x05F,0x063,0x066,0x06A,0x06E,0x071,0x075,0x079,0x07C,0x080,0x084,0x088,0x08C
- ,0x090,0x094,0x098,0x09D,0x0A1,0x0A5,0x0A9,0x0AE,0x0B2,0x0B7,0x0BB,0x0C0,0x0C4,0x0C9,0x0CE,0x0D2
- ,0x0D7,0x0DC,0x0E1,0x0E6,0x0EA,0x0EF,0x0F4,0x0F9,0x0FE,0x103,0x109,0x10E,0x113,0x118,0x11D,0x123
- ,0x128,0x12D,0x133,0x138,0x13D,0x143,0x148,0x14E,0x153,0x159,0x15E,0x164,0x169,0x16F,0x175,0x17A
- ,0x180,0x185,0x18B,0x191,0x197,0x19C,0x1A2,0x1A8,0x1AE,0x1B3,0x1B9,0x1BF,0x1C5,0x1CB,0x1D0,0x1D6
- ,0x1DC,0x1E2,0x1E8,0x1ED,0x1F3,0x1F9
- };
- void spi_init(void)
- {
- RCC->APB2ENR |=(1<<2);//PORTA时钟使能
- RCC->APB2ENR |=(1<<12);//SPI1时钟使能
-
- GPIOA->CRL &=0x000fffff;
- GPIOA->CRL |=0xbbb00000;//PA5,6,7设置为复用,
- GPIOA->ODR |=(0x07<<5); //PA5,6,7上拉,
-
- SPI1->CR1 &=~(1<<10);//全双工模式
- SPI1->CR1 |=(1<<9);//软件管理nss
- SPI1->CR1 |=(1<<8);
-
- SPI1->CR1 |=(1<<2);//SPI设置为主机
- SPI1->CR1 |=(1<<11);//16位数据格式
- // SPI1->CR1 &=~(1<<11);//8位数据格式
- SPI1->CR1 &=~(1<<1);//空闲时sclk为0,
- SPI1->CR1 |=(1<<0);//数据采样从第二个时钟边沿开始
- SPI1->CR1 |=(7<<3); //Fsck=Fcpu/256
- SPI1->CR1 &=~(1<<7);//高位在前
- SPI1->CR1 |=(1<<6); //开启SPI
- }
- void spi_write(unsigned short Txdata)
- {
- // delay_ms(50);
- SPI1->DR=Txdata;//传输开始
- while(!(SPI1->SR&(1<<1)));
- // GPIOA->ODR |=(1<<2);
- // delay_us(10);
- }
- unsigned short tlv5608_deal(unsigned short dat,unsigned short chnnel)
- {
- unsigned short dat_ch;
- if(chnnel==9)
- {
- dat=(dat&0x3ff);
- }
- else
- {
- dat=((dat&0x3ff)<<2);
- }
- chnnel=((chnnel&0x0f)<<12);
- dat_ch=dat|chnnel;
- return dat_ch;
- }
复制代码
- //#include "stm32f10x_lib.h"
- #include "stm32f10x.h"
- #include "sys.h"
- #include "timer.h"
- #include "gpio.h"
- #include "spi.h"
- //LDAC=======>PA0
- //MODE=======>PA1
- //FS=======>PA3
- //www.doflye.net
- /*------------------------------------------------------------
- 主函数
- ------------------------------------------------------------*/
- int main()
- {
-
- unsigned int i=0;
- unsigned int ff;
- Stm32_Clock_Init();//系统时钟设置,,设置为72M
- RCC->APB2ENR |= 0x00000001; //开启afio时钟
- //AFIO->MAPR = (0x00FFFFFF & AFIO->MAPR)|0x04000000; //关闭JTAG
-
- RCC->APB2ENR|=0X0000001c;//先使能外设IO PORTa,b,c时钟
- //到此为止,时钟设置完成
- gpio_init();
- spi_init();
- GPIOA->ODR |=(1<<1); //
- // timer_init(5000,7199);//500ms中断一次,定时器的计数频率为72M/7199,计数次数为5000;
- while (1)
- {
- GPIOA->ODR |=(1<<2);
- GPIOA->ODR &=~(1<<3); //开片选
- // delay_us(10);
- spi_write(tlv5608_deal(sin[i],0));
- i++;
- if(i>549)
- {
- i=0;
- }
- GPIOA->ODR |=(1<<3); //关片选
- GPIOA->ODR &=~(1<<0);//更新数据
- }
- }
复制代码
|