#include<iom16v.h>
#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<<PD5)
//25LC256//////////////////////
#define WRSR 0x01 //写状态寄存器
#define WRITE 0x02 //写命令
#define READ 0x03 //读命令
#define WRDI 0x04 //写禁止
#define RDSR 0x05 //读状态寄存器
#define WREN 0x06 //写允许
//PORTB////////////////////////
#define WP 3
#define SS 4
#define MOSI 5
#define MISO 6
#define SCK 7
//PORTD////////////////////////
#define EN_485 4
#define RUN 5
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<<RXEN)|(1<<TXEN); //接收器与发送器使能
UCSRC = (1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1); //设置帧格式:8个数据位,1个停止位
}
///////////////458发送///////////////////
void UART_TX485( unsigned char data )
{
PORTD |= (1<<EN_485);
delay(2);
while ( !( UCSRA & (1<<UDRE)) );
UDR = data;
while(!(UCSRA & (1<<UDRE)));
delay(4);
PORTD &= ~(1<<EN_485); // 将数据放入缓冲器,发送数据
}
/////////////////SPI 主机设置//////////////////////////////
void SPI_MasterInit(void)
{
DDRB |= (1<<SS)|(1<<MOSI)|(1<<SCK);
PORTB |= (1<<SS);
SPCR = (1<<SPE)|(1<<MSTR);// 设置为主机模式,使能SPI,时钟为fosc/4
}
////////////////////SPI发送////////////////
void SPI_TX(unsigned char cdata)
{
SPDR = cdata;
while(!(SPSR & (1<<SPIF)));
}
//================================================
//SPI读
//================================================
unsigned char SPIRead(unsigned int SpiAddress)
{
unsigned char i,add_H,add_L;
add_H = SpiAddress >> 8;
add_L = SpiAddress & 0x00FF;
PORTB &= ~(1<<SS);
SPI_TX(READ);
SPI_TX(add_H);
SPI_TX(add_L);
SPI_TX(0xFF);
i = SPDR;
PORTB |= (1<<SS);
return i;
}
//==================================================
//SPI写
//==================================================
void SPIWrite(unsigned int SpiAddress,unsigned char edata)//*ramadr,unsigned char width)
{
unsigned char i,add_H,add_L;
PORTB &= ~(1<<SS);
SPI_TX(WREN); //写使能
PORTB |= (1<<SS);//锁存写使能命令
delay(50);
add_H = SpiAddress >> 8;
add_L = SpiAddress && 0x00FF;
PORTB &= ~(1<<SS);
SPI_TX(WRITE);
SPI_TX(add_H);
SPI_TX(add_L);
SPI_TX(edata);
PORTB |= (1<<SS);
}
//端口初始化///////////////////////////
void PORT_Init()
{
DDRB = 0x00;
DDRB |= (1<<WP);
DDRD |= (1<<EN_485)|(1<<RUN);
PORTD |= (1<<RUN);
PORTB |= (1<<WP); //低电平为写保护,高电平写使能允许
}
//主程序////////////////////////
void main(void)
{
unsigned char temp,i;
i = 0x43;
SREG = 0x00;
USART_Init();
SPI_MasterInit();
PORT_Init();
SREG = 0x80;
while(1)
{
SPIWrite(0x0001,i);
_nop();
_nop();
delay(200);
temp = SPIRead(0x0001);
_nop();
_nop();
delay(5);
UCSRB &= ~(1<<RXCIE);
_nop();
UART_TX485(temp);
_nop();
UCSRB |= (1<<RXCIE);
UART_TX485(0x45);
_nop();
_nop();
}
}
用cvavr2.03.4写的程序
是不是初始化时要发WRSR命令,写状态寄存,然后写入0X02
麻烦各位看看是否我程序有什么地方错了吗,i=0X43但不管怎么样,写进去, 读出来的永远都是00 |