先贴上一个单独的EEPROM写程序,很简单
#include<iom8v.h>
#include<eeprom.h>
unsigned char buf1[32];
unsigned char buf2[32];
#define sendEnable() (PORTD |= (1 << PD2))
#define receEnable() (PORTD &= ~(1 << PD2))
//串口初始化
void uart0_init(void)
{
UCSRA = 0x02;
UCSRC = 0xA6;
UBRRL = 12;
UBRRH = 0x00;
UCSRB = 0xD8;
DDRD |= (1 << PD2);
receEnable();
}
//IO初始化
void port_init(void)
{
PORTB = 0xFF;
DDRB = 0xFD;
PORTC = 0xFF;
DDRC = 0x00;
PORTD = 0xFF;
DDRD = 0xFA;
}
//延时
void delayms(unsigned int n)
{
unsigned int i=0;
for (i=0;i<20;i++)
{unsigned int i;
for (i=0;i<1140;i++);}
}
//单个字符发送
void put_char(unsigned char i)
{
while (!(UCSRA&(1<<UDRE)));
UDR=i;
}
//字符串发送
void puts(char *s)
{
while(*s)
{
PORTD |= (1 << PD2);
put_char(*s);
s++;
}
}
//查询接收
unsigned char receive( void )
{
while (!(UCSRA & (1<<RXC)));
return UDR; }
//刷新接受缓存起
void USART_Flush( void )
{
unsigned char dummy;
while ( UCSRA & (1<<RXC) ) dummy = UDR; }
//主程序
void main(void)
{
unsigned char temp,s=0;
port_init();
uart0_init();
while(1)
{
USART_Flush();
while(s<4)
{
receEnable();
temp=receive();
buf1[s]=temp;
s++;
}
EEPROM_WRITE(0x20,buf1); //buf1内容写入EEPROM 0x20
s=0;
EEPROM_READ(0x20,buf2); //读出EEPROM 0x20内容到buf2
sendEnable();
puts(buf2); //输出buf2
delayms(100);
receEnable();
}
}
这个是没问题的,随便定义写几位都可以,但是看下面加在modbus协议里之后的程序
void EEPROMrede(void)
{
uint8 i=0,s=0;
unsigned int temp,c;
//输出指定4个字符
for(i=0;i<4;i++)
{
sendBuf[i]=receBuf[i+2];
}
sendCount =4;
beginSend();
//给buf1赋值
for(s=0;s<4;s++)
{
buf1[s]=receBuf[s+2];
}
EEPROM_WRITE(0x60,buf1); //buf1写入EEPROM 0x60处
delayms(2);
}
和第一个代码是一样的,但是第一个代码可以写任意字符串,加在协议里之后就只能写前2位字符。而且在执行EEPROM_WRITE(0x60,buf1) 写EEPROM之后,这句话后面的代码好像都不执行了。好几天了,没琢磨出来怎么回事 |