完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
在论坛里很少看到有人用这种组合请问是为什么呢,是价格问题还是SPI很差呢???
我的程序如下 #include #define BAUD 9600 //波特率采用9600ps #define CRYSTAL 8000000 //系统时钟8MHz #define BAUD_SETtiNG (unsigned char)((unsigned long)CRYSTAL/(16*(unsigned long)BAUD)-1) // 设置波特率 #define BAUD_H (unsigned char)(BAUD_SETTING>>8) #define BAUD_L (unsigned char)(BAUD_SETTING) #define _nop() asm("nop") #define LED_COM PORTD ^= (1< //25LC256////////////////////// #define WRSR 0x01 //写状态寄存器 #define WRITE 0x02 //写命令 #define READ 0x03 //读命令 #define WRDI 0x04 //写禁止 #define RDSR 0x05 //读状态寄存器 #define WREN 0x06 //写允许 //PORTB//////////////////////// #define SS 4 #define MOSI 5 #define MISO 6 #define SCK 7 //PORTD//////////////////////// #define EN_485 4 #define RUN 5 unsigned char TX_buff[16],RX_buff[16],SPI_buff[16]; unsigned char LED_Counter; unsigned char RX_Flag; unsigned char M95256_status; void delay(unsigned char k) //延时ms { unsigned char j; for (;k>0;k--) for(j=220;j>0;j--) { _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); _nop(); } } ////////////////RS232//////////////////// void USART_Init() //232波特率初始化 { UBRRH = BAUD_H; UBRRL = BAUD_L; UCSRB |= (1< UCSRC = (1< } ///////////////458发送/////////////////// void UART_TX485( unsigned char data ) { PORTD |= (1< delay(2); while ( !( UCSRA & (1< UDR = data; while(!(UCSRA & (1< delay(4); PORTD &= ~(1< } /////////////////SPI 主机设置////////////////////////////// void SPI_MasterInit(void) { DDRB |= (1< PORTB |= (1< SPCR = (1< } ////////////////////SPI发送//////////////// void SPI_TX(unsigned char cdata) { SPDR = cdata; while(!(SPSR & (1< } //25LC256_SPI设置/////////////////////////// void SPI_25LC256_Init() { PORTB &= ~(1< SPI_TX(WRSR);//写寄存器状态 SPI_TX(0x02); PORTB |= (1< } //================================================ //SPI读 //================================================ unsigned char SPIRead(unsigned int SpiAddress) { unsigned char i,add_H,add_L; PORTB &= ~(1< _nop(); add_H = SpiAddress >> 8; add_L = SpiAddress && 0x00FF; SPI_TX(READ); SPI_TX(add_H); SPI_TX(add_L); SPI_TX(0xFF); i = SPDR; PORTB |= (1< return i; } //================================================== //SPI写 //================================================== void SPIWrite(unsigned int SpiAddress,unsigned char edata) { unsigned char i,add_H,add_L; PORTB &= ~(1< SPI_TX(WREN); PORTB |= (1< _nop(); add_H = SpiAddress >> 8; add_L = SpiAddress && 0x00FF; PORTB &= ~(1< SPI_TX(WRITE); SPI_TX(add_H); SPI_TX(add_L); SPI_TX(edata); PORTB |= (1< } //字符初始化/////////////////////// void DATA_Init() { LED_Counter = 0; RX_Flag = 0; } //端口初始化/////////////////////////// void PORT_Init() { DDRB = 0x00; DDRB|=(1< PORTD |= (1< } //主程序//////////////////////// void main(void) { unsigned char temp,i; temp = 0x42; SREG = 0x00; UCSRB |= (1< TIMSK |= (1< TCCR1B = 0x00; TCNT1 = 0x3CAF; //50MS TCCR1B |= (1< USART_Init(); SPI_MasterInit(); SPI_25LC256_Init(); PORT_Init(); DATA_Init(); SREG = 0x80; while(1) { SPIWrite(0x0001,0x42); _nop(); _nop(); delayms(50); i = SPIRead(0x0001); _nop(); _nop(); UCSRB &= ~(1< _nop(); UART_TX485(i); _nop(); UCSRB |= (1< _nop(); _nop(); } } 485发送出来的永远是00 CVAVR2.03.4版本 |
|
相关推荐 |
|
只有小组成员才能发言,加入小组>>
AVR Atmega16 Bootloader程序与上位机LabView程序
5130 浏览 6 评论
#include <ioavr.h>这个头文件我应该下什么编译器
7772 浏览 0 评论
3033 浏览 2 评论
3107 浏览 1 评论
10061 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-28 01:07 , Processed in 0.541980 second(s), Total 51, Slave 37 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号